-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Distinguish between constants and bindings in patterns #1618
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
Comments
Agreed, we're probably unnecessarily lazy in this case... (though some paths of course can only be resolved during inference, so we need to handle partially resolved paths similarly to how rustc does it (that doesn't affect this issue though, one-element paths should always be resolvable during lowering)) |
One thing that is easier with the current setup is that we can get scopes for completion easily. I think the query setup we want is
such that we don't remmeber scopes unnceseccary, but also don't repeat special logic for completion |
Hm, I've looked recently into this and an (simpler?) idea I had is to add a targeted hack somewhere around here: Check if the pattern "resolves" and, if it does, just substitute it with a |
I think it would still mean that that name would resolve to the pattern inside the match arm 😕 |
3262: Fix handling of const patterns r=matklad a=flodiebold E.g. in `match x { None => ... }`, `None` is a path pattern (resolving to the option variant), not a binding. To determine this, we need to try to resolve the name during lowering. This isn't too hard since we already need to resolve names for macro expansion anyway (though maybe a bit hacky). Fixes #1618. Co-authored-by: Florian Diebold <[email protected]>
3262: Fix handling of const patterns r=matklad a=flodiebold E.g. in `match x { None => ... }`, `None` is a path pattern (resolving to the option variant), not a binding. To determine this, we need to try to resolve the name during lowering. This isn't too hard since we already need to resolve names for macro expansion anyway (though maybe a bit hacky). Fixes rust-lang#1618. Co-authored-by: Florian Diebold <[email protected]>
3278: Show more inlay hints r=matklad a=SomeoneToIgnore Closes #3273 As suggested in #1606 (comment) , there is a simpler way to handle inlay hints after #1618 is closed. This PR uses the approach suggested (which results in more type hints for various bindings) and also adds more name hints for function parameters. Examples can be found in the issue: * #3273 (comment) * #3273 (comment) Co-authored-by: Kirill Bulatov <[email protected]>
E.g. in `match x { None => ... }`, `None` is a path pattern (resolving to the option variant), not a binding. To determine this, we need to try to resolve the name during lowering. This isn't too hard since we already need to resolve names for macro expansion anyway (though maybe a bit hacky). Fixes rust-lang#1618.
In rust, a pattern like
x
inmatch foo() { x => () }
might mean one of two things:cont x: i32 = 92
, in which case we match against a literal constant.x
The rule for distinguishing the two cases is: "if there's a constant in scope, the pattern is constant, otherwise it is a binding". We currently treat all such cases as bindings.
I think this should be fixed here:
https://github.com/rust-analyzer/rust-analyzer/blob/ad63fbe61a2e024bab8baadc22863a0795d20a95/crates/ra_hir/src/expr/scope.rs#L90-L100
However, we can't do that with the current code, because we don't have access to
Resolver
there, and so can't check if a name binds to a constant already. The problem with using Resolver here is that resolver already knows about expression scopes.... So looks like we should useModuleItemMap
directly instead.We also should record the fact that binding is a constant somewhere. A table in
InferenceResult
seems like a good place for this:https://github.com/rust-analyzer/rust-analyzer/blob/ad63fbe61a2e024bab8baadc22863a0795d20a95/crates/ra_hir/src/ty/infer.rs#L110-L123
This also makes me think that maybe we should redo our
Expr
infra and resolve expressions during lowering, and not duting type inference....The text was updated successfully, but these errors were encountered: