Skip to content

Commit 1cb555a

Browse files
committed
Backport only: avoid ICE on bad placeholder type
#69148 has a proper fix, but it is too big to backport. This change avoids the ICE by actually emitting an appropriate error. The output will be duplicated in some cases, but that's better than the avoidable ICE.
1 parent a643ee8 commit 1cb555a

7 files changed

+329
-68
lines changed

src/librustc_typeck/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl AstConv<'tcx> for ItemCtxt<'tcx> {
319319
}
320320

321321
fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
322-
self.tcx().sess.delay_span_bug(span, "bad placeholder type");
322+
placeholder_type_error(self.tcx(), span, &[], vec![span], false);
323323
self.tcx().types.err
324324
}
325325

src/test/ui/did_you_mean/bad-assoc-ty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type D = (u8, u8)::AssocTy;
1717
type E = _::AssocTy;
1818
//~^ ERROR missing angle brackets in associated item path
1919
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
20+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
2021

2122
type F = &'static (u8)::AssocTy;
2223
//~^ ERROR missing angle brackets in associated item path

src/test/ui/did_you_mean/bad-assoc-ty.stderr

+16-10
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ LL | type E = _::AssocTy;
2929
| ^^^^^^^^^^ help: try: `<_>::AssocTy`
3030

3131
error: missing angle brackets in associated item path
32-
--> $DIR/bad-assoc-ty.rs:21:19
32+
--> $DIR/bad-assoc-ty.rs:22:19
3333
|
3434
LL | type F = &'static (u8)::AssocTy;
3535
| ^^^^^^^^^^^^^ help: try: `<(u8)>::AssocTy`
3636

3737
error: missing angle brackets in associated item path
38-
--> $DIR/bad-assoc-ty.rs:27:10
38+
--> $DIR/bad-assoc-ty.rs:28:10
3939
|
4040
LL | type G = dyn 'static + (Send)::AssocTy;
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `<dyn 'static + (Send)>::AssocTy`
4242

4343
error: missing angle brackets in associated item path
44-
--> $DIR/bad-assoc-ty.rs:44:10
44+
--> $DIR/bad-assoc-ty.rs:45:10
4545
|
4646
LL | type I = ty!()::AssocTy;
4747
| ^^^^^^^^^^^^^^ help: try: `<ty!()>::AssocTy`
4848

4949
error: missing angle brackets in associated item path
50-
--> $DIR/bad-assoc-ty.rs:37:19
50+
--> $DIR/bad-assoc-ty.rs:38:19
5151
|
5252
LL | ($ty: ty) => ($ty::AssocTy);
5353
| ^^^^^^^^^^^^ help: try: `<$ty>::AssocTy`
@@ -87,26 +87,32 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa
8787
LL | type E = _::AssocTy;
8888
| ^ not allowed in type signatures
8989

90+
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
91+
--> $DIR/bad-assoc-ty.rs:17:10
92+
|
93+
LL | type E = _::AssocTy;
94+
| ^ not allowed in type signatures
95+
9096
error[E0223]: ambiguous associated type
91-
--> $DIR/bad-assoc-ty.rs:21:19
97+
--> $DIR/bad-assoc-ty.rs:22:19
9298
|
9399
LL | type F = &'static (u8)::AssocTy;
94100
| ^^^^^^^^^^^^^ help: use fully-qualified syntax: `<u8 as Trait>::AssocTy`
95101

96102
error[E0223]: ambiguous associated type
97-
--> $DIR/bad-assoc-ty.rs:27:10
103+
--> $DIR/bad-assoc-ty.rs:28:10
98104
|
99105
LL | type G = dyn 'static + (Send)::AssocTy;
100106
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<(dyn std::marker::Send + 'static) as Trait>::AssocTy`
101107

102108
error[E0223]: ambiguous associated type
103-
--> $DIR/bad-assoc-ty.rs:33:10
109+
--> $DIR/bad-assoc-ty.rs:34:10
104110
|
105111
LL | type H = Fn(u8) -> (u8)::Output;
106112
| ^^^^^^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<(dyn std::ops::Fn(u8) -> u8 + 'static) as Trait>::Output`
107113

108114
error[E0223]: ambiguous associated type
109-
--> $DIR/bad-assoc-ty.rs:37:19
115+
--> $DIR/bad-assoc-ty.rs:38:19
110116
|
111117
LL | ($ty: ty) => ($ty::AssocTy);
112118
| ^^^^^^^^^^^^ help: use fully-qualified syntax: `<u8 as Trait>::AssocTy`
@@ -117,12 +123,12 @@ LL | type J = ty!(u8);
117123
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
118124

119125
error[E0223]: ambiguous associated type
120-
--> $DIR/bad-assoc-ty.rs:44:10
126+
--> $DIR/bad-assoc-ty.rs:45:10
121127
|
122128
LL | type I = ty!()::AssocTy;
123129
| ^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<u8 as Trait>::AssocTy`
124130

125-
error: aborting due to 19 previous errors
131+
error: aborting due to 20 previous errors
126132

127133
Some errors have detailed explanations: E0121, E0223.
128134
For more information about an error, try `rustc --explain E0121`.

src/test/ui/self/self-infer.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ struct S;
22

33
impl S {
44
fn f(self: _) {} //~ERROR the type placeholder `_` is not allowed within types on item sig
5+
//~^ ERROR the type placeholder `_` is not allowed within types on item sig
56
fn g(self: &_) {} //~ERROR the type placeholder `_` is not allowed within types on item sig
7+
//~^ ERROR the type placeholder `_` is not allowed within types on item sig
68
}
79

810
fn main() {}

src/test/ui/self/self-infer.stderr

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
2+
--> $DIR/self-infer.rs:4:16
3+
|
4+
LL | fn f(self: _) {}
5+
| ^ not allowed in type signatures
6+
17
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
28
--> $DIR/self-infer.rs:4:16
39
|
@@ -10,7 +16,13 @@ LL | fn f<T>(self: T) {}
1016
| ^^^ ^
1117

1218
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
13-
--> $DIR/self-infer.rs:5:17
19+
--> $DIR/self-infer.rs:6:17
20+
|
21+
LL | fn g(self: &_) {}
22+
| ^ not allowed in type signatures
23+
24+
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
25+
--> $DIR/self-infer.rs:6:17
1426
|
1527
LL | fn g(self: &_) {}
1628
| ^ not allowed in type signatures
@@ -20,6 +32,6 @@ help: use type parameters instead
2032
LL | fn g<T>(self: &T) {}
2133
| ^^^ ^
2234

23-
error: aborting due to 2 previous errors
35+
error: aborting due to 4 previous errors
2436

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

src/test/ui/typeck/typeck_type_placeholder_item.rs

+34
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,24 @@ static TEST5: (_, _) = (1, 2);
1919

2020
fn test6(_: _) { }
2121
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
22+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
2223

2324
fn test6_b<T>(_: _, _: T) { }
2425
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
26+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
2527

2628
fn test6_c<T, K, L, A, B>(_: _, _: (T, K, L, A, B)) { }
2729
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
30+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
2831

2932
fn test7(x: _) { let _x: usize = x; }
3033
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
34+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
3135

3236
fn test8(_f: fn() -> _) { }
3337
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
3438
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
39+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
3540

3641
struct Test9;
3742

@@ -41,6 +46,7 @@ impl Test9 {
4146

4247
fn test10(&self, _x : _) { }
4348
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
49+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
4450
}
4551

4652
fn test11(x: &usize) -> &_ {
@@ -59,12 +65,16 @@ impl Clone for Test9 {
5965

6066
fn clone_from(&mut self, other: _) { *self = Test9; }
6167
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
68+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
6269
}
6370

6471
struct Test10 {
6572
a: _,
6673
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
74+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
6775
b: (_, _),
76+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
77+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
6878
}
6979

7080
pub fn main() {
@@ -92,13 +102,16 @@ pub fn main() {
92102

93103
fn fn_test6(_: _) { }
94104
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
105+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
95106

96107
fn fn_test7(x: _) { let _x: usize = x; }
97108
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
109+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
98110

99111
fn fn_test8(_f: fn() -> _) { }
100112
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
101113
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
114+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
102115

103116
struct FnTest9;
104117

@@ -108,6 +121,7 @@ pub fn main() {
108121

109122
fn fn_test10(&self, _x : _) { }
110123
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
124+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
111125
}
112126

113127
impl Clone for FnTest9 {
@@ -116,12 +130,16 @@ pub fn main() {
116130

117131
fn clone_from(&mut self, other: _) { *self = FnTest9; }
118132
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
133+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
119134
}
120135

121136
struct FnTest10 {
122137
a: _,
123138
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
139+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
124140
b: (_, _),
141+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
142+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
125143
}
126144

127145
fn fn_test11(_: _) -> (_, _) { panic!() }
@@ -138,28 +156,40 @@ pub fn main() {
138156
trait T {
139157
fn method_test1(&self, x: _);
140158
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
159+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
141160
fn method_test2(&self, x: _) -> _;
142161
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
162+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
163+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
143164
fn method_test3(&self) -> _;
144165
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
166+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
145167
fn assoc_fn_test1(x: _);
146168
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
169+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
147170
fn assoc_fn_test2(x: _) -> _;
148171
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
172+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
173+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
149174
fn assoc_fn_test3() -> _;
150175
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
176+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
151177
}
152178

153179
struct BadStruct<_>(_);
154180
//~^ ERROR expected identifier, found reserved identifier `_`
155181
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
182+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
156183
trait BadTrait<_> {}
157184
//~^ ERROR expected identifier, found reserved identifier `_`
158185
impl BadTrait<_> for BadStruct<_> {}
159186
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
187+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
188+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
160189

161190
fn impl_trait() -> impl BadTrait<_> {
162191
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
192+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
163193
unimplemented!()
164194
}
165195

@@ -168,18 +198,22 @@ struct BadStruct1<_, _>(_);
168198
//~| ERROR expected identifier, found reserved identifier `_`
169199
//~| ERROR the name `_` is already used
170200
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
201+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
171202
struct BadStruct2<_, T>(_, T);
172203
//~^ ERROR expected identifier, found reserved identifier `_`
173204
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
205+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
174206

175207
type X = Box<_>;
176208
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
209+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
177210

178211
struct Struct;
179212
trait Trait<T> {}
180213
impl Trait<usize> for Struct {}
181214
type Y = impl Trait<_>;
182215
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
216+
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
183217
fn foo() -> Y {
184218
Struct
185219
}

0 commit comments

Comments
 (0)