don't panic on invalid primval types, report an error instead

上级 19c44dab
use std::error::Error;
use std::fmt;
use rustc::mir;
use rustc::ty::BareFnTy;
use rustc::ty::{BareFnTy, Ty};
use memory::Pointer;
use rustc_const_math::ConstMathErr;
use syntax::codemap::Span;
......@@ -46,6 +46,7 @@ pub enum EvalError<'tcx> {
ModifiedConstantMemory,
AssumptionNotHeld,
InlineAsm,
TypeNotPrimitive(Ty<'tcx>),
}
pub type EvalResult<'tcx, T> = Result<T, EvalError<'tcx>>;
......@@ -106,6 +107,8 @@ fn description(&self) -> &str {
"`assume` argument was false",
EvalError::InlineAsm =>
"cannot evaluate inline assembly",
EvalError::TypeNotPrimitive(_) =>
"expected primitive type, got nonprimitive",
}
}
......@@ -134,6 +137,8 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
EvalError::AlignmentCheckFailed { required, has } =>
write!(f, "tried to access memory with alignment {}, but alignment {} is required",
has, required),
EvalError::TypeNotPrimitive(ref ty) =>
write!(f, "expected primitive type, got {}", ty),
_ => write!(f, "{}", self.description()),
}
}
......
......@@ -1325,11 +1325,11 @@ fn ty_to_primval_kind(&self, ty: Ty<'tcx>) -> EvalResult<'tcx, PrimValKind> {
PrimValKind::from_uint_size(size)
}
} else {
bug!("primitive read of non-clike enum: {:?}", ty);
return Err(EvalError::TypeNotPrimitive(ty));
}
},
_ => bug!("primitive read of non-primitive type: {:?}", ty),
_ => return Err(EvalError::TypeNotPrimitive(ty)),
};
Ok(kind)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册