-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Lower range patterns from AST to HIR #12210
Comments
How go to definition and semantic highlighting is working in those pattern currently? In this code: pub const L: i32 = 6;
mod x {
pub const R: i32 = 100;
}
const fn f(x: i32) -> i32 {
match x {
-1..=5 => x * 10,
L..=x::R => x * 100,
_ => x,
}
} Everything works for |
rust-analyzer/crates/hir/src/semantics.rs Lines 450 to 452 in c06f088
rust-analyzer/crates/hir/src/source_analyzer.rs Lines 493 to 509 in c06f088
As that function fails there, |
RANGE_PAT@617..625
IDENT_PAT@617..618
NAME@617..618
IDENT@617..618 "L"
DOT2EQ@618..621 "..="
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "x"
COLON2@622..624 "::"
PATH_SEGMENT@624..625
NAME_REF@624..625
IDENT@624..625 "R" Isn't this wrong? The reference says that only path is allowed here. I think it would work correctly if it was parsed as |
Our grammar differs from the reference in a few parts and that's fine (and in parts deliberate), so I think we want to keep the parse tree as is. |
yup, on the level of concrete syntax,
The general philosophy is to parse what's natural on purely syntactic basis, and then place additional restrictions when checking ast/lowering to hir. |
Current state of this is the given fixme here rust-analyzer/crates/hir-def/src/body/lower.rs Line 1688 in 70348fa
Also of note is that 2d4d6b6 blurred the line between expressions and patterns for lowering now, where destructing expressions are lowered to patterns. This is demanding the other part, lowering patterns to expressions. |
Hm, to me it seems correct to use
pat..pat
in the syntax, butexpr..expr
in the hir.Syntactically, they are patterns, as
..
is an infix binary operator. We could add some LR-style re-structuring of the tree, where we don't create a pat until we haven't seen..
, but that seems complicated. Feels more natural to parse a pat, than notice an..
, and parse anotherpat
.But yeah, in semantics we want to keep those as expressions, so during lowering we need to figure out that, what was a pattern syntactically, actually is an expression semantically.
This feel dual to recently stabilized destructive assignment, where we parse stuff like
(a, b) = (b, a)
asexpr = expr
, but, during lowering, lower the LHS as a pattern.Though, seeing the two example side-by-side, maybe we should bite the bullet and just don't distinguish patterns and expressions at the level of syntax? That is, we'd use
ast::Expr
for both patterns and expressions. We'd then have an API likeOriginally posted by @matklad in #12158 (comment)
The text was updated successfully, but these errors were encountered: