From 61ece4ca3ca0bc893fccd0cf1d26deb2c5dd8c74 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 6 Oct 2022 17:36:06 -0700 Subject: [PATCH] Fix left shift after macro metavariable in type position --- src/expr.rs | 12 +++++++++--- src/ty.rs | 24 ++++++++++++++++++------ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index fbab681474..93a59b0e20 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1371,7 +1371,9 @@ pub(crate) mod parsing { }); } else if Precedence::Cast >= base && input.peek(Token![as]) { let as_token: Token![as] = input.parse()?; - let ty = input.call(Type::without_plus)?; + let allow_plus = false; + let allow_group_generic = false; + let ty = ty::parsing::ambig_ty(input, allow_plus, allow_group_generic)?; check_cast(input)?; lhs = Expr::Cast(ExprCast { attrs: Vec::new(), @@ -1381,7 +1383,9 @@ pub(crate) mod parsing { }); } else if Precedence::Cast >= base && input.peek(Token![:]) && !input.peek(Token![::]) { let colon_token: Token![:] = input.parse()?; - let ty = input.call(Type::without_plus)?; + let allow_plus = false; + let allow_group_generic = false; + let ty = ty::parsing::ambig_ty(input, allow_plus, allow_group_generic)?; check_cast(input)?; lhs = Expr::Type(ExprType { attrs: Vec::new(), @@ -1429,7 +1433,9 @@ pub(crate) mod parsing { }); } else if Precedence::Cast >= base && input.peek(Token![as]) { let as_token: Token![as] = input.parse()?; - let ty = input.call(Type::without_plus)?; + let allow_plus = false; + let allow_group_generic = false; + let ty = ty::parsing::ambig_ty(input, allow_plus, allow_group_generic)?; check_cast(input)?; lhs = Expr::Cast(ExprCast { attrs: Vec::new(), diff --git a/src/ty.rs b/src/ty.rs index 3322709294..4068be3c75 100644 --- a/src/ty.rs +++ b/src/ty.rs @@ -343,7 +343,8 @@ pub mod parsing { impl Parse for Type { fn parse(input: ParseStream) -> Result { let allow_plus = true; - ambig_ty(input, allow_plus) + let allow_group_generic = true; + ambig_ty(input, allow_plus, allow_group_generic) } } @@ -356,11 +357,16 @@ pub mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] pub fn without_plus(input: ParseStream) -> Result { let allow_plus = false; - ambig_ty(input, allow_plus) + let allow_group_generic = true; + ambig_ty(input, allow_plus, allow_group_generic) } } - fn ambig_ty(input: ParseStream, allow_plus: bool) -> Result { + pub(crate) fn ambig_ty( + input: ParseStream, + allow_plus: bool, + allow_group_generic: bool, + ) -> Result { let begin = input.fork(); if input.peek(token::Group) { @@ -381,7 +387,9 @@ pub mod parsing { path: Path::parse_helper(input, false)?, })); } - } else if input.peek(Token![<]) || input.peek(Token![::]) && input.peek3(Token![<]) { + } else if input.peek(Token![<]) && allow_group_generic + || input.peek(Token![::]) && input.peek3(Token![<]) + { if let Type::Path(mut ty) = *group.elem { let arguments = &mut ty.path.segments.last_mut().unwrap().arguments; if let PathArguments::None = arguments { @@ -863,7 +871,8 @@ pub mod parsing { pub(crate) fn parse(input: ParseStream, allow_plus: bool) -> Result { if input.peek(Token![->]) { let arrow = input.parse()?; - let ty = ambig_ty(input, allow_plus)?; + let allow_group_generic = true; + let ty = ambig_ty(input, allow_plus, allow_group_generic)?; Ok(ReturnType::Type(arrow, Box::new(ty))) } else { Ok(ReturnType::Default) @@ -986,7 +995,10 @@ pub mod parsing { let content; Ok(TypeParen { paren_token: parenthesized!(content in input), - elem: Box::new(ambig_ty(&content, allow_plus)?), + elem: Box::new({ + let allow_group_generic = true; + ambig_ty(&content, allow_plus, allow_group_generic)? + }), }) } }