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

Do not accept interpolated types as traits in trait impls #48502

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5518,6 +5518,7 @@ impl<'a> Parser<'a> {
};

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

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

let ty_first = ty_first.into_inner();
let path = match ty_first.node {
// This notably includes paths passed through `ty` macro fragments (#46438).
TyKind::Path(None, path) => path,
TyKind::Path(None, path) => {
if let Some(span) = ty_first_interpolated {
self.diagnostic()
.struct_span_warn(span, "expected a trait, found type")
.note("this warning will become a hard error in a future release")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we can't use the standard lint infrastructure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's too early in the parser, we don't even have NodeIds yet to attach the lint.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Before we merge, let's try to follow the other steps though, i.e., create a tracking issue and so forth.

.span_label(span, "try using `path` instead of `ty` \
for this macro fragment").emit();
}
path
}
_ => {
self.span_err(ty_first.span, "expected a trait, found type");
ast::Path::from_ident(ty_first.span, keywords::Invalid.ident())
Expand Down
10 changes: 10 additions & 0 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@ impl Token {
false
}

/// Returns `true` if the token is an interpolated type.
pub fn is_ty(&self) -> bool {
if let Interpolated(ref nt) = *self {
if let NtTy(..) = nt.0 {
return true;
}
}
false
}

/// Returns a lifetime with the span and a dummy id if it is a lifetime,
/// or the original lifetime if it is an interpolated lifetime, ignoring
/// the span.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

macro_rules! m {
($my_type: ty) => {
impl $my_type for u8 {}
impl $my_type for u8 {} //~ WARN expected a trait, found type
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/issue-46438.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
warning: expected a trait, found type
--> $DIR/issue-46438.rs:13:14
|
LL | impl $my_type for u8 {} //~ WARN expected a trait, found type
| ^^^^^^^^ try using `path` instead of `ty` for this macro fragment
...
LL | m!(Tr);
| ------- in this macro invocation
|
= note: this warning will become a hard error in a future release

error: expected a trait, found type
--> $DIR/issue-46438.rs:21:4
|
LL | m!(&'static u8); //~ ERROR expected a trait, found type
| ^^^^^^^^^^^

error: aborting due to previous error