Skip to content

Fix diag span errors for bad_placeholder #116511

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

Closed
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
12 changes: 11 additions & 1 deletion compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,18 @@ pub(crate) fn placeholder_type_error_diag<'tcx>(

let params = generics.map(|g| g.params).unwrap_or_default();
let type_name = params.next_type_param_name(None);

let mut placeholder_spans: Vec<Span> = vec![];
for span in &placeholder_types {
if placeholder_spans.iter().any(|sp| sp.source_equal(*span)) {
continue;
} else {
placeholder_spans.push(*span);
}
}

let mut sugg: Vec<_> =
placeholder_types.iter().map(|sp| (*sp, (*type_name).to_string())).collect();
placeholder_spans.iter().map(|sp| (*sp, (*type_name).to_string())).collect();

if let Some(generics) = generics {
if let Some(arg) = params.iter().find(|arg| {
Expand Down
42 changes: 42 additions & 0 deletions tests/ui/errors/issue-116502-span-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![allow(dead_code)]
#![allow(unused_variables)]
macro_rules! Tuple {
{ $A:ty,$B:ty } => { ($A, $B) }
}

fn main() {
let x: Tuple!(i32, i32) = (1, 2);
}

fn issue_36540() {
let _ = 0;
macro_rules! m {
() => {
_
//~^ ERROR in expressions
//~| ERROR in expressions
//~| ERROR the placeholder `_` is not allowed
//~| ERROR the placeholder `_` is not allowed
//~| ERROR the placeholder `_` is not allowed
//~| ERROR the placeholder `_` is not allowed
};
}
struct S<T = m!()>(m!(), T)
where
T: Trait<m!()>;

let x: m!() = m!();
std::cell::Cell::<m!()>::new(m!());
impl<T> std::ops::Index<m!()> for dyn Trait<(m!(), T)>
where
T: Trait<m!()>,
{
type Output = m!();
fn index(&self, i: m!()) -> &m!() {
unimplemented!()
}
}
}

trait Trait<T> {}
impl Trait<i32> for i32 {}
120 changes: 120 additions & 0 deletions tests/ui/errors/issue-116502-span-error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
error: in expressions, `_` can only be used on the left-hand side of an assignment
--> $DIR/issue-116502-span-error.rs:15:13
|
LL | _
| ^ `_` not allowed here
...
LL | let x: m!() = m!();
| ---- in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)

error: in expressions, `_` can only be used on the left-hand side of an assignment
--> $DIR/issue-116502-span-error.rs:15:13
|
LL | _
| ^ `_` not allowed here
...
LL | std::cell::Cell::<m!()>::new(m!());
| ---- in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0121]: the placeholder `_` is not allowed within types on item signatures for structs
--> $DIR/issue-116502-span-error.rs:15:13
|
LL | _
| ^
| |
| not allowed in type signatures
| not allowed in type signatures
| not allowed in type signatures
...
LL | struct S<T = m!()>(m!(), T)
| ---- ---- in this macro invocation
| |
| in this macro invocation
LL | where
LL | T: Trait<m!()>;
| ---- in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use type parameters instead
|
LL ~ U
LL |
...
LL | }
LL ~ struct S<U>(m!(), T)
|

error[E0121]: the placeholder `_` is not allowed within types on item signatures for implementations
--> $DIR/issue-116502-span-error.rs:15:13
|
LL | _
| ^
| |
| not allowed in type signatures
| not allowed in type signatures
| not allowed in type signatures
...
LL | impl<T> std::ops::Index<m!()> for dyn Trait<(m!(), T)>
| ---- ---- in this macro invocation
| |
| in this macro invocation
LL | where
LL | T: Trait<m!()>,
| ---- in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use type parameters instead
|
LL ~ U
LL |
...
LL | std::cell::Cell::<m!()>::new(m!());
LL ~ impl<T, U> std::ops::Index<m!()> for dyn Trait<(m!(), T)>
|

error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated types
--> $DIR/issue-116502-span-error.rs:15:13
|
LL | _
| ^ not allowed in type signatures
...
LL | type Output = m!();
| ---- in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
--> $DIR/issue-116502-span-error.rs:15:13
|
LL | _
| ^
| |
| not allowed in type signatures
| not allowed in type signatures
...
LL | fn index(&self, i: m!()) -> &m!() {
| ---- ---- in this macro invocation
| |
| in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use type parameters instead
|
LL ~ T
LL |
...
LL | type Output = m!();
LL ~ fn index<T>(&self, i: m!()) -> &m!() {
|
help: try replacing `_` with the type in the corresponding trait method signature
|
LL | {type error}
Copy link
Member

Choose a reason for hiding this comment

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

We probably should fix this suggestion anyways

Copy link
Member Author

Choose a reason for hiding this comment

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

Seems the help diag you pointed out is another issue.

The root cause in the issue is we inserted duplicated span into HirPlaceholderCollector here:

self.0.push(t.span);

in the scenario of code like:

    macro_rules! m {
        () => {
            _         // This is the duplicated span ...
        };
    }
    struct S<T = m!()>(m!(), T)
    where
        T: Trait<m!()>;

This PR is trying to remove the duplicated ones, since we're mapping spans with same type type_name.

Maybe we need to use Set instead of Vec in HirPlaceholderCollector? I'm not sure about it.

|

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0121`.