Skip to content

Commit 0599477

Browse files
committed
Move everything over from middle::const_val to mir::interpret
1 parent 6005b0a commit 0599477

34 files changed

+158
-166
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,11 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::FieldDef {
370370
}
371371

372372
impl<'a, 'gcx> HashStable<StableHashingContext<'a>>
373-
for ::middle::const_val::ConstVal<'gcx> {
373+
for ::mir::interpret::ConstVal<'gcx> {
374374
fn hash_stable<W: StableHasherResult>(&self,
375375
hcx: &mut StableHashingContext<'a>,
376376
hasher: &mut StableHasher<W>) {
377-
use middle::const_val::ConstVal::*;
377+
use mir::interpret::ConstVal::*;
378378

379379
mem::discriminant(self).hash_stable(hcx, hasher);
380380

@@ -503,13 +503,13 @@ impl_stable_hash_for!(struct ty::Const<'tcx> {
503503
val
504504
});
505505

506-
impl_stable_hash_for!(struct ::middle::const_val::ConstEvalErr<'tcx> {
506+
impl_stable_hash_for!(struct ::mir::interpret::ConstEvalErr<'tcx> {
507507
span,
508508
stacktrace,
509509
error
510510
});
511511

512-
impl_stable_hash_for!(struct ::middle::const_val::FrameInfo {
512+
impl_stable_hash_for!(struct ::mir::interpret::FrameInfo {
513513
span,
514514
lint_root,
515515
location

src/librustc/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ pub mod middle {
132132
pub mod allocator;
133133
pub mod borrowck;
134134
pub mod expr_use_visitor;
135-
pub mod const_val;
136135
pub mod cstore;
137136
pub mod dataflow;
138137
pub mod dead;

src/librustc/middle/const_val.rs

Lines changed: 0 additions & 125 deletions
This file was deleted.

src/librustc/mir/interpret/error.rs

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{fmt, env};
22

33
use mir;
4-
use middle::const_val::ConstEvalErr;
54
use ty::{FnSig, Ty, layout};
65
use ty::layout::{Size, Align};
76
use rustc_data_structures::sync::Lrc;
@@ -12,6 +11,115 @@ use super::{
1211

1312
use backtrace::Backtrace;
1413

14+
15+
use hir::def_id::DefId;
16+
use ty;
17+
use ty::subst::Substs;
18+
use ty::query::TyCtxtAt;
19+
use mir::interpret::ConstValue;
20+
use errors::DiagnosticBuilder;
21+
22+
use syntax_pos::Span;
23+
use syntax::ast;
24+
25+
pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, Lrc<ConstEvalErr<'tcx>>>;
26+
27+
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
28+
pub struct ConstEvalErr<'tcx> {
29+
pub span: Span,
30+
pub error: ::mir::interpret::EvalError<'tcx>,
31+
pub stacktrace: Vec<FrameInfo>,
32+
}
33+
34+
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
35+
pub struct FrameInfo {
36+
pub span: Span,
37+
pub location: String,
38+
pub lint_root: Option<ast::NodeId>,
39+
}
40+
41+
impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
42+
pub fn struct_error(&self,
43+
tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
44+
message: &str)
45+
-> Option<DiagnosticBuilder<'tcx>>
46+
{
47+
self.struct_generic(tcx, message, None)
48+
}
49+
50+
pub fn report_as_error(&self,
51+
tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
52+
message: &str
53+
) {
54+
let err = self.struct_generic(tcx, message, None);
55+
if let Some(mut err) = err {
56+
err.emit();
57+
}
58+
}
59+
60+
pub fn report_as_lint(&self,
61+
tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
62+
message: &str,
63+
lint_root: ast::NodeId,
64+
) {
65+
let lint = self.struct_generic(
66+
tcx,
67+
message,
68+
Some(lint_root),
69+
);
70+
if let Some(mut lint) = lint {
71+
lint.emit();
72+
}
73+
}
74+
75+
fn struct_generic(
76+
&self,
77+
tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
78+
message: &str,
79+
lint_root: Option<ast::NodeId>,
80+
) -> Option<DiagnosticBuilder<'tcx>> {
81+
match self.error.kind {
82+
::mir::interpret::EvalErrorKind::TypeckError |
83+
::mir::interpret::EvalErrorKind::TooGeneric |
84+
::mir::interpret::EvalErrorKind::CheckMatchError |
85+
::mir::interpret::EvalErrorKind::Layout(_) => return None,
86+
::mir::interpret::EvalErrorKind::ReferencedConstant(ref inner) => {
87+
inner.struct_generic(tcx, "referenced constant", lint_root)?.emit();
88+
},
89+
_ => {},
90+
}
91+
trace!("reporting const eval failure at {:?}", self.span);
92+
let mut err = if let Some(lint_root) = lint_root {
93+
let node_id = self.stacktrace
94+
.iter()
95+
.rev()
96+
.filter_map(|frame| frame.lint_root)
97+
.next()
98+
.unwrap_or(lint_root);
99+
tcx.struct_span_lint_node(
100+
::rustc::lint::builtin::CONST_ERR,
101+
node_id,
102+
tcx.span,
103+
message,
104+
)
105+
} else {
106+
struct_error(tcx, message)
107+
};
108+
err.span_label(self.span, self.error.to_string());
109+
for FrameInfo { span, location, .. } in &self.stacktrace {
110+
err.span_label(*span, format!("inside call to `{}`", location));
111+
}
112+
Some(err)
113+
}
114+
}
115+
116+
pub fn struct_error<'a, 'gcx, 'tcx>(
117+
tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
118+
msg: &str,
119+
) -> DiagnosticBuilder<'tcx> {
120+
struct_span_err!(tcx.sess, tcx.span, E0080, "{}", msg)
121+
}
122+
15123
#[derive(Debug, Clone, RustcEncodable, RustcDecodable)]
16124
pub struct EvalError<'tcx> {
17125
pub kind: EvalErrorKind<'tcx, u64>,

src/librustc/mir/interpret/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ macro_rules! err {
88
mod error;
99
mod value;
1010

11-
pub use self::error::{EvalError, EvalResult, EvalErrorKind, AssertMessage};
11+
pub use self::error::{
12+
EvalError, EvalResult, EvalErrorKind, AssertMessage, ConstVal, ConstEvalErr, struct_error,
13+
FrameInfo, ConstEvalResult,
14+
};
1215

1316
pub use self::value::{Scalar, Value, ConstValue};
1417

src/librustc/mir/interpret/value.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ use ty;
55

66
use super::{EvalResult, Pointer, PointerArithmetic, Allocation};
77

8+
#[derive(Copy, Clone, Debug, Hash, RustcEncodable, RustcDecodable, Eq, PartialEq, Ord, PartialOrd)]
9+
pub enum ConstVal<'tcx> {
10+
Value(ConstValue<'tcx>),
11+
}
12+
813
/// Represents a constant value in Rust. ByVal and ScalarPair are optimizations which
914
/// matches Value's optimizations for easy conversions between these two types
1015
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord, RustcEncodable, RustcDecodable, Hash)]
1116
pub enum ConstValue<'tcx> {
17+
/// Never returned from the `const_eval` query, but the HIR contains these frequently in order
18+
/// to allow HIR creation to happen for everything before needing
19+
Unevaluated(DefId, &'tcx Substs<'tcx>),
1220
/// Used only for types with layout::abi::Scalar ABI and ZSTs which use Scalar::undef()
1321
Scalar(Scalar),
1422
/// Used only for types with layout::abi::ScalarPair

src/librustc/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2164,7 +2164,7 @@ impl<'tcx> Debug for Literal<'tcx> {
21642164

21652165
/// Write a `ConstVal` in a way closer to the original source code than the `Debug` output.
21662166
pub fn fmt_const_val<W: Write>(fmt: &mut W, const_val: &ty::Const) -> fmt::Result {
2167-
use middle::const_val::ConstVal;
2167+
use mir::interpret::ConstVal;
21682168
match const_val.val {
21692169
ConstVal::Unevaluated(..) => write!(fmt, "{:?}", const_val),
21702170
ConstVal::Value(val) => {

src/librustc/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ 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;
19+
use mir::interpret::ConstEvalErr;
2020
use mir::interpret::EvalErrorKind;
2121

2222
use super::CodeAmbiguity;

src/librustc/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use hir;
2222
use hir::def_id::DefId;
2323
use infer::outlives::env::OutlivesEnvironment;
2424
use middle::region;
25-
use middle::const_val::ConstEvalErr;
25+
use mir::interpret::ConstEvalErr;
2626
use ty::subst::Substs;
2727
use ty::{self, AdtKind, Slice, Ty, TyCtxt, GenericParamDefKind, ToPredicate};
2828
use ty::error::{ExpectedFound, TypeError};

src/librustc/traits/project.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use super::util;
2828
use hir::def_id::DefId;
2929
use infer::{InferCtxt, InferOk};
3030
use infer::type_variable::TypeVariableOrigin;
31-
use middle::const_val::ConstVal;
31+
use mir::interpret::ConstVal;
3232
use mir::interpret::{GlobalId};
3333
use rustc_data_structures::snapshot_map::{Snapshot, SnapshotMap};
3434
use syntax::symbol::Symbol;

src/librustc/traits/query/normalize.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
1515
use infer::{InferCtxt, InferOk};
1616
use infer::at::At;
17-
use middle::const_val::ConstVal;
18-
use mir::interpret::GlobalId;
17+
use mir::interpret::{GlobalId, ConstVal};
1918
use traits::{Obligation, ObligationCause, PredicateObligation, Reveal};
2019
use traits::project::Normalized;
2120
use ty::{self, Ty, TyCtxt};

src/librustc/ty/flags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use middle::const_val::ConstVal;
11+
use mir::interpret::ConstVal;
1212
use ty::subst::Substs;
1313
use ty::{self, Ty, TypeFlags, TypeFoldable};
1414

src/librustc/ty/fold.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
//! These methods return true to indicate that the visitor has found what it is looking for
4040
//! and does not need to visit anything else.
4141
42-
use middle::const_val::ConstVal;
42+
use mir::interpret::ConstVal;
4343
use hir::def_id::DefId;
4444
use ty::{self, Binder, Ty, TyCtxt, TypeFlags};
4545

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
20652065
})
20662066
} else {
20672067
info!("invalid enum discriminant: {:#?}", val);
2068-
::middle::const_val::struct_error(
2068+
::mir::interpret::struct_error(
20692069
tcx.at(tcx.def_span(expr_did)),
20702070
"constant evaluation of enum discriminant resulted in non-integer",
20712071
).emit();

0 commit comments

Comments
 (0)