|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 |
| -use syntax::symbol::InternedString; |
12 |
| -use syntax::ast; |
13 |
| -use std::rc::Rc; |
| 11 | +use self::ConstVal::*; |
| 12 | +pub use rustc_const_math::ConstInt; |
| 13 | + |
| 14 | +use hir; |
| 15 | +use hir::def::Def; |
14 | 16 | use hir::def_id::DefId;
|
| 17 | +use ty::{self, TyCtxt}; |
15 | 18 | use ty::subst::Substs;
|
| 19 | +use util::common::ErrorReported; |
16 | 20 | use rustc_const_math::*;
|
17 | 21 |
|
18 |
| -use self::ConstVal::*; |
19 |
| -pub use rustc_const_math::ConstInt; |
| 22 | +use graphviz::IntoCow; |
| 23 | +use errors::DiagnosticBuilder; |
| 24 | +use syntax::symbol::InternedString; |
| 25 | +use syntax::ast; |
| 26 | +use syntax_pos::Span; |
20 | 27 |
|
| 28 | +use std::borrow::Cow; |
21 | 29 | use std::collections::BTreeMap;
|
| 30 | +use std::rc::Rc; |
| 31 | + |
| 32 | +pub type EvalResult<'tcx> = Result<ConstVal<'tcx>, ConstEvalErr<'tcx>>; |
22 | 33 |
|
23 | 34 | #[derive(Clone, Debug, Hash, RustcEncodable, RustcDecodable, Eq, PartialEq)]
|
24 | 35 | pub enum ConstVal<'tcx> {
|
@@ -61,3 +72,181 @@ impl<'tcx> ConstVal<'tcx> {
|
61 | 72 | }
|
62 | 73 | }
|
63 | 74 | }
|
| 75 | + |
| 76 | +#[derive(Clone, Debug)] |
| 77 | +pub struct ConstEvalErr<'tcx> { |
| 78 | + pub span: Span, |
| 79 | + pub kind: ErrKind<'tcx>, |
| 80 | +} |
| 81 | + |
| 82 | +#[derive(Clone, Debug)] |
| 83 | +pub enum ErrKind<'tcx> { |
| 84 | + CannotCast, |
| 85 | + MissingStructField, |
| 86 | + NegateOn(ConstVal<'tcx>), |
| 87 | + NotOn(ConstVal<'tcx>), |
| 88 | + CallOn(ConstVal<'tcx>), |
| 89 | + |
| 90 | + NonConstPath, |
| 91 | + UnimplementedConstVal(&'static str), |
| 92 | + ExpectedConstTuple, |
| 93 | + ExpectedConstStruct, |
| 94 | + IndexedNonVec, |
| 95 | + IndexNotUsize, |
| 96 | + IndexOutOfBounds { len: u64, index: u64 }, |
| 97 | + |
| 98 | + MiscBinaryOp, |
| 99 | + MiscCatchAll, |
| 100 | + |
| 101 | + IndexOpFeatureGated, |
| 102 | + Math(ConstMathErr), |
| 103 | + |
| 104 | + ErroneousReferencedConstant(Box<ConstEvalErr<'tcx>>), |
| 105 | + |
| 106 | + TypeckError |
| 107 | +} |
| 108 | + |
| 109 | +impl<'tcx> From<ConstMathErr> for ErrKind<'tcx> { |
| 110 | + fn from(err: ConstMathErr) -> ErrKind<'tcx> { |
| 111 | + match err { |
| 112 | + ConstMathErr::UnsignedNegation => ErrKind::TypeckError, |
| 113 | + _ => ErrKind::Math(err) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[derive(Clone, Debug)] |
| 119 | +pub enum ConstEvalErrDescription<'a> { |
| 120 | + Simple(Cow<'a, str>), |
| 121 | +} |
| 122 | + |
| 123 | +impl<'a> ConstEvalErrDescription<'a> { |
| 124 | + /// Return a one-line description of the error, for lints and such |
| 125 | + pub fn into_oneline(self) -> Cow<'a, str> { |
| 126 | + match self { |
| 127 | + ConstEvalErrDescription::Simple(simple) => simple, |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> { |
| 133 | + pub fn description(&self) -> ConstEvalErrDescription { |
| 134 | + use self::ErrKind::*; |
| 135 | + use self::ConstEvalErrDescription::*; |
| 136 | + |
| 137 | + macro_rules! simple { |
| 138 | + ($msg:expr) => ({ Simple($msg.into_cow()) }); |
| 139 | + ($fmt:expr, $($arg:tt)+) => ({ |
| 140 | + Simple(format!($fmt, $($arg)+).into_cow()) |
| 141 | + }) |
| 142 | + } |
| 143 | + |
| 144 | + match self.kind { |
| 145 | + CannotCast => simple!("can't cast this type"), |
| 146 | + NegateOn(ref const_val) => simple!("negate on {}", const_val.description()), |
| 147 | + NotOn(ref const_val) => simple!("not on {}", const_val.description()), |
| 148 | + CallOn(ref const_val) => simple!("call on {}", const_val.description()), |
| 149 | + |
| 150 | + MissingStructField => simple!("nonexistent struct field"), |
| 151 | + NonConstPath => simple!("non-constant path in constant expression"), |
| 152 | + UnimplementedConstVal(what) => |
| 153 | + simple!("unimplemented constant expression: {}", what), |
| 154 | + ExpectedConstTuple => simple!("expected constant tuple"), |
| 155 | + ExpectedConstStruct => simple!("expected constant struct"), |
| 156 | + IndexedNonVec => simple!("indexing is only supported for arrays"), |
| 157 | + IndexNotUsize => simple!("indices must be of type `usize`"), |
| 158 | + IndexOutOfBounds { len, index } => { |
| 159 | + simple!("index out of bounds: the len is {} but the index is {}", |
| 160 | + len, index) |
| 161 | + } |
| 162 | + |
| 163 | + MiscBinaryOp => simple!("bad operands for binary"), |
| 164 | + MiscCatchAll => simple!("unsupported constant expr"), |
| 165 | + IndexOpFeatureGated => simple!("the index operation on const values is unstable"), |
| 166 | + Math(ref err) => Simple(err.description().into_cow()), |
| 167 | + |
| 168 | + ErroneousReferencedConstant(_) => simple!("could not evaluate referenced constant"), |
| 169 | + |
| 170 | + TypeckError => simple!("type-checking failed"), |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + pub fn struct_error(&self, |
| 175 | + tcx: TyCtxt<'a, 'gcx, 'tcx>, |
| 176 | + primary_span: Span, |
| 177 | + primary_kind: &str) |
| 178 | + -> DiagnosticBuilder<'gcx> |
| 179 | + { |
| 180 | + let mut err = self; |
| 181 | + while let &ConstEvalErr { |
| 182 | + kind: ErrKind::ErroneousReferencedConstant(box ref i_err), .. |
| 183 | + } = err { |
| 184 | + err = i_err; |
| 185 | + } |
| 186 | + |
| 187 | + let mut diag = struct_span_err!(tcx.sess, err.span, E0080, "constant evaluation error"); |
| 188 | + err.note(tcx, primary_span, primary_kind, &mut diag); |
| 189 | + diag |
| 190 | + } |
| 191 | + |
| 192 | + pub fn note(&self, |
| 193 | + _tcx: TyCtxt<'a, 'gcx, 'tcx>, |
| 194 | + primary_span: Span, |
| 195 | + primary_kind: &str, |
| 196 | + diag: &mut DiagnosticBuilder) |
| 197 | + { |
| 198 | + match self.description() { |
| 199 | + ConstEvalErrDescription::Simple(message) => { |
| 200 | + diag.span_label(self.span, &message); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + if !primary_span.contains(self.span) { |
| 205 | + diag.span_note(primary_span, |
| 206 | + &format!("for {} here", primary_kind)); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + pub fn report(&self, |
| 211 | + tcx: TyCtxt<'a, 'gcx, 'tcx>, |
| 212 | + primary_span: Span, |
| 213 | + primary_kind: &str) |
| 214 | + { |
| 215 | + if let ErrKind::TypeckError = self.kind { |
| 216 | + return; |
| 217 | + } |
| 218 | + self.struct_error(tcx, primary_span, primary_kind).emit(); |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +/// Returns the value of the length-valued expression |
| 223 | +pub fn eval_length(tcx: TyCtxt, |
| 224 | + count: hir::BodyId, |
| 225 | + reason: &str) |
| 226 | + -> Result<usize, ErrorReported> |
| 227 | +{ |
| 228 | + let count_expr = &tcx.hir.body(count).value; |
| 229 | + let count_def_id = tcx.hir.body_owner_def_id(count); |
| 230 | + match ty::queries::monomorphic_const_eval::get(tcx, count_expr.span, count_def_id) { |
| 231 | + Ok(Integral(Usize(count))) => { |
| 232 | + let val = count.as_u64(tcx.sess.target.uint_type); |
| 233 | + assert_eq!(val as usize as u64, val); |
| 234 | + Ok(val as usize) |
| 235 | + }, |
| 236 | + Ok(_) | |
| 237 | + Err(ConstEvalErr { kind: ErrKind::TypeckError, .. }) => Err(ErrorReported), |
| 238 | + Err(err) => { |
| 239 | + let mut diag = err.struct_error(tcx, count_expr.span, reason); |
| 240 | + |
| 241 | + if let hir::ExprPath(hir::QPath::Resolved(None, ref path)) = count_expr.node { |
| 242 | + if let Def::Local(..) = path.def { |
| 243 | + diag.note(&format!("`{}` is a variable", |
| 244 | + tcx.hir.node_to_pretty_string(count_expr.id))); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + diag.emit(); |
| 249 | + Err(ErrorReported) |
| 250 | + } |
| 251 | + } |
| 252 | +} |
0 commit comments