1
Fork 0

Prefer regions with an external_name in approx_universal_upper_bound

Fixes #75785

When displaying a MIR borrowcheck error, we may need to find an upper
bound for a region, which gives us a region to point to in the error
message. However, a region might outlive multiple distinct universal
regions, in which case the only upper bound is 'static

To try to display a meaningful error message, we compute an
'approximate' upper bound by picking one of the universal regions.
Currently, we pick the region with the lowest index - however, this
caused us to produce a suboptimal error message in issue #75785

This PR `approx_universal_upper_bound` to prefer regions with an
`external_name`. This causes us to prefer regions from function
arguments/upvars, which seems to lead to a nicer error message in some
cases.
This commit is contained in:
Aaron Hill 2020-10-20 16:31:54 -04:00
parent d23e084483
commit 419d3ae028
No known key found for this signature in database
GPG key ID: B4087E510E98B164
4 changed files with 46 additions and 2 deletions

View file

@ -1145,8 +1145,24 @@ impl<'tcx> RegionInferenceContext<'tcx> {
for ur in self.scc_values.universal_regions_outlived_by(r_scc) {
let new_lub = self.universal_region_relations.postdom_upper_bound(lub, ur);
debug!("approx_universal_upper_bound: ur={:?} lub={:?} new_lub={:?}", ur, lub, new_lub);
// The upper bound of two non-static regions is static: this
// means we know nothing about the relationship between these
// two regions. Pick a 'better' one to use when constructing
// a diagnostic
if ur != static_r && lub != static_r && new_lub == static_r {
lub = std::cmp::min(ur, lub);
// Prefer the region with an `external_name` - this
// indicates that the region is early-bound, so working with
// it can produce a nicer error.
if self.region_definition(ur).external_name.is_some() {
lub = ur;
} else if self.region_definition(lub).external_name.is_some() {
// Leave lub unchanged
} else {
// If we get here, we don't have any reason to prefer
// one region over the other. Just pick the
// one with the lower index for now.
lub = std::cmp::min(ur, lub);
}
} else {
lub = new_lub;
}