1
Fork 0

Rollup merge of #103915 - chenyukang:yukang/fix-103874, r=lcnr

Improve use of ErrorGuaranteed and code cleanup

Part of #103874
This commit is contained in:
Matthias Krüger 2022-11-04 06:40:32 +01:00 committed by GitHub
commit 61c6cdb5f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 30 additions and 51 deletions

View file

@ -4,7 +4,7 @@
use rustc_hir::def::Namespace; use rustc_hir::def::Namespace;
use rustc_middle::ty::layout::{LayoutOf, PrimitiveExt, TyAndLayout}; use rustc_middle::ty::layout::{LayoutOf, PrimitiveExt, TyAndLayout};
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter}; use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter};
use rustc_middle::ty::{ConstInt, DelaySpanBugEmitted, Ty}; use rustc_middle::ty::{ConstInt, Ty};
use rustc_middle::{mir, ty}; use rustc_middle::{mir, ty};
use rustc_target::abi::{self, Abi, Align, HasDataLayout, Size, TagEncoding}; use rustc_target::abi::{self, Abi, Align, HasDataLayout, Size, TagEncoding};
use rustc_target::abi::{VariantIdx, Variants}; use rustc_target::abi::{VariantIdx, Variants};
@ -567,7 +567,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(..) => { ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(..) => {
throw_inval!(TooGeneric) throw_inval!(TooGeneric)
} }
ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => { ty::ConstKind::Error(reported) => {
throw_inval!(AlreadyReported(reported)) throw_inval!(AlreadyReported(reported))
} }
ty::ConstKind::Unevaluated(uv) => { ty::ConstKind::Unevaluated(uv) => {

View file

@ -45,11 +45,10 @@ impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> {
// There's not really any point in promoting errorful MIR. // There's not really any point in promoting errorful MIR.
// //
// This does not include MIR that failed const-checking, which we still try to promote. // This does not include MIR that failed const-checking, which we still try to promote.
if body.return_ty().references_error() { if let Err(_) = body.return_ty().error_reported() {
tcx.sess.delay_span_bug(body.span, "PromoteTemps: MIR had errors"); debug!("PromoteTemps: MIR had errors");
return; return;
} }
if body.source.promoted.is_some() { if body.source.promoted.is_some() {
return; return;
} }

View file

@ -1979,7 +1979,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
} }
err.emit() err.emit()
} else if let Some(reported) = qself_ty.error_reported() { } else if let Err(reported) = qself_ty.error_reported() {
reported reported
} else { } else {
// Don't print `TyErr` to the user. // Don't print `TyErr` to the user.

View file

@ -23,9 +23,7 @@ pub(crate) fn orphan_check_impl(
impl_def_id: LocalDefId, impl_def_id: LocalDefId,
) -> Result<(), ErrorGuaranteed> { ) -> Result<(), ErrorGuaranteed> {
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap(); let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
if let Some(err) = trait_ref.error_reported() { trait_ref.error_reported()?;
return Err(err);
}
let ret = do_orphan_check_impl(tcx, trait_ref, impl_def_id); let ret = do_orphan_check_impl(tcx, trait_ref, impl_def_id);
if tcx.trait_is_auto(trait_ref.def_id) { if tcx.trait_is_auto(trait_ref.def_id) {

View file

@ -94,10 +94,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
debug!("pointer_kind({:?}, {:?})", t, span); debug!("pointer_kind({:?}, {:?})", t, span);
let t = self.resolve_vars_if_possible(t); let t = self.resolve_vars_if_possible(t);
t.error_reported()?;
if let Some(reported) = t.error_reported() {
return Err(reported);
}
if self.type_is_sized_modulo_regions(self.param_env, t, span) { if self.type_is_sized_modulo_regions(self.param_env, t, span) {
return Ok(Some(PointerKind::Thin)); return Ok(Some(PointerKind::Thin));
@ -222,8 +219,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
// inference is more completely known. // inference is more completely known.
match cast_ty.kind() { match cast_ty.kind() {
ty::Dynamic(_, _, ty::Dyn) | ty::Slice(..) => { ty::Dynamic(_, _, ty::Dyn) | ty::Slice(..) => {
let reported = check.report_cast_to_unsized_type(fcx); Err(check.report_cast_to_unsized_type(fcx))
Err(reported)
} }
_ => Ok(check), _ => Ok(check),
} }
@ -614,10 +610,11 @@ impl<'a, 'tcx> CastCheck<'tcx> {
} }
fn report_cast_to_unsized_type(&self, fcx: &FnCtxt<'a, 'tcx>) -> ErrorGuaranteed { fn report_cast_to_unsized_type(&self, fcx: &FnCtxt<'a, 'tcx>) -> ErrorGuaranteed {
if let Some(reported) = if let Err(err) = self.cast_ty.error_reported() {
self.cast_ty.error_reported().or_else(|| self.expr_ty.error_reported()) return err;
{ }
return reported; if let Err(err) = self.expr_ty.error_reported() {
return err;
} }
let tstr = fcx.ty_to_string(self.cast_ty); let tstr = fcx.ty_to_string(self.cast_ty);

View file

@ -262,7 +262,7 @@ pub fn ancestors<'tcx>(
if let Some(reported) = specialization_graph.has_errored { if let Some(reported) = specialization_graph.has_errored {
Err(reported) Err(reported)
} else if let Some(reported) = tcx.type_of(start_from_impl).error_reported() { } else if let Err(reported) = tcx.type_of(start_from_impl).error_reported() {
Err(reported) Err(reported)
} else { } else {
Ok(Ancestors { Ok(Ancestors {

View file

@ -1,7 +1,7 @@
//! A subset of a mir body used for const evaluatability checking. //! A subset of a mir body used for const evaluatability checking.
use crate::mir; use crate::mir;
use crate::ty::visit::TypeVisitable; use crate::ty::visit::TypeVisitable;
use crate::ty::{self, DelaySpanBugEmitted, EarlyBinder, SubstsRef, Ty, TyCtxt}; use crate::ty::{self, EarlyBinder, SubstsRef, Ty, TyCtxt};
use rustc_errors::ErrorGuaranteed; use rustc_errors::ErrorGuaranteed;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use std::cmp; use std::cmp;
@ -43,7 +43,7 @@ impl<'tcx> AbstractConst<'tcx> {
) -> Result<Option<AbstractConst<'tcx>>, ErrorGuaranteed> { ) -> Result<Option<AbstractConst<'tcx>>, ErrorGuaranteed> {
match ct.kind() { match ct.kind() {
ty::ConstKind::Unevaluated(uv) => AbstractConst::new(tcx, uv), ty::ConstKind::Unevaluated(uv) => AbstractConst::new(tcx, uv),
ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => Err(reported), ty::ConstKind::Error(reported) => Err(reported),
_ => Ok(None), _ => Ok(None),
} }
} }

View file

@ -69,7 +69,7 @@ pub enum ConstKind<'tcx> {
/// A placeholder for a const which could not be computed; this is /// A placeholder for a const which could not be computed; this is
/// propagated to avoid useless error messages. /// propagated to avoid useless error messages.
Error(ty::DelaySpanBugEmitted), Error(ErrorGuaranteed),
} }
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]

View file

@ -117,7 +117,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type BoundTy = ty::BoundTy; type BoundTy = ty::BoundTy;
type PlaceholderType = ty::PlaceholderType; type PlaceholderType = ty::PlaceholderType;
type InferTy = InferTy; type InferTy = InferTy;
type DelaySpanBugEmitted = DelaySpanBugEmitted; type ErrorGuaranteed = ErrorGuaranteed;
type PredicateKind = ty::PredicateKind<'tcx>; type PredicateKind = ty::PredicateKind<'tcx>;
type AllocId = crate::mir::interpret::AllocId; type AllocId = crate::mir::interpret::AllocId;
@ -128,15 +128,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type PlaceholderRegion = ty::PlaceholderRegion; type PlaceholderRegion = ty::PlaceholderRegion;
} }
/// A type that is not publicly constructable. This prevents people from making [`TyKind::Error`]s
/// except through the error-reporting functions on a [`tcx`][TyCtxt].
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[derive(TyEncodable, TyDecodable, HashStable)]
pub struct DelaySpanBugEmitted {
pub reported: ErrorGuaranteed,
_priv: (),
}
type InternedSet<'tcx, T> = ShardedHashMap<InternedInSet<'tcx, T>, ()>; type InternedSet<'tcx, T> = ShardedHashMap<InternedInSet<'tcx, T>, ()>;
pub struct CtxtInterners<'tcx> { pub struct CtxtInterners<'tcx> {
@ -1303,7 +1294,7 @@ impl<'tcx> TyCtxt<'tcx> {
#[track_caller] #[track_caller]
pub fn ty_error_with_message<S: Into<MultiSpan>>(self, span: S, msg: &str) -> Ty<'tcx> { pub fn ty_error_with_message<S: Into<MultiSpan>>(self, span: S, msg: &str) -> Ty<'tcx> {
let reported = self.sess.delay_span_bug(span, msg); let reported = self.sess.delay_span_bug(span, msg);
self.mk_ty(Error(DelaySpanBugEmitted { reported, _priv: () })) self.mk_ty(Error(reported))
} }
/// Like [TyCtxt::ty_error] but for constants. /// Like [TyCtxt::ty_error] but for constants.
@ -1325,10 +1316,7 @@ impl<'tcx> TyCtxt<'tcx> {
msg: &str, msg: &str,
) -> Const<'tcx> { ) -> Const<'tcx> {
let reported = self.sess.delay_span_bug(span, msg); let reported = self.sess.delay_span_bug(span, msg);
self.mk_const(ty::ConstS { self.mk_const(ty::ConstS { kind: ty::ConstKind::Error(reported), ty })
kind: ty::ConstKind::Error(DelaySpanBugEmitted { reported, _priv: () }),
ty,
})
} }
pub fn consider_optimizing<T: Fn() -> String>(self, msg: T) -> bool { pub fn consider_optimizing<T: Fn() -> String>(self, msg: T) -> bool {

View file

@ -80,7 +80,7 @@ pub use self::consts::{
}; };
pub use self::context::{ pub use self::context::{
tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
CtxtInterners, DeducedParamAttrs, DelaySpanBugEmitted, FreeRegionInfo, GeneratorDiagnosticData, CtxtInterners, DeducedParamAttrs, FreeRegionInfo, GeneratorDiagnosticData,
GeneratorInteriorTypeCause, GlobalCtxt, Lift, OnDiskCache, TyCtxt, TypeckResults, UserType, GeneratorInteriorTypeCause, GlobalCtxt, Lift, OnDiskCache, TyCtxt, TypeckResults, UserType,
UserTypeAnnotationIndex, UserTypeAnnotationIndex,
}; };

View file

@ -240,7 +240,6 @@ TrivialTypeTraversalAndLiftImpls! {
Field, Field,
interpret::Scalar, interpret::Scalar,
rustc_target::abi::Size, rustc_target::abi::Size,
ty::DelaySpanBugEmitted,
rustc_type_ir::DebruijnIndex, rustc_type_ir::DebruijnIndex,
ty::BoundVar, ty::BoundVar,
ty::Placeholder<ty::BoundVar>, ty::Placeholder<ty::BoundVar>,

View file

@ -95,11 +95,11 @@ pub trait TypeVisitable<'tcx>: fmt::Debug + Clone {
fn references_error(&self) -> bool { fn references_error(&self) -> bool {
self.has_type_flags(TypeFlags::HAS_ERROR) self.has_type_flags(TypeFlags::HAS_ERROR)
} }
fn error_reported(&self) -> Option<ErrorGuaranteed> { fn error_reported(&self) -> Result<(), ErrorGuaranteed> {
if self.references_error() { if self.references_error() {
Some(ErrorGuaranteed::unchecked_claim_error_was_emitted()) Err(ErrorGuaranteed::unchecked_claim_error_was_emitted())
} else { } else {
None Ok(())
} }
} }
fn has_non_region_param(&self) -> bool { fn has_non_region_param(&self) -> bool {

View file

@ -122,7 +122,7 @@ mod rustc {
let c = c.eval(tcx, param_env); let c = c.eval(tcx, param_env);
if let Some(err) = c.error_reported() { if let Err(err) = c.error_reported() {
return Some(Self { return Some(Self {
alignment: true, alignment: true,
lifetimes: true, lifetimes: true,

View file

@ -45,7 +45,7 @@ pub trait Interner {
type BoundTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; type BoundTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
type PlaceholderType: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; type PlaceholderType: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
type InferTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; type InferTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
type DelaySpanBugEmitted: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; type ErrorGuaranteed: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
type PredicateKind: Clone + Debug + Hash + PartialEq + Eq; type PredicateKind: Clone + Debug + Hash + PartialEq + Eq;
type AllocId: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; type AllocId: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;

View file

@ -217,7 +217,7 @@ pub enum TyKind<I: Interner> {
/// A placeholder for a type which could not be computed; this is /// A placeholder for a type which could not be computed; this is
/// propagated to avoid useless error messages. /// propagated to avoid useless error messages.
Error(I::DelaySpanBugEmitted), Error(I::ErrorGuaranteed),
} }
impl<I: Interner> TyKind<I> { impl<I: Interner> TyKind<I> {
@ -626,7 +626,7 @@ impl<I: Interner> fmt::Debug for TyKind<I> {
// This is manually implemented because a derive would require `I: Encodable` // This is manually implemented because a derive would require `I: Encodable`
impl<I: Interner, E: TyEncoder> Encodable<E> for TyKind<I> impl<I: Interner, E: TyEncoder> Encodable<E> for TyKind<I>
where where
I::DelaySpanBugEmitted: Encodable<E>, I::ErrorGuaranteed: Encodable<E>,
I::AdtDef: Encodable<E>, I::AdtDef: Encodable<E>,
I::SubstsRef: Encodable<E>, I::SubstsRef: Encodable<E>,
I::DefId: Encodable<E>, I::DefId: Encodable<E>,
@ -645,7 +645,6 @@ where
I::BoundTy: Encodable<E>, I::BoundTy: Encodable<E>,
I::PlaceholderType: Encodable<E>, I::PlaceholderType: Encodable<E>,
I::InferTy: Encodable<E>, I::InferTy: Encodable<E>,
I::DelaySpanBugEmitted: Encodable<E>,
I::PredicateKind: Encodable<E>, I::PredicateKind: Encodable<E>,
I::AllocId: Encodable<E>, I::AllocId: Encodable<E>,
{ {
@ -744,7 +743,7 @@ where
// This is manually implemented because a derive would require `I: Decodable` // This is manually implemented because a derive would require `I: Decodable`
impl<I: Interner, D: TyDecoder<I = I>> Decodable<D> for TyKind<I> impl<I: Interner, D: TyDecoder<I = I>> Decodable<D> for TyKind<I>
where where
I::DelaySpanBugEmitted: Decodable<D>, I::ErrorGuaranteed: Decodable<D>,
I::AdtDef: Decodable<D>, I::AdtDef: Decodable<D>,
I::SubstsRef: Decodable<D>, I::SubstsRef: Decodable<D>,
I::DefId: Decodable<D>, I::DefId: Decodable<D>,
@ -763,7 +762,6 @@ where
I::BoundTy: Decodable<D>, I::BoundTy: Decodable<D>,
I::PlaceholderType: Decodable<D>, I::PlaceholderType: Decodable<D>,
I::InferTy: Decodable<D>, I::InferTy: Decodable<D>,
I::DelaySpanBugEmitted: Decodable<D>,
I::PredicateKind: Decodable<D>, I::PredicateKind: Decodable<D>,
I::AllocId: Decodable<D>, I::AllocId: Decodable<D>,
{ {
@ -829,7 +827,7 @@ where
I::ParamTy: HashStable<CTX>, I::ParamTy: HashStable<CTX>,
I::PlaceholderType: HashStable<CTX>, I::PlaceholderType: HashStable<CTX>,
I::InferTy: HashStable<CTX>, I::InferTy: HashStable<CTX>,
I::DelaySpanBugEmitted: HashStable<CTX>, I::ErrorGuaranteed: HashStable<CTX>,
{ {
#[inline] #[inline]
fn hash_stable( fn hash_stable(