diff --git a/Cargo.lock b/Cargo.lock index fbb79e01fc2f..891cff55e372 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1827,9 +1827,9 @@ checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c" [[package]] name = "ungrammar" -version = "1.4.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68951379f3ced25754472ca5addbf74d7dab58c9818f49290a3d8caa3ab44fb7" +checksum = "c11bffada52edc8f2a56160b286ea4640acf90ffcb21bded361ccb8ed43a1457" [[package]] name = "unicase" diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 0f404be1b7c4..978c3a32498c 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -933,7 +933,9 @@ fn collect_pat(&mut self, pat: ast::Pat) -> PatId { Pat::Box { inner } } // FIXME: implement - ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing, + ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) | ast::Pat::ConstBlockPat(_) => { + Pat::Missing + } }; let ptr = AstPtr::new(&pat); self.alloc_pat(pattern, Either::Left(ptr)) diff --git a/crates/parser/src/grammar/expressions/atom.rs b/crates/parser/src/grammar/expressions/atom.rs index e897d5a52f04..c7a3556a7750 100644 --- a/crates/parser/src/grammar/expressions/atom.rs +++ b/crates/parser/src/grammar/expressions/atom.rs @@ -46,6 +46,7 @@ pub(crate) fn literal(p: &mut Parser) -> Option { T![continue], T![async], T![try], + T![const], T![loop], T![for], LIFETIME_IDENT, @@ -115,6 +116,14 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar block_expr(p); m.complete(p, EFFECT_EXPR) } + // test const_block + // fn f() { const { } } + T![const] if la == T!['{'] => { + let m = p.start(); + p.bump(T![const]); + block_expr(p); + m.complete(p, EFFECT_EXPR) + } T!['{'] => { // test for_range_from // fn foo() { diff --git a/crates/parser/src/grammar/items.rs b/crates/parser/src/grammar/items.rs index 8999829b437f..72b73f89174d 100644 --- a/crates/parser/src/grammar/items.rs +++ b/crates/parser/src/grammar/items.rs @@ -96,7 +96,10 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> { let mut has_mods = false; // modifiers - has_mods |= p.eat(T![const]); + if p.at(T![const]) && p.nth(1) != T!['{'] { + p.eat(T![const]); + has_mods = true; + } // test_err async_without_semicolon // fn foo() { let _ = async {} } @@ -167,7 +170,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> { m.complete(p, TRAIT); } - T![const] => { + T![const] if p.nth(1) != T!['{'] => { consts::konst(p, m); } diff --git a/crates/parser/src/grammar/patterns.rs b/crates/parser/src/grammar/patterns.rs index 7e7f73deeac9..b53d5749f42b 100644 --- a/crates/parser/src/grammar/patterns.rs +++ b/crates/parser/src/grammar/patterns.rs @@ -89,6 +89,7 @@ fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option { let m = match p.nth(0) { T![box] => box_pat(p), T![ref] | T![mut] => ident_pat(p, true), + T![const] => const_block_pat(p), IDENT => match p.nth(1) { // Checks the token after an IDENT to see if a pattern is a path (Struct { .. }) or macro // (T![x]). @@ -386,3 +387,16 @@ fn box_pat(p: &mut Parser) -> CompletedMarker { pattern_single(p); m.complete(p, BOX_PAT) } + +// test const_block_pat +// fn main() { +// let const { 15 } = (); +// let const { foo(); bar() } = (); +// } +fn const_block_pat(p: &mut Parser) -> CompletedMarker { + assert!(p.at(T![const])); + let m = p.start(); + p.bump(T![const]); + expressions::block_expr(p); + m.complete(p, CONST_BLOCK_PAT) +} diff --git a/crates/parser/src/syntax_kind/generated.rs b/crates/parser/src/syntax_kind/generated.rs index 980aa59794f8..f69e71bdba65 100644 --- a/crates/parser/src/syntax_kind/generated.rs +++ b/crates/parser/src/syntax_kind/generated.rs @@ -170,6 +170,7 @@ pub enum SyntaxKind { RANGE_PAT, LITERAL_PAT, MACRO_PAT, + CONST_BLOCK_PAT, TUPLE_EXPR, ARRAY_EXPR, PAREN_EXPR, diff --git a/crates/syntax/src/ast/generated/nodes.rs b/crates/syntax/src/ast/generated/nodes.rs index 1588ba93ea27..c5b80bffe742 100644 --- a/crates/syntax/src/ast/generated/nodes.rs +++ b/crates/syntax/src/ast/generated/nodes.rs @@ -763,6 +763,7 @@ pub fn label(&self) -> Option