Skip to content

Commit bb24706

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

File tree

3 files changed

+173
-1
lines changed

3 files changed

+173
-1
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,18 @@ 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 mut placeholder_spans: Vec<Span> = vec![];
191+
for span in &placeholder_types {
192+
if placeholder_spans.iter().any(|sp| sp.source_equal(*span)) {
193+
continue;
194+
} else {
195+
placeholder_spans.push(*span);
196+
}
197+
}
198+
189199
let mut sugg: Vec<_> =
190-
placeholder_types.iter().map(|sp| (*sp, (*type_name).to_string())).collect();
200+
placeholder_spans.iter().map(|sp| (*sp, (*type_name).to_string())).collect();
191201

192202
if let Some(generics) = generics {
193203
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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
LL | where
38+
LL | T: Trait<m!()>;
39+
| ---- in this macro invocation
40+
|
41+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
42+
help: use type parameters instead
43+
|
44+
LL ~ U
45+
LL |
46+
...
47+
LL | }
48+
LL ~ struct S<U>(m!(), T)
49+
|
50+
51+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for implementations
52+
--> $DIR/issue-116502-span-error.rs:15:13
53+
|
54+
LL | _
55+
| ^
56+
| |
57+
| not allowed in type signatures
58+
| not allowed in type signatures
59+
| not allowed in type signatures
60+
...
61+
LL | impl<T> std::ops::Index<m!()> for dyn Trait<(m!(), T)>
62+
| ---- ---- in this macro invocation
63+
| |
64+
| in this macro invocation
65+
LL | where
66+
LL | T: Trait<m!()>,
67+
| ---- in this macro invocation
68+
|
69+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
70+
help: use type parameters instead
71+
|
72+
LL ~ U
73+
LL |
74+
...
75+
LL | std::cell::Cell::<m!()>::new(m!());
76+
LL ~ impl<T, U> std::ops::Index<m!()> for dyn Trait<(m!(), T)>
77+
|
78+
79+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated types
80+
--> $DIR/issue-116502-span-error.rs:15:13
81+
|
82+
LL | _
83+
| ^ not allowed in type signatures
84+
...
85+
LL | type Output = m!();
86+
| ---- in this macro invocation
87+
|
88+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
89+
90+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
91+
--> $DIR/issue-116502-span-error.rs:15:13
92+
|
93+
LL | _
94+
| ^
95+
| |
96+
| not allowed in type signatures
97+
| not allowed in type signatures
98+
...
99+
LL | fn index(&self, i: m!()) -> &m!() {
100+
| ---- ---- in this macro invocation
101+
| |
102+
| in this macro invocation
103+
|
104+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
105+
help: use type parameters instead
106+
|
107+
LL ~ T
108+
LL |
109+
...
110+
LL | type Output = m!();
111+
LL ~ fn index<T>(&self, i: m!()) -> &m!() {
112+
|
113+
help: try replacing `_` with the type in the corresponding trait method signature
114+
|
115+
LL | {type error}
116+
|
117+
118+
error: aborting due to 6 previous errors
119+
120+
For more information about this error, try `rustc --explain E0121`.

0 commit comments

Comments
 (0)