Skip to content

Commit

Permalink
Fix diag span errors for bad_placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Oct 7, 2023
1 parent 8fdb0a9 commit 949ee62
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 1 deletion.
9 changes: 8 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,15 @@ 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 placeholder_spans = placeholder_types
.clone()
.into_iter()
.filter(|sp| !placeholder_types.iter().any(|sp2| sp2.source_equal(*sp) && sp2 != sp))
.collect::<Vec<_>>();

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 {}
106 changes: 106 additions & 0 deletions tests/ui/errors/issue-116502-span-error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
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
| help: use type parameters instead: `<U>`
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)

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
| help: use type parameters instead: `, U`
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)

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}
|

error: aborting due to 6 previous errors

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

0 comments on commit 949ee62

Please sign in to comment.