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-14971] Spurious error after wrapped 'var' declaration without type annotation. #57313

Closed
rintaro mannequin opened this issue Jul 24, 2021 · 6 comments
Closed

[SR-14971] Spurious error after wrapped 'var' declaration without type annotation. #57313

rintaro mannequin opened this issue Jul 24, 2021 · 6 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself duplicate Resolution: Duplicates another issue good first issue Good for newcomers parser Area → compiler: The legacy C++ parser property wrappers Feature: property wrappers

Comments

@rintaro
Copy link
Mannequin

rintaro mannequin commented Jul 24, 2021

Previous ID SR-14971
Radar None
Original Reporter @rintaro
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Parser, StarterBug
Assignee nate041 (JIRA)
Priority Medium

md5: b6db08514a985df9a7c359dbc1560fe1

Issue Description:

@propertyWrapper
struct Wrap {
  var wrappedValue: Int = 1
}
func test() {
  @Wrap var val
  _ = val
}

This results:

test.swift:8:3: error: found an unexpected second identifier in variable declaration; is there an accidental break?
  _ = val
  ^
test.swift:8:3: note: join the identifiers together
  _ = val
~~^
val_
test.swift:8:7: error: use of local variable 'val' before its declaration
  _ = val
      ^
test.swift:7:13: note: 'val' declared here
  @Wrap var val
            ^

But this should be valid syntax.

@rintaro
Copy link
Mannequin Author

rintaro mannequin commented Jul 24, 2021

This diagnostic logic (error: found an unexpected second identifier in variable declaration; is there an accidental break?) should not be invoked if the second identifier or _ token is at a new line.

@xwu
Copy link
Collaborator

xwu commented Jul 29, 2021

Mmm, I’m not certain on whether it should be valid syntax. Declaring a variable has always required either an explicit type annotation or an assignment, and this has neither. Certainly I see the point that the wrapper type has left no ambiguity regarding either the type or the initial value here, but we don’t allow users to omit everything to the fullest extent that it can be inferred (for example, in declarations, we always require explicit type annotations): this is a judgment call that has to balance readability and other considerations.

But more to the point, whatever your feeling on the question of “should,” unless I'm mistaken, the compiler is correct that it isn’t valid syntax today. Changing that would require a Swift Evolution proposal, and we could debate then whether it should be allowed, but I don’t think it’s just a starter bug to be fixed. The diagnostic could always be improved though, and the compiler could certainly volunteer here to fill in “= 1”.

@rintaro
Copy link
Mannequin Author

rintaro mannequin commented Jul 29, 2021

I mean the point is not "@Wrap var val;" should be valid or not.

@propertyWrapper
struct Wrap {
  var wrappedValue: Int = 1
}
func test() {
  @Wrap var val
}

This is actually accepted in current Swift5.5.

The point is, if "@Wrap var val;" is valid,

func test() {
  @Wrap var val
  _ = val
}

should be accepted as well because there's no ambiguity here.

@xwu
Copy link
Collaborator

xwu commented Jul 30, 2021

Ah, thanks. I wasn’t aware of that. It makes sense that if the first line is accepted on its own, so should the example above. 🙂

@swift-ci
Copy link
Collaborator

Comment by Nhat Dinh (JIRA)

@rintaro interesting issue. Since no one is assigned yet. I assigned myself. Will work on this after finishing setting up my local environment. One question, what code version or tag is appropriate for reproducing this issue?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@bradleymackey
Copy link
Contributor

Duplicate of #57542

@AnthonyLatsis AnthonyLatsis added property wrappers Feature: property wrappers duplicate Resolution: Duplicates another issue labels Nov 30, 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 duplicate Resolution: Duplicates another issue good first issue Good for newcomers parser Area → compiler: The legacy C++ parser property wrappers Feature: property wrappers
Projects
None yet
Development

No branches or pull requests

4 participants