Skip to content

Commit 833c80a

Browse files
committed
make tykind::error bear a proof and delay span bug
1 parent 38114ff commit 833c80a

File tree

67 files changed

+323
-251
lines changed

Some content is hidden

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

67 files changed

+323
-251
lines changed

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#![feature(rustc_attrs)]
5151
#![feature(hash_raw_entry)]
5252
#![feature(int_error_matching)]
53+
#![feature(track_caller)]
5354
#![recursion_limit = "512"]
5455

5556
#[macro_use]

src/librustc/middle/stability.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ pub enum StabilityLevel {
3030

3131
impl StabilityLevel {
3232
pub fn from_attr_level(level: &attr::StabilityLevel) -> Self {
33-
if level.is_stable() { Stable } else { Unstable }
33+
if level.is_stable() {
34+
Stable
35+
} else {
36+
Unstable
37+
}
3438
}
3539
}
3640

@@ -110,7 +114,11 @@ pub fn report_unstable(
110114
let span_key = msp.primary_span().and_then(|sp: Span| {
111115
if !sp.is_dummy() {
112116
let file = sm.lookup_char_pos(sp.lo()).file;
113-
if file.is_imported() { None } else { Some(span) }
117+
if file.is_imported() {
118+
None
119+
} else {
120+
Some(span)
121+
}
114122
} else {
115123
None
116124
}
@@ -227,7 +235,7 @@ fn late_report_deprecation(
227235
if let hir::Node::Expr(_) = tcx.hir().get(hir_id) {
228236
deprecation_suggestion(&mut diag, suggestion, span);
229237
}
230-
diag.emit()
238+
diag.emit();
231239
});
232240
if hir_id == hir::DUMMY_HIR_ID {
233241
span_bug!(span, "emitted a {} lint with dummy HIR id: {:?}", lint.name, def_id);
@@ -391,7 +399,7 @@ impl<'tcx> TyCtxt<'tcx> {
391399
pub fn check_stability(self, def_id: DefId, id: Option<HirId>, span: Span) {
392400
let soft_handler = |lint, span, msg: &_| {
393401
self.struct_span_lint_hir(lint, id.unwrap_or(hir::CRATE_HIR_ID), span, |lint| {
394-
lint.build(msg).emit()
402+
lint.build(msg).emit();
395403
})
396404
};
397405
match self.eval_stability(def_id, id, span) {

src/librustc/mir/interpret/error.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ impl<'tcx> ConstEvalErr<'tcx> {
8989
}
9090

9191
pub fn report_as_error(&self, tcx: TyCtxtAt<'tcx>, message: &str) -> ErrorHandled {
92-
match self.struct_error(tcx, message, |mut e| e.emit()) {
92+
match self.struct_error(tcx, message, |mut e| {
93+
e.emit();
94+
}) {
9395
Ok(_) => ErrorHandled::Reported,
9496
Err(x) => x,
9597
}

src/librustc/traits/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
221221
| ty::Ref(..)
222222
| ty::Str
223223
| ty::Foreign(..)
224-
| ty::Error => true,
224+
| ty::Error(..) => true,
225225

226226
// [T; N] and [T] have same properties as T.
227227
ty::Array(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, ty),

src/librustc/traits/specialization_graph.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ pub struct Graph {
3232
pub children: DefIdMap<Children>,
3333

3434
/// Whether an error was emitted while constructing the graph.
35-
pub has_errored: bool,
35+
pub error_reported: Option<ErrorReported>,
3636
}
3737

3838
impl Graph {
3939
pub fn new() -> Graph {
40-
Graph { parent: Default::default(), children: Default::default(), has_errored: false }
40+
Graph { parent: Default::default(), children: Default::default(), error_reported: None }
4141
}
4242

4343
/// The parent of a given impl, which is the `DefId` of the trait when the
@@ -191,8 +191,8 @@ pub fn ancestors(
191191
start_from_impl: DefId,
192192
) -> Result<Ancestors<'tcx>, ErrorReported> {
193193
let specialization_graph = tcx.specialization_graph_of(trait_def_id);
194-
if specialization_graph.has_errored {
195-
Err(ErrorReported)
194+
if let Some(error_reported) = specialization_graph.error_reported {
195+
Err(error_reported)
196196
} else {
197197
Ok(Ancestors {
198198
trait_def_id,

src/librustc/ty/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl TypeRelation<'tcx> for Match<'tcx> {
7676
Err(TypeError::Sorts(relate::expected_found(self, &a, &b)))
7777
}
7878

79-
(&ty::Error, _) | (_, &ty::Error) => Ok(self.tcx().types.err),
79+
(&ty::Error(proof), _) | (_, &ty::Error(proof)) => Ok(self.tcx().err(proof)),
8080

8181
_ => relate::super_relate_tys(self, a, b),
8282
}

src/librustc/ty/context.rs

+29-25
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ pub struct CommonTypes<'tcx> {
164164
pub f64: Ty<'tcx>,
165165
pub never: Ty<'tcx>,
166166
pub self_param: Ty<'tcx>,
167-
pub err: Ty<'tcx>,
168167

169168
/// Dummy type used for the `Self` of a `TraitRef` created for converting
170169
/// a trait object, and which gets removed in `ExistentialTraitRef`.
@@ -183,10 +182,6 @@ pub struct CommonLifetimes<'tcx> {
183182
pub re_erased: Region<'tcx>,
184183
}
185184

186-
pub struct CommonConsts<'tcx> {
187-
pub err: &'tcx Const<'tcx>,
188-
}
189-
190185
pub struct LocalTableInContext<'a, V> {
191186
hir_owner: Option<LocalDefId>,
192187
data: &'a ItemLocalMap<V>,
@@ -821,7 +816,6 @@ impl<'tcx> CommonTypes<'tcx> {
821816
bool: mk(Bool),
822817
char: mk(Char),
823818
never: mk(Never),
824-
err: mk(Error),
825819
isize: mk(Int(ast::IntTy::Isize)),
826820
i8: mk(Int(ast::IntTy::I8)),
827821
i16: mk(Int(ast::IntTy::I16)),
@@ -855,19 +849,6 @@ impl<'tcx> CommonLifetimes<'tcx> {
855849
}
856850
}
857851

858-
impl<'tcx> CommonConsts<'tcx> {
859-
fn new(interners: &CtxtInterners<'tcx>, types: &CommonTypes<'tcx>) -> CommonConsts<'tcx> {
860-
let mk_const = |c| interners.const_.intern(c, |c| Interned(interners.arena.alloc(c))).0;
861-
862-
CommonConsts {
863-
err: mk_const(ty::Const {
864-
val: ty::ConstKind::Value(ConstValue::Scalar(Scalar::zst())),
865-
ty: types.err,
866-
}),
867-
}
868-
}
869-
}
870-
871852
// This struct contains information regarding the `ReFree(FreeRegion)` corresponding to a lifetime
872853
// conflict.
873854
#[derive(Debug)]
@@ -925,9 +906,6 @@ pub struct GlobalCtxt<'tcx> {
925906
/// Common lifetimes, pre-interned for your convenience.
926907
pub lifetimes: CommonLifetimes<'tcx>,
927908

928-
/// Common consts, pre-interned for your convenience.
929-
pub consts: CommonConsts<'tcx>,
930-
931909
/// Resolutions of `extern crate` items produced by resolver.
932910
extern_crate_map: NodeMap<CrateNum>,
933911

@@ -992,6 +970,34 @@ pub struct GlobalCtxt<'tcx> {
992970
}
993971

994972
impl<'tcx> TyCtxt<'tcx> {
973+
/// Construct an `Error` type. This requires proof that an error has already been emited or
974+
/// will be emited.
975+
#[track_caller]
976+
#[inline]
977+
pub fn err(self, proof: rustc_errors::ErrorProof) -> Ty<'tcx> {
978+
self.sess.diagnostic().delay_span_bug(
979+
rustc_span::DUMMY_SP,
980+
&format!("Error constructed but not emited {}", std::panic::Location::caller()),
981+
);
982+
self.mk_ty(Error(proof))
983+
}
984+
985+
/// Construct an error `const`. This requires proof that an error has already been emited or
986+
/// will be emited.
987+
#[track_caller]
988+
#[inline]
989+
pub fn const_err(self, proof: rustc_errors::ErrorProof) -> Const<'tcx> {
990+
self.sess.diagnostic().delay_span_bug(
991+
rustc_span::DUMMY_SP,
992+
&format!("Error `const` constructed but not emited {}", std::panic::Location::caller()),
993+
);
994+
995+
*self.mk_const(ty::Const {
996+
val: ty::ConstKind::Value(ConstValue::Scalar(Scalar::zst())),
997+
ty: self.err(proof),
998+
})
999+
}
1000+
9951001
pub fn alloc_steal_mir(self, mir: BodyAndCache<'tcx>) -> &'tcx Steal<BodyAndCache<'tcx>> {
9961002
self.arena.alloc(Steal::new(mir))
9971003
}
@@ -1096,7 +1102,6 @@ impl<'tcx> TyCtxt<'tcx> {
10961102
let interners = CtxtInterners::new(arena);
10971103
let common_types = CommonTypes::new(&interners);
10981104
let common_lifetimes = CommonLifetimes::new(&interners);
1099-
let common_consts = CommonConsts::new(&interners, &common_types);
11001105
let cstore = resolutions.cstore;
11011106
let crates = cstore.crates_untracked();
11021107
let max_cnum = crates.iter().map(|c| c.as_usize()).max().unwrap_or(0);
@@ -1146,7 +1151,6 @@ impl<'tcx> TyCtxt<'tcx> {
11461151
prof: s.prof.clone(),
11471152
types: common_types,
11481153
lifetimes: common_lifetimes,
1149-
consts: common_consts,
11501154
extern_crate_map: resolutions.extern_crate_map,
11511155
trait_map,
11521156
export_map: resolutions
@@ -1840,7 +1844,7 @@ macro_rules! sty_debug_print {
18401844
let variant = match t.kind {
18411845
ty::Bool | ty::Char | ty::Int(..) | ty::Uint(..) |
18421846
ty::Float(..) | ty::Str | ty::Never => continue,
1843-
ty::Error => /* unimportant */ continue,
1847+
ty::Error(..) => /* unimportant */ continue,
18441848
$(ty::$variant(..) => &mut $variant,)*
18451849
};
18461850
let lt = t.flags.intersects(ty::TypeFlags::HAS_RE_INFER);

src/librustc/ty/error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,14 @@ impl<'tcx> ty::TyS<'tcx> {
275275
ty::UnnormalizedProjection(_) => "non-normalized associated type".into(),
276276
ty::Param(p) => format!("type parameter `{}`", p).into(),
277277
ty::Opaque(..) => "opaque type".into(),
278-
ty::Error => "type error".into(),
278+
ty::Error(..) => "type error".into(),
279279
}
280280
}
281281

282282
pub fn prefix_string(&self) -> Cow<'static, str> {
283283
match self.kind {
284284
ty::Infer(_)
285-
| ty::Error
285+
| ty::Error(..)
286286
| ty::Bool
287287
| ty::Char
288288
| ty::Int(_)

src/librustc/ty/fast_reject.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub fn simplify_type(
105105
}
106106
ty::Opaque(def_id, _) => Some(OpaqueSimplifiedType(def_id)),
107107
ty::Foreign(def_id) => Some(ForeignSimplifiedType(def_id)),
108-
ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) | ty::Error => None,
108+
ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) | ty::Error(..) => None,
109109
}
110110
}
111111

src/librustc/ty/flags.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl FlagComputation {
7777
// But doing so caused sporadic memory corruption, and
7878
// neither I (tjc) nor nmatsakis could figure out why,
7979
// so we're doing it this way.
80-
&ty::Error => self.add_flags(TypeFlags::HAS_TY_ERR),
80+
&ty::Error(..) => self.add_flags(TypeFlags::HAS_TY_ERR),
8181

8282
&ty::Param(_) => {
8383
self.add_flags(TypeFlags::HAS_TY_PARAM);

src/librustc/ty/layout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
12491249
| ty::GeneratorWitness(..)
12501250
| ty::Infer(_) => bug!("LayoutDetails::compute: unexpected type `{}`", ty),
12511251

1252-
ty::Param(_) | ty::Error => {
1252+
ty::Param(_) | ty::Error(..) => {
12531253
return Err(LayoutError::Unknown(ty));
12541254
}
12551255
})
@@ -2144,7 +2144,7 @@ where
21442144
| ty::Opaque(..)
21452145
| ty::Param(_)
21462146
| ty::Infer(_)
2147-
| ty::Error => bug!("TyLayout::field_type: unexpected type `{}`", this.ty),
2147+
| ty::Error(..) => bug!("TyLayout::field_type: unexpected type `{}`", this.ty),
21482148
})
21492149
}
21502150

src/librustc/ty/outlives.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ fn compute_components(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, out: &mut SmallVec<[Compo
147147
ty::Dynamic(..) | // OutlivesObject, OutlivesFragment (*)
148148
ty::Placeholder(..) |
149149
ty::Bound(..) |
150-
ty::Error => {
150+
ty::Error(..) => {
151151
// (*) Bare functions and traits are both binders. In the
152152
// RFC, this means we would add the bound regions to the
153153
// "bound regions list". In our representation, no such

src/librustc/ty/print/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
299299
| ty::Opaque(..)
300300
| ty::Infer(_)
301301
| ty::Bound(..)
302-
| ty::Error
302+
| ty::Error(..)
303303
| ty::GeneratorWitness(..)
304304
| ty::Never
305305
| ty::Float(_) => None,

src/librustc/ty/print/obsolete.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl DefPathBasedNames<'tcx> {
144144
let substs = substs.truncate_to(self.tcx, generics);
145145
self.push_generic_params(substs, iter::empty(), output, debug);
146146
}
147-
ty::Error
147+
ty::Error(..)
148148
| ty::Bound(..)
149149
| ty::Infer(_)
150150
| ty::Placeholder(..)

src/librustc/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ pub trait PrettyPrinter<'tcx>:
530530
p!(write("{}", infer_ty))
531531
}
532532
}
533-
ty::Error => p!(write("[type error]")),
533+
ty::Error(..) => p!(write("[type error]")),
534534
ty::Param(ref param_ty) => p!(write("{}", param_ty)),
535535
ty::Bound(debruijn, bound_ty) => match bound_ty.kind {
536536
ty::BoundTyKind::Anon => self.pretty_print_bound_var(debruijn, bound_ty.var)?,

src/librustc/ty/query/plumbing.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -804,17 +804,17 @@ impl<'tcx> TyCtxt<'tcx> {
804804

805805
macro_rules! handle_cycle_error {
806806
([][$tcx: expr, $error:expr]) => {{
807-
$tcx.report_cycle($error).emit();
808-
Value::from_cycle_error($tcx)
807+
let proof = $tcx.report_cycle($error).emit().proof();
808+
Value::from_cycle_error($tcx, proof)
809809
}};
810810
([fatal_cycle $($rest:tt)*][$tcx:expr, $error:expr]) => {{
811811
$tcx.report_cycle($error).emit();
812812
$tcx.sess.abort_if_errors();
813813
unreachable!()
814814
}};
815815
([cycle_delay_bug $($rest:tt)*][$tcx:expr, $error:expr]) => {{
816-
$tcx.report_cycle($error).delay_as_bug();
817-
Value::from_cycle_error($tcx)
816+
let proof = $tcx.report_cycle($error).delay_as_bug();
817+
Value::from_cycle_error($tcx, proof)
818818
}};
819819
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
820820
handle_cycle_error!([$($($modifiers)*)*][$($args)*])

src/librustc/ty/query/values.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
use crate::ty::{self, AdtSizedConstraint, Ty, TyCtxt};
22

3+
use rustc_errors::ErrorProof;
34
use rustc_span::symbol::Symbol;
45

56
pub(super) trait Value<'tcx>: Sized {
6-
fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self;
7+
fn from_cycle_error(tcx: TyCtxt<'tcx>, proof: ErrorProof) -> Self;
78
}
89

910
impl<'tcx, T> Value<'tcx> for T {
10-
default fn from_cycle_error(tcx: TyCtxt<'tcx>) -> T {
11+
default fn from_cycle_error(tcx: TyCtxt<'tcx>, _: ErrorProof) -> T {
1112
tcx.sess.abort_if_errors();
1213
bug!("Value::from_cycle_error called without errors");
1314
}
1415
}
1516

1617
impl<'tcx> Value<'tcx> for Ty<'tcx> {
17-
fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
18-
tcx.types.err
18+
fn from_cycle_error(tcx: TyCtxt<'tcx>, proof: ErrorProof) -> Ty<'tcx> {
19+
tcx.err(proof)
1920
}
2021
}
2122

2223
impl<'tcx> Value<'tcx> for ty::SymbolName {
23-
fn from_cycle_error(_: TyCtxt<'tcx>) -> Self {
24+
fn from_cycle_error(_: TyCtxt<'tcx>, _: ErrorProof) -> Self {
2425
ty::SymbolName { name: Symbol::intern("<error>") }
2526
}
2627
}
2728

2829
impl<'tcx> Value<'tcx> for AdtSizedConstraint<'tcx> {
29-
fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self {
30-
AdtSizedConstraint(tcx.intern_type_list(&[tcx.types.err]))
30+
fn from_cycle_error(tcx: TyCtxt<'tcx>, proof: ErrorProof) -> Self {
31+
AdtSizedConstraint(tcx.intern_type_list(&[tcx.err(proof)]))
3132
}
3233
}

src/librustc/ty/relate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ pub fn super_relate_tys<R: TypeRelation<'tcx>>(
354354
bug!("bound types encountered in super_relate_tys")
355355
}
356356

357-
(&ty::Error, _) | (_, &ty::Error) => Ok(tcx.types.err),
357+
(&ty::Error(proof), _) | (_, &ty::Error(proof)) => Ok(tcx.err(proof)),
358358

359359
(&ty::Never, _)
360360
| (&ty::Char, _)

0 commit comments

Comments
 (0)