Skip to content

Commit c217c37

Browse files
committed
name regions in expected/found borrowck errors
1 parent 86ce958 commit c217c37

File tree

8 files changed

+30
-9
lines changed

8 files changed

+30
-9
lines changed

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ impl<'tcx> UniverseInfo<'tcx> {
5656
) {
5757
match self.0 {
5858
UniverseInfoInner::RelateTys { expected, found } => {
59+
let (expected, found) =
60+
mbcx.regioncx.name_regions(mbcx.infcx.tcx, (expected, found));
5961
let err = mbcx.infcx.report_mismatched_types(
6062
&cause,
6163
expected,

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
208208
}
209209

210210
RegionErrorKind::UnexpectedHiddenRegion { span, hidden_ty, key, member_region } => {
211-
let named_ty = self.regioncx.name_regions(self.infcx.tcx, hidden_ty);
212-
let named_key = self.regioncx.name_regions(self.infcx.tcx, key);
213-
let named_region = self.regioncx.name_regions(self.infcx.tcx, member_region);
211+
let named_ty = self.regioncx.name_regions_opaque(self.infcx.tcx, hidden_ty);
212+
let named_key = self.regioncx.name_regions_opaque(self.infcx.tcx, key);
213+
let named_region =
214+
self.regioncx.name_regions_opaque(self.infcx.tcx, member_region);
214215
self.buffer_error(unexpected_hidden_region_diagnostic(
215216
self.infcx.tcx,
216217
span,

compiler/rustc_borrowck/src/region_infer/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -2271,6 +2271,24 @@ impl<'tcx> RegionInferenceContext<'tcx> {
22712271
categorized_path.remove(0)
22722272
}
22732273

2274+
/// Replaces region variables with equal universal regions where possible.
2275+
pub(crate) fn name_regions<T>(&self, tcx: TyCtxt<'tcx>, ty: T) -> T
2276+
where
2277+
T: TypeFoldable<'tcx>,
2278+
{
2279+
tcx.fold_regions(ty, |region, _| match *region {
2280+
ty::ReVar(vid) => {
2281+
let scc = self.constraint_sccs.scc(vid);
2282+
let normalized = self.scc_representatives[scc];
2283+
match &self.definitions[normalized].external_name {
2284+
Some(universal_r) => *universal_r,
2285+
None => region,
2286+
}
2287+
}
2288+
_ => region,
2289+
})
2290+
}
2291+
22742292
pub(crate) fn universe_info(&self, universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
22752293
self.universe_causes[&universe].clone()
22762294
}

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
153153
/// that the regions produced are in fact equal to the named region they are
154154
/// replaced with. This is fine because this function is only to improve the
155155
/// region names in error messages.
156-
pub(crate) fn name_regions<T>(&self, tcx: TyCtxt<'tcx>, ty: T) -> T
156+
pub(crate) fn name_regions_opaque<T>(&self, tcx: TyCtxt<'tcx>, ty: T) -> T
157157
where
158158
T: TypeFoldable<'tcx>,
159159
{

src/test/ui/closure-expected-type/expect-fn-supply-fn.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ error[E0308]: mismatched types
3434
LL | with_closure_expecting_fn_with_bound_region(|x: fn(&'x u32), y| {});
3535
| ^ one type is more general than the other
3636
|
37-
= note: expected fn pointer `fn(&u32)`
37+
= note: expected fn pointer `fn(&'x u32)`
3838
found fn pointer `for<'r> fn(&'r u32)`
3939

4040
error[E0308]: mismatched types

src/test/ui/const-generics/invariant.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ error[E0308]: mismatched types
1818
LL | v
1919
| ^ one type is more general than the other
2020
|
21-
= note: expected reference `&Foo<fn(&())>`
22-
found reference `&Foo<for<'a> fn(&'a ())>`
21+
= note: expected reference `&'static Foo<fn(&'static ())>`
22+
found reference `&'static Foo<for<'a> fn(&'a ())>`
2323

2424
error: aborting due to previous error; 1 warning emitted
2525

src/test/ui/nll/relate_tys/fn-subtype.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let y: for<'a> fn(&'a ()) = x;
55
| ^ one type is more general than the other
66
|
77
= note: expected fn pointer `for<'a> fn(&'a ())`
8-
found fn pointer `fn(&())`
8+
found fn pointer `fn(&'static ())`
99

1010
error: aborting due to previous error
1111

src/test/ui/nll/relate_tys/universe-violation.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let b: fn(&u32) -> &u32 = a;
55
| ^ one type is more general than the other
66
|
77
= note: expected fn pointer `for<'r> fn(&'r u32) -> &'r u32`
8-
found fn pointer `fn(&u32) -> &u32`
8+
found fn pointer `fn(&'static u32) -> &'static u32`
99

1010
error: aborting due to previous error
1111

0 commit comments

Comments
 (0)