Skip to content

Commit d1ac43a

Browse files
committed
Auto merge of #107652 - estebank:re_error, r=oli-obk
Introduce `ReError` CC #69314 r? `@nagisa`
2 parents 9b8dbd5 + 3689295 commit d1ac43a

File tree

50 files changed

+248
-227
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+248
-227
lines changed

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,11 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
343343
let note = match closure_kind_ty.to_opt_closure_kind() {
344344
Some(ty::ClosureKind::Fn) => {
345345
"closure implements `Fn`, so references to captured variables \
346-
can't escape the closure"
346+
can't escape the closure"
347347
}
348348
Some(ty::ClosureKind::FnMut) => {
349349
"closure implements `FnMut`, so references to captured variables \
350-
can't escape the closure"
350+
can't escape the closure"
351351
}
352352
Some(ty::ClosureKind::FnOnce) => {
353353
bug!("BrEnv in a `FnOnce` closure");
@@ -364,7 +364,11 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
364364
ty::BoundRegionKind::BrAnon(..) => None,
365365
},
366366

367-
ty::ReLateBound(..) | ty::ReVar(..) | ty::RePlaceholder(..) | ty::ReErased => None,
367+
ty::ReLateBound(..)
368+
| ty::ReVar(..)
369+
| ty::RePlaceholder(..)
370+
| ty::ReErased
371+
| ty::ReError(_) => None,
368372
}
369373
}
370374

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
9191
}
9292
None => {
9393
subst_regions.push(vid);
94-
infcx.tcx.sess.delay_span_bug(
94+
infcx.tcx.re_error_with_message(
9595
concrete_type.span,
9696
"opaque type with non-universal region substs",
97-
);
98-
infcx.tcx.lifetimes.re_static
97+
)
9998
}
10099
}
101100
};

compiler/rustc_borrowck/src/universal_regions.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ struct UniversalRegionIndices<'tcx> {
167167
/// contains an entry for `ReStatic` -- it might be nice to just
168168
/// use a substs, and then handle `ReStatic` another way.
169169
indices: FxHashMap<ty::Region<'tcx>, RegionVid>,
170+
171+
/// The vid assigned to `'static`. Used only for diagnostics.
172+
pub fr_static: RegionVid,
170173
}
171174

172175
#[derive(Debug, PartialEq)]
@@ -609,7 +612,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
609612
let subst_mapping =
610613
iter::zip(identity_substs.regions(), fr_substs.regions().map(|r| r.to_region_vid()));
611614

612-
UniversalRegionIndices { indices: global_mapping.chain(subst_mapping).collect() }
615+
UniversalRegionIndices { indices: global_mapping.chain(subst_mapping).collect(), fr_static }
613616
}
614617

615618
fn compute_inputs_and_output(
@@ -821,6 +824,11 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
821824
pub fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
822825
if let ty::ReVar(..) = *r {
823826
r.to_region_vid()
827+
} else if r.is_error() {
828+
// We use the `'static` `RegionVid` because `ReError` doesn't actually exist in the
829+
// `UniversalRegionIndices`. This is fine because 1) it is a fallback only used if
830+
// errors are being emitted and 2) it leaves the happy path unaffected.
831+
self.fr_static
824832
} else {
825833
*self
826834
.indices

compiler/rustc_hir_analysis/src/astconv/mod.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
263263
// elision. `resolve_lifetime` should have
264264
// reported an error in this case -- but if
265265
// not, let's error out.
266-
tcx.sess.delay_span_bug(lifetime.ident.span, "unelided lifetime in signature");
267-
268-
// Supply some dummy value. We don't have an
269-
// `re_error`, annoyingly, so use `'static`.
270-
tcx.lifetimes.re_static
266+
tcx.re_error_with_message(lifetime.ident.span, "unelided lifetime in signature")
271267
})
272268
}
273269
}
@@ -481,11 +477,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
481477
debug!(?param, "unelided lifetime in signature");
482478

483479
// This indicates an illegal lifetime in a non-assoc-trait position
484-
tcx.sess.delay_span_bug(self.span, "unelided lifetime in signature");
485-
486-
// Supply some dummy value. We don't have an
487-
// `re_error`, annoyingly, so use `'static`.
488-
tcx.lifetimes.re_static
480+
tcx.re_error_with_message(self.span, "unelided lifetime in signature")
489481
})
490482
.into(),
491483
GenericParamDefKind::Type { has_default, .. } => {
@@ -1622,14 +1614,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
16221614
"the lifetime bound for this object type cannot be deduced \
16231615
from context; please supply an explicit bound"
16241616
);
1625-
if borrowed {
1617+
let e = if borrowed {
16261618
// We will have already emitted an error E0106 complaining about a
16271619
// missing named lifetime in `&dyn Trait`, so we elide this one.
1628-
err.delay_as_bug();
1620+
err.delay_as_bug()
16291621
} else {
1630-
err.emit();
1631-
}
1632-
tcx.lifetimes.re_static
1622+
err.emit()
1623+
};
1624+
tcx.re_error(e)
16331625
})
16341626
}
16351627
})

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -786,13 +786,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
786786
}
787787
let Some(ty::ReEarlyBound(e)) = map.get(&region.into()).map(|r| r.expect_region().kind())
788788
else {
789-
tcx
790-
.sess
791-
.delay_span_bug(
792-
return_span,
793-
"expected ReFree to map to ReEarlyBound"
794-
);
795-
return tcx.lifetimes.re_static;
789+
return tcx.re_error_with_message(return_span, "expected ReFree to map to ReEarlyBound")
796790
};
797791
tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
798792
def_id: e.def_id,

compiler/rustc_hir_analysis/src/outlives/utils.rs

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ fn is_free_region(region: Region<'_>) -> bool {
170170
// ignore it. We can't put it on the struct header anyway.
171171
ty::ReLateBound(..) => false,
172172

173+
ty::ReError(_) => false,
174+
173175
// These regions don't appear in types from type declarations:
174176
ty::ReErased | ty::ReVar(..) | ty::RePlaceholder(..) | ty::ReFree(..) => {
175177
bug!("unexpected region in outlives inference: {:?}", region);

compiler/rustc_hir_analysis/src/variance/constraints.rs

+2
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
409409
// way early-bound regions do, so we skip them here.
410410
}
411411

412+
ty::ReError(_) => {}
413+
412414
ty::ReFree(..) | ty::ReVar(..) | ty::RePlaceholder(..) | ty::ReErased => {
413415
// We don't expect to see anything but 'static or bound
414416
// regions when visiting member types or method types.

compiler/rustc_infer/src/errors/note_and_explain.rs

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ impl<'a> DescriptionCtx<'a> {
3131

3232
ty::RePlaceholder(_) => return None,
3333

34+
ty::ReError(_) => return None,
35+
3436
// FIXME(#13998) RePlaceholder should probably print like
3537
// ReFree rather than dumping Debug output on the user.
3638
//

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

+1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
369369

370370
ty::ReStatic
371371
| ty::ReEarlyBound(..)
372+
| ty::ReError(_)
372373
| ty::ReFree(_)
373374
| ty::RePlaceholder(..)
374375
| ty::ReErased => self.canonicalize_mode.canonicalize_free_region(self, r),

compiler/rustc_infer/src/infer/combine.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,10 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
705705
return Ok(r);
706706
}
707707

708+
ty::ReError(_) => {
709+
return Ok(r);
710+
}
711+
708712
ty::RePlaceholder(..)
709713
| ty::ReVar(..)
710714
| ty::ReStatic
@@ -861,7 +865,7 @@ impl<'tcx> FallibleTypeFolder<'tcx> for ConstInferUnifier<'_, 'tcx> {
861865
match *r {
862866
// Never make variables for regions bound within the type itself,
863867
// nor for erased regions.
864-
ty::ReLateBound(..) | ty::ReErased => {
868+
ty::ReLateBound(..) | ty::ReErased | ty::ReError(_) => {
865869
return Ok(r);
866870
}
867871

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ pub(super) fn note_and_explain_region<'tcx>(
134134

135135
ty::RePlaceholder(_) => return,
136136

137+
ty::ReError(_) => return,
138+
137139
// FIXME(#13998) RePlaceholder should probably print like
138140
// ReFree rather than dumping Debug output on the user.
139141
//
@@ -313,6 +315,9 @@ pub fn unexpected_hidden_region_diagnostic<'tcx>(
313315
)
314316
}
315317
}
318+
ty::ReError(_) => {
319+
err.delay_as_bug();
320+
}
316321
_ => {
317322
// Ugh. This is a painful case: the hidden region is not one
318323
// that we can easily summarize or explain. This can happen
@@ -2546,7 +2551,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
25462551
);
25472552

25482553
err.note_expected_found(&"", sup_expected, &"", sup_found);
2549-
err.emit();
2554+
if sub_region.is_error() | sup_region.is_error() {
2555+
err.delay_as_bug();
2556+
} else {
2557+
err.emit();
2558+
}
25502559
return;
25512560
}
25522561

@@ -2562,7 +2571,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
25622571
);
25632572

25642573
self.note_region_origin(&mut err, &sub_origin);
2565-
err.emit();
2574+
if sub_region.is_error() | sup_region.is_error() {
2575+
err.delay_as_bug();
2576+
} else {
2577+
err.emit();
2578+
}
25662579
}
25672580

25682581
/// Determine whether an error associated with the given span and definition

compiler/rustc_infer/src/infer/error_reporting/note.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
7878
sub: Region<'tcx>,
7979
sup: Region<'tcx>,
8080
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
81-
match origin {
81+
let mut err = match origin {
8282
infer::Subtype(box trace) => {
8383
let terr = TypeError::RegionsDoesNotOutlive(sup, sub);
8484
let mut err = self.report_and_explain_type_error(trace, terr);
@@ -299,7 +299,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
299299
);
300300
err
301301
}
302+
};
303+
if sub.is_error() || sup.is_error() {
304+
err.delay_as_bug();
302305
}
306+
err
303307
}
304308

305309
pub fn suggest_copy_trait_method_bounds(

compiler/rustc_infer/src/infer/freshen.rs

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
126126
| ty::ReFree(_)
127127
| ty::ReVar(_)
128128
| ty::RePlaceholder(..)
129+
| ty::ReError(_)
129130
| ty::ReErased => {
130131
// replace all free regions with 'erased
131132
self.tcx().lifetimes.re_erased

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_index::vec::{Idx, IndexVec};
1717
use rustc_middle::ty::fold::TypeFoldable;
1818
use rustc_middle::ty::PlaceholderRegion;
1919
use rustc_middle::ty::{self, Ty, TyCtxt};
20-
use rustc_middle::ty::{ReEarlyBound, ReErased, ReFree, ReStatic};
20+
use rustc_middle::ty::{ReEarlyBound, ReErased, ReError, ReFree, ReStatic};
2121
use rustc_middle::ty::{ReLateBound, RePlaceholder, ReVar};
2222
use rustc_middle::ty::{Region, RegionVid};
2323
use rustc_span::Span;
@@ -216,6 +216,8 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
216216
Ok(self.tcx().lifetimes.re_static)
217217
}
218218

219+
ReError(_) => Ok(a_region),
220+
219221
ReEarlyBound(_) | ReFree(_) => {
220222
// All empty regions are less than early-bound, free,
221223
// and scope regions.
@@ -436,7 +438,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
436438
}
437439
(VarValue::Value(a), VarValue::Empty(_)) => {
438440
match *a {
439-
ReLateBound(..) | ReErased => {
441+
ReLateBound(..) | ReErased | ReError(_) => {
440442
bug!("cannot relate region: {:?}", a);
441443
}
442444

@@ -465,7 +467,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
465467
}
466468
(VarValue::Empty(a_ui), VarValue::Value(b)) => {
467469
match *b {
468-
ReLateBound(..) | ReErased => {
470+
ReLateBound(..) | ReErased | ReError(_) => {
469471
bug!("cannot relate region: {:?}", b);
470472
}
471473

@@ -546,6 +548,10 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
546548
);
547549
}
548550

551+
(ReError(_), _) => a,
552+
553+
(_, ReError(_)) => b,
554+
549555
(ReStatic, _) | (_, ReStatic) => {
550556
// nothing lives longer than `'static`
551557
self.tcx().lifetimes.re_static
@@ -1040,7 +1046,7 @@ impl<'tcx> LexicalRegionResolutions<'tcx> {
10401046
ty::ReVar(rid) => match self.values[rid] {
10411047
VarValue::Empty(_) => r,
10421048
VarValue::Value(r) => r,
1043-
VarValue::ErrorValue => tcx.lifetimes.re_static,
1049+
VarValue::ErrorValue => tcx.re_error_misc(),
10441050
},
10451051
_ => r,
10461052
};

compiler/rustc_infer/src/infer/region_constraints/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,11 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
696696

697697
pub fn universe(&self, region: Region<'tcx>) -> ty::UniverseIndex {
698698
match *region {
699-
ty::ReStatic | ty::ReErased | ty::ReFree(..) | ty::ReEarlyBound(..) => {
700-
ty::UniverseIndex::ROOT
701-
}
699+
ty::ReStatic
700+
| ty::ReErased
701+
| ty::ReFree(..)
702+
| ty::ReEarlyBound(..)
703+
| ty::ReError(_) => ty::UniverseIndex::ROOT,
702704
ty::RePlaceholder(placeholder) => placeholder.universe,
703705
ty::ReVar(vid) => self.var_universe(vid),
704706
ty::ReLateBound(..) => bug!("universe(): encountered bound region {:?}", region),

compiler/rustc_middle/src/ty/context.rs

+24
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,30 @@ impl<'tcx> TyCtxt<'tcx> {
661661
self.mk_ty(Error(reported))
662662
}
663663

664+
/// Constructs a `RegionKind::ReError` lifetime.
665+
#[track_caller]
666+
pub fn re_error(self, reported: ErrorGuaranteed) -> Region<'tcx> {
667+
self.mk_region(ty::ReError(reported))
668+
}
669+
670+
/// Constructs a `RegionKind::ReError` lifetime and registers a `delay_span_bug` to ensure it
671+
/// gets used.
672+
#[track_caller]
673+
pub fn re_error_misc(self) -> Region<'tcx> {
674+
self.re_error_with_message(
675+
DUMMY_SP,
676+
"RegionKind::ReError constructed but no error reported",
677+
)
678+
}
679+
680+
/// Constructs a `RegionKind::ReError` lifetime and registers a `delay_span_bug` with the given
681+
/// `msg` to ensure it gets used.
682+
#[track_caller]
683+
pub fn re_error_with_message<S: Into<MultiSpan>>(self, span: S, msg: &str) -> Region<'tcx> {
684+
let reported = self.sess.delay_span_bug(span, msg);
685+
self.re_error(reported)
686+
}
687+
664688
/// Like [TyCtxt::ty_error] but for constants, with current `ErrorGuaranteed`
665689
#[track_caller]
666690
pub fn const_error_with_guaranteed(

compiler/rustc_middle/src/ty/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl GenericParamDef {
100100
preceding_substs: &[ty::GenericArg<'tcx>],
101101
) -> ty::GenericArg<'tcx> {
102102
match &self.kind {
103-
ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_static.into(),
103+
ty::GenericParamDefKind::Lifetime => tcx.re_error_misc().into(),
104104
ty::GenericParamDefKind::Type { .. } => tcx.ty_error().into(),
105105
ty::GenericParamDefKind::Const { .. } => {
106106
tcx.const_error(tcx.bound_type_of(self.def_id).subst(tcx, preceding_substs)).into()

0 commit comments

Comments
 (0)