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-9171] inout-to-pointer in a subscript arg could use a better diagnostic #51664

Closed
hamishknight opened this issue Nov 3, 2018 · 8 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers type checker Area → compiler: Semantic analysis

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-9171
Radar rdar://problem/45819956
Original Reporter @hamishknight
Type Bug
Status Closed
Resolution Done
Environment

Swift version 4.2-dev (LLVM b70847456e, Clang 86404441b8, Swift 17808c32fe)
Target: x86_64-apple-darwin17.7.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, StarterBug, TypeChecker
Assignee @hamishknight
Priority Medium

md5: 838ee52675f035a4d8432cf8786d8b1b

relates to:

  • SR-9170 array-to-pointer in subscript arg crashes compiler

Issue Description:

The diagnostic for the following could be improved to let the user know that you cannot pass an inout argument to a subscript:

struct S1 {
  subscript(takesPtr ptr: UnsafeMutablePointer<Int>) -> Int {
    get { return 0 }
  }
}

let s1 = S1()
var i = 0
_ = s1[takesPtr: &i] // error: Use of extraneous '&'
@belkadan
Copy link
Contributor

belkadan commented Nov 5, 2018

Think this is simple enough for a StarterBug? cc @xedin too

@xedin
Copy link
Member

xedin commented Nov 5, 2018

Yes, I think it makes sense to mark it as StarterBug, the problem is related to `PreCheckExpression`, we don't properly recognize all of the cases there when checking `InOutExpr` position.

@xedin
Copy link
Member

xedin commented Nov 5, 2018

@swift-ci create

@hamishknight
Copy link
Collaborator Author

Also agreed that it makes a good starter bug. If you're interested in fixing, as Pavel says, you'll want to start here, which is where the "Use of extraneous '&'" diag gets emitted before the expression is type checked.

The condition is checking for TupleExpr and ParenExpr, which are currently parent expressions of an argument, and then checking for the parent expression of that, which should be the expression making the call. You'd want to add a condition checking if that parent is a SubscriptExpr, and if so emit a custom diagnostic (adding the message to DiagnosticsSema.def, and using a call similar to diag::extraneous_address_of).

Note this won't handle obscure things like s1[takesPtr: (&i)], you'd need to keep walking up the parent chain to handle that (and that would also be a good opportunity to emit a better diagnostic for applies with inout arguments wrapped in parentheses, which we currently also reject with "Use of extraneous '&'").

@xedin
Copy link
Member

xedin commented Nov 5, 2018

I'm going to push a trivial change to make it so type-checker gets past PreCheckExpression in situations like this, but improving diagnostic to be aware of implicit conversions would be less trivial, logic in MiscDiagnostics would have to be adjusted to make it work.

@xedin
Copy link
Member

xedin commented Nov 5, 2018

Actually I think we should be able to diagnose this directly if we see call like that in `PreCheckExpression`.

@xedin
Copy link
Member

xedin commented Nov 6, 2018

Should be fixed by #20336

Please verify using next master snapshot.

@hamishknight
Copy link
Collaborator Author

Thanks Pavel!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants