From 0b11ad436630fa8700845337f4d2197853ed0b21 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 28 Mar 2021 17:07:15 -0400 Subject: [PATCH] Limit const generic default value expr to lit or braced expr --- src/generics.rs | 2 +- src/path.rs | 48 ++++++++++++++++++++++++++++------------------- tests/repo/mod.rs | 9 --------- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/generics.rs b/src/generics.rs index 87f7fdd865..de0772a704 100644 --- a/src/generics.rs +++ b/src/generics.rs @@ -880,7 +880,7 @@ pub mod parsing { eq_token: { if input.peek(Token![=]) { let eq_token = input.parse()?; - default = Some(input.parse::()?); + default = Some(path::parsing::const_argument(input)?); Some(eq_token) } else { None diff --git a/src/path.rs b/src/path.rs index 6c72f9cffa..90e2970d30 100644 --- a/src/path.rs +++ b/src/path.rs @@ -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::()?; - let verbatim = verbatim::between(begin, input); - return Ok(GenericArgument::Const(Expr::Verbatim(verbatim))); - } + pub fn const_argument(input: ParseStream) -> Result { + 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::()?; + let verbatim = verbatim::between(begin, input); + return Ok(Expr::Verbatim(verbatim)); + } } + + Err(lookahead.error()) } #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] diff --git a/tests/repo/mod.rs b/tests/repo/mod.rs index d27893b6cc..eeebbb8279 100644 --- a/tests/repo/mod.rs +++ b/tests/repo/mod.rs @@ -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",