Skip to content

Commit

Permalink
More precise decision to parse expression after return keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 6, 2023
1 parent c6a651a commit 20497e1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
39 changes: 28 additions & 11 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,8 @@ pub(crate) fn requires_terminator(expr: &Expr) -> bool {
#[cfg(feature = "parsing")]
pub(crate) mod parsing {
use super::*;
#[cfg(feature = "full")]
use crate::ext::IdentExt;
use crate::parse::discouraged::Speculative;
#[cfg(feature = "full")]
use crate::parse::ParseBuffer;
Expand Down Expand Up @@ -1155,6 +1157,25 @@ pub(crate) mod parsing {
}
}

#[cfg(feature = "full")]
fn can_begin_expr(input: ParseStream) -> bool {
input.peek(Ident::peek_any) // value name or keyword
|| input.peek(token::Paren) // tuple
|| input.peek(token::Bracket) // array
|| input.peek(token::Brace) // block
|| input.peek(Lit) // literal
|| input.peek(Token![!]) && !input.peek(Token![!=]) // operator not
|| input.peek(Token![-]) && !input.peek(Token![-=]) && !input.peek(Token![->]) // unary minus
|| input.peek(Token![*]) && !input.peek(Token![*=]) // dereference
|| input.peek(Token![|]) && !input.peek(Token![|=]) // closure
|| input.peek(Token![&]) && !input.peek(Token![&=]) // reference
|| input.peek(Token![..]) // range notation
|| input.peek(Token![<]) && !input.peek(Token![<=]) && !input.peek(Token![<<=]) // associated path
|| input.peek(Token![::]) // global path
|| input.peek(Lifetime) // labeled loop
|| input.peek(Token![#]) // expression attributes
}

#[cfg(feature = "full")]
fn parse_expr(
input: ParseStream,
Expand Down Expand Up @@ -2247,7 +2268,7 @@ pub(crate) mod parsing {
attrs: Vec::new(),
yield_token: input.parse()?,
expr: {
if !input.is_empty() && !input.peek(Token![,]) && !input.peek(Token![;]) {
if can_begin_expr(input) {
Some(input.parse()?)
} else {
None
Expand Down Expand Up @@ -2447,15 +2468,11 @@ pub(crate) mod parsing {
break_token: input.parse()?,
label: input.parse()?,
expr: {
if input.is_empty()
|| input.peek(Token![,])
|| input.peek(Token![;])
|| !allow_struct.0 && input.peek(token::Brace)
{
None
} else {
if can_begin_expr(input) && (allow_struct.0 || !input.peek(token::Brace)) {
let expr = ambiguous_expr(input, allow_struct)?;
Some(Box::new(expr))
} else {
None
}
},
})
Expand All @@ -2467,16 +2484,16 @@ pub(crate) mod parsing {
attrs: Vec::new(),
return_token: input.parse()?,
expr: {
if input.is_empty() || input.peek(Token![,]) || input.peek(Token![;]) {
None
} else {
if can_begin_expr(input) {
// NOTE: return is greedy and eats blocks after it even when in a
// position where structs are not allowed, such as in if statement
// conditions. For example:
//
// if return { println!("A") } {} // Prints "A"
let expr = ambiguous_expr(input, allow_struct)?;
Some(Box::new(expr))
} else {
None
}
},
})
Expand Down
4 changes: 0 additions & 4 deletions tests/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ static EXCLUDE_FILES: &[&str] = &[
// https://github.com/dtolnay/syn/issues/1527
"tests/ui/parser/struct-literal-in-match-guard.rs",

// TODO: precedence of return in match guard
// https://github.com/dtolnay/syn/issues/1528
"tests/ui/unreachable-code.rs",

// Compile-fail expr parameter in const generic position: f::<1 + 2>()
"tests/ui/const-generics/early/closing-args-token.rs",
"tests/ui/const-generics/early/const-expression-parameter.rs",
Expand Down

0 comments on commit 20497e1

Please sign in to comment.