Skip to content

Commit c75817b

Browse files
committed
Better errors for implied static bound
1 parent ff623ff commit c75817b

File tree

23 files changed

+345
-99
lines changed

23 files changed

+345
-99
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::ty::{self, RegionVid, TyCtxt};
1515
use rustc_span::symbol::{kw, Symbol};
1616
use rustc_span::{sym, DesugaringKind, Span};
1717

18-
use crate::region_infer::BlameConstraint;
18+
use crate::region_infer::{BlameConstraint, ExtraConstraintInfo};
1919
use crate::{
2020
borrow_set::BorrowData, nll::ConstraintDescription, region_infer::Cause, MirBorrowckCtxt,
2121
WriteKind,
@@ -38,6 +38,7 @@ pub(crate) enum BorrowExplanation<'tcx> {
3838
span: Span,
3939
region_name: RegionName,
4040
opt_place_desc: Option<String>,
41+
extra_info: Vec<ExtraConstraintInfo>,
4142
},
4243
Unexplained,
4344
}
@@ -243,6 +244,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
243244
ref region_name,
244245
ref opt_place_desc,
245246
from_closure: _,
247+
ref extra_info,
246248
} => {
247249
region_name.highlight_region_name(err);
248250

@@ -268,6 +270,14 @@ impl<'tcx> BorrowExplanation<'tcx> {
268270
);
269271
};
270272

273+
for extra in extra_info {
274+
match extra {
275+
ExtraConstraintInfo::PlaceholderFromPredicate(span) => {
276+
err.span_note(*span, format!("due to current limitations in the borrow checker, this implies a `'static` lifetime"));
277+
}
278+
}
279+
}
280+
271281
self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name);
272282
}
273283
_ => {}
@@ -309,16 +319,17 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
309319
&self,
310320
borrow_region: RegionVid,
311321
outlived_region: RegionVid,
312-
) -> (ConstraintCategory<'tcx>, bool, Span, Option<RegionName>) {
313-
let BlameConstraint { category, from_closure, cause, variance_info: _ } = self
314-
.regioncx
315-
.best_blame_constraint(borrow_region, NllRegionVariableOrigin::FreeRegion, |r| {
316-
self.regioncx.provides_universal_region(r, borrow_region, outlived_region)
317-
});
322+
) -> (ConstraintCategory<'tcx>, bool, Span, Option<RegionName>, Vec<ExtraConstraintInfo>) {
323+
let (blame_constraint, extra_info) = self.regioncx.best_blame_constraint(
324+
borrow_region,
325+
NllRegionVariableOrigin::FreeRegion,
326+
|r| self.regioncx.provides_universal_region(r, borrow_region, outlived_region),
327+
);
328+
let BlameConstraint { category, from_closure, cause, .. } = blame_constraint;
318329

319330
let outlived_fr_name = self.give_region_a_name(outlived_region);
320331

321-
(category, from_closure, cause.span, outlived_fr_name)
332+
(category, from_closure, cause.span, outlived_fr_name, extra_info)
322333
}
323334

324335
/// Returns structured explanation for *why* the borrow contains the
@@ -390,7 +401,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
390401

391402
None => {
392403
if let Some(region) = self.to_error_region_vid(borrow_region_vid) {
393-
let (category, from_closure, span, region_name) =
404+
let (category, from_closure, span, region_name, extra_info) =
394405
self.free_region_constraint_info(borrow_region_vid, region);
395406
if let Some(region_name) = region_name {
396407
let opt_place_desc = self.describe_place(borrow.borrowed_place.as_ref());
@@ -400,6 +411,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
400411
span,
401412
region_name,
402413
opt_place_desc,
414+
extra_info,
403415
}
404416
} else {
405417
debug!("Could not generate a region name");

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
354354
) {
355355
debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
356356

357-
let BlameConstraint { category, cause, variance_info, from_closure: _ } =
358-
self.regioncx.best_blame_constraint(fr, fr_origin, |r| {
357+
let BlameConstraint { category, cause, variance_info, .. } = self
358+
.regioncx
359+
.best_blame_constraint(fr, fr_origin, |r| {
359360
self.regioncx.provides_universal_region(r, fr, outlived_fr)
360-
});
361+
})
362+
.0;
361363

362364
debug!("report_region_error: category={:?} {:?} {:?}", category, cause, variance_info);
363365

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ enum Trace<'tcx> {
245245
NotVisited,
246246
}
247247

248+
#[derive(Clone, PartialEq, Eq, Debug)]
249+
pub enum ExtraConstraintInfo {
250+
PlaceholderFromPredicate(Span),
251+
}
252+
248253
impl<'tcx> RegionInferenceContext<'tcx> {
249254
/// Creates a new region inference context with a total of
250255
/// `num_region_variables` valid inference variables; the first N
@@ -1818,10 +1823,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
18181823
fr1_origin: NllRegionVariableOrigin,
18191824
fr2: RegionVid,
18201825
) -> (ConstraintCategory<'tcx>, ObligationCause<'tcx>) {
1821-
let BlameConstraint { category, cause, .. } =
1822-
self.best_blame_constraint(fr1, fr1_origin, |r| {
1823-
self.provides_universal_region(r, fr1, fr2)
1824-
});
1826+
let BlameConstraint { category, cause, .. } = self
1827+
.best_blame_constraint(fr1, fr1_origin, |r| self.provides_universal_region(r, fr1, fr2))
1828+
.0;
18251829
(category, cause)
18261830
}
18271831

@@ -2010,7 +2014,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20102014
from_region: RegionVid,
20112015
from_region_origin: NllRegionVariableOrigin,
20122016
target_test: impl Fn(RegionVid) -> bool,
2013-
) -> BlameConstraint<'tcx> {
2017+
) -> (BlameConstraint<'tcx>, Vec<ExtraConstraintInfo>) {
20142018
// Find all paths
20152019
let (path, target_region) =
20162020
self.find_constraint_paths_between_regions(from_region, target_test).unwrap();
@@ -2026,6 +2030,18 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20262030
.collect::<Vec<_>>()
20272031
);
20282032

2033+
let mut extra_info = vec![];
2034+
for constraint in path.iter() {
2035+
let outlived = constraint.sub;
2036+
let Some(origin) = self.var_infos.get(outlived) else { continue; };
2037+
let RegionVariableOrigin::Nll(NllRegionVariableOrigin::Placeholder(p)) = origin.origin else { continue; };
2038+
debug!(?constraint, ?p);
2039+
let ConstraintCategory::Predicate(span) = constraint.category else { continue; };
2040+
extra_info.push(ExtraConstraintInfo::PlaceholderFromPredicate(span));
2041+
// We only want to point to one
2042+
break;
2043+
}
2044+
20292045
// We try to avoid reporting a `ConstraintCategory::Predicate` as our best constraint.
20302046
// Instead, we use it to produce an improved `ObligationCauseCode`.
20312047
// FIXME - determine what we should do if we encounter multiple `ConstraintCategory::Predicate`
@@ -2073,6 +2089,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20732089
from_closure,
20742090
cause: ObligationCause::new(span, CRATE_HIR_ID, cause_code),
20752091
variance_info: constraint.variance_info,
2092+
outlives_constraint: *constraint,
20762093
}
20772094
})
20782095
.collect();
@@ -2174,7 +2191,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
21742191
let best_choice =
21752192
if blame_source { range.rev().find(find_region) } else { range.find(find_region) };
21762193

2177-
debug!(?best_choice, ?blame_source);
2194+
debug!(?best_choice, ?blame_source, ?extra_info);
21782195

21792196
if let Some(i) = best_choice {
21802197
if let Some(next) = categorized_path.get(i + 1) {
@@ -2183,7 +2200,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
21832200
{
21842201
// The return expression is being influenced by the return type being
21852202
// impl Trait, point at the return type and not the return expr.
2186-
return next.clone();
2203+
return (next.clone(), extra_info);
21872204
}
21882205
}
21892206

@@ -2203,7 +2220,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
22032220
}
22042221
}
22052222

2206-
return categorized_path[i].clone();
2223+
return (categorized_path[i].clone(), extra_info);
22072224
}
22082225

22092226
// If that search fails, that is.. unusual. Maybe everything
@@ -2213,7 +2230,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
22132230
categorized_path.sort_by(|p0, p1| p0.category.cmp(&p1.category));
22142231
debug!("sorted_path={:#?}", categorized_path);
22152232

2216-
categorized_path.remove(0)
2233+
(categorized_path.remove(0), extra_info)
22172234
}
22182235

22192236
pub(crate) fn universe_info(&self, universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
@@ -2295,7 +2312,13 @@ impl<'tcx> ClosureRegionRequirementsExt<'tcx> for ClosureRegionRequirements<'tcx
22952312
outlives_requirement={:?}",
22962313
region, outlived_region, outlives_requirement,
22972314
);
2298-
ty::Binder::dummy(ty::OutlivesPredicate(region.into(), outlived_region))
2315+
(
2316+
ty::Binder::dummy(ty::OutlivesPredicate(
2317+
region.into(),
2318+
outlived_region,
2319+
)),
2320+
ConstraintCategory::BoringNoLocation,
2321+
)
22992322
}
23002323

23012324
ClosureOutlivesSubject::Ty(ty) => {
@@ -2305,7 +2328,10 @@ impl<'tcx> ClosureRegionRequirementsExt<'tcx> for ClosureRegionRequirements<'tcx
23052328
outlives_requirement={:?}",
23062329
ty, outlived_region, outlives_requirement,
23072330
);
2308-
ty::Binder::dummy(ty::OutlivesPredicate(ty.into(), outlived_region))
2331+
(
2332+
ty::Binder::dummy(ty::OutlivesPredicate(ty.into(), outlived_region)),
2333+
ConstraintCategory::BoringNoLocation,
2334+
)
23092335
}
23102336
}
23112337
})
@@ -2319,4 +2345,5 @@ pub struct BlameConstraint<'tcx> {
23192345
pub from_closure: bool,
23202346
pub cause: ObligationCause<'tcx>,
23212347
pub variance_info: ty::VarianceDiagInfo<'tcx>,
2348+
pub outlives_constraint: OutlivesConstraint<'tcx>,
23222349
}

compiler/rustc_borrowck/src/type_check/canonical.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
2525
/// constraints should occur within this method so that those
2626
/// constraints can be properly localized!**
2727
#[instrument(skip(self, op), level = "trace")]
28-
pub(super) fn fully_perform_op<R, Op>(
28+
pub(super) fn fully_perform_op<R: fmt::Debug, Op>(
2929
&mut self,
3030
locations: Locations,
3131
category: ConstraintCategory<'tcx>,
@@ -39,6 +39,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
3939

4040
let TypeOpOutput { output, constraints, error_info } = op.fully_perform(self.infcx)?;
4141

42+
debug!(?output, ?constraints);
43+
4244
if let Some(data) = constraints {
4345
self.push_region_constraints(locations, category, data);
4446
}

compiler/rustc_borrowck/src/type_check/constraint_conversion.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
8686
}
8787
}
8888

89-
pub(super) fn convert(&mut self, query_constraint: &QueryOutlivesConstraint<'tcx>) {
89+
fn convert(&mut self, query_constraint: &QueryOutlivesConstraint<'tcx>) {
9090
debug!("generate: constraints at: {:#?}", self.locations);
9191

9292
// Extract out various useful fields we'll need below.
@@ -98,15 +98,18 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
9898
// region constraints like `for<'a> 'a: 'b`. At some point
9999
// when we move to universes, we will, and this assertion
100100
// will start to fail.
101-
let ty::OutlivesPredicate(k1, r2) = query_constraint.no_bound_vars().unwrap_or_else(|| {
102-
bug!("query_constraint {:?} contained bound vars", query_constraint,);
103-
});
101+
let ty::OutlivesPredicate(k1, r2) =
102+
query_constraint.0.no_bound_vars().unwrap_or_else(|| {
103+
bug!("query_constraint {:?} contained bound vars", query_constraint,);
104+
});
105+
106+
let constraint_category = query_constraint.1;
104107

105108
match k1.unpack() {
106109
GenericArgKind::Lifetime(r1) => {
107110
let r1_vid = self.to_region_vid(r1);
108111
let r2_vid = self.to_region_vid(r2);
109-
self.add_outlives(r1_vid, r2_vid);
112+
self.add_outlives(r1_vid, r2_vid, constraint_category);
110113
}
111114

112115
GenericArgKind::Type(t1) => {
@@ -121,7 +124,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
121124
Some(implicit_region_bound),
122125
param_env,
123126
)
124-
.type_must_outlive(origin, t1, r2);
127+
.type_must_outlive(origin, t1, r2, constraint_category);
125128
}
126129

127130
GenericArgKind::Const(_) => {
@@ -168,10 +171,19 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
168171
}
169172
}
170173

171-
fn add_outlives(&mut self, sup: ty::RegionVid, sub: ty::RegionVid) {
174+
fn add_outlives(
175+
&mut self,
176+
sup: ty::RegionVid,
177+
sub: ty::RegionVid,
178+
category: ConstraintCategory<'tcx>,
179+
) {
180+
let category = match self.category {
181+
ConstraintCategory::Boring | ConstraintCategory::BoringNoLocation => category,
182+
_ => self.category,
183+
};
172184
self.constraints.outlives_constraints.push(OutlivesConstraint {
173185
locations: self.locations,
174-
category: self.category,
186+
category,
175187
span: self.span,
176188
sub,
177189
sup,
@@ -191,10 +203,11 @@ impl<'a, 'b, 'tcx> TypeOutlivesDelegate<'tcx> for &'a mut ConstraintConversion<'
191203
_origin: SubregionOrigin<'tcx>,
192204
a: ty::Region<'tcx>,
193205
b: ty::Region<'tcx>,
206+
constraint_category: ConstraintCategory<'tcx>,
194207
) {
195208
let b = self.to_region_vid(b);
196209
let a = self.to_region_vid(a);
197-
self.add_outlives(b, a);
210+
self.add_outlives(b, a, constraint_category);
198211
}
199212

200213
fn push_verify(

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2560,7 +2560,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25602560
.enumerate()
25612561
.filter_map(|(idx, constraint)| {
25622562
let ty::OutlivesPredicate(k1, r2) =
2563-
constraint.no_bound_vars().unwrap_or_else(|| {
2563+
constraint.0.no_bound_vars().unwrap_or_else(|| {
25642564
bug!("query_constraint {:?} contained bound vars", constraint,);
25652565
});
25662566

compiler/rustc_error_messages/locales/en-US/infer.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ infer_relate_param_bound = ...so that the type `{$name}` will meet its required
110110
infer_relate_param_bound_2 = ...that is required by this bound
111111
infer_relate_region_param_bound = ...so that the declared lifetime parameter bounds are satisfied
112112
infer_compare_impl_item_obligation = ...so that the definition in impl matches the definition from the trait
113+
infer_ascribe_user_type_prove_predicate = ...so that the where clause holds
113114
114115
infer_nothing = {""}
115116

0 commit comments

Comments
 (0)