Skip to content

Update ungrammar for const block patterns #7010

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

Merged
merged 4 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/hir_def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,9 @@ impl ExprCollector<'_> {
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))
Expand Down
9 changes: 9 additions & 0 deletions crates/parser/src/grammar/expressions/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet =
T![continue],
T![async],
T![try],
T![const],
T![loop],
T![for],
LIFETIME_IDENT,
Expand Down Expand Up @@ -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() {
Expand Down
7 changes: 5 additions & 2 deletions crates/parser/src/grammar/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {} }
Expand Down Expand Up @@ -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);
}

Expand Down
14 changes: 14 additions & 0 deletions crates/parser/src/grammar/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option<CompletedMarker> {
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]).
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions crates/parser/src/syntax_kind/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ pub enum SyntaxKind {
RANGE_PAT,
LITERAL_PAT,
MACRO_PAT,
CONST_BLOCK_PAT,
TUPLE_EXPR,
ARRAY_EXPR,
PAREN_EXPR,
Expand Down
33 changes: 32 additions & 1 deletion crates/syntax/src/ast/generated/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ impl EffectExpr {
pub fn try_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![try]) }
pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) }
pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) }
pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
pub fn block_expr(&self) -> Option<BlockExpr> { support::child(&self.syntax) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -1251,6 +1252,14 @@ impl TupleStructPat {
pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConstBlockPat {
pub(crate) syntax: SyntaxNode,
}
impl ConstBlockPat {
pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
pub fn block_expr(&self) -> Option<BlockExpr> { support::child(&self.syntax) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RecordPatFieldList {
pub(crate) syntax: SyntaxNode,
}
Expand Down Expand Up @@ -1369,6 +1378,7 @@ pub enum Pat {
SlicePat(SlicePat),
TuplePat(TuplePat),
TupleStructPat(TupleStructPat),
ConstBlockPat(ConstBlockPat),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum FieldList {
Expand Down Expand Up @@ -2772,6 +2782,17 @@ impl AstNode for TupleStructPat {
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
impl AstNode for ConstBlockPat {
fn can_cast(kind: SyntaxKind) -> bool { kind == CONST_BLOCK_PAT }
fn cast(syntax: SyntaxNode) -> Option<Self> {
if Self::can_cast(syntax.kind()) {
Some(Self { syntax })
} else {
None
}
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
impl AstNode for RecordPatFieldList {
fn can_cast(kind: SyntaxKind) -> bool { kind == RECORD_PAT_FIELD_LIST }
fn cast(syntax: SyntaxNode) -> Option<Self> {
Expand Down Expand Up @@ -3242,12 +3263,15 @@ impl From<TuplePat> for Pat {
impl From<TupleStructPat> for Pat {
fn from(node: TupleStructPat) -> Pat { Pat::TupleStructPat(node) }
}
impl From<ConstBlockPat> for Pat {
fn from(node: ConstBlockPat) -> Pat { Pat::ConstBlockPat(node) }
}
impl AstNode for Pat {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
IDENT_PAT | BOX_PAT | REST_PAT | LITERAL_PAT | MACRO_PAT | OR_PAT | PAREN_PAT
| PATH_PAT | WILDCARD_PAT | RANGE_PAT | RECORD_PAT | REF_PAT | SLICE_PAT
| TUPLE_PAT | TUPLE_STRUCT_PAT => true,
| TUPLE_PAT | TUPLE_STRUCT_PAT | CONST_BLOCK_PAT => true,
_ => false,
}
}
Expand All @@ -3268,6 +3292,7 @@ impl AstNode for Pat {
SLICE_PAT => Pat::SlicePat(SlicePat { syntax }),
TUPLE_PAT => Pat::TuplePat(TuplePat { syntax }),
TUPLE_STRUCT_PAT => Pat::TupleStructPat(TupleStructPat { syntax }),
CONST_BLOCK_PAT => Pat::ConstBlockPat(ConstBlockPat { syntax }),
_ => return None,
};
Some(res)
Expand All @@ -3289,6 +3314,7 @@ impl AstNode for Pat {
Pat::SlicePat(it) => &it.syntax,
Pat::TuplePat(it) => &it.syntax,
Pat::TupleStructPat(it) => &it.syntax,
Pat::ConstBlockPat(it) => &it.syntax,
}
}
}
Expand Down Expand Up @@ -4137,6 +4163,11 @@ impl std::fmt::Display for TupleStructPat {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for ConstBlockPat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for RecordPatFieldList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
Expand Down
76 changes: 76 additions & 0 deletions crates/syntax/test_data/parser/inline/ok/0156_const_block_pat.rast
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
[email protected]
[email protected]
[email protected] "fn"
[email protected] " "
[email protected]
[email protected] "main"
[email protected]
[email protected] "("
[email protected] ")"
[email protected] " "
[email protected]
[email protected] "{"
[email protected] "\n "
[email protected]
[email protected] "let"
[email protected] " "
[email protected]
[email protected] "const"
[email protected] " "
[email protected]
[email protected] "{"
[email protected] " "
[email protected]
[email protected] "15"
[email protected] " "
[email protected] "}"
[email protected] " "
[email protected] "="
[email protected] " "
[email protected]
[email protected] "("
[email protected] ")"
[email protected] ";"
[email protected] "\n "
[email protected]
[email protected] "let"
[email protected] " "
[email protected]
[email protected] "const"
[email protected] " "
[email protected]
[email protected] "{"
[email protected] " "
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "foo"
[email protected]
[email protected] "("
[email protected] ")"
[email protected] ";"
[email protected] " "
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "bar"
[email protected]
[email protected] "("
[email protected] ")"
[email protected] " "
[email protected] "}"
[email protected] " "
[email protected] "="
[email protected] " "
[email protected]
[email protected] "("
[email protected] ")"
[email protected] ";"
[email protected] "\n"
[email protected] "}"
[email protected] "\n"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let const { 15 } = ();
let const { foo(); bar() } = ();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f() { const { } }
2 changes: 1 addition & 1 deletion xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ flate2 = "1.0"
pico-args = "0.3.1"
proc-macro2 = "1.0.8"
quote = "1.0.2"
ungrammar = "1.4"
ungrammar = "1.5"
walkdir = "2.3.1"
write-json = "0.1.0"
xshell = "0.1"
Expand Down
1 change: 1 addition & 0 deletions xtask/src/ast_src.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pub(crate) const KINDS_SRC: KindsSrc = KindsSrc {
"RANGE_PAT",
"LITERAL_PAT",
"MACRO_PAT",
"CONST_BLOCK_PAT",
// atoms
"TUPLE_EXPR",
"ARRAY_EXPR",
Expand Down