Skip to content

Commit cc2f5f9

Browse files
committed
Do not accept interpolated types as traits in trait impls
1 parent c90f682 commit cc2f5f9

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/libsyntax/parse/parser.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -5518,6 +5518,7 @@ impl<'a> Parser<'a> {
55185518
};
55195519

55205520
// Parse both types and traits as a type, then reinterpret if necessary.
5521+
let ty_first_interpolated = if self.token.is_ty() { Some(self.span) } else { None };
55215522
let ty_first = self.parse_ty()?;
55225523

55235524
// If `for` is missing we try to recover.
@@ -5547,8 +5548,16 @@ impl<'a> Parser<'a> {
55475548

55485549
let ty_first = ty_first.into_inner();
55495550
let path = match ty_first.node {
5550-
// This notably includes paths passed through `ty` macro fragments (#46438).
5551-
TyKind::Path(None, path) => path,
5551+
TyKind::Path(None, path) => {
5552+
if let Some(span) = ty_first_interpolated {
5553+
self.diagnostic()
5554+
.struct_span_warn(span, "expected a trait, found type")
5555+
.note("this warning will become a hard error in a future release")
5556+
.span_label(span, "try using `path` instead of `ty` \
5557+
for this macro fragment").emit();
5558+
}
5559+
path
5560+
}
55525561
_ => {
55535562
self.span_err(ty_first.span, "expected a trait, found type");
55545563
ast::Path::from_ident(ty_first.span, keywords::Invalid.ident())

src/libsyntax/parse/token.rs

+10
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,16 @@ impl Token {
315315
false
316316
}
317317

318+
/// Returns `true` if the token is an interpolated type.
319+
pub fn is_ty(&self) -> bool {
320+
if let Interpolated(ref nt) = *self {
321+
if let NtTy(..) = nt.0 {
322+
return true;
323+
}
324+
}
325+
false
326+
}
327+
318328
/// Returns a lifetime with the span and a dummy id if it is a lifetime,
319329
/// or the original lifetime if it is an interpolated lifetime, ignoring
320330
/// the span.

src/test/compile-fail/issue-46438.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
macro_rules! m {
1212
($my_type: ty) => {
13-
impl $my_type for u8 {}
13+
impl $my_type for u8 {} //~ WARN expected a trait, found type
1414
}
1515
}
1616

0 commit comments

Comments
 (0)