Skip to content

Commit 7d2cad6

Browse files
committed
Deduplicate the error printing code for hidden type mismatches
1 parent 25876b3 commit 7d2cad6

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

Diff for: compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
128128
if let Some(prev) = result.get_mut(&opaque_type_key.def_id) {
129129
if prev.ty != ty {
130130
if !ty.references_error() {
131-
let mut err = infcx.tcx.sess.struct_span_err(
132-
concrete_type.span,
133-
"concrete type differs from previous defining opaque type use",
131+
prev.report_mismatch(
132+
&OpaqueHiddenType { ty, span: concrete_type.span },
133+
infcx.tcx,
134134
);
135-
err.span_label(prev.span, format!("expected `{}`, got `{}`", prev.ty, ty));
136-
if prev.span == concrete_type.span {
137-
err.span_label(prev.span, "this expression supplies two conflicting concrete types for the same opaque type");
138-
} else {
139-
err.span_note(prev.span, "previous use here");
140-
}
141-
err.emit();
142135
}
143136
prev.ty = infcx.tcx.ty_error();
144137
}

Diff for: compiler/rustc_middle/src/ty/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,26 @@ pub struct OpaqueHiddenType<'tcx> {
11121112
pub ty: Ty<'tcx>,
11131113
}
11141114

1115+
impl<'tcx> OpaqueHiddenType<'tcx> {
1116+
pub fn report_mismatch(&self, other: &Self, tcx: TyCtxt<'tcx>) {
1117+
// Found different concrete types for the opaque type.
1118+
let mut err = tcx.sess.struct_span_err(
1119+
other.span,
1120+
"concrete type differs from previous defining opaque type use",
1121+
);
1122+
err.span_label(other.span, format!("expected `{}`, got `{}`", self.ty, other.ty));
1123+
if self.span == other.span {
1124+
err.span_label(
1125+
self.span,
1126+
"this expression supplies two conflicting concrete types for the same opaque type",
1127+
);
1128+
} else {
1129+
err.span_note(self.span, "previous use here");
1130+
}
1131+
err.emit();
1132+
}
1133+
}
1134+
11151135
rustc_index::newtype_index! {
11161136
/// "Universes" are used during type- and trait-checking in the
11171137
/// presence of `for<..>` binders to control what sets of names are

Diff for: compiler/rustc_typeck/src/collect/type_of.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -601,21 +601,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
601601

602602
if let Some(prev) = self.found {
603603
if concrete_type.ty != prev.ty && !(concrete_type, prev).references_error() {
604-
// Found different concrete types for the opaque type.
605-
let mut err = self.tcx.sess.struct_span_err(
606-
concrete_type.span,
607-
"concrete type differs from previous defining opaque type use",
608-
);
609-
err.span_label(
610-
concrete_type.span,
611-
format!("expected `{}`, got `{}`", prev.ty, concrete_type.ty),
612-
);
613-
if prev.span == concrete_type.span {
614-
err.span_label(prev.span, "this expression supplies two conflicting concrete types for the same opaque type");
615-
} else {
616-
err.span_note(prev.span, "previous use here");
617-
}
618-
err.emit();
604+
prev.report_mismatch(&concrete_type, self.tcx);
619605
}
620606
} else {
621607
self.found = Some(concrete_type);

0 commit comments

Comments
 (0)