Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix left shift after macro metavariable in type position #1230

Merged
merged 1 commit into from Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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