Skip to content

Commit 9e016a8

Browse files
committed
Avoid emitting type mismatches against {type error}
1 parent 66bd645 commit 9e016a8

File tree

9 files changed

+55
-84
lines changed

9 files changed

+55
-84
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,14 @@ impl<'tcx> RegionInferenceContext<'tcx> {
153153
if let Some(prev) = result.get_mut(&opaque_type_key.def_id) {
154154
if prev.ty != ty {
155155
let guar = ty.error_reported().err().unwrap_or_else(|| {
156-
prev.report_mismatch(
157-
&OpaqueHiddenType { ty, span: concrete_type.span },
158-
opaque_type_key.def_id,
159-
infcx.tcx,
160-
)
161-
.emit()
156+
let (Ok(e) | Err(e)) = prev
157+
.report_mismatch(
158+
&OpaqueHiddenType { ty, span: concrete_type.span },
159+
opaque_type_key.def_id,
160+
infcx.tcx,
161+
)
162+
.map(|d| d.emit());
163+
e
162164
});
163165
prev.ty = Ty::new_error(infcx.tcx, guar);
164166
}

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ fn sanity_check_found_hidden_type<'tcx>(
478478
} else {
479479
let span = tcx.def_span(key.def_id);
480480
let other = ty::OpaqueHiddenType { ty: hidden_ty, span };
481-
Err(ty.report_mismatch(&other, key.def_id, tcx).emit())
481+
Err(ty.report_mismatch(&other, key.def_id, tcx)?.emit())
482482
}
483483
}
484484

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ pub(super) fn find_opaque_ty_constraints_for_impl_trait_in_assoc_type(
5858
// Only check against typeck if we didn't already error
5959
if !hidden.ty.references_error() {
6060
for concrete_type in locator.typeck_types {
61-
if concrete_type.ty != tcx.erase_regions(hidden.ty)
62-
&& !(concrete_type, hidden).references_error()
63-
{
64-
hidden.report_mismatch(&concrete_type, def_id, tcx).emit();
61+
if concrete_type.ty != tcx.erase_regions(hidden.ty) {
62+
if let Ok(d) = hidden.report_mismatch(&concrete_type, def_id, tcx) {
63+
d.emit();
64+
}
6565
}
6666
}
6767
}
@@ -134,10 +134,10 @@ pub(super) fn find_opaque_ty_constraints_for_tait(tcx: TyCtxt<'_>, def_id: Local
134134
// Only check against typeck if we didn't already error
135135
if !hidden.ty.references_error() {
136136
for concrete_type in locator.typeck_types {
137-
if concrete_type.ty != tcx.erase_regions(hidden.ty)
138-
&& !(concrete_type, hidden).references_error()
139-
{
140-
hidden.report_mismatch(&concrete_type, def_id, tcx).emit();
137+
if concrete_type.ty != tcx.erase_regions(hidden.ty) {
138+
if let Ok(d) = hidden.report_mismatch(&concrete_type, def_id, tcx) {
139+
d.emit();
140+
}
141141
}
142142
}
143143
}
@@ -287,8 +287,10 @@ impl TaitConstraintLocator<'_> {
287287
if let Some(&concrete_type) = borrowck_results.concrete_opaque_types.get(&self.def_id) {
288288
debug!(?concrete_type, "found constraint");
289289
if let Some(prev) = &mut self.found {
290-
if concrete_type.ty != prev.ty && !(concrete_type, prev.ty).references_error() {
291-
let guar = prev.report_mismatch(&concrete_type, self.def_id, self.tcx).emit();
290+
if concrete_type.ty != prev.ty {
291+
let (Ok(guar) | Err(guar)) = prev
292+
.report_mismatch(&concrete_type, self.def_id, self.tcx)
293+
.map(|d| d.emit());
292294
prev.ty = Ty::new_error(self.tcx, guar);
293295
}
294296
} else {
@@ -361,11 +363,13 @@ pub(super) fn find_opaque_ty_constraints_for_rpit<'tcx>(
361363
hidden_type.remap_generic_params_to_declaration_params(opaque_type_key, tcx, true),
362364
);
363365
if let Some(prev) = &mut hir_opaque_ty {
364-
if concrete_type.ty != prev.ty && !(concrete_type, prev.ty).references_error() {
365-
prev.report_mismatch(&concrete_type, def_id, tcx).stash(
366-
tcx.def_span(opaque_type_key.def_id),
367-
StashKey::OpaqueHiddenTypeMismatch,
368-
);
366+
if concrete_type.ty != prev.ty {
367+
if let Ok(d) = prev.report_mismatch(&concrete_type, def_id, tcx) {
368+
d.stash(
369+
tcx.def_span(opaque_type_key.def_id),
370+
StashKey::OpaqueHiddenTypeMismatch,
371+
);
372+
}
369373
}
370374
} else {
371375
hir_opaque_ty = Some(concrete_type);
@@ -436,9 +440,10 @@ impl RpitConstraintChecker<'_> {
436440

437441
debug!(?concrete_type, "found constraint");
438442

439-
if concrete_type.ty != self.found.ty && !(concrete_type, self.found).references_error()
440-
{
441-
self.found.report_mismatch(&concrete_type, self.def_id, self.tcx).emit();
443+
if concrete_type.ty != self.found.ty {
444+
if let Ok(d) = self.found.report_mismatch(&concrete_type, self.def_id, self.tcx) {
445+
d.emit();
446+
}
442447
}
443448
}
444449
}

compiler/rustc_hir_typeck/src/writeback.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,14 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
589589
&& last_opaque_ty.ty != hidden_type.ty
590590
{
591591
assert!(!self.fcx.next_trait_solver());
592-
hidden_type
593-
.report_mismatch(&last_opaque_ty, opaque_type_key.def_id, self.tcx())
594-
.stash(
592+
if let Ok(d) =
593+
hidden_type.report_mismatch(&last_opaque_ty, opaque_type_key.def_id, self.tcx())
594+
{
595+
d.stash(
595596
self.tcx().def_span(opaque_type_key.def_id),
596597
StashKey::OpaqueHiddenTypeMismatch,
597598
);
599+
}
598600
}
599601
}
600602
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,26 +845,27 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
845845
other: &Self,
846846
opaque_def_id: LocalDefId,
847847
tcx: TyCtxt<'tcx>,
848-
) -> DiagnosticBuilder<'tcx> {
848+
) -> Result<DiagnosticBuilder<'tcx>, ErrorGuaranteed> {
849849
if let Some(diag) = tcx
850850
.sess
851851
.dcx()
852852
.steal_diagnostic(tcx.def_span(opaque_def_id), StashKey::OpaqueHiddenTypeMismatch)
853853
{
854854
diag.cancel();
855855
}
856+
(self.ty, other.ty).error_reported()?;
856857
// Found different concrete types for the opaque type.
857858
let sub_diag = if self.span == other.span {
858859
TypeMismatchReason::ConflictType { span: self.span }
859860
} else {
860861
TypeMismatchReason::PreviousUse { span: self.span }
861862
};
862-
tcx.dcx().create_err(OpaqueHiddenTypeMismatch {
863+
Ok(tcx.dcx().create_err(OpaqueHiddenTypeMismatch {
863864
self_ty: self.ty,
864865
other_ty: other.ty,
865866
other_span: other.span,
866867
sub: sub_diag,
867-
})
868+
}))
868869
}
869870

870871
#[instrument(level = "debug", skip(tcx), ret)]

tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-basic.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ mod capture_tait_complex_pass {
3737
use super::*;
3838
type Opq0<'a> = impl Sized;
3939
type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'b>>; // <- Note 'b
40-
//~^ ERROR: concrete type differs from previous defining opaque type use
4140
type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
4241
fn test() -> Opq2 {}
4342
//~^ ERROR: expected generic lifetime parameter, found `'a`
@@ -75,7 +74,6 @@ mod constrain_pass {
7574
use super::*;
7675
type Opq0<'a, 'b> = impl Sized;
7776
type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'a, 'b>>;
78-
//~^ ERROR concrete type differs
7977
type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
8078
fn test() -> Opq2 {}
8179
//~^ ERROR: expected generic lifetime parameter, found `'a`

tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-basic.stderr

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,16 @@ LL | fn test() -> Opq2 {}
2727
| ^^
2828

2929
error[E0792]: expected generic lifetime parameter, found `'a`
30-
--> $DIR/higher-ranked-regions-basic.rs:42:23
30+
--> $DIR/higher-ranked-regions-basic.rs:41:23
3131
|
3232
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'b>>; // <- Note 'b
3333
| -- this generic parameter must be used with a generic lifetime parameter
34-
...
34+
LL | type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
3535
LL | fn test() -> Opq2 {}
3636
| ^^
3737

38-
error: concrete type differs from previous defining opaque type use
39-
--> $DIR/higher-ranked-regions-basic.rs:39:21
40-
|
41-
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'b>>; // <- Note 'b
42-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&'a ()`, got `{type error}`
43-
|
44-
note: previous use here
45-
--> $DIR/higher-ranked-regions-basic.rs:41:17
46-
|
47-
LL | type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
48-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
49-
5038
error[E0700]: hidden type for `capture_tait_complex_fail::Opq0<'a>` captures lifetime that does not appear in bounds
51-
--> $DIR/higher-ranked-regions-basic.rs:52:23
39+
--> $DIR/higher-ranked-regions-basic.rs:51:23
5240
|
5341
LL | type Opq0<'a> = impl Sized;
5442
| ---------- opaque type defined here
@@ -59,67 +47,55 @@ LL | fn test() -> Opq2 {}
5947
| ^^
6048

6149
error[E0792]: non-defining opaque type use in defining scope
62-
--> $DIR/higher-ranked-regions-basic.rs:60:41
50+
--> $DIR/higher-ranked-regions-basic.rs:59:41
6351
|
6452
LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'static>> {}
6553
| ^^^^^^^^^^^^^^^^^^^^^^ argument `'static` is not a generic parameter
6654
|
6755
note: for this opaque type
68-
--> $DIR/higher-ranked-regions-basic.rs:59:25
56+
--> $DIR/higher-ranked-regions-basic.rs:58:25
6957
|
7058
LL | type Opq0<'a, 'b> = impl Sized;
7159
| ^^^^^^^^^^
7260

7361
error[E0792]: expected generic lifetime parameter, found `'a`
74-
--> $DIR/higher-ranked-regions-basic.rs:60:65
62+
--> $DIR/higher-ranked-regions-basic.rs:59:65
7563
|
7664
LL | type Opq0<'a, 'b> = impl Sized;
7765
| -- this generic parameter must be used with a generic lifetime parameter
7866
LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'static>> {}
7967
| ^^
8068

8169
error: non-defining opaque type use in defining scope
82-
--> $DIR/higher-ranked-regions-basic.rs:69:41
70+
--> $DIR/higher-ranked-regions-basic.rs:68:41
8371
|
8472
LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'a>> {}
8573
| ^^^^^^^^^^^^^^^^^ generic argument `'a` used twice
8674
|
8775
note: for this opaque type
88-
--> $DIR/higher-ranked-regions-basic.rs:68:25
76+
--> $DIR/higher-ranked-regions-basic.rs:67:25
8977
|
9078
LL | type Opq0<'a, 'b> = impl Sized;
9179
| ^^^^^^^^^^
9280

9381
error[E0792]: expected generic lifetime parameter, found `'a`
94-
--> $DIR/higher-ranked-regions-basic.rs:69:60
82+
--> $DIR/higher-ranked-regions-basic.rs:68:60
9583
|
9684
LL | type Opq0<'a, 'b> = impl Sized;
9785
| -- this generic parameter must be used with a generic lifetime parameter
9886
LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'a>> {}
9987
| ^^
10088

10189
error[E0792]: expected generic lifetime parameter, found `'a`
102-
--> $DIR/higher-ranked-regions-basic.rs:80:23
90+
--> $DIR/higher-ranked-regions-basic.rs:78:23
10391
|
10492
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'a, 'b>>;
10593
| -- this generic parameter must be used with a generic lifetime parameter
106-
...
94+
LL | type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
10795
LL | fn test() -> Opq2 {}
10896
| ^^
10997

110-
error: concrete type differs from previous defining opaque type use
111-
--> $DIR/higher-ranked-regions-basic.rs:77:21
112-
|
113-
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'a, 'b>>;
114-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&'a ()`, got `{type error}`
115-
|
116-
note: previous use here
117-
--> $DIR/higher-ranked-regions-basic.rs:79:17
118-
|
119-
LL | type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
120-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
121-
122-
error: aborting due to 12 previous errors
98+
error: aborting due to 10 previous errors
12399

124100
Some errors have detailed explanations: E0700, E0792.
125101
For more information about an error, try `rustc --explain E0700`.

tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-gat.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub trait Trait {
88

99
pub type Foo = impl for<'a> Trait<Assoc<'a> = FooAssoc<'a>>;
1010
pub type FooAssoc<'a> = impl Sized;
11-
//~^ ERROR: concrete type differs from previous defining opaque type use
1211

1312
struct Struct;
1413
impl Trait for Struct {
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
11
error[E0792]: expected generic lifetime parameter, found `'a`
2-
--> $DIR/higher-ranked-regions-gat.rs:18:18
2+
--> $DIR/higher-ranked-regions-gat.rs:17:18
33
|
44
LL | pub type FooAssoc<'a> = impl Sized;
55
| -- this generic parameter must be used with a generic lifetime parameter
66
...
77
LL | const FOO: Foo = Struct;
88
| ^^^^^^
99

10-
error: concrete type differs from previous defining opaque type use
11-
--> $DIR/higher-ranked-regions-gat.rs:10:25
12-
|
13-
LL | pub type FooAssoc<'a> = impl Sized;
14-
| ^^^^^^^^^^ expected `&'a u32`, got `{type error}`
15-
|
16-
note: previous use here
17-
--> $DIR/higher-ranked-regions-gat.rs:9:16
18-
|
19-
LL | pub type Foo = impl for<'a> Trait<Assoc<'a> = FooAssoc<'a>>;
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21-
22-
error: aborting due to 2 previous errors
10+
error: aborting due to 1 previous error
2311

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

0 commit comments

Comments
 (0)