1
Fork 0

Introduce ReError

CC #69314
This commit is contained in:
Esteban Küber 2023-02-03 23:21:56 +00:00
parent c40919b7a7
commit 30cf7a3f51
51 changed files with 208 additions and 211 deletions

View file

@ -343,11 +343,11 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
let note = match closure_kind_ty.to_opt_closure_kind() {
Some(ty::ClosureKind::Fn) => {
"closure implements `Fn`, so references to captured variables \
can't escape the closure"
can't escape the closure"
}
Some(ty::ClosureKind::FnMut) => {
"closure implements `FnMut`, so references to captured variables \
can't escape the closure"
can't escape the closure"
}
Some(ty::ClosureKind::FnOnce) => {
bug!("BrEnv in a `FnOnce` closure");
@ -364,7 +364,11 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
ty::BoundRegionKind::BrAnon(..) => None,
},
ty::ReLateBound(..) | ty::ReVar(..) | ty::RePlaceholder(..) | ty::ReErased => None,
ty::ReLateBound(..)
| ty::ReVar(..)
| ty::RePlaceholder(..)
| ty::ReErased
| ty::ReError => None,
}
}

View file

@ -95,7 +95,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
concrete_type.span,
"opaque type with non-universal region substs",
);
infcx.tcx.lifetimes.re_static
infcx.tcx.lifetimes.re_error
}
}
};

View file

@ -821,6 +821,8 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
pub fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
if let ty::ReVar(..) = *r {
r.to_region_vid()
} else if let ty::ReError = *r {
RegionVid::new(0)
} else {
*self
.indices

View file

@ -264,10 +264,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
// reported an error in this case -- but if
// not, let's error out.
tcx.sess.delay_span_bug(lifetime.ident.span, "unelided lifetime in signature");
// Supply some dummy value. We don't have an
// `re_error`, annoyingly, so use `'static`.
tcx.lifetimes.re_static
tcx.lifetimes.re_error
})
}
}
@ -482,10 +479,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
// This indicates an illegal lifetime in a non-assoc-trait position
tcx.sess.delay_span_bug(self.span, "unelided lifetime in signature");
// Supply some dummy value. We don't have an
// `re_error`, annoyingly, so use `'static`.
tcx.lifetimes.re_static
tcx.lifetimes.re_error
})
.into(),
GenericParamDefKind::Type { has_default, .. } => {
@ -1629,7 +1623,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
} else {
err.emit();
}
tcx.lifetimes.re_static
tcx.lifetimes.re_error
})
}
})

View file

@ -792,7 +792,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
return_span,
"expected ReFree to map to ReEarlyBound"
);
return tcx.lifetimes.re_static;
return tcx.lifetimes.re_error;
};
tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
def_id: e.def_id,

View file

@ -928,7 +928,7 @@ fn infer_placeholder_type<'a>(
// Typeck doesn't expect erased regions to be returned from `type_of`.
tcx.fold_regions(ty, |r, _| match *r {
ty::ReErased => tcx.lifetimes.re_static,
ty::ReErased | ty::ReError => tcx.lifetimes.re_static,
_ => r,
})
}

View file

@ -170,6 +170,8 @@ fn is_free_region(region: Region<'_>) -> bool {
// ignore it. We can't put it on the struct header anyway.
ty::ReLateBound(..) => false,
ty::ReError => false,
// These regions don't appear in types from type declarations:
ty::ReErased | ty::ReVar(..) | ty::RePlaceholder(..) | ty::ReFree(..) => {
bug!("unexpected region in outlives inference: {:?}", region);

View file

@ -409,6 +409,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
// way early-bound regions do, so we skip them here.
}
ty::ReError => {}
ty::ReFree(..) | ty::ReVar(..) | ty::RePlaceholder(..) | ty::ReErased => {
// We don't expect to see anything but 'static or bound
// regions when visiting member types or method types.

View file

@ -31,6 +31,8 @@ impl<'a> DescriptionCtx<'a> {
ty::RePlaceholder(_) => return None,
ty::ReError => return None,
// FIXME(#13998) RePlaceholder should probably print like
// ReFree rather than dumping Debug output on the user.
//

View file

@ -371,6 +371,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
ty::ReStatic
| ty::ReEarlyBound(..)
| ty::ReError
| ty::ReFree(_)
| ty::RePlaceholder(..)
| ty::ReErased => self.canonicalize_mode.canonicalize_free_region(self, r),

View file

@ -705,6 +705,10 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
return Ok(r);
}
ty::ReError => {
return Ok(r);
}
ty::RePlaceholder(..)
| ty::ReVar(..)
| ty::ReStatic
@ -861,7 +865,7 @@ impl<'tcx> FallibleTypeFolder<'tcx> for ConstInferUnifier<'_, 'tcx> {
match *r {
// Never make variables for regions bound within the type itself,
// nor for erased regions.
ty::ReLateBound(..) | ty::ReErased => {
ty::ReLateBound(..) | ty::ReErased | ty::ReError => {
return Ok(r);
}

View file

@ -134,6 +134,8 @@ pub(super) fn note_and_explain_region<'tcx>(
ty::RePlaceholder(_) => return,
ty::ReError => return,
// FIXME(#13998) RePlaceholder should probably print like
// ReFree rather than dumping Debug output on the user.
//
@ -313,6 +315,9 @@ pub fn unexpected_hidden_region_diagnostic<'tcx>(
)
}
}
ty::ReError => {
err.delay_as_bug();
}
_ => {
// Ugh. This is a painful case: the hidden region is not one
// that we can easily summarize or explain. This can happen
@ -2546,7 +2551,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
);
err.note_expected_found(&"", sup_expected, &"", sup_found);
err.emit();
if sub_region.is_error() | sup_region.is_error() {
err.delay_as_bug();
} else {
err.emit();
}
return;
}
@ -2562,7 +2571,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
);
self.note_region_origin(&mut err, &sub_origin);
err.emit();
if sub_region.is_error() | sup_region.is_error() {
err.delay_as_bug();
} else {
err.emit();
}
}
/// Determine whether an error associated with the given span and definition

View file

@ -78,7 +78,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
sub: Region<'tcx>,
sup: Region<'tcx>,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
match origin {
let mut err = match origin {
infer::Subtype(box trace) => {
let terr = TypeError::RegionsDoesNotOutlive(sup, sub);
let mut err = self.report_and_explain_type_error(trace, terr);
@ -299,7 +299,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
);
err
}
};
if sub.is_error() || sup.is_error() {
err.delay_as_bug();
}
err
}
pub fn suggest_copy_trait_method_bounds(

View file

@ -126,6 +126,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
| ty::ReFree(_)
| ty::ReVar(_)
| ty::RePlaceholder(..)
| ty::ReError
| ty::ReErased => {
// replace all free regions with 'erased
self.tcx().lifetimes.re_erased

View file

@ -17,7 +17,7 @@ use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::ty::fold::TypeFoldable;
use rustc_middle::ty::PlaceholderRegion;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::ty::{ReEarlyBound, ReErased, ReFree, ReStatic};
use rustc_middle::ty::{ReEarlyBound, ReErased, ReError, ReFree, ReStatic};
use rustc_middle::ty::{ReLateBound, RePlaceholder, ReVar};
use rustc_middle::ty::{Region, RegionVid};
use rustc_span::Span;
@ -211,7 +211,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
);
}
ReStatic => {
ReStatic | ReError => {
// nothing lives longer than `'static`
Ok(self.tcx().lifetimes.re_static)
}
@ -436,7 +436,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
}
(VarValue::Value(a), VarValue::Empty(_)) => {
match *a {
ReLateBound(..) | ReErased => {
ReLateBound(..) | ReErased | ReError => {
bug!("cannot relate region: {:?}", a);
}
@ -465,7 +465,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
}
(VarValue::Empty(a_ui), VarValue::Value(b)) => {
match *b {
ReLateBound(..) | ReErased => {
ReLateBound(..) | ReErased | ReError => {
bug!("cannot relate region: {:?}", b);
}
@ -546,6 +546,8 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
);
}
(ReError, _) | (_, ReError) => self.tcx().lifetimes.re_error,
(ReStatic, _) | (_, ReStatic) => {
// nothing lives longer than `'static`
self.tcx().lifetimes.re_static
@ -1040,7 +1042,7 @@ impl<'tcx> LexicalRegionResolutions<'tcx> {
ty::ReVar(rid) => match self.values[rid] {
VarValue::Empty(_) => r,
VarValue::Value(r) => r,
VarValue::ErrorValue => tcx.lifetimes.re_static,
VarValue::ErrorValue => tcx.lifetimes.re_error,
},
_ => r,
};

View file

@ -696,7 +696,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
pub fn universe(&self, region: Region<'tcx>) -> ty::UniverseIndex {
match *region {
ty::ReStatic | ty::ReErased | ty::ReFree(..) | ty::ReEarlyBound(..) => {
ty::ReStatic | ty::ReErased | ty::ReFree(..) | ty::ReEarlyBound(..) | ty::ReError => {
ty::UniverseIndex::ROOT
}
ty::RePlaceholder(placeholder) => placeholder.universe,

View file

@ -275,6 +275,9 @@ pub struct CommonLifetimes<'tcx> {
/// Erased region, used outside of type inference.
pub re_erased: Region<'tcx>,
/// Error region, used only for error reporting.
pub re_error: Region<'tcx>,
}
pub struct CommonConsts<'tcx> {
@ -324,7 +327,11 @@ impl<'tcx> CommonLifetimes<'tcx> {
))
};
CommonLifetimes { re_static: mk(ty::ReStatic), re_erased: mk(ty::ReErased) }
CommonLifetimes {
re_static: mk(ty::ReStatic),
re_erased: mk(ty::ReErased),
re_error: mk(ty::ReError),
}
}
}

View file

@ -100,7 +100,7 @@ impl GenericParamDef {
preceding_substs: &[ty::GenericArg<'tcx>],
) -> ty::GenericArg<'tcx> {
match &self.kind {
ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_static.into(),
ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_error.into(),
ty::GenericParamDefKind::Type { .. } => tcx.ty_error().into(),
ty::GenericParamDefKind::Const { .. } => {
tcx.const_error(tcx.bound_type_of(self.def_id).subst(tcx, preceding_substs)).into()

View file

@ -109,6 +109,8 @@ impl<'tcx> TypeFolder<'tcx> for ReverseMapper<'tcx> {
// them.
ty::ReErased => return r,
ty::ReError => return r,
// The regions that we expect from borrow checking.
ty::ReEarlyBound(_) | ty::ReFree(_) => {}
@ -132,13 +134,13 @@ impl<'tcx> TypeFolder<'tcx> for ReverseMapper<'tcx> {
self.span,
format!(
"lifetime `{}` is part of concrete type but not used in \
parameter list of the `impl Trait` type alias",
parameter list of the `impl Trait` type alias",
r
),
)
.emit();
self.tcx().lifetimes.re_static
self.tcx().lifetimes.re_error
}
}
}

View file

@ -2114,7 +2114,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
ty::ReVar(_) if identify_regions => true,
ty::ReVar(_) | ty::ReErased => false,
ty::ReVar(_) | ty::ReErased | ty::ReError => false,
ty::ReStatic => true,
}
@ -2194,6 +2194,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
}
ty::ReVar(_) => {}
ty::ReErased => {}
ty::ReError => {}
ty::ReStatic => {
p!("'static");
return Ok(self);

View file

@ -1623,9 +1623,15 @@ impl<'tcx> Region<'tcx> {
ty::ReVar(..) => false,
ty::RePlaceholder(placeholder) => placeholder.name.is_named(),
ty::ReErased => false,
ty::ReError => false,
}
}
#[inline]
pub fn is_error(self) -> bool {
matches!(*self, ty::ReError)
}
#[inline]
pub fn is_static(self) -> bool {
matches!(*self, ty::ReStatic)
@ -1686,6 +1692,7 @@ impl<'tcx> Region<'tcx> {
ty::ReErased => {
flags = flags | TypeFlags::HAS_RE_ERASED;
}
ty::ReError => {}
}
debug!("type_flags({:?}) = {:?}", self, flags);

View file

@ -1725,7 +1725,6 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
!segment.has_generic_args,
elided_lifetime_span,
);
err.note("assuming a `'static` lifetime...");
err.emit();
should_lint = false;

View file

@ -299,6 +299,7 @@ fn encode_region<'tcx>(
RegionKind::ReEarlyBound(..)
| RegionKind::ReFree(..)
| RegionKind::ReStatic
| RegionKind::ReError
| RegionKind::ReVar(..)
| RegionKind::RePlaceholder(..) => {
bug!("encode_region: unexpected `{:?}`", region.kind());

View file

@ -493,6 +493,9 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Lifetime<RustInterner<'tcx>>> for Region<'t
ty::ReEarlyBound(_) => {
panic!("Should have already been substituted.");
}
ty::ReError => {
panic!("Error lifetime should not have already been lowered.");
}
ty::ReLateBound(db, br) => chalk_ir::LifetimeData::BoundVar(chalk_ir::BoundVar::new(
chalk_ir::DebruijnIndex::new(db.as_u32()),
br.var.as_usize(),

View file

@ -960,6 +960,9 @@ pub enum RegionKind<I: Interner> {
/// Erased region, used by trait selection, in MIR and during codegen.
ReErased,
/// A region that resulted from some other error. Used exclusively for diagnostics.
ReError,
}
// This is manually implemented for `RegionKind` because `std::mem::discriminant`
@ -974,6 +977,7 @@ const fn regionkind_discriminant<I: Interner>(value: &RegionKind<I>) -> usize {
ReVar(_) => 4,
RePlaceholder(_) => 5,
ReErased => 6,
ReError => 7,
}
}
@ -999,6 +1003,7 @@ impl<I: Interner> Clone for RegionKind<I> {
ReVar(r) => ReVar(r.clone()),
RePlaceholder(r) => RePlaceholder(r.clone()),
ReErased => ReErased,
ReError => ReError,
}
}
}
@ -1077,6 +1082,7 @@ impl<I: Interner> hash::Hash for RegionKind<I> {
ReVar(r) => r.hash(state),
RePlaceholder(r) => r.hash(state),
ReErased => (),
ReError => (),
}
}
}
@ -1100,6 +1106,8 @@ impl<I: Interner> fmt::Debug for RegionKind<I> {
RePlaceholder(placeholder) => write!(f, "RePlaceholder({placeholder:?})"),
ReErased => f.write_str("ReErased"),
ReError => f.write_str("ReError"),
}
}
}
@ -1134,6 +1142,7 @@ where
a.encode(e);
}),
ReErased => e.emit_enum_variant(disc, |_| {}),
ReError => e.emit_enum_variant(disc, |_| {}),
}
}
}
@ -1156,6 +1165,7 @@ where
4 => ReVar(Decodable::decode(d)),
5 => RePlaceholder(Decodable::decode(d)),
6 => ReErased,
7 => ReError,
_ => panic!(
"{}",
format!(
@ -1184,7 +1194,7 @@ where
) {
std::mem::discriminant(self).hash_stable(hcx, hasher);
match self {
ReErased | ReStatic => {
ReErased | ReStatic | ReError => {
// No variant fields to hash for these ...
}
ReLateBound(d, r) => {