Skip to content

Commit 61c6cdb

Browse files
authored
Rollup merge of #103915 - chenyukang:yukang/fix-103874, r=lcnr
Improve use of ErrorGuaranteed and code cleanup Part of #103874
2 parents 47bf743 + c6d23bd commit 61c6cdb

File tree

15 files changed

+30
-51
lines changed

15 files changed

+30
-51
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use rustc_hir::def::Namespace;
55
use rustc_middle::ty::layout::{LayoutOf, PrimitiveExt, TyAndLayout};
66
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter};
7-
use rustc_middle::ty::{ConstInt, DelaySpanBugEmitted, Ty};
7+
use rustc_middle::ty::{ConstInt, Ty};
88
use rustc_middle::{mir, ty};
99
use rustc_target::abi::{self, Abi, Align, HasDataLayout, Size, TagEncoding};
1010
use rustc_target::abi::{VariantIdx, Variants};
@@ -567,7 +567,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
567567
ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(..) => {
568568
throw_inval!(TooGeneric)
569569
}
570-
ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => {
570+
ty::ConstKind::Error(reported) => {
571571
throw_inval!(AlreadyReported(reported))
572572
}
573573
ty::ConstKind::Unevaluated(uv) => {

compiler/rustc_const_eval/src/transform/promote_consts.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> {
4545
// There's not really any point in promoting errorful MIR.
4646
//
4747
// This does not include MIR that failed const-checking, which we still try to promote.
48-
if body.return_ty().references_error() {
49-
tcx.sess.delay_span_bug(body.span, "PromoteTemps: MIR had errors");
48+
if let Err(_) = body.return_ty().error_reported() {
49+
debug!("PromoteTemps: MIR had errors");
5050
return;
5151
}
52-
5352
if body.source.promoted.is_some() {
5453
return;
5554
}

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19791979
}
19801980

19811981
err.emit()
1982-
} else if let Some(reported) = qself_ty.error_reported() {
1982+
} else if let Err(reported) = qself_ty.error_reported() {
19831983
reported
19841984
} else {
19851985
// Don't print `TyErr` to the user.

compiler/rustc_hir_analysis/src/coherence/orphan.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ pub(crate) fn orphan_check_impl(
2323
impl_def_id: LocalDefId,
2424
) -> Result<(), ErrorGuaranteed> {
2525
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
26-
if let Some(err) = trait_ref.error_reported() {
27-
return Err(err);
28-
}
26+
trait_ref.error_reported()?;
2927

3028
let ret = do_orphan_check_impl(tcx, trait_ref, impl_def_id);
3129
if tcx.trait_is_auto(trait_ref.def_id) {

compiler/rustc_hir_typeck/src/cast.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9494
debug!("pointer_kind({:?}, {:?})", t, span);
9595

9696
let t = self.resolve_vars_if_possible(t);
97-
98-
if let Some(reported) = t.error_reported() {
99-
return Err(reported);
100-
}
97+
t.error_reported()?;
10198

10299
if self.type_is_sized_modulo_regions(self.param_env, t, span) {
103100
return Ok(Some(PointerKind::Thin));
@@ -222,8 +219,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
222219
// inference is more completely known.
223220
match cast_ty.kind() {
224221
ty::Dynamic(_, _, ty::Dyn) | ty::Slice(..) => {
225-
let reported = check.report_cast_to_unsized_type(fcx);
226-
Err(reported)
222+
Err(check.report_cast_to_unsized_type(fcx))
227223
}
228224
_ => Ok(check),
229225
}
@@ -614,10 +610,11 @@ impl<'a, 'tcx> CastCheck<'tcx> {
614610
}
615611

616612
fn report_cast_to_unsized_type(&self, fcx: &FnCtxt<'a, 'tcx>) -> ErrorGuaranteed {
617-
if let Some(reported) =
618-
self.cast_ty.error_reported().or_else(|| self.expr_ty.error_reported())
619-
{
620-
return reported;
613+
if let Err(err) = self.cast_ty.error_reported() {
614+
return err;
615+
}
616+
if let Err(err) = self.expr_ty.error_reported() {
617+
return err;
621618
}
622619

623620
let tstr = fcx.ty_to_string(self.cast_ty);

compiler/rustc_middle/src/traits/specialization_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ pub fn ancestors<'tcx>(
262262

263263
if let Some(reported) = specialization_graph.has_errored {
264264
Err(reported)
265-
} else if let Some(reported) = tcx.type_of(start_from_impl).error_reported() {
265+
} else if let Err(reported) = tcx.type_of(start_from_impl).error_reported() {
266266
Err(reported)
267267
} else {
268268
Ok(Ancestors {

compiler/rustc_middle/src/ty/abstract_const.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A subset of a mir body used for const evaluatability checking.
22
use crate::mir;
33
use crate::ty::visit::TypeVisitable;
4-
use crate::ty::{self, DelaySpanBugEmitted, EarlyBinder, SubstsRef, Ty, TyCtxt};
4+
use crate::ty::{self, EarlyBinder, SubstsRef, Ty, TyCtxt};
55
use rustc_errors::ErrorGuaranteed;
66
use rustc_hir::def_id::DefId;
77
use std::cmp;
@@ -43,7 +43,7 @@ impl<'tcx> AbstractConst<'tcx> {
4343
) -> Result<Option<AbstractConst<'tcx>>, ErrorGuaranteed> {
4444
match ct.kind() {
4545
ty::ConstKind::Unevaluated(uv) => AbstractConst::new(tcx, uv),
46-
ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => Err(reported),
46+
ty::ConstKind::Error(reported) => Err(reported),
4747
_ => Ok(None),
4848
}
4949
}

compiler/rustc_middle/src/ty/consts/kind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub enum ConstKind<'tcx> {
6969

7070
/// A placeholder for a const which could not be computed; this is
7171
/// propagated to avoid useless error messages.
72-
Error(ty::DelaySpanBugEmitted),
72+
Error(ErrorGuaranteed),
7373
}
7474

7575
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]

compiler/rustc_middle/src/ty/context.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
117117
type BoundTy = ty::BoundTy;
118118
type PlaceholderType = ty::PlaceholderType;
119119
type InferTy = InferTy;
120-
type DelaySpanBugEmitted = DelaySpanBugEmitted;
120+
type ErrorGuaranteed = ErrorGuaranteed;
121121
type PredicateKind = ty::PredicateKind<'tcx>;
122122
type AllocId = crate::mir::interpret::AllocId;
123123

@@ -128,15 +128,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
128128
type PlaceholderRegion = ty::PlaceholderRegion;
129129
}
130130

131-
/// A type that is not publicly constructable. This prevents people from making [`TyKind::Error`]s
132-
/// except through the error-reporting functions on a [`tcx`][TyCtxt].
133-
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
134-
#[derive(TyEncodable, TyDecodable, HashStable)]
135-
pub struct DelaySpanBugEmitted {
136-
pub reported: ErrorGuaranteed,
137-
_priv: (),
138-
}
139-
140131
type InternedSet<'tcx, T> = ShardedHashMap<InternedInSet<'tcx, T>, ()>;
141132

142133
pub struct CtxtInterners<'tcx> {
@@ -1303,7 +1294,7 @@ impl<'tcx> TyCtxt<'tcx> {
13031294
#[track_caller]
13041295
pub fn ty_error_with_message<S: Into<MultiSpan>>(self, span: S, msg: &str) -> Ty<'tcx> {
13051296
let reported = self.sess.delay_span_bug(span, msg);
1306-
self.mk_ty(Error(DelaySpanBugEmitted { reported, _priv: () }))
1297+
self.mk_ty(Error(reported))
13071298
}
13081299

13091300
/// Like [TyCtxt::ty_error] but for constants.
@@ -1325,10 +1316,7 @@ impl<'tcx> TyCtxt<'tcx> {
13251316
msg: &str,
13261317
) -> Const<'tcx> {
13271318
let reported = self.sess.delay_span_bug(span, msg);
1328-
self.mk_const(ty::ConstS {
1329-
kind: ty::ConstKind::Error(DelaySpanBugEmitted { reported, _priv: () }),
1330-
ty,
1331-
})
1319+
self.mk_const(ty::ConstS { kind: ty::ConstKind::Error(reported), ty })
13321320
}
13331321

13341322
pub fn consider_optimizing<T: Fn() -> String>(self, msg: T) -> bool {

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub use self::consts::{
8080
};
8181
pub use self::context::{
8282
tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
83-
CtxtInterners, DeducedParamAttrs, DelaySpanBugEmitted, FreeRegionInfo, GeneratorDiagnosticData,
83+
CtxtInterners, DeducedParamAttrs, FreeRegionInfo, GeneratorDiagnosticData,
8484
GeneratorInteriorTypeCause, GlobalCtxt, Lift, OnDiskCache, TyCtxt, TypeckResults, UserType,
8585
UserTypeAnnotationIndex,
8686
};

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ TrivialTypeTraversalAndLiftImpls! {
240240
Field,
241241
interpret::Scalar,
242242
rustc_target::abi::Size,
243-
ty::DelaySpanBugEmitted,
244243
rustc_type_ir::DebruijnIndex,
245244
ty::BoundVar,
246245
ty::Placeholder<ty::BoundVar>,

compiler/rustc_middle/src/ty/visit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ pub trait TypeVisitable<'tcx>: fmt::Debug + Clone {
9595
fn references_error(&self) -> bool {
9696
self.has_type_flags(TypeFlags::HAS_ERROR)
9797
}
98-
fn error_reported(&self) -> Option<ErrorGuaranteed> {
98+
fn error_reported(&self) -> Result<(), ErrorGuaranteed> {
9999
if self.references_error() {
100-
Some(ErrorGuaranteed::unchecked_claim_error_was_emitted())
100+
Err(ErrorGuaranteed::unchecked_claim_error_was_emitted())
101101
} else {
102-
None
102+
Ok(())
103103
}
104104
}
105105
fn has_non_region_param(&self) -> bool {

compiler/rustc_transmute/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ mod rustc {
122122

123123
let c = c.eval(tcx, param_env);
124124

125-
if let Some(err) = c.error_reported() {
125+
if let Err(err) = c.error_reported() {
126126
return Some(Self {
127127
alignment: true,
128128
lifetimes: true,

compiler/rustc_type_ir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub trait Interner {
4545
type BoundTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
4646
type PlaceholderType: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
4747
type InferTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
48-
type DelaySpanBugEmitted: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
48+
type ErrorGuaranteed: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
4949
type PredicateKind: Clone + Debug + Hash + PartialEq + Eq;
5050
type AllocId: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord;
5151

compiler/rustc_type_ir/src/sty.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ pub enum TyKind<I: Interner> {
217217

218218
/// A placeholder for a type which could not be computed; this is
219219
/// propagated to avoid useless error messages.
220-
Error(I::DelaySpanBugEmitted),
220+
Error(I::ErrorGuaranteed),
221221
}
222222

223223
impl<I: Interner> TyKind<I> {
@@ -626,7 +626,7 @@ impl<I: Interner> fmt::Debug for TyKind<I> {
626626
// This is manually implemented because a derive would require `I: Encodable`
627627
impl<I: Interner, E: TyEncoder> Encodable<E> for TyKind<I>
628628
where
629-
I::DelaySpanBugEmitted: Encodable<E>,
629+
I::ErrorGuaranteed: Encodable<E>,
630630
I::AdtDef: Encodable<E>,
631631
I::SubstsRef: Encodable<E>,
632632
I::DefId: Encodable<E>,
@@ -645,7 +645,6 @@ where
645645
I::BoundTy: Encodable<E>,
646646
I::PlaceholderType: Encodable<E>,
647647
I::InferTy: Encodable<E>,
648-
I::DelaySpanBugEmitted: Encodable<E>,
649648
I::PredicateKind: Encodable<E>,
650649
I::AllocId: Encodable<E>,
651650
{
@@ -744,7 +743,7 @@ where
744743
// This is manually implemented because a derive would require `I: Decodable`
745744
impl<I: Interner, D: TyDecoder<I = I>> Decodable<D> for TyKind<I>
746745
where
747-
I::DelaySpanBugEmitted: Decodable<D>,
746+
I::ErrorGuaranteed: Decodable<D>,
748747
I::AdtDef: Decodable<D>,
749748
I::SubstsRef: Decodable<D>,
750749
I::DefId: Decodable<D>,
@@ -763,7 +762,6 @@ where
763762
I::BoundTy: Decodable<D>,
764763
I::PlaceholderType: Decodable<D>,
765764
I::InferTy: Decodable<D>,
766-
I::DelaySpanBugEmitted: Decodable<D>,
767765
I::PredicateKind: Decodable<D>,
768766
I::AllocId: Decodable<D>,
769767
{
@@ -829,7 +827,7 @@ where
829827
I::ParamTy: HashStable<CTX>,
830828
I::PlaceholderType: HashStable<CTX>,
831829
I::InferTy: HashStable<CTX>,
832-
I::DelaySpanBugEmitted: HashStable<CTX>,
830+
I::ErrorGuaranteed: HashStable<CTX>,
833831
{
834832
#[inline]
835833
fn hash_stable(

0 commit comments

Comments
 (0)