-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Parse] Discriminate local variables #16130
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
Conversation
@swift-ci Please smoke test |
do { | ||
try arg() | ||
} catch let x as Err1 { | ||
print(xRenamed) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let x
above should be renamed as well. Investigating.
@swift-ci Please smoke test |
@@ -581,7 +581,7 @@ std::pair<bool, Pattern*> NameMatcher::walkToPatternPre(Pattern *P) { | |||
if (isDone() || shouldSkip(P->getSourceRange())) | |||
return std::make_pair(false, P); | |||
|
|||
tryResolve(ASTWalker::ParentTy(P), P->getLoc()); | |||
tryResolve(ASTWalker::ParentTy(P), P->getStartLoc()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nkcsgexi is this OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine for patterns. We need to be careful for decls though, sometimes getLoc()
and getStartLoc
point to different position IIRC.
Computing and storing a discriminator means time and memory overhead, and we don't actually need a discriminator because an ordinary local variable doesn't require the creation of anything outside the code-generation of the function. |
If you're really going to attach discriminators to every single local variable, the side-table storage approach is probably unreasonable. |
95f0475
to
3b19a00
Compare
I embedded JustFYI, I measured how many local discriminators are actually set, using Swift stdlib source files. Before: Focused on |
@swift-ci Please smoke test |
I see you removing the local discriminator from serialization completely. Is it really not needed? |
@jrose-apple I believe it's not needed because local discriminators are serialized/deserialized as a part of Decl emission for every |
Ah, I didn't realize that was already there! Okay then. |
Do we really only have 3700 local variable declarations in the stdlib? That seems extremely low. Are we not setting a discriminator on parameters? I guess maybe we don't need to because we can identify them reliably relative to the function they're parameters of; that would definitely keep the count down. |
3b19a00
to
d086445
Compare
Ah, I forgot about func foo(_ arg: Int) {
var arg = arg;
} Updated the PR. As a side note, this PR impacts the parse time by around 1% (0.654s -> 0.662s) and max rss by around 1% (165,376,000 -> 167,411,712). |
@swift-ci Please smoke test |
1 similar comment
@swift-ci Please smoke test |
Are you sure you can't distinguish ParamDecls implicitly? A ParamDecl should always be uniquely identified by (1) the function it's a parameter of, (2) the fact that it's a parameter, and (3) its index. |
Not that it really hurts to discriminate them, given that we're paying the memory cost in the base class anyway. |
Distinguishing ParamDecls without using LocalDiscriminator is technically feasible, but I think using LocalDiscriminator is easiest/convenient way to achieve that. And, as you said, we already have facility for that. |
@swift-ci Please smoke test |
@rjmccall Could you approve this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR looks good to me, but I think you should consider adding some way to test local discriminators more directly. Maybe a testing switch that prints declaration names with their line, column, and discriminator?
Set local discriminator for all local `VarDecl`s. Otherwise, they cannot be discriminated with USRs. This change is needed for rename refactoring which uses USR for discrimiating variable names. https://bugs.swift.org/browse/SR-7205, rdar://problem/34701880
For Example `getLoc()` for `foo as Ty`(`IsPattern`) is at 'as'. We should try to resolve this at position of 'foo'.
Local discriminators are emitted as a part of decl.
We have to discriminate between params and local variables.
d086445
to
a6f7a8e
Compare
Thanks. That is actually a good idea! |
Resolved conflicts. |
Set local discriminator for all local
VarDecl
s. Otherwise, they cannot be discriminated with USRs. This change is needed for rename refactoring which uses USR for discrimiating variable names.SR-7205, rdar://problem/34701880