Skip to content

[SR-354] Non-optional variable tuple of optional values should not have default value #42974

Open
@swift-ci

Description

@swift-ci
Previous ID SR-354
Radar None
Original Reporter ianterrell (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 5b5256f99ab266dee19af877b39508b3

relates to:

Issue Description:

Given the following class:

class Foo {
    var bar: (String?, String?)
}

I expect it not to compile, considering bar is a non-optional type without a default value. Instead, it compiles and the synthesized initializer sets bar to (nil, nil):

let f = Foo()
f.bar
// => (nil, nil)

If the tuple is instead a constant, it does not compile:

class Foo {
    let bar: (String?, String?)
}

This results in:

tuple.swift:1:7: error: class 'Foo' has no initializers
class Foo {
      ^

Further, the messaging is not consistent, as non-tuple types communicate more information in the compiler error. With a string:

class Foo {
    let baz: String
}

the following additional error is communicated:

tuple.swift:2:9: note: stored property 'baz' without initial value prevents synthesized initializers
    let baz: String
        ^
                    = ""

While that is the stored property usage, similar behavior results in normal variable usage.

var bar: (String?, String?)
print(bar)
// => (nil, nil)

But with a constant:

let bar: (String?, String?)
print(bar)

It does not compile:

tuple.swift:11:7: error: constant 'bar.0' used before being initialized
print(bar)
      ^

If this behavior is expected, I do not see it documented in the language guide.

Metadata

Metadata

Assignees

No one assigned

    Labels

    OptionalArea → standard library: The `Optional` typebugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdefault initializationFeature: Default initialization of variablesswift evolution proposal neededFlag → feature: A feature that warrants a Swift evolution proposaltuplesFeature: tuplestype checkerArea → compiler: Semantic analysistypesFeature: typesunexpected behaviorBug: Unexpected behavior or incorrect output

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions