use lint;
use middle::cast::{CastKind, CastTy};
-use middle::ty;
-use middle::ty::Ty;
+use middle::ty::{self, Ty, HasTypeFlags};
use syntax::ast;
use syntax::ast::UintTy::{TyU8};
use syntax::codemap::Span;
ty::TySlice(_) | ty::TyStr => Some(UnsizeKind::Length),
ty::TyTrait(ref tty) => Some(UnsizeKind::Vtable(tty.principal_def_id())),
ty::TyStruct(did, substs) => {
- match ty::struct_fields(fcx.tcx(), did, substs).pop() {
+ match fcx.tcx().struct_fields(did, substs).pop() {
None => None,
Some(f) => unsize_kind(fcx, f.mt.ty)
}
CastError::NeedViaInt |
CastError::NeedViaUsize => {
fcx.type_error_message(self.span, |actual| {
- format!("illegal cast; cast through {} first: `{}` as `{}`",
- match e {
- CastError::NeedViaPtr => "a raw pointer",
- CastError::NeedViaInt => "an integer",
- CastError::NeedViaUsize => "a usize",
- _ => unreachable!()
- },
+ format!("casting `{}` as `{}` is invalid",
actual,
fcx.infcx().ty_to_string(self.cast_ty))
- }, self.expr_ty, None)
+ }, self.expr_ty, None);
+ fcx.ccx.tcx.sess.fileline_help(self.span,
+ &format!("cast through {} first", match e {
+ CastError::NeedViaPtr => "a raw pointer",
+ CastError::NeedViaInt => "an integer",
+ CastError::NeedViaUsize => "a usize",
+ _ => unreachable!()
+ }));
}
CastError::CastToBool => {
- span_err!(fcx.tcx().sess, self.span, E0054,
- "cannot cast as `bool`, compare with zero instead");
+ span_err!(fcx.tcx().sess, self.span, E0054, "cannot cast as `bool`");
+ fcx.ccx.tcx.sess.fileline_help(self.span, "compare with zero instead");
}
CastError::CastToChar => {
fcx.type_error_message(self.span, |actual| {
}
CastError::IllegalCast => {
fcx.type_error_message(self.span, |actual| {
- format!("illegal cast: `{}` as `{}`",
+ format!("casting `{}` as `{}` is invalid",
actual,
fcx.infcx().ty_to_string(self.cast_ty))
}, self.expr_ty, None);
}
CastError::DifferingKinds => {
fcx.type_error_message(self.span, |actual| {
- format!("illegal cast: `{}` as `{}`; vtable kinds may not match",
+ format!("casting `{}` as `{}` is invalid",
actual,
fcx.infcx().ty_to_string(self.cast_ty))
}, self.expr_ty, None);
+ fcx.ccx.tcx.sess.fileline_note(self.span, "vtable kinds may not match");
}
}
}
fn trivial_cast_lint<'a>(&self, fcx: &FnCtxt<'a, 'tcx>) {
let t_cast = self.cast_ty;
let t_expr = self.expr_ty;
- if ty::type_is_numeric(t_cast) && ty::type_is_numeric(t_expr) {
+ if t_cast.is_numeric() && t_expr.is_numeric() {
fcx.tcx().sess.add_lint(lint::builtin::TRIVIAL_NUMERIC_CASTS,
self.expr.id,
self.span,
debug!("check_cast({}, {:?} as {:?})", self.expr.id, self.expr_ty,
self.cast_ty);
- if ty::type_is_error(self.expr_ty) || ty::type_is_error(self.cast_ty) {
+ if self.expr_ty.references_error() || self.cast_ty.references_error() {
// No sense in giving duplicate error messages
} else if self.try_coercion_cast(fcx) {
self.trivial_cast_lint(fcx);
fn check_ptr_ptr_cast<'a>(&self,
fcx: &FnCtxt<'a, 'tcx>,
- m_expr: &'tcx ty::mt<'tcx>,
- m_cast: &'tcx ty::mt<'tcx>)
+ m_expr: &'tcx ty::TypeAndMut<'tcx>,
+ m_cast: &'tcx ty::TypeAndMut<'tcx>)
-> Result<CastKind, CastError>
{
debug!("check_ptr_ptr_cast m_expr={:?} m_cast={:?}",
return Ok(CastKind::PtrPtrCast);
}
- // sized -> unsized? report illegal cast (don't complain about vtable kinds)
+ // sized -> unsized? report invalid cast (don't complain about vtable kinds)
if fcx.type_is_known_to_be_sized(m_expr.ty, self.span) {
return Err(CastError::IllegalCast);
}
fn check_fptr_ptr_cast<'a>(&self,
fcx: &FnCtxt<'a, 'tcx>,
- m_cast: &'tcx ty::mt<'tcx>)
+ m_cast: &'tcx ty::TypeAndMut<'tcx>)
-> Result<CastKind, CastError>
{
// fptr-ptr cast. must be to sized ptr
fn check_ptr_addr_cast<'a>(&self,
fcx: &FnCtxt<'a, 'tcx>,
- m_expr: &'tcx ty::mt<'tcx>)
+ m_expr: &'tcx ty::TypeAndMut<'tcx>)
-> Result<CastKind, CastError>
{
// ptr-addr cast. must be from sized ptr
fn check_ref_cast<'a>(&self,
fcx: &FnCtxt<'a, 'tcx>,
- m_expr: &'tcx ty::mt<'tcx>,
- m_cast: &'tcx ty::mt<'tcx>)
+ m_expr: &'tcx ty::TypeAndMut<'tcx>,
+ m_cast: &'tcx ty::TypeAndMut<'tcx>)
-> Result<CastKind, CastError>
{
// array-ptr-cast.
fn check_addr_ptr_cast<'a>(&self,
fcx: &FnCtxt<'a, 'tcx>,
- m_cast: &'tcx ty::mt<'tcx>)
+ m_cast: &'tcx ty::TypeAndMut<'tcx>)
-> Result<CastKind, CastError>
{
// ptr-addr cast. pointer must be thin.