Skip to content

Commit f329df3

Browse files
committed
Avoid reporting errors in a Drop impl
1 parent d67e21c commit f329df3

File tree

9 files changed

+160
-138
lines changed

9 files changed

+160
-138
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! crate as a kind of pass. This should eventually be factored away.
1616
1717
use std::assert_matches::assert_matches;
18-
use std::cell::{Cell, RefCell};
18+
use std::cell::Cell;
1919
use std::iter;
2020
use std::ops::Bound;
2121

@@ -130,8 +130,6 @@ pub(crate) fn provide(providers: &mut Providers) {
130130
pub(crate) struct ItemCtxt<'tcx> {
131131
tcx: TyCtxt<'tcx>,
132132
item_def_id: LocalDefId,
133-
placeholder_types: RefCell<Vec<Span>>,
134-
infer_replacements: RefCell<Vec<(Span, String)>>,
135133
tainted_by_errors: Cell<Option<ErrorGuaranteed>>,
136134
}
137135

@@ -243,13 +241,7 @@ fn bad_placeholder<'cx, 'tcx>(
243241

244242
impl<'tcx> ItemCtxt<'tcx> {
245243
pub(crate) fn new(tcx: TyCtxt<'tcx>, item_def_id: LocalDefId) -> ItemCtxt<'tcx> {
246-
ItemCtxt {
247-
tcx,
248-
item_def_id,
249-
tainted_by_errors: Cell::new(None),
250-
placeholder_types: Default::default(),
251-
infer_replacements: Default::default(),
252-
}
244+
ItemCtxt { tcx, item_def_id, tainted_by_errors: Cell::new(None) }
253245
}
254246

255247
pub(crate) fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
@@ -265,12 +257,6 @@ impl<'tcx> ItemCtxt<'tcx> {
265257
}
266258

267259
fn check_tainted_by_errors(&self) -> Result<(), ErrorGuaranteed> {
268-
let placeholder_types = std::mem::take(&mut *self.placeholder_types.borrow_mut());
269-
let infer_replacements = std::mem::take(&mut *self.infer_replacements.borrow_mut());
270-
271-
if !placeholder_types.is_empty() || !infer_replacements.is_empty() {
272-
self.report_placeholder_type_error(placeholder_types, infer_replacements);
273-
}
274260
match self.tainted_by_errors.get() {
275261
Some(err) => Err(err),
276262
None => Ok(()),
@@ -281,7 +267,7 @@ impl<'tcx> ItemCtxt<'tcx> {
281267
&self,
282268
placeholder_types: Vec<Span>,
283269
infer_replacements: Vec<(Span, String)>,
284-
) {
270+
) -> ErrorGuaranteed {
285271
let node = self.tcx.hir_node_by_def_id(self.item_def_id);
286272
let generics = node.generics();
287273
let kind_id = match node {
@@ -320,13 +306,7 @@ impl<'tcx> ItemCtxt<'tcx> {
320306
);
321307
}
322308

323-
diag.emit();
324-
}
325-
}
326-
327-
impl Drop for ItemCtxt<'_> {
328-
fn drop(&mut self) {
329-
_ = self.check_tainted_by_errors();
309+
diag.emit()
330310
}
331311
}
332312

@@ -362,13 +342,13 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
362342

363343
fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
364344
if !self.tcx.dcx().has_stashed_diagnostic(span, StashKey::ItemNoType) {
365-
self.placeholder_types.borrow_mut().push(span);
345+
self.report_placeholder_type_error(vec![span], vec![]);
366346
}
367347
Ty::new_error_with_message(self.tcx(), span, "bad placeholder type")
368348
}
369349

370350
fn ct_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Const<'tcx> {
371-
self.placeholder_types.borrow_mut().push(span);
351+
self.report_placeholder_type_error(vec![span], vec![]);
372352
ty::Const::new_error_with_message(self.tcx(), span, "bad placeholder constant")
373353
}
374354

@@ -549,6 +529,8 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
549529
) -> (Vec<Ty<'tcx>>, Ty<'tcx>) {
550530
let tcx = self.tcx();
551531

532+
let mut infer_replacements = vec![];
533+
552534
let input_tys = decl
553535
.inputs
554536
.iter()
@@ -558,9 +540,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
558540
if let Some(suggested_ty) =
559541
self.lowerer().suggest_trait_fn_ty_for_impl_fn_infer(hir_id, Some(i))
560542
{
561-
self.infer_replacements
562-
.borrow_mut()
563-
.push((a.span, suggested_ty.to_string()));
543+
infer_replacements.push((a.span, suggested_ty.to_string()));
564544
return Ty::new_error_with_message(tcx, a.span, suggested_ty.to_string());
565545
}
566546
}
@@ -575,9 +555,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
575555
&& let Some(suggested_ty) =
576556
self.lowerer().suggest_trait_fn_ty_for_impl_fn_infer(hir_id, None)
577557
{
578-
self.infer_replacements
579-
.borrow_mut()
580-
.push((output.span, suggested_ty.to_string()));
558+
infer_replacements.push((output.span, suggested_ty.to_string()));
581559
Ty::new_error_with_message(tcx, output.span, suggested_ty.to_string())
582560
} else {
583561
self.lower_ty(output)
@@ -586,6 +564,9 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
586564
hir::FnRetTy::DefaultReturn(..) => tcx.types.unit,
587565
};
588566

567+
if !infer_replacements.is_empty() {
568+
self.report_placeholder_type_error(vec![], infer_replacements);
569+
}
589570
(input_tys, output_ty)
590571
}
591572

tests/ui/const-generics/generic_arg_infer/in-signature.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ trait TyAssocConst {
4141
trait TyAssocConstMixed {
4242
const ARR: Bar<_, _>;
4343
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
44+
//~| ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
4445
}
4546

4647
trait AssocTy {
@@ -57,4 +58,5 @@ impl AssocTy for i16 {
5758
impl AssocTy for i32 {
5859
type Assoc = Bar<_, _>;
5960
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types
61+
//~| ERROR the placeholder `_` is not allowed within types on item signatures for associated types
6062
}

tests/ui/const-generics/generic_arg_infer/in-signature.stderr

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,28 @@ LL + static TY_STATIC_MIXED: Bar<i32, 3> = Bar::<i32, 3>(0);
103103
|
104104

105105
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated types
106-
--> $DIR/in-signature.rs:50:23
106+
--> $DIR/in-signature.rs:51:23
107107
|
108108
LL | type Assoc = [u8; _];
109109
| ^ not allowed in type signatures
110110

111111
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated types
112-
--> $DIR/in-signature.rs:54:27
112+
--> $DIR/in-signature.rs:55:27
113113
|
114114
LL | type Assoc = Bar<i32, _>;
115115
| ^ not allowed in type signatures
116116

117117
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated types
118-
--> $DIR/in-signature.rs:58:22
118+
--> $DIR/in-signature.rs:59:22
119119
|
120120
LL | type Assoc = Bar<_, _>;
121-
| ^ ^ not allowed in type signatures
122-
| |
123-
| not allowed in type signatures
121+
| ^ not allowed in type signatures
122+
123+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated types
124+
--> $DIR/in-signature.rs:59:25
125+
|
126+
LL | type Assoc = Bar<_, _>;
127+
| ^ not allowed in type signatures
124128

125129
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
126130
--> $DIR/in-signature.rs:34:21
@@ -138,10 +142,14 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
138142
--> $DIR/in-signature.rs:42:20
139143
|
140144
LL | const ARR: Bar<_, _>;
141-
| ^ ^ not allowed in type signatures
142-
| |
143-
| not allowed in type signatures
145+
| ^ not allowed in type signatures
146+
147+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
148+
--> $DIR/in-signature.rs:42:23
149+
|
150+
LL | const ARR: Bar<_, _>;
151+
| ^ not allowed in type signatures
144152

145-
error: aborting due to 15 previous errors
153+
error: aborting due to 17 previous errors
146154

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

tests/ui/did_you_mean/bad-assoc-ty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type I = ty!()::AssocTy;
5050
trait K<A, B> {}
5151
fn foo<X: K<_, _>>(x: X) {}
5252
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
53+
//~| ERROR the placeholder `_` is not allowed within types on item signatures for functions
5354

5455
fn bar<F>(_: F) where F: Fn() -> _ {}
5556
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions

tests/ui/did_you_mean/bad-assoc-ty.stderr

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,48 +240,52 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
240240
--> $DIR/bad-assoc-ty.rs:51:13
241241
|
242242
LL | fn foo<X: K<_, _>>(x: X) {}
243-
| ^ ^ not allowed in type signatures
244-
| |
245-
| not allowed in type signatures
243+
| ^ not allowed in type signatures
246244

247245
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
248-
--> $DIR/bad-assoc-ty.rs:54:34
246+
--> $DIR/bad-assoc-ty.rs:51:16
247+
|
248+
LL | fn foo<X: K<_, _>>(x: X) {}
249+
| ^ not allowed in type signatures
250+
251+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
252+
--> $DIR/bad-assoc-ty.rs:55:34
249253
|
250254
LL | fn bar<F>(_: F) where F: Fn() -> _ {}
251255
| ^ not allowed in type signatures
252256

253257
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
254-
--> $DIR/bad-assoc-ty.rs:57:19
258+
--> $DIR/bad-assoc-ty.rs:58:19
255259
|
256260
LL | fn baz<F: Fn() -> _>(_: F) {}
257261
| ^ not allowed in type signatures
258262

259263
error[E0121]: the placeholder `_` is not allowed within types on item signatures for structs
260-
--> $DIR/bad-assoc-ty.rs:60:33
264+
--> $DIR/bad-assoc-ty.rs:61:33
261265
|
262266
LL | struct L<F>(F) where F: Fn() -> _;
263267
| ^ not allowed in type signatures
264268

265269
error[E0121]: the placeholder `_` is not allowed within types on item signatures for structs
266-
--> $DIR/bad-assoc-ty.rs:62:30
270+
--> $DIR/bad-assoc-ty.rs:63:30
267271
|
268272
LL | struct M<F> where F: Fn() -> _ {
269273
| ^ not allowed in type signatures
270274

271275
error[E0121]: the placeholder `_` is not allowed within types on item signatures for enums
272-
--> $DIR/bad-assoc-ty.rs:66:28
276+
--> $DIR/bad-assoc-ty.rs:67:28
273277
|
274278
LL | enum N<F> where F: Fn() -> _ {
275279
| ^ not allowed in type signatures
276280

277281
error[E0121]: the placeholder `_` is not allowed within types on item signatures for unions
278-
--> $DIR/bad-assoc-ty.rs:71:29
282+
--> $DIR/bad-assoc-ty.rs:72:29
279283
|
280284
LL | union O<F> where F: Fn() -> _ {
281285
| ^ not allowed in type signatures
282286

283287
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
284-
--> $DIR/bad-assoc-ty.rs:73:5
288+
--> $DIR/bad-assoc-ty.rs:74:5
285289
|
286290
LL | foo: F,
287291
| ^^^^^^
@@ -293,18 +297,18 @@ LL | foo: std::mem::ManuallyDrop<F>,
293297
| +++++++++++++++++++++++ +
294298

295299
error[E0121]: the placeholder `_` is not allowed within types on item signatures for traits
296-
--> $DIR/bad-assoc-ty.rs:77:29
300+
--> $DIR/bad-assoc-ty.rs:78:29
297301
|
298302
LL | trait P<F> where F: Fn() -> _ {
299303
| ^ not allowed in type signatures
300304

301305
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
302-
--> $DIR/bad-assoc-ty.rs:82:38
306+
--> $DIR/bad-assoc-ty.rs:83:38
303307
|
304308
LL | fn foo<F>(_: F) where F: Fn() -> _ {}
305309
| ^ not allowed in type signatures
306310

307-
error: aborting due to 29 previous errors; 1 warning emitted
311+
error: aborting due to 30 previous errors; 1 warning emitted
308312

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

tests/ui/macros/issue-118048.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ macro_rules! foo {
66

77
foo!(_);
88
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
9+
//~| ERROR the placeholder `_` is not allowed within types on item signatures for functions
910

1011
fn main() {}

tests/ui/macros/issue-118048.stderr

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
22
--> $DIR/issue-118048.rs:7:6
33
|
44
LL | foo!(_);
5-
| ^
6-
| |
7-
| not allowed in type signatures
8-
| not allowed in type signatures
5+
| ^ not allowed in type signatures
96

10-
error: aborting due to 1 previous error
7+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
8+
--> $DIR/issue-118048.rs:7:6
9+
|
10+
LL | foo!(_);
11+
| ^ not allowed in type signatures
12+
|
13+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
14+
15+
error: aborting due to 2 previous errors
1116

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

tests/ui/typeck/typeck_type_placeholder_item.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct Test10 {
6767
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for structs
6868
b: (_, _),
6969
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for structs
70+
//~| ERROR the placeholder `_` is not allowed within types on item signatures for structs
7071
}
7172

7273
pub fn main() {
@@ -123,6 +124,7 @@ pub fn main() {
123124
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for structs
124125
b: (_, _),
125126
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for structs
127+
//~| ERROR the placeholder `_` is not allowed within types on item signatures for structs
126128
}
127129

128130
fn fn_test11(_: _) -> (_, _) { panic!() }
@@ -141,12 +143,14 @@ trait T {
141143
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
142144
fn method_test2(&self, x: _) -> _;
143145
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
146+
//~| ERROR the placeholder `_` is not allowed within types on item signatures for functions
144147
fn method_test3(&self) -> _;
145148
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
146149
fn assoc_fn_test1(x: _);
147150
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
148151
fn assoc_fn_test2(x: _) -> _;
149152
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
153+
//~| ERROR the placeholder `_` is not allowed within types on item signatures for functions
150154
fn assoc_fn_test3() -> _;
151155
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
152156
}

0 commit comments

Comments
 (0)