|
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 | 14 | use hir::def_id::DefId;
|
| 15 | +use ty::TyCtxt; |
15 | 16 | use ty::subst::Substs;
|
16 | 17 | use rustc_const_math::*;
|
17 | 18 |
|
18 |
| -use self::ConstVal::*; |
19 |
| -pub use rustc_const_math::ConstInt; |
| 19 | +use graphviz::IntoCow; |
| 20 | +use errors::DiagnosticBuilder; |
| 21 | +use syntax::symbol::InternedString; |
| 22 | +use syntax::ast; |
| 23 | +use syntax_pos::Span; |
20 | 24 |
|
| 25 | +use std::borrow::Cow; |
21 | 26 | use std::collections::BTreeMap;
|
| 27 | +use std::rc::Rc; |
| 28 | + |
| 29 | +pub type EvalResult<'tcx> = Result<ConstVal<'tcx>, ConstEvalErr<'tcx>>; |
22 | 30 |
|
23 | 31 | #[derive(Clone, Debug, Hash, RustcEncodable, RustcDecodable, Eq, PartialEq)]
|
24 | 32 | pub enum ConstVal<'tcx> {
|
@@ -61,3 +69,149 @@ impl<'tcx> ConstVal<'tcx> {
|
61 | 69 | }
|
62 | 70 | }
|
63 | 71 | }
|
| 72 | + |
| 73 | +#[derive(Clone, Debug)] |
| 74 | +pub struct ConstEvalErr<'tcx> { |
| 75 | + pub span: Span, |
| 76 | + pub kind: ErrKind<'tcx>, |
| 77 | +} |
| 78 | + |
| 79 | +#[derive(Clone, Debug)] |
| 80 | +pub enum ErrKind<'tcx> { |
| 81 | + CannotCast, |
| 82 | + MissingStructField, |
| 83 | + NegateOn(ConstVal<'tcx>), |
| 84 | + NotOn(ConstVal<'tcx>), |
| 85 | + CallOn(ConstVal<'tcx>), |
| 86 | + |
| 87 | + NonConstPath, |
| 88 | + UnimplementedConstVal(&'static str), |
| 89 | + ExpectedConstTuple, |
| 90 | + ExpectedConstStruct, |
| 91 | + IndexedNonVec, |
| 92 | + IndexNotUsize, |
| 93 | + IndexOutOfBounds { len: u64, index: u64 }, |
| 94 | + |
| 95 | + MiscBinaryOp, |
| 96 | + MiscCatchAll, |
| 97 | + |
| 98 | + IndexOpFeatureGated, |
| 99 | + Math(ConstMathErr), |
| 100 | + |
| 101 | + ErroneousReferencedConstant(Box<ConstEvalErr<'tcx>>), |
| 102 | + |
| 103 | + TypeckError |
| 104 | +} |
| 105 | + |
| 106 | +impl<'tcx> From<ConstMathErr> for ErrKind<'tcx> { |
| 107 | + fn from(err: ConstMathErr) -> ErrKind<'tcx> { |
| 108 | + match err { |
| 109 | + ConstMathErr::UnsignedNegation => ErrKind::TypeckError, |
| 110 | + _ => ErrKind::Math(err) |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +#[derive(Clone, Debug)] |
| 116 | +pub enum ConstEvalErrDescription<'a> { |
| 117 | + Simple(Cow<'a, str>), |
| 118 | +} |
| 119 | + |
| 120 | +impl<'a> ConstEvalErrDescription<'a> { |
| 121 | + /// Return a one-line description of the error, for lints and such |
| 122 | + pub fn into_oneline(self) -> Cow<'a, str> { |
| 123 | + match self { |
| 124 | + ConstEvalErrDescription::Simple(simple) => simple, |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> { |
| 130 | + pub fn description(&self) -> ConstEvalErrDescription { |
| 131 | + use self::ErrKind::*; |
| 132 | + use self::ConstEvalErrDescription::*; |
| 133 | + |
| 134 | + macro_rules! simple { |
| 135 | + ($msg:expr) => ({ Simple($msg.into_cow()) }); |
| 136 | + ($fmt:expr, $($arg:tt)+) => ({ |
| 137 | + Simple(format!($fmt, $($arg)+).into_cow()) |
| 138 | + }) |
| 139 | + } |
| 140 | + |
| 141 | + match self.kind { |
| 142 | + CannotCast => simple!("can't cast this type"), |
| 143 | + NegateOn(ref const_val) => simple!("negate on {}", const_val.description()), |
| 144 | + NotOn(ref const_val) => simple!("not on {}", const_val.description()), |
| 145 | + CallOn(ref const_val) => simple!("call on {}", const_val.description()), |
| 146 | + |
| 147 | + MissingStructField => simple!("nonexistent struct field"), |
| 148 | + NonConstPath => simple!("non-constant path in constant expression"), |
| 149 | + UnimplementedConstVal(what) => |
| 150 | + simple!("unimplemented constant expression: {}", what), |
| 151 | + ExpectedConstTuple => simple!("expected constant tuple"), |
| 152 | + ExpectedConstStruct => simple!("expected constant struct"), |
| 153 | + IndexedNonVec => simple!("indexing is only supported for arrays"), |
| 154 | + IndexNotUsize => simple!("indices must be of type `usize`"), |
| 155 | + IndexOutOfBounds { len, index } => { |
| 156 | + simple!("index out of bounds: the len is {} but the index is {}", |
| 157 | + len, index) |
| 158 | + } |
| 159 | + |
| 160 | + MiscBinaryOp => simple!("bad operands for binary"), |
| 161 | + MiscCatchAll => simple!("unsupported constant expr"), |
| 162 | + IndexOpFeatureGated => simple!("the index operation on const values is unstable"), |
| 163 | + Math(ref err) => Simple(err.description().into_cow()), |
| 164 | + |
| 165 | + ErroneousReferencedConstant(_) => simple!("could not evaluate referenced constant"), |
| 166 | + |
| 167 | + TypeckError => simple!("type-checking failed"), |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + pub fn struct_error(&self, |
| 172 | + tcx: TyCtxt<'a, 'gcx, 'tcx>, |
| 173 | + primary_span: Span, |
| 174 | + primary_kind: &str) |
| 175 | + -> DiagnosticBuilder<'gcx> |
| 176 | + { |
| 177 | + let mut err = self; |
| 178 | + while let &ConstEvalErr { |
| 179 | + kind: ErrKind::ErroneousReferencedConstant(box ref i_err), .. |
| 180 | + } = err { |
| 181 | + err = i_err; |
| 182 | + } |
| 183 | + |
| 184 | + let mut diag = struct_span_err!(tcx.sess, err.span, E0080, "constant evaluation error"); |
| 185 | + err.note(tcx, primary_span, primary_kind, &mut diag); |
| 186 | + diag |
| 187 | + } |
| 188 | + |
| 189 | + pub fn note(&self, |
| 190 | + _tcx: TyCtxt<'a, 'gcx, 'tcx>, |
| 191 | + primary_span: Span, |
| 192 | + primary_kind: &str, |
| 193 | + diag: &mut DiagnosticBuilder) |
| 194 | + { |
| 195 | + match self.description() { |
| 196 | + ConstEvalErrDescription::Simple(message) => { |
| 197 | + diag.span_label(self.span, &message); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + if !primary_span.contains(self.span) { |
| 202 | + diag.span_note(primary_span, |
| 203 | + &format!("for {} here", primary_kind)); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + pub fn report(&self, |
| 208 | + tcx: TyCtxt<'a, 'gcx, 'tcx>, |
| 209 | + primary_span: Span, |
| 210 | + primary_kind: &str) |
| 211 | + { |
| 212 | + if let ErrKind::TypeckError = self.kind { |
| 213 | + return; |
| 214 | + } |
| 215 | + self.struct_error(tcx, primary_span, primary_kind).emit(); |
| 216 | + } |
| 217 | +} |
0 commit comments