Skip to content

Commit

Permalink
Merge pull request #774 from dtolnay/raw
Browse files Browse the repository at this point in the history
Fix raw operator parsing to require const keyword
  • Loading branch information
dtolnay authored Feb 24, 2020
2 parents 76134b7 + d7ccd42 commit fc9b10e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1480,10 +1480,16 @@ pub(crate) mod parsing {
input.advance_to(&ahead);
if input.peek(Token![&]) {
let and_token: Token![&] = input.parse()?;
let raw: Option<raw> = input.parse()?;
let raw: Option<raw> = if input.peek(raw)
&& (input.peek2(Token![mut]) || input.peek2(Token![const]))
{
Some(input.parse()?)
} else {
None
};
let mutability: Option<Token![mut]> = input.parse()?;
if raw.is_some() && mutability.is_none() {
input.parse::<Option<Token![const]>>()?;
input.parse::<Token![const]>()?;
}
let expr = Box::new(unary_expr(input, allow_struct)?);
if raw.is_some() {
Expand Down
44 changes: 44 additions & 0 deletions tests/test_stmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#[macro_use]
mod macros;

use syn::Stmt;

#[test]
fn test_raw_operator() {
let stmt = syn::parse_str::<Stmt>("let _ = &raw const x;").unwrap();

snapshot!(stmt, @r###"
Local(Local {
pat: Pat::Wild,
init: Some(Verbatim(`& raw const x`)),
})
"###);
}

#[test]
fn test_raw_variable() {
let stmt = syn::parse_str::<Stmt>("let _ = &raw;").unwrap();

snapshot!(stmt, @r###"
Local(Local {
pat: Pat::Wild,
init: Some(Expr::Reference {
expr: Expr::Path {
path: Path {
segments: [
PathSegment {
ident: "raw",
arguments: None,
},
],
},
},
}),
})
"###);
}

#[test]
fn test_raw_invalid() {
assert!(syn::parse_str::<Stmt>("let _ = &raw x;").is_err());
}

0 comments on commit fc9b10e

Please sign in to comment.