Skip to content

Commit

Permalink
make tykind::error bear a proof and delay span bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-i-m committed Mar 23, 2020
1 parent 38114ff commit 833c80a
Show file tree
Hide file tree
Showing 67 changed files with 323 additions and 251 deletions.
1 change: 1 addition & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#![feature(rustc_attrs)]
#![feature(hash_raw_entry)]
#![feature(int_error_matching)]
#![feature(track_caller)]
#![recursion_limit = "512"]

#[macro_use]
Expand Down
16 changes: 12 additions & 4 deletions src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ pub enum StabilityLevel {

impl StabilityLevel {
pub fn from_attr_level(level: &attr::StabilityLevel) -> Self {
if level.is_stable() { Stable } else { Unstable }
if level.is_stable() {
Stable
} else {
Unstable
}
}
}

Expand Down Expand Up @@ -110,7 +114,11 @@ pub fn report_unstable(
let span_key = msp.primary_span().and_then(|sp: Span| {
if !sp.is_dummy() {
let file = sm.lookup_char_pos(sp.lo()).file;
if file.is_imported() { None } else { Some(span) }
if file.is_imported() {
None
} else {
Some(span)
}
} else {
None
}
Expand Down Expand Up @@ -227,7 +235,7 @@ fn late_report_deprecation(
if let hir::Node::Expr(_) = tcx.hir().get(hir_id) {
deprecation_suggestion(&mut diag, suggestion, span);
}
diag.emit()
diag.emit();
});
if hir_id == hir::DUMMY_HIR_ID {
span_bug!(span, "emitted a {} lint with dummy HIR id: {:?}", lint.name, def_id);
Expand Down Expand Up @@ -391,7 +399,7 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn check_stability(self, def_id: DefId, id: Option<HirId>, span: Span) {
let soft_handler = |lint, span, msg: &_| {
self.struct_span_lint_hir(lint, id.unwrap_or(hir::CRATE_HIR_ID), span, |lint| {
lint.build(msg).emit()
lint.build(msg).emit();
})
};
match self.eval_stability(def_id, id, span) {
Expand Down
4 changes: 3 additions & 1 deletion src/librustc/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ impl<'tcx> ConstEvalErr<'tcx> {
}

pub fn report_as_error(&self, tcx: TyCtxtAt<'tcx>, message: &str) -> ErrorHandled {
match self.struct_error(tcx, message, |mut e| e.emit()) {
match self.struct_error(tcx, message, |mut e| {
e.emit();
}) {
Ok(_) => ErrorHandled::Reported,
Err(x) => x,
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
| ty::Ref(..)
| ty::Str
| ty::Foreign(..)
| ty::Error => true,
| ty::Error(..) => true,

// [T; N] and [T] have same properties as T.
ty::Array(ty, _) | ty::Slice(ty) => trivial_dropck_outlives(tcx, ty),
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/traits/specialization_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ pub struct Graph {
pub children: DefIdMap<Children>,

/// Whether an error was emitted while constructing the graph.
pub has_errored: bool,
pub error_reported: Option<ErrorReported>,
}

impl Graph {
pub fn new() -> Graph {
Graph { parent: Default::default(), children: Default::default(), has_errored: false }
Graph { parent: Default::default(), children: Default::default(), error_reported: None }
}

/// The parent of a given impl, which is the `DefId` of the trait when the
Expand Down Expand Up @@ -191,8 +191,8 @@ pub fn ancestors(
start_from_impl: DefId,
) -> Result<Ancestors<'tcx>, ErrorReported> {
let specialization_graph = tcx.specialization_graph_of(trait_def_id);
if specialization_graph.has_errored {
Err(ErrorReported)
if let Some(error_reported) = specialization_graph.error_reported {
Err(error_reported)
} else {
Ok(Ancestors {
trait_def_id,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl TypeRelation<'tcx> for Match<'tcx> {
Err(TypeError::Sorts(relate::expected_found(self, &a, &b)))
}

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

_ => relate::super_relate_tys(self, a, b),
}
Expand Down
54 changes: 29 additions & 25 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ pub struct CommonTypes<'tcx> {
pub f64: Ty<'tcx>,
pub never: Ty<'tcx>,
pub self_param: Ty<'tcx>,
pub err: Ty<'tcx>,

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

pub struct CommonConsts<'tcx> {
pub err: &'tcx Const<'tcx>,
}

pub struct LocalTableInContext<'a, V> {
hir_owner: Option<LocalDefId>,
data: &'a ItemLocalMap<V>,
Expand Down Expand Up @@ -821,7 +816,6 @@ impl<'tcx> CommonTypes<'tcx> {
bool: mk(Bool),
char: mk(Char),
never: mk(Never),
err: mk(Error),
isize: mk(Int(ast::IntTy::Isize)),
i8: mk(Int(ast::IntTy::I8)),
i16: mk(Int(ast::IntTy::I16)),
Expand Down Expand Up @@ -855,19 +849,6 @@ impl<'tcx> CommonLifetimes<'tcx> {
}
}

impl<'tcx> CommonConsts<'tcx> {
fn new(interners: &CtxtInterners<'tcx>, types: &CommonTypes<'tcx>) -> CommonConsts<'tcx> {
let mk_const = |c| interners.const_.intern(c, |c| Interned(interners.arena.alloc(c))).0;

CommonConsts {
err: mk_const(ty::Const {
val: ty::ConstKind::Value(ConstValue::Scalar(Scalar::zst())),
ty: types.err,
}),
}
}
}

// This struct contains information regarding the `ReFree(FreeRegion)` corresponding to a lifetime
// conflict.
#[derive(Debug)]
Expand Down Expand Up @@ -925,9 +906,6 @@ pub struct GlobalCtxt<'tcx> {
/// Common lifetimes, pre-interned for your convenience.
pub lifetimes: CommonLifetimes<'tcx>,

/// Common consts, pre-interned for your convenience.
pub consts: CommonConsts<'tcx>,

/// Resolutions of `extern crate` items produced by resolver.
extern_crate_map: NodeMap<CrateNum>,

Expand Down Expand Up @@ -992,6 +970,34 @@ pub struct GlobalCtxt<'tcx> {
}

impl<'tcx> TyCtxt<'tcx> {
/// Construct an `Error` type. This requires proof that an error has already been emited or
/// will be emited.
#[track_caller]
#[inline]
pub fn err(self, proof: rustc_errors::ErrorProof) -> Ty<'tcx> {
self.sess.diagnostic().delay_span_bug(
rustc_span::DUMMY_SP,
&format!("Error constructed but not emited {}", std::panic::Location::caller()),
);
self.mk_ty(Error(proof))
}

/// Construct an error `const`. This requires proof that an error has already been emited or
/// will be emited.
#[track_caller]
#[inline]
pub fn const_err(self, proof: rustc_errors::ErrorProof) -> Const<'tcx> {
self.sess.diagnostic().delay_span_bug(
rustc_span::DUMMY_SP,
&format!("Error `const` constructed but not emited {}", std::panic::Location::caller()),
);

*self.mk_const(ty::Const {
val: ty::ConstKind::Value(ConstValue::Scalar(Scalar::zst())),
ty: self.err(proof),
})
}

pub fn alloc_steal_mir(self, mir: BodyAndCache<'tcx>) -> &'tcx Steal<BodyAndCache<'tcx>> {
self.arena.alloc(Steal::new(mir))
}
Expand Down Expand Up @@ -1096,7 +1102,6 @@ impl<'tcx> TyCtxt<'tcx> {
let interners = CtxtInterners::new(arena);
let common_types = CommonTypes::new(&interners);
let common_lifetimes = CommonLifetimes::new(&interners);
let common_consts = CommonConsts::new(&interners, &common_types);
let cstore = resolutions.cstore;
let crates = cstore.crates_untracked();
let max_cnum = crates.iter().map(|c| c.as_usize()).max().unwrap_or(0);
Expand Down Expand Up @@ -1146,7 +1151,6 @@ impl<'tcx> TyCtxt<'tcx> {
prof: s.prof.clone(),
types: common_types,
lifetimes: common_lifetimes,
consts: common_consts,
extern_crate_map: resolutions.extern_crate_map,
trait_map,
export_map: resolutions
Expand Down Expand Up @@ -1840,7 +1844,7 @@ macro_rules! sty_debug_print {
let variant = match t.kind {
ty::Bool | ty::Char | ty::Int(..) | ty::Uint(..) |
ty::Float(..) | ty::Str | ty::Never => continue,
ty::Error => /* unimportant */ continue,
ty::Error(..) => /* unimportant */ continue,
$(ty::$variant(..) => &mut $variant,)*
};
let lt = t.flags.intersects(ty::TypeFlags::HAS_RE_INFER);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,14 @@ impl<'tcx> ty::TyS<'tcx> {
ty::UnnormalizedProjection(_) => "non-normalized associated type".into(),
ty::Param(p) => format!("type parameter `{}`", p).into(),
ty::Opaque(..) => "opaque type".into(),
ty::Error => "type error".into(),
ty::Error(..) => "type error".into(),
}
}

pub fn prefix_string(&self) -> Cow<'static, str> {
match self.kind {
ty::Infer(_)
| ty::Error
| ty::Error(..)
| ty::Bool
| ty::Char
| ty::Int(_)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/fast_reject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub fn simplify_type(
}
ty::Opaque(def_id, _) => Some(OpaqueSimplifiedType(def_id)),
ty::Foreign(def_id) => Some(ForeignSimplifiedType(def_id)),
ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) | ty::Error => None,
ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) | ty::Error(..) => None,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl FlagComputation {
// But doing so caused sporadic memory corruption, and
// neither I (tjc) nor nmatsakis could figure out why,
// so we're doing it this way.
&ty::Error => self.add_flags(TypeFlags::HAS_TY_ERR),
&ty::Error(..) => self.add_flags(TypeFlags::HAS_TY_ERR),

&ty::Param(_) => {
self.add_flags(TypeFlags::HAS_TY_PARAM);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
| ty::GeneratorWitness(..)
| ty::Infer(_) => bug!("LayoutDetails::compute: unexpected type `{}`", ty),

ty::Param(_) | ty::Error => {
ty::Param(_) | ty::Error(..) => {
return Err(LayoutError::Unknown(ty));
}
})
Expand Down Expand Up @@ -2144,7 +2144,7 @@ where
| ty::Opaque(..)
| ty::Param(_)
| ty::Infer(_)
| ty::Error => bug!("TyLayout::field_type: unexpected type `{}`", this.ty),
| ty::Error(..) => bug!("TyLayout::field_type: unexpected type `{}`", this.ty),
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/outlives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn compute_components(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, out: &mut SmallVec<[Compo
ty::Dynamic(..) | // OutlivesObject, OutlivesFragment (*)
ty::Placeholder(..) |
ty::Bound(..) |
ty::Error => {
ty::Error(..) => {
// (*) Bare functions and traits are both binders. In the
// RFC, this means we would add the bound regions to the
// "bound regions list". In our representation, no such
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
| ty::Opaque(..)
| ty::Infer(_)
| ty::Bound(..)
| ty::Error
| ty::Error(..)
| ty::GeneratorWitness(..)
| ty::Never
| ty::Float(_) => None,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/print/obsolete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl DefPathBasedNames<'tcx> {
let substs = substs.truncate_to(self.tcx, generics);
self.push_generic_params(substs, iter::empty(), output, debug);
}
ty::Error
ty::Error(..)
| ty::Bound(..)
| ty::Infer(_)
| ty::Placeholder(..)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ pub trait PrettyPrinter<'tcx>:
p!(write("{}", infer_ty))
}
}
ty::Error => p!(write("[type error]")),
ty::Error(..) => p!(write("[type error]")),
ty::Param(ref param_ty) => p!(write("{}", param_ty)),
ty::Bound(debruijn, bound_ty) => match bound_ty.kind {
ty::BoundTyKind::Anon => self.pretty_print_bound_var(debruijn, bound_ty.var)?,
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/ty/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,17 +804,17 @@ impl<'tcx> TyCtxt<'tcx> {

macro_rules! handle_cycle_error {
([][$tcx: expr, $error:expr]) => {{
$tcx.report_cycle($error).emit();
Value::from_cycle_error($tcx)
let proof = $tcx.report_cycle($error).emit().proof();
Value::from_cycle_error($tcx, proof)
}};
([fatal_cycle $($rest:tt)*][$tcx:expr, $error:expr]) => {{
$tcx.report_cycle($error).emit();
$tcx.sess.abort_if_errors();
unreachable!()
}};
([cycle_delay_bug $($rest:tt)*][$tcx:expr, $error:expr]) => {{
$tcx.report_cycle($error).delay_as_bug();
Value::from_cycle_error($tcx)
let proof = $tcx.report_cycle($error).delay_as_bug();
Value::from_cycle_error($tcx, proof)
}};
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
handle_cycle_error!([$($($modifiers)*)*][$($args)*])
Expand Down
15 changes: 8 additions & 7 deletions src/librustc/ty/query/values.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
use crate::ty::{self, AdtSizedConstraint, Ty, TyCtxt};

use rustc_errors::ErrorProof;
use rustc_span::symbol::Symbol;

pub(super) trait Value<'tcx>: Sized {
fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self;
fn from_cycle_error(tcx: TyCtxt<'tcx>, proof: ErrorProof) -> Self;
}

impl<'tcx, T> Value<'tcx> for T {
default fn from_cycle_error(tcx: TyCtxt<'tcx>) -> T {
default fn from_cycle_error(tcx: TyCtxt<'tcx>, _: ErrorProof) -> T {
tcx.sess.abort_if_errors();
bug!("Value::from_cycle_error called without errors");
}
}

impl<'tcx> Value<'tcx> for Ty<'tcx> {
fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
tcx.types.err
fn from_cycle_error(tcx: TyCtxt<'tcx>, proof: ErrorProof) -> Ty<'tcx> {
tcx.err(proof)
}
}

impl<'tcx> Value<'tcx> for ty::SymbolName {
fn from_cycle_error(_: TyCtxt<'tcx>) -> Self {
fn from_cycle_error(_: TyCtxt<'tcx>, _: ErrorProof) -> Self {
ty::SymbolName { name: Symbol::intern("<error>") }
}
}

impl<'tcx> Value<'tcx> for AdtSizedConstraint<'tcx> {
fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self {
AdtSizedConstraint(tcx.intern_type_list(&[tcx.types.err]))
fn from_cycle_error(tcx: TyCtxt<'tcx>, proof: ErrorProof) -> Self {
AdtSizedConstraint(tcx.intern_type_list(&[tcx.err(proof)]))
}
}
2 changes: 1 addition & 1 deletion src/librustc/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ pub fn super_relate_tys<R: TypeRelation<'tcx>>(
bug!("bound types encountered in super_relate_tys")
}

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

(&ty::Never, _)
| (&ty::Char, _)
Expand Down
Loading

0 comments on commit 833c80a

Please sign in to comment.