Skip to content

Commit

Permalink
Merge pull request #1000 from dtolnay/constdefault
Browse files Browse the repository at this point in the history
Limit const generic default value expr to lit or braced expr
  • Loading branch information
dtolnay authored Mar 29, 2021
2 parents 1d6e3a3 + 0b11ad4 commit 1a31fb2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ pub mod parsing {
eq_token: {
if input.peek(Token![=]) {
let eq_token = input.parse()?;
default = Some(input.parse::<Expr>()?);
default = Some(path::parsing::const_argument(input)?);
Some(eq_token)
} else {
None
Expand Down
48 changes: 29 additions & 19 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,31 +241,41 @@ pub mod parsing {
}
}

if input.peek(Lit) {
let lit = input.parse()?;
return Ok(GenericArgument::Const(Expr::Lit(lit)));
if input.peek(Lit) || input.peek(token::Brace) {
return const_argument(input).map(GenericArgument::Const);
}

if input.peek(token::Brace) {
#[cfg(feature = "full")]
{
let block: ExprBlock = input.parse()?;
return Ok(GenericArgument::Const(Expr::Block(block)));
}
input.parse().map(GenericArgument::Type)
}
}

#[cfg(not(feature = "full"))]
{
let begin = input.fork();
let content;
braced!(content in input);
content.parse::<Expr>()?;
let verbatim = verbatim::between(begin, input);
return Ok(GenericArgument::Const(Expr::Verbatim(verbatim)));
}
pub fn const_argument(input: ParseStream) -> Result<Expr> {
let lookahead = input.lookahead1();

if input.peek(Lit) {
let lit = input.parse()?;
return Ok(Expr::Lit(lit));
}

if input.peek(token::Brace) {
#[cfg(feature = "full")]
{
let block: ExprBlock = input.parse()?;
return Ok(Expr::Block(block));
}

input.parse().map(GenericArgument::Type)
#[cfg(not(feature = "full"))]
{
let begin = input.fork();
let content;
braced!(content in input);
content.parse::<Expr>()?;
let verbatim = verbatim::between(begin, input);
return Ok(Expr::Verbatim(verbatim));
}
}

Err(lookahead.error())
}

#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
Expand Down
9 changes: 0 additions & 9 deletions tests/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ static EXCLUDE: &[&str] = &[
"src/test/ui/generic-associated-types/generic-associated-type-bounds.rs",
"src/test/ui/generic-associated-types/issue-80433-reduced.rs",

// TODO: const generics parameter default value
// https://github.com/dtolnay/syn/issues/980
"src/test/ui/const-generics/defaults/auxiliary/const_defaulty.rs",
"src/test/ui/const-generics/defaults/const-default.rs",
"src/test/ui/const-generics/defaults/default-annotation.rs",
"src/test/ui/const-generics/defaults/external.rs",
"src/test/ui/const-generics/defaults/pretty-printing-ast.rs",
"src/test/ui/const-generics/min_const_generics/type_and_const_defaults.rs",

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

0 comments on commit 1a31fb2

Please sign in to comment.