-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Shorthand optional binding has no reference to original variable #61509
Comments
cc @bnbarham |
rdar://100006415 This is also the case for closure capture lists, I suspect it's just very uncommon for that to be anything other than If a shorthand if-let or closure capture shadows a non-local variable (including parameter when there's no explicit label), we'll need to add that local + a relation to the original variable. @SimplyDanny what were you looking for in:
? I wouldn't expect jump to def from the
(this is the no explicit label case I'm referring to above) |
Well, I was thinking about exactly that. From the
I don't understand the difference between a parameter with or without a label. Why does it need special consideration? In both cases I would expect a reference to the parameter name (not the argument label). |
We could add a relation for the variable being shadowed in general, I can see that being useful. I just wouldn't expect it to show up in either help or jump to definition.
We don't index locals by default (though it is an option). But we do index parameters without labels as they can be referenced externally in the call site. So for the default case (of not indexing locals), we still need to add this information to the index, despite it being a local. |
Both make sense to me now. Thank you! |
Oh thanks for reminding me @ileitch! FWIW I didn't fix this the way I commented on here. It was indeed 7b4ab8d. Indexing now ignores the if let binding and just treats all references as references to the shadowed-decl. This is technically not "correct", but I think works for most cases since the decl location == the reference location on the binding anyway. @SimplyDanny does this work for your purposes? |
Yes, works very well now for my use cases. Thank you, @bnbarham! |
Describe the bug
The new shorthand syntax for optional binding in
if
,guard
andwhile
conditions does not provide a reference to the original bound variable. This is also not the case for the extended syntaxlet i = i
but with it we are able to jump to the original via the variable reference on the right-hand side.Steps To Reproduce
Run
swift-ide-test -print-indexed-symbols -source-filename
for the filemain.swift
with the contentWe receive the following output:
Notice that while the binding
let i = i
references the original class propertyi
this is not the case for the bindinglet i
.This is an issue in editors where a "Jump to Definition" doesn't work anymore. Even more problematic, however, it is for linter tools which are now unable to answer questions like "Are all declared variables used somewhere?". I especially want to mention SwiftLint, the
unused_declaration
rule of which is currently not working.Expected behavior
A redeclared variable should have a reference to its original.
Writing this sentence I feel like this is more a feature request than a bug. It would also be helpful in code like
where "Jump to Definition" on
var a
would jump to the function parameter.Environment
The text was updated successfully, but these errors were encountered: