Skip to content

Commit 6f8fe4e

Browse files
committed
Eliminate old CTFE's ErrKind
1 parent 58fdac6 commit 6f8fe4e

File tree

10 files changed

+50
-114
lines changed

10 files changed

+50
-114
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ impl_stable_hash_for!(struct ty::Const<'tcx> {
505505

506506
impl_stable_hash_for!(struct ::middle::const_val::ConstEvalErr<'tcx> {
507507
span,
508-
kind
508+
data
509509
});
510510

511511
impl_stable_hash_for!(struct ::middle::const_val::FrameInfo {
@@ -514,29 +514,6 @@ impl_stable_hash_for!(struct ::middle::const_val::FrameInfo {
514514
location
515515
});
516516

517-
impl<'a, 'gcx> HashStable<StableHashingContext<'a>>
518-
for ::middle::const_val::ErrKind<'gcx> {
519-
fn hash_stable<W: StableHasherResult>(&self,
520-
hcx: &mut StableHashingContext<'a>,
521-
hasher: &mut StableHasher<W>) {
522-
use middle::const_val::ErrKind::*;
523-
524-
mem::discriminant(self).hash_stable(hcx, hasher);
525-
526-
match *self {
527-
TypeckError |
528-
CouldNotResolve |
529-
CheckMatchError => {
530-
// nothing to do
531-
}
532-
Miri(ref err, ref trace) => {
533-
err.hash_stable(hcx, hasher);
534-
trace.hash_stable(hcx, hasher);
535-
},
536-
}
537-
}
538-
}
539-
540517
impl_stable_hash_for!(struct ty::ClosureSubsts<'tcx> { substs });
541518
impl_stable_hash_for!(struct ty::GeneratorSubsts<'tcx> { substs });
542519

@@ -593,6 +570,8 @@ for ::mir::interpret::EvalErrorKind<'gcx, O> {
593570
ReadFromReturnPointer |
594571
UnimplementedTraitSelection |
595572
TypeckError |
573+
ResolutionFailed |
574+
CheckMatchError |
596575
DerefFunctionPointer |
597576
ExecuteMemory |
598577
OverflowNeg |

src/librustc/middle/const_val.rs

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,7 @@ pub enum ConstVal<'tcx> {
3333
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
3434
pub struct ConstEvalErr<'tcx> {
3535
pub span: Span,
36-
pub kind: Lrc<ErrKind<'tcx>>,
37-
}
38-
39-
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
40-
pub enum ErrKind<'tcx> {
41-
42-
CouldNotResolve,
43-
TypeckError,
44-
CheckMatchError,
45-
Miri(::mir::interpret::EvalError<'tcx>, Vec<FrameInfo>),
36+
pub data: Lrc<(::mir::interpret::EvalError<'tcx>, Vec<FrameInfo>)>,
4637
}
4738

4839
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
@@ -69,25 +60,6 @@ impl<'a, 'tcx> ConstEvalErrDescription<'a, 'tcx> {
6960
}
7061

7162
impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
72-
pub fn description(&'a self) -> ConstEvalErrDescription<'a, 'tcx> {
73-
use self::ErrKind::*;
74-
use self::ConstEvalErrDescription::*;
75-
76-
macro_rules! simple {
77-
($msg:expr) => ({ Simple($msg.into_cow()) });
78-
($fmt:expr, $($arg:tt)+) => ({
79-
Simple(format!($fmt, $($arg)+).into_cow())
80-
})
81-
}
82-
83-
match *self.kind {
84-
CouldNotResolve => simple!("could not resolve"),
85-
TypeckError => simple!("type-checking failed"),
86-
CheckMatchError => simple!("match-checking failed"),
87-
Miri(ref err, ref trace) => Backtrace(err, trace),
88-
}
89-
}
90-
9163
pub fn struct_error(&self,
9264
tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
9365
message: &str)
@@ -127,24 +99,19 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
12799
message: &str,
128100
lint_root: Option<ast::NodeId>,
129101
) -> Option<DiagnosticBuilder<'tcx>> {
130-
let (msg, frames): (_, &[_]) = match *self.kind {
131-
ErrKind::TypeckError | ErrKind::CheckMatchError => return None,
132-
ErrKind::Miri(ref miri, ref frames) => {
133-
match miri.kind {
134-
::mir::interpret::EvalErrorKind::TypeckError |
135-
::mir::interpret::EvalErrorKind::Layout(_) => return None,
136-
::mir::interpret::EvalErrorKind::ReferencedConstant(ref inner) => {
137-
inner.struct_generic(tcx, "referenced constant", lint_root)?.emit();
138-
(miri.to_string(), frames)
139-
},
140-
_ => (miri.to_string(), frames),
141-
}
142-
}
143-
_ => (self.description().into_oneline().to_string(), &[]),
144-
};
102+
match self.data.0.kind {
103+
::mir::interpret::EvalErrorKind::TypeckError |
104+
::mir::interpret::EvalErrorKind::ResolutionFailed |
105+
::mir::interpret::EvalErrorKind::CheckMatchError |
106+
::mir::interpret::EvalErrorKind::Layout(_) => return None,
107+
::mir::interpret::EvalErrorKind::ReferencedConstant(ref inner) => {
108+
inner.struct_generic(tcx, "referenced constant", lint_root)?.emit();
109+
},
110+
_ => {},
111+
}
145112
trace!("reporting const eval failure at {:?}", self.span);
146113
let mut err = if let Some(lint_root) = lint_root {
147-
let node_id = frames
114+
let node_id = self.data.1
148115
.iter()
149116
.rev()
150117
.filter_map(|frame| frame.lint_root)
@@ -159,8 +126,8 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
159126
} else {
160127
struct_error(tcx, message)
161128
};
162-
err.span_label(self.span, msg);
163-
for FrameInfo { span, location, .. } in frames {
129+
err.span_label(self.span, self.data.0.to_string());
130+
for FrameInfo { span, location, .. } in &self.data.1 {
164131
err.span_label(*span, format!("inside call to `{}`", location));
165132
}
166133
Some(err)

src/librustc/mir/interpret/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ pub enum EvalErrorKind<'tcx, O> {
150150
UnimplementedTraitSelection,
151151
/// Abort in case type errors are reached
152152
TypeckError,
153+
/// Resolution can fail if we are in a too generic context
154+
ResolutionFailed,
155+
CheckMatchError,
153156
/// Cannot compute this constant because it depends on another one
154157
/// which already produced an error
155158
ReferencedConstant(ConstEvalErr<'tcx>),
@@ -268,6 +271,10 @@ impl<'tcx, O> EvalErrorKind<'tcx, O> {
268271
"there were unresolved type arguments during trait selection",
269272
TypeckError =>
270273
"encountered constants with type errors, stopping evaluation",
274+
ResolutionFailed =>
275+
"encountered overly generic constant",
276+
CheckMatchError =>
277+
"match checking failed",
271278
ReferencedConstant(_) =>
272279
"referenced constant has errors",
273280
Overflow(mir::BinOp::Add) => "attempt to add with overflow",

src/librustc/traits/fulfill.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use rustc_data_structures::obligation_forest::{Error, ForestObligation, Obligati
1616
use rustc_data_structures::obligation_forest::{ObligationProcessor, ProcessResult};
1717
use std::marker::PhantomData;
1818
use hir::def_id::DefId;
19-
use middle::const_val::{ConstEvalErr, ErrKind};
19+
use middle::const_val::ConstEvalErr;
20+
use mir::interpret::EvalErrorKind;
2021

2122
use super::CodeAmbiguity;
2223
use super::CodeProjectionError;
@@ -498,10 +499,11 @@ impl<'a, 'b, 'gcx, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'gcx,
498499
CodeSelectionError(ConstEvalFailure(err)))
499500
}
500501
} else {
502+
let err = EvalErrorKind::ResolutionFailed.into();
501503
ProcessResult::Error(
502504
CodeSelectionError(ConstEvalFailure(ConstEvalErr {
503505
span: obligation.cause.span,
504-
kind: ErrKind::CouldNotResolve.into(),
506+
data: (err, Vec::new()).into(),
505507
}))
506508
)
507509
}

src/librustc/ty/structural_impls.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! hand, though we've recently added some macros (e.g.,
1414
//! `BraceStructLiftImpl!`) to help with the tedium.
1515
16-
use middle::const_val::{self, ConstVal, ConstEvalErr};
16+
use middle::const_val::{ConstVal, ConstEvalErr};
1717
use ty::{self, Lift, Ty, TyCtxt};
1818
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1919
use rustc_data_structures::accumulate_vec::AccumulateVec;
@@ -462,10 +462,10 @@ impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
462462
impl<'a, 'tcx> Lift<'tcx> for ConstEvalErr<'a> {
463463
type Lifted = ConstEvalErr<'tcx>;
464464
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
465-
tcx.lift(&*self.kind).map(|kind| {
465+
tcx.lift(&self.data.0).map(|data| {
466466
ConstEvalErr {
467467
span: self.span,
468-
kind: Lrc::new(kind),
468+
data: Lrc::new((data, self.data.1.clone())),
469469
}
470470
})
471471
}
@@ -577,6 +577,8 @@ impl<'a, 'tcx, O: Lift<'tcx>> Lift<'tcx> for interpret::EvalErrorKind<'a, O> {
577577
PathNotFound(ref v) => PathNotFound(v.clone()),
578578
UnimplementedTraitSelection => UnimplementedTraitSelection,
579579
TypeckError => TypeckError,
580+
ResolutionFailed => ResolutionFailed,
581+
CheckMatchError => CheckMatchError,
580582
ReferencedConstant(ref err) => ReferencedConstant(tcx.lift(err)?),
581583
OverflowNeg => OverflowNeg,
582584
Overflow(op) => Overflow(op),
@@ -588,20 +590,6 @@ impl<'a, 'tcx, O: Lift<'tcx>> Lift<'tcx> for interpret::EvalErrorKind<'a, O> {
588590
}
589591
}
590592

591-
impl<'a, 'tcx> Lift<'tcx> for const_val::ErrKind<'a> {
592-
type Lifted = const_val::ErrKind<'tcx>;
593-
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
594-
use middle::const_val::ErrKind::*;
595-
596-
Some(match *self {
597-
CouldNotResolve => CouldNotResolve,
598-
TypeckError => TypeckError,
599-
CheckMatchError => CheckMatchError,
600-
Miri(ref e, ref frames) => return tcx.lift(e).map(|e| Miri(e, frames.clone())),
601-
})
602-
}
603-
}
604-
605593
impl<'a, 'tcx> Lift<'tcx> for ty::layout::LayoutError<'a> {
606594
type Lifted = ty::layout::LayoutError<'tcx>;
607595
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {

src/librustc_mir/interpret/const_eval.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc::hir;
2-
use rustc::middle::const_val::{ConstEvalErr, ErrKind};
3-
use rustc::middle::const_val::ErrKind::{TypeckError, CheckMatchError};
2+
use rustc::middle::const_val::{ConstEvalErr};
43
use rustc::mir;
54
use rustc::ty::{self, TyCtxt, Ty, Instance};
65
use rustc::ty::layout::{self, LayoutOf, Primitive};
@@ -18,7 +17,6 @@ use super::{Place, EvalContext, StackPopCleanup, ValTy, PlaceExtra, Memory, Memo
1817

1918
use std::fmt;
2019
use std::error::Error;
21-
use rustc_data_structures::sync::Lrc;
2220

2321
pub fn mk_borrowck_eval_cx<'a, 'mir, 'tcx>(
2422
tcx: TyCtxt<'a, 'tcx, 'tcx>,
@@ -106,7 +104,7 @@ pub fn value_to_const_value<'tcx>(
106104
let (frames, span) = ecx.generate_stacktrace(None);
107105
let err = ConstEvalErr {
108106
span,
109-
kind: ErrKind::Miri(err, frames).into(),
107+
data: (err, frames).into(),
110108
};
111109
err.report_as_error(
112110
ecx.tcx,
@@ -467,9 +465,8 @@ pub fn const_val_field<'a, 'tcx>(
467465
})();
468466
result.map_err(|err| {
469467
let (trace, span) = ecx.generate_stacktrace(None);
470-
let err = ErrKind::Miri(err, trace);
471468
ConstEvalErr {
472-
kind: err.into(),
469+
data: (err, trace).into(),
473470
span,
474471
}
475472
})
@@ -540,7 +537,7 @@ pub fn const_eval_provider<'a, 'tcx>(
540537
// Do match-check before building MIR
541538
if tcx.check_match(def_id).is_err() {
542539
return Err(ConstEvalErr {
543-
kind: Lrc::new(CheckMatchError),
540+
data: (EvalErrorKind::CheckMatchError.into(), Vec::new()).into(),
544541
span,
545542
});
546543
}
@@ -552,7 +549,7 @@ pub fn const_eval_provider<'a, 'tcx>(
552549
// Do not continue into miri if typeck errors occurred; it will fail horribly
553550
if tables.tainted_by_errors {
554551
return Err(ConstEvalErr {
555-
kind: Lrc::new(TypeckError),
552+
data: (EvalErrorKind::TypeckError.into(), Vec::new()).into(),
556553
span,
557554
});
558555
}
@@ -566,9 +563,8 @@ pub fn const_eval_provider<'a, 'tcx>(
566563
Ok(value_to_const_value(&ecx, val, miri_ty))
567564
}).map_err(|err| {
568565
let (trace, span) = ecx.generate_stacktrace(None);
569-
let err = ErrKind::Miri(err, trace);
570566
let err = ConstEvalErr {
571-
kind: err.into(),
567+
data: (err, trace).into(),
572568
span,
573569
};
574570
if tcx.is_static(def_id).is_some() {

src/librustc_mir/interpret/eval_context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
280280
self.param_env,
281281
def_id,
282282
substs,
283-
).ok_or_else(|| EvalErrorKind::TypeckError.into()) // turn error prop into a panic to expose associated type in const issue
283+
).ok_or_else(|| EvalErrorKind::ResolutionFailed.into())
284284
}
285285

286286
pub(super) fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
@@ -739,7 +739,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
739739
self.param_env,
740740
def_id,
741741
substs,
742-
).ok_or_else(|| EvalErrorKind::TypeckError.into());
742+
).ok_or_else(|| EvalErrorKind::ResolutionFailed.into());
743743
let fn_ptr = self.memory.create_fn_alloc(instance?);
744744
let valty = ValTy {
745745
value: Value::Scalar(fn_ptr.into()),

src/librustc_mir/interpret/memory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
285285
instance,
286286
promoted: None,
287287
};
288-
self.tcx.const_eval(ParamEnv::reveal_all().and(gid)).map_err(|_| {
288+
self.tcx.const_eval(ParamEnv::reveal_all().and(gid)).map_err(|err| {
289289
// no need to report anything, the const_eval call takes care of that for statics
290290
assert!(self.tcx.is_static(def_id).is_some());
291-
EvalErrorKind::TypeckError.into()
291+
EvalErrorKind::ReferencedConstant(err).into()
292292
}).map(|val| {
293293
let const_val = match val.val {
294294
ConstVal::Value(val) => val,

src/librustc_mir/monomorphize/collector.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,15 +1209,12 @@ fn collect_neighbours<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12091209
match tcx.const_eval(param_env.and(cid)) {
12101210
Ok(val) => collect_const(tcx, val, instance.substs, output),
12111211
Err(err) => {
1212-
use rustc::middle::const_val::ErrKind;
12131212
use rustc::mir::interpret::EvalErrorKind;
1214-
if let ErrKind::Miri(ref miri, ..) = *err.kind {
1215-
if let EvalErrorKind::ReferencedConstant(_) = miri.kind {
1216-
err.report_as_error(
1217-
tcx.at(mir.promoted[i].span),
1218-
"erroneous constant used",
1219-
);
1220-
}
1213+
if let EvalErrorKind::ReferencedConstant(_) = err.data.0.kind {
1214+
err.report_as_error(
1215+
tcx.at(mir.promoted[i].span),
1216+
"erroneous constant used",
1217+
);
12211218
}
12221219
},
12231220
}

src/librustc_mir/transform/const_prop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc::mir::{Constant, Literal, Location, Place, Mir, Operand, Rvalue, Local
1717
use rustc::mir::{NullOp, StatementKind, Statement, BasicBlock, LocalKind};
1818
use rustc::mir::{TerminatorKind, ClearCrossCrate, SourceInfo, BinOp, ProjectionElem};
1919
use rustc::mir::visit::{Visitor, PlaceContext};
20-
use rustc::middle::const_val::{ConstVal, ConstEvalErr, ErrKind};
20+
use rustc::middle::const_val::{ConstVal, ConstEvalErr};
2121
use rustc::ty::{TyCtxt, self, Instance};
2222
use rustc::mir::interpret::{Value, Scalar, GlobalId, EvalResult};
2323
use interpret::EvalContext;
@@ -145,7 +145,7 @@ impl<'b, 'a, 'tcx:'b> ConstPropagator<'b, 'a, 'tcx> {
145145
let (frames, span) = self.ecx.generate_stacktrace(None);
146146
let err = ConstEvalErr {
147147
span,
148-
kind: ErrKind::Miri(err, frames).into(),
148+
data: (err, frames).into(),
149149
};
150150
err.report_as_lint(
151151
self.ecx.tcx,

0 commit comments

Comments
 (0)