Skip to content

Commit 4301231

Browse files
authored
Rollup merge of #101433 - jackh726:better-static-placeholder-error, r=compiler-errors
Emit a note that static bounds from HRTBs are a bug This note isn't perfect, but opening this to either 1) land as is or 2) get some feedback on how to improve it Let r? `@compiler-errors` and cc. `@nikomatsakis`
2 parents c97922d + aae37f8 commit 4301231

File tree

34 files changed

+543
-173
lines changed

34 files changed

+543
-173
lines changed

compiler/rustc_borrowck/src/constraints/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ pub(crate) struct OutlivesConstraintSet<'tcx> {
2121

2222
impl<'tcx> OutlivesConstraintSet<'tcx> {
2323
pub(crate) fn push(&mut self, constraint: OutlivesConstraint<'tcx>) {
24-
debug!(
25-
"OutlivesConstraintSet::push({:?}: {:?} @ {:?}",
26-
constraint.sup, constraint.sub, constraint.locations
27-
);
24+
debug!("OutlivesConstraintSet::push({:?})", constraint);
2825
if constraint.sup == constraint.sub {
2926
// 'a: 'a is pretty uninteresting
3027
return;
@@ -73,7 +70,7 @@ impl<'tcx> Index<OutlivesConstraintIndex> for OutlivesConstraintSet<'tcx> {
7370
}
7471
}
7572

76-
#[derive(Clone, PartialEq, Eq)]
73+
#[derive(Copy, Clone, PartialEq, Eq)]
7774
pub struct OutlivesConstraint<'tcx> {
7875
// NB. The ordering here is not significant for correctness, but
7976
// it is for convenience. Before we dump the constraints in the

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+21-11
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,18 +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: _ } =
314-
self.regioncx.best_blame_constraint(
315-
&self.body,
316-
borrow_region,
317-
NllRegionVariableOrigin::FreeRegion,
318-
|r| self.regioncx.provides_universal_region(r, borrow_region, outlived_region),
319-
);
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;
320329

321330
let outlived_fr_name = self.give_region_a_name(outlived_region);
322331

323-
(category, from_closure, cause.span, outlived_fr_name)
332+
(category, from_closure, cause.span, outlived_fr_name, extra_info)
324333
}
325334

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

393402
None => {
394403
if let Some(region) = self.to_error_region_vid(borrow_region_vid) {
395-
let (category, from_closure, span, region_name) =
404+
let (category, from_closure, span, region_name, extra_info) =
396405
self.free_region_constraint_info(borrow_region_vid, region);
397406
if let Some(region_name) = region_name {
398407
let opt_place_desc = self.describe_place(borrow.borrowed_place.as_ref());
@@ -402,6 +411,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
402411
span,
403412
region_name,
404413
opt_place_desc,
414+
extra_info,
405415
}
406416
} else {
407417
debug!("Could not generate a region name");

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::session_diagnostics::{
3131
};
3232

3333
use super::{OutlivesSuggestionBuilder, RegionName};
34-
use crate::region_infer::BlameConstraint;
34+
use crate::region_infer::{BlameConstraint, ExtraConstraintInfo};
3535
use crate::{
3636
nll::ConstraintDescription,
3737
region_infer::{values::RegionElement, TypeTest},
@@ -234,7 +234,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
234234

235235
// Find the code to blame for the fact that `longer_fr` outlives `error_fr`.
236236
let (_, cause) = self.regioncx.find_outlives_blame_span(
237-
&self.body,
238237
longer_fr,
239238
NllRegionVariableOrigin::Placeholder(placeholder),
240239
error_vid,
@@ -355,10 +354,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
355354
) {
356355
debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
357356

358-
let BlameConstraint { category, cause, variance_info, from_closure: _ } =
359-
self.regioncx.best_blame_constraint(&self.body, fr, fr_origin, |r| {
357+
let (blame_constraint, extra_info) =
358+
self.regioncx.best_blame_constraint(fr, fr_origin, |r| {
360359
self.regioncx.provides_universal_region(r, fr, outlived_fr)
361360
});
361+
let BlameConstraint { category, cause, variance_info, .. } = blame_constraint;
362362

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

@@ -467,6 +467,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
467467
}
468468
}
469469

470+
for extra in extra_info {
471+
match extra {
472+
ExtraConstraintInfo::PlaceholderFromPredicate(span) => {
473+
diag.span_note(span, format!("due to current limitations in the borrow checker, this implies a `'static` lifetime"));
474+
}
475+
}
476+
}
477+
470478
self.buffer_error(diag);
471479
}
472480

@@ -558,6 +566,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
558566
/// LL | ref_obj(x)
559567
/// | ^^^^^^^^^^ `x` escapes the function body here
560568
/// ```
569+
#[instrument(level = "debug", skip(self))]
561570
fn report_escaping_data_error(
562571
&self,
563572
errci: &ErrorConstraintInfo<'tcx>,

0 commit comments

Comments
 (0)