Skip to content

Commit

Permalink
Merge pull request #1230 from dtolnay/groupgeneric
Browse files Browse the repository at this point in the history
Fix left shift after macro metavariable in type position
  • Loading branch information
dtolnay committed Oct 7, 2022
2 parents 7909596 + 61ece4c commit 691fc06
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/expr.rs
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
24 changes: 18 additions & 6 deletions src/ty.rs
Expand Up @@ -343,7 +343,8 @@ pub mod parsing {
impl Parse for Type {
fn parse(input: ParseStream) -> Result<Self> {
let allow_plus = true;
ambig_ty(input, allow_plus)
let allow_group_generic = true;
ambig_ty(input, allow_plus, allow_group_generic)
}
}

Expand All @@ -356,11 +357,16 @@ pub mod parsing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
pub fn without_plus(input: ParseStream) -> Result<Self> {
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<Type> {
pub(crate) fn ambig_ty(
input: ParseStream,
allow_plus: bool,
allow_group_generic: bool,
) -> Result<Type> {
let begin = input.fork();

if input.peek(token::Group) {
Expand All @@ -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 {
Expand Down Expand Up @@ -863,7 +871,8 @@ pub mod parsing {
pub(crate) fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> {
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)
Expand Down Expand Up @@ -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)?
}),
})
}
}
Expand Down

0 comments on commit 691fc06

Please sign in to comment.