Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
swift-ci opened this issue Dec 23, 2015 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

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:

  • SR-10931 Optional initialization inconsistent between Optional vs T?

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.

@belkadan
Copy link
Contributor

@DougGregor, what do you think?

@Dante-Broggi
Copy link
Contributor

This still occurs in the Xcode 9.4.1 toolchain.

@masters3d
Copy link
Contributor

Does this need to go over evolution? This is just a weird side effect.

@belkadan
Copy link
Contributor

It'd be a source-incompatible change to remove it, so it should at least start there, but the core team might decide it doesn't need a full review.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

4 participants