Skip to content

Commit 691fc06

Browse files
authored
Merge pull request #1230 from dtolnay/groupgeneric
Fix left shift after macro metavariable in type position
2 parents 7909596 + 61ece4c commit 691fc06

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/expr.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,9 @@ pub(crate) mod parsing {
13711371
});
13721372
} else if Precedence::Cast >= base && input.peek(Token![as]) {
13731373
let as_token: Token![as] = input.parse()?;
1374-
let ty = input.call(Type::without_plus)?;
1374+
let allow_plus = false;
1375+
let allow_group_generic = false;
1376+
let ty = ty::parsing::ambig_ty(input, allow_plus, allow_group_generic)?;
13751377
check_cast(input)?;
13761378
lhs = Expr::Cast(ExprCast {
13771379
attrs: Vec::new(),
@@ -1381,7 +1383,9 @@ pub(crate) mod parsing {
13811383
});
13821384
} else if Precedence::Cast >= base && input.peek(Token![:]) && !input.peek(Token![::]) {
13831385
let colon_token: Token![:] = input.parse()?;
1384-
let ty = input.call(Type::without_plus)?;
1386+
let allow_plus = false;
1387+
let allow_group_generic = false;
1388+
let ty = ty::parsing::ambig_ty(input, allow_plus, allow_group_generic)?;
13851389
check_cast(input)?;
13861390
lhs = Expr::Type(ExprType {
13871391
attrs: Vec::new(),
@@ -1429,7 +1433,9 @@ pub(crate) mod parsing {
14291433
});
14301434
} else if Precedence::Cast >= base && input.peek(Token![as]) {
14311435
let as_token: Token![as] = input.parse()?;
1432-
let ty = input.call(Type::without_plus)?;
1436+
let allow_plus = false;
1437+
let allow_group_generic = false;
1438+
let ty = ty::parsing::ambig_ty(input, allow_plus, allow_group_generic)?;
14331439
check_cast(input)?;
14341440
lhs = Expr::Cast(ExprCast {
14351441
attrs: Vec::new(),

src/ty.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ pub mod parsing {
343343
impl Parse for Type {
344344
fn parse(input: ParseStream) -> Result<Self> {
345345
let allow_plus = true;
346-
ambig_ty(input, allow_plus)
346+
let allow_group_generic = true;
347+
ambig_ty(input, allow_plus, allow_group_generic)
347348
}
348349
}
349350

@@ -356,11 +357,16 @@ pub mod parsing {
356357
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
357358
pub fn without_plus(input: ParseStream) -> Result<Self> {
358359
let allow_plus = false;
359-
ambig_ty(input, allow_plus)
360+
let allow_group_generic = true;
361+
ambig_ty(input, allow_plus, allow_group_generic)
360362
}
361363
}
362364

363-
fn ambig_ty(input: ParseStream, allow_plus: bool) -> Result<Type> {
365+
pub(crate) fn ambig_ty(
366+
input: ParseStream,
367+
allow_plus: bool,
368+
allow_group_generic: bool,
369+
) -> Result<Type> {
364370
let begin = input.fork();
365371

366372
if input.peek(token::Group) {
@@ -381,7 +387,9 @@ pub mod parsing {
381387
path: Path::parse_helper(input, false)?,
382388
}));
383389
}
384-
} else if input.peek(Token![<]) || input.peek(Token![::]) && input.peek3(Token![<]) {
390+
} else if input.peek(Token![<]) && allow_group_generic
391+
|| input.peek(Token![::]) && input.peek3(Token![<])
392+
{
385393
if let Type::Path(mut ty) = *group.elem {
386394
let arguments = &mut ty.path.segments.last_mut().unwrap().arguments;
387395
if let PathArguments::None = arguments {
@@ -863,7 +871,8 @@ pub mod parsing {
863871
pub(crate) fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> {
864872
if input.peek(Token![->]) {
865873
let arrow = input.parse()?;
866-
let ty = ambig_ty(input, allow_plus)?;
874+
let allow_group_generic = true;
875+
let ty = ambig_ty(input, allow_plus, allow_group_generic)?;
867876
Ok(ReturnType::Type(arrow, Box::new(ty)))
868877
} else {
869878
Ok(ReturnType::Default)
@@ -986,7 +995,10 @@ pub mod parsing {
986995
let content;
987996
Ok(TypeParen {
988997
paren_token: parenthesized!(content in input),
989-
elem: Box::new(ambig_ty(&content, allow_plus)?),
998+
elem: Box::new({
999+
let allow_group_generic = true;
1000+
ambig_ty(&content, allow_plus, allow_group_generic)?
1001+
}),
9901002
})
9911003
}
9921004
}

0 commit comments

Comments
 (0)