Skip to content

Commit 949ee62

Browse files
committed
Fix diag span errors for bad_placeholder
1 parent 8fdb0a9 commit 949ee62

File tree

3 files changed

+156
-1
lines changed

3 files changed

+156
-1
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,15 @@ pub(crate) fn placeholder_type_error_diag<'tcx>(
186186

187187
let params = generics.map(|g| g.params).unwrap_or_default();
188188
let type_name = params.next_type_param_name(None);
189+
190+
let placeholder_spans = placeholder_types
191+
.clone()
192+
.into_iter()
193+
.filter(|sp| !placeholder_types.iter().any(|sp2| sp2.source_equal(*sp) && sp2 != sp))
194+
.collect::<Vec<_>>();
195+
189196
let mut sugg: Vec<_> =
190-
placeholder_types.iter().map(|sp| (*sp, (*type_name).to_string())).collect();
197+
placeholder_spans.iter().map(|sp| (*sp, (*type_name).to_string())).collect();
191198

192199
if let Some(generics) = generics {
193200
if let Some(arg) = params.iter().find(|arg| {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#![allow(dead_code)]
2+
#![allow(unused_variables)]
3+
macro_rules! Tuple {
4+
{ $A:ty,$B:ty } => { ($A, $B) }
5+
}
6+
7+
fn main() {
8+
let x: Tuple!(i32, i32) = (1, 2);
9+
}
10+
11+
fn issue_36540() {
12+
let _ = 0;
13+
macro_rules! m {
14+
() => {
15+
_
16+
//~^ ERROR in expressions
17+
//~| ERROR in expressions
18+
//~| ERROR the placeholder `_` is not allowed
19+
//~| ERROR the placeholder `_` is not allowed
20+
//~| ERROR the placeholder `_` is not allowed
21+
//~| ERROR the placeholder `_` is not allowed
22+
};
23+
}
24+
struct S<T = m!()>(m!(), T)
25+
where
26+
T: Trait<m!()>;
27+
28+
let x: m!() = m!();
29+
std::cell::Cell::<m!()>::new(m!());
30+
impl<T> std::ops::Index<m!()> for dyn Trait<(m!(), T)>
31+
where
32+
T: Trait<m!()>,
33+
{
34+
type Output = m!();
35+
fn index(&self, i: m!()) -> &m!() {
36+
unimplemented!()
37+
}
38+
}
39+
}
40+
41+
trait Trait<T> {}
42+
impl Trait<i32> for i32 {}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
error: in expressions, `_` can only be used on the left-hand side of an assignment
2+
--> $DIR/issue-116502-span-error.rs:15:13
3+
|
4+
LL | _
5+
| ^ `_` not allowed here
6+
...
7+
LL | let x: m!() = m!();
8+
| ---- in this macro invocation
9+
|
10+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error: in expressions, `_` can only be used on the left-hand side of an assignment
13+
--> $DIR/issue-116502-span-error.rs:15:13
14+
|
15+
LL | _
16+
| ^ `_` not allowed here
17+
...
18+
LL | std::cell::Cell::<m!()>::new(m!());
19+
| ---- in this macro invocation
20+
|
21+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
22+
23+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for structs
24+
--> $DIR/issue-116502-span-error.rs:15:13
25+
|
26+
LL | _
27+
| ^
28+
| |
29+
| not allowed in type signatures
30+
| not allowed in type signatures
31+
| not allowed in type signatures
32+
...
33+
LL | struct S<T = m!()>(m!(), T)
34+
| ---------- ---- in this macro invocation
35+
| | |
36+
| | in this macro invocation
37+
| help: use type parameters instead: `<U>`
38+
LL | where
39+
LL | T: Trait<m!()>;
40+
| ---- in this macro invocation
41+
|
42+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
43+
44+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for implementations
45+
--> $DIR/issue-116502-span-error.rs:15:13
46+
|
47+
LL | _
48+
| ^
49+
| |
50+
| not allowed in type signatures
51+
| not allowed in type signatures
52+
| not allowed in type signatures
53+
...
54+
LL | impl<T> std::ops::Index<m!()> for dyn Trait<(m!(), T)>
55+
| - ---- ---- in this macro invocation
56+
| | |
57+
| | in this macro invocation
58+
| help: use type parameters instead: `, U`
59+
LL | where
60+
LL | T: Trait<m!()>,
61+
| ---- in this macro invocation
62+
|
63+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
64+
65+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated types
66+
--> $DIR/issue-116502-span-error.rs:15:13
67+
|
68+
LL | _
69+
| ^ not allowed in type signatures
70+
...
71+
LL | type Output = m!();
72+
| ---- in this macro invocation
73+
|
74+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
75+
76+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
77+
--> $DIR/issue-116502-span-error.rs:15:13
78+
|
79+
LL | _
80+
| ^
81+
| |
82+
| not allowed in type signatures
83+
| not allowed in type signatures
84+
...
85+
LL | fn index(&self, i: m!()) -> &m!() {
86+
| ---- ---- in this macro invocation
87+
| |
88+
| in this macro invocation
89+
|
90+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
91+
help: use type parameters instead
92+
|
93+
LL ~ T
94+
LL |
95+
...
96+
LL | type Output = m!();
97+
LL ~ fn index<T>(&self, i: m!()) -> &m!() {
98+
|
99+
help: try replacing `_` with the type in the corresponding trait method signature
100+
|
101+
LL | {type error}
102+
|
103+
104+
error: aborting due to 6 previous errors
105+
106+
For more information about this error, try `rustc --explain E0121`.

0 commit comments

Comments
 (0)