Avoid emitting type mismatches against {type error}

This commit is contained in:
Oli Scherer 2024-02-22 09:22:15 +00:00
parent 66bd6453e0
commit 9e016a8b84
9 changed files with 55 additions and 84 deletions

View file

@ -153,12 +153,14 @@ impl<'tcx> RegionInferenceContext<'tcx> {
if let Some(prev) = result.get_mut(&opaque_type_key.def_id) {
if prev.ty != ty {
let guar = ty.error_reported().err().unwrap_or_else(|| {
prev.report_mismatch(
&OpaqueHiddenType { ty, span: concrete_type.span },
opaque_type_key.def_id,
infcx.tcx,
)
.emit()
let (Ok(e) | Err(e)) = prev
.report_mismatch(
&OpaqueHiddenType { ty, span: concrete_type.span },
opaque_type_key.def_id,
infcx.tcx,
)
.map(|d| d.emit());
e
});
prev.ty = Ty::new_error(infcx.tcx, guar);
}

View file

@ -478,7 +478,7 @@ fn sanity_check_found_hidden_type<'tcx>(
} else {
let span = tcx.def_span(key.def_id);
let other = ty::OpaqueHiddenType { ty: hidden_ty, span };
Err(ty.report_mismatch(&other, key.def_id, tcx).emit())
Err(ty.report_mismatch(&other, key.def_id, tcx)?.emit())
}
}

View file

@ -58,10 +58,10 @@ pub(super) fn find_opaque_ty_constraints_for_impl_trait_in_assoc_type(
// Only check against typeck if we didn't already error
if !hidden.ty.references_error() {
for concrete_type in locator.typeck_types {
if concrete_type.ty != tcx.erase_regions(hidden.ty)
&& !(concrete_type, hidden).references_error()
{
hidden.report_mismatch(&concrete_type, def_id, tcx).emit();
if concrete_type.ty != tcx.erase_regions(hidden.ty) {
if let Ok(d) = hidden.report_mismatch(&concrete_type, def_id, tcx) {
d.emit();
}
}
}
}
@ -134,10 +134,10 @@ pub(super) fn find_opaque_ty_constraints_for_tait(tcx: TyCtxt<'_>, def_id: Local
// Only check against typeck if we didn't already error
if !hidden.ty.references_error() {
for concrete_type in locator.typeck_types {
if concrete_type.ty != tcx.erase_regions(hidden.ty)
&& !(concrete_type, hidden).references_error()
{
hidden.report_mismatch(&concrete_type, def_id, tcx).emit();
if concrete_type.ty != tcx.erase_regions(hidden.ty) {
if let Ok(d) = hidden.report_mismatch(&concrete_type, def_id, tcx) {
d.emit();
}
}
}
}
@ -287,8 +287,10 @@ impl TaitConstraintLocator<'_> {
if let Some(&concrete_type) = borrowck_results.concrete_opaque_types.get(&self.def_id) {
debug!(?concrete_type, "found constraint");
if let Some(prev) = &mut self.found {
if concrete_type.ty != prev.ty && !(concrete_type, prev.ty).references_error() {
let guar = prev.report_mismatch(&concrete_type, self.def_id, self.tcx).emit();
if concrete_type.ty != prev.ty {
let (Ok(guar) | Err(guar)) = prev
.report_mismatch(&concrete_type, self.def_id, self.tcx)
.map(|d| d.emit());
prev.ty = Ty::new_error(self.tcx, guar);
}
} else {
@ -361,11 +363,13 @@ pub(super) fn find_opaque_ty_constraints_for_rpit<'tcx>(
hidden_type.remap_generic_params_to_declaration_params(opaque_type_key, tcx, true),
);
if let Some(prev) = &mut hir_opaque_ty {
if concrete_type.ty != prev.ty && !(concrete_type, prev.ty).references_error() {
prev.report_mismatch(&concrete_type, def_id, tcx).stash(
tcx.def_span(opaque_type_key.def_id),
StashKey::OpaqueHiddenTypeMismatch,
);
if concrete_type.ty != prev.ty {
if let Ok(d) = prev.report_mismatch(&concrete_type, def_id, tcx) {
d.stash(
tcx.def_span(opaque_type_key.def_id),
StashKey::OpaqueHiddenTypeMismatch,
);
}
}
} else {
hir_opaque_ty = Some(concrete_type);
@ -436,9 +440,10 @@ impl RpitConstraintChecker<'_> {
debug!(?concrete_type, "found constraint");
if concrete_type.ty != self.found.ty && !(concrete_type, self.found).references_error()
{
self.found.report_mismatch(&concrete_type, self.def_id, self.tcx).emit();
if concrete_type.ty != self.found.ty {
if let Ok(d) = self.found.report_mismatch(&concrete_type, self.def_id, self.tcx) {
d.emit();
}
}
}
}

View file

@ -589,12 +589,14 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
&& last_opaque_ty.ty != hidden_type.ty
{
assert!(!self.fcx.next_trait_solver());
hidden_type
.report_mismatch(&last_opaque_ty, opaque_type_key.def_id, self.tcx())
.stash(
if let Ok(d) =
hidden_type.report_mismatch(&last_opaque_ty, opaque_type_key.def_id, self.tcx())
{
d.stash(
self.tcx().def_span(opaque_type_key.def_id),
StashKey::OpaqueHiddenTypeMismatch,
);
}
}
}
}

View file

@ -845,7 +845,7 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
other: &Self,
opaque_def_id: LocalDefId,
tcx: TyCtxt<'tcx>,
) -> DiagnosticBuilder<'tcx> {
) -> Result<DiagnosticBuilder<'tcx>, ErrorGuaranteed> {
if let Some(diag) = tcx
.sess
.dcx()
@ -853,18 +853,19 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
{
diag.cancel();
}
(self.ty, other.ty).error_reported()?;
// Found different concrete types for the opaque type.
let sub_diag = if self.span == other.span {
TypeMismatchReason::ConflictType { span: self.span }
} else {
TypeMismatchReason::PreviousUse { span: self.span }
};
tcx.dcx().create_err(OpaqueHiddenTypeMismatch {
Ok(tcx.dcx().create_err(OpaqueHiddenTypeMismatch {
self_ty: self.ty,
other_ty: other.ty,
other_span: other.span,
sub: sub_diag,
})
}))
}
#[instrument(level = "debug", skip(tcx), ret)]

View file

@ -37,7 +37,6 @@ mod capture_tait_complex_pass {
use super::*;
type Opq0<'a> = impl Sized;
type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'b>>; // <- Note 'b
//~^ ERROR: concrete type differs from previous defining opaque type use
type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
fn test() -> Opq2 {}
//~^ ERROR: expected generic lifetime parameter, found `'a`
@ -75,7 +74,6 @@ mod constrain_pass {
use super::*;
type Opq0<'a, 'b> = impl Sized;
type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'a, 'b>>;
//~^ ERROR concrete type differs
type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
fn test() -> Opq2 {}
//~^ ERROR: expected generic lifetime parameter, found `'a`

View file

@ -27,28 +27,16 @@ LL | fn test() -> Opq2 {}
| ^^
error[E0792]: expected generic lifetime parameter, found `'a`
--> $DIR/higher-ranked-regions-basic.rs:42:23
--> $DIR/higher-ranked-regions-basic.rs:41:23
|
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'b>>; // <- Note 'b
| -- this generic parameter must be used with a generic lifetime parameter
...
LL | type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
LL | fn test() -> Opq2 {}
| ^^
error: concrete type differs from previous defining opaque type use
--> $DIR/higher-ranked-regions-basic.rs:39:21
|
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'b>>; // <- Note 'b
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&'a ()`, got `{type error}`
|
note: previous use here
--> $DIR/higher-ranked-regions-basic.rs:41:17
|
LL | type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0700]: hidden type for `capture_tait_complex_fail::Opq0<'a>` captures lifetime that does not appear in bounds
--> $DIR/higher-ranked-regions-basic.rs:52:23
--> $DIR/higher-ranked-regions-basic.rs:51:23
|
LL | type Opq0<'a> = impl Sized;
| ---------- opaque type defined here
@ -59,19 +47,19 @@ LL | fn test() -> Opq2 {}
| ^^
error[E0792]: non-defining opaque type use in defining scope
--> $DIR/higher-ranked-regions-basic.rs:60:41
--> $DIR/higher-ranked-regions-basic.rs:59:41
|
LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'static>> {}
| ^^^^^^^^^^^^^^^^^^^^^^ argument `'static` is not a generic parameter
|
note: for this opaque type
--> $DIR/higher-ranked-regions-basic.rs:59:25
--> $DIR/higher-ranked-regions-basic.rs:58:25
|
LL | type Opq0<'a, 'b> = impl Sized;
| ^^^^^^^^^^
error[E0792]: expected generic lifetime parameter, found `'a`
--> $DIR/higher-ranked-regions-basic.rs:60:65
--> $DIR/higher-ranked-regions-basic.rs:59:65
|
LL | type Opq0<'a, 'b> = impl Sized;
| -- this generic parameter must be used with a generic lifetime parameter
@ -79,19 +67,19 @@ LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'static>> {}
| ^^
error: non-defining opaque type use in defining scope
--> $DIR/higher-ranked-regions-basic.rs:69:41
--> $DIR/higher-ranked-regions-basic.rs:68:41
|
LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'a>> {}
| ^^^^^^^^^^^^^^^^^ generic argument `'a` used twice
|
note: for this opaque type
--> $DIR/higher-ranked-regions-basic.rs:68:25
--> $DIR/higher-ranked-regions-basic.rs:67:25
|
LL | type Opq0<'a, 'b> = impl Sized;
| ^^^^^^^^^^
error[E0792]: expected generic lifetime parameter, found `'a`
--> $DIR/higher-ranked-regions-basic.rs:69:60
--> $DIR/higher-ranked-regions-basic.rs:68:60
|
LL | type Opq0<'a, 'b> = impl Sized;
| -- this generic parameter must be used with a generic lifetime parameter
@ -99,27 +87,15 @@ LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'a>> {}
| ^^
error[E0792]: expected generic lifetime parameter, found `'a`
--> $DIR/higher-ranked-regions-basic.rs:80:23
--> $DIR/higher-ranked-regions-basic.rs:78:23
|
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'a, 'b>>;
| -- this generic parameter must be used with a generic lifetime parameter
...
LL | type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
LL | fn test() -> Opq2 {}
| ^^
error: concrete type differs from previous defining opaque type use
--> $DIR/higher-ranked-regions-basic.rs:77:21
|
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'a, 'b>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&'a ()`, got `{type error}`
|
note: previous use here
--> $DIR/higher-ranked-regions-basic.rs:79:17
|
LL | type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 12 previous errors
error: aborting due to 10 previous errors
Some errors have detailed explanations: E0700, E0792.
For more information about an error, try `rustc --explain E0700`.

View file

@ -8,7 +8,6 @@ pub trait Trait {
pub type Foo = impl for<'a> Trait<Assoc<'a> = FooAssoc<'a>>;
pub type FooAssoc<'a> = impl Sized;
//~^ ERROR: concrete type differs from previous defining opaque type use
struct Struct;
impl Trait for Struct {

View file

@ -1,5 +1,5 @@
error[E0792]: expected generic lifetime parameter, found `'a`
--> $DIR/higher-ranked-regions-gat.rs:18:18
--> $DIR/higher-ranked-regions-gat.rs:17:18
|
LL | pub type FooAssoc<'a> = impl Sized;
| -- this generic parameter must be used with a generic lifetime parameter
@ -7,18 +7,6 @@ LL | pub type FooAssoc<'a> = impl Sized;
LL | const FOO: Foo = Struct;
| ^^^^^^
error: concrete type differs from previous defining opaque type use
--> $DIR/higher-ranked-regions-gat.rs:10:25
|
LL | pub type FooAssoc<'a> = impl Sized;
| ^^^^^^^^^^ expected `&'a u32`, got `{type error}`
|
note: previous use here
--> $DIR/higher-ranked-regions-gat.rs:9:16
|
LL | pub type Foo = impl for<'a> Trait<Assoc<'a> = FooAssoc<'a>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0792`.