Skip to content

Commit

Permalink
Parse const blocks in expression position
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 27, 2020
1 parent f6185b9 commit 895cff4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,8 @@ pub(crate) mod parsing {
input.call(expr_yield).map(Expr::Yield)
} else if input.peek(Token![unsafe]) {
input.call(expr_unsafe).map(Expr::Unsafe)
} else if input.peek(Token![const]) {
input.call(expr_const).map(Expr::Verbatim)
} else if input.peek(token::Brace) {
input.call(expr_block).map(Expr::Block)
} else if input.peek(Token![..]) {
Expand Down Expand Up @@ -1912,6 +1914,8 @@ pub(crate) mod parsing {
Expr::TryBlock(input.call(expr_try_block)?)
} else if input.peek(Token![unsafe]) {
Expr::Unsafe(input.call(expr_unsafe)?)
} else if input.peek(Token![const]) {
Expr::Verbatim(input.call(expr_const)?)
} else if input.peek(token::Brace) {
Expr::Block(input.call(expr_block)?)
} else {
Expand Down Expand Up @@ -2485,6 +2489,19 @@ pub(crate) mod parsing {
})
}

#[cfg(feature = "full")]
fn expr_const(input: ParseStream) -> Result<TokenStream> {
let begin = input.fork();
input.parse::<Token![const]>()?;

let content;
braced!(content in input);
content.call(Attribute::parse_inner)?;
content.call(Block::parse_within)?;

Ok(verbatim::between(begin, input))
}

#[cfg(feature = "full")]
pub fn expr_block(input: ParseStream) -> Result<ExprBlock> {
let label: Option<Label> = input.parse()?;
Expand Down
2 changes: 1 addition & 1 deletion src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub mod parsing {
|| input.peek(Token![extern])
|| input.peek(Token![use])
|| input.peek(Token![static]) && (input.peek2(Token![mut]) || input.peek2(Ident))
|| input.peek(Token![const])
|| input.peek(Token![const]) && !input.peek2(token::Brace)
|| input.peek(Token![unsafe]) && !input.peek2(token::Brace)
|| input.peek(Token![async])
&& (input.peek2(Token![unsafe])
Expand Down
5 changes: 1 addition & 4 deletions tests/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ const REVISION: &str = "9d78d1d02761b906038ba4d54c5f3427f920f5fb";

#[rustfmt::skip]
static EXCLUDE: &[&str] = &[
// TODO: const block (#921)
"src/test/ui/inline-const/const-expr-array-init.rs",
"src/test/ui/inline-const/const-expr-basic.rs",
"src/test/ui/inline-const/const-expr-reference.rs",
// TODO: const block in pattern position (#921)
"src/test/ui/inline-const/const-match-pat-range.rs",
"src/test/ui/inline-const/const-match-pat.rs",

Expand Down
5 changes: 4 additions & 1 deletion tests/test_precedence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ fn librustc_brackets(mut librustc_expr: P<ast::Expr>) -> Option<P<ast::Expr>> {

impl MutVisitor for BracketsVisitor {
fn visit_expr(&mut self, e: &mut P<Expr>) {
noop_visit_expr(e, self);
match e.kind {
ExprKind::ConstBlock(..) => {}
_ => noop_visit_expr(e, self),
}
match e.kind {
ExprKind::If(..) | ExprKind::Block(..) | ExprKind::Let(..) => {}
_ => {
Expand Down

0 comments on commit 895cff4

Please sign in to comment.