2014-06-12 16:08:44 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-12-22 15:35:02 -06:00
|
|
|
use dep_graph::DepNode;
|
2016-03-29 04:54:26 -05:00
|
|
|
use hir::def::Def;
|
|
|
|
use hir::def_id::DefId;
|
2016-03-10 18:31:38 -06:00
|
|
|
use infer::InferCtxt;
|
2016-06-30 13:22:47 -05:00
|
|
|
use traits::Reveal;
|
2016-04-19 09:03:30 -05:00
|
|
|
use ty::{self, Ty, TyCtxt};
|
|
|
|
use ty::layout::{LayoutError, Pointer, SizeSkeleton};
|
2015-06-18 00:51:23 -05:00
|
|
|
|
2016-02-05 06:13:36 -06:00
|
|
|
use syntax::abi::Abi::RustIntrinsic;
|
2016-06-21 17:08:13 -05:00
|
|
|
use syntax_pos::Span;
|
2017-01-06 13:54:24 -06:00
|
|
|
use hir::intravisit::{self, Visitor, NestedVisitorMap};
|
2016-03-29 00:50:44 -05:00
|
|
|
use hir;
|
2014-06-12 16:08:44 -05:00
|
|
|
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
|
2016-04-19 09:03:30 -05:00
|
|
|
let mut visitor = ItemVisitor {
|
|
|
|
tcx: tcx
|
2014-12-22 19:57:14 -06:00
|
|
|
};
|
2016-11-02 17:22:59 -05:00
|
|
|
tcx.visit_all_item_likes_in_krate(DepNode::IntrinsicCheck, &mut visitor.as_deep_visitor());
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
|
|
|
|
2016-04-19 09:03:30 -05:00
|
|
|
struct ItemVisitor<'a, 'tcx: 'a> {
|
2016-05-02 21:23:22 -05:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>
|
2016-04-19 09:03:30 -05:00
|
|
|
}
|
2014-12-22 19:57:14 -06:00
|
|
|
|
2016-05-02 21:23:22 -05:00
|
|
|
struct ExprVisitor<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
|
|
|
|
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
|
|
|
|
2016-06-10 05:00:21 -05:00
|
|
|
/// If the type is `Option<T>`, it will return `T`, otherwise
|
|
|
|
/// the type itself. Works on most `Option`-like types.
|
|
|
|
fn unpack_option_like<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
ty: Ty<'tcx>)
|
|
|
|
-> Ty<'tcx> {
|
|
|
|
let (def, substs) = match ty.sty {
|
|
|
|
ty::TyAdt(def, substs) => (def, substs),
|
|
|
|
_ => return ty
|
|
|
|
};
|
|
|
|
|
|
|
|
if def.variants.len() == 2 && !def.repr.c && def.repr.int.is_none() {
|
|
|
|
let data_idx;
|
|
|
|
|
|
|
|
if def.variants[0].fields.is_empty() {
|
|
|
|
data_idx = 1;
|
|
|
|
} else if def.variants[1].fields.is_empty() {
|
|
|
|
data_idx = 0;
|
|
|
|
} else {
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
if def.variants[data_idx].fields.len() == 1 {
|
|
|
|
return def.variants[data_idx].fields[0].ty(tcx, substs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
|
2016-05-10 20:14:41 -05:00
|
|
|
impl<'a, 'gcx, 'tcx> ExprVisitor<'a, 'gcx, 'tcx> {
|
2014-06-12 16:08:44 -05:00
|
|
|
fn def_id_is_transmute(&self, def_id: DefId) -> bool {
|
2016-11-10 08:49:53 -06:00
|
|
|
let intrinsic = match self.infcx.tcx.item_type(def_id).sty {
|
2017-02-13 02:51:06 -06:00
|
|
|
ty::TyFnDef(.., bfty) => bfty.abi() == RustIntrinsic,
|
2014-08-25 13:45:19 -05:00
|
|
|
_ => return false
|
|
|
|
};
|
2016-11-17 08:04:20 -06:00
|
|
|
intrinsic && self.infcx.tcx.item_name(def_id) == "transmute"
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
|
|
|
|
2016-06-10 05:00:21 -05:00
|
|
|
fn check_transmute(&self, span: Span, from: Ty<'gcx>, to: Ty<'gcx>) {
|
2016-04-19 09:03:30 -05:00
|
|
|
let sk_from = SizeSkeleton::compute(from, self.infcx);
|
|
|
|
let sk_to = SizeSkeleton::compute(to, self.infcx);
|
2014-12-22 19:57:14 -06:00
|
|
|
|
2016-04-19 09:03:30 -05:00
|
|
|
// Check for same size using the skeletons.
|
|
|
|
if let (Ok(sk_from), Ok(sk_to)) = (sk_from, sk_to) {
|
|
|
|
if sk_from.same_size(sk_to) {
|
|
|
|
return;
|
2014-12-22 19:57:14 -06:00
|
|
|
}
|
|
|
|
|
2016-06-10 05:00:21 -05:00
|
|
|
// Special-case transmutting from `typeof(function)` and
|
|
|
|
// `Option<typeof(function)>` to present a clearer error.
|
|
|
|
let from = unpack_option_like(self.infcx.tcx.global_tcx(), from);
|
2016-04-19 09:03:30 -05:00
|
|
|
match (&from.sty, sk_to) {
|
|
|
|
(&ty::TyFnDef(..), SizeSkeleton::Known(size_to))
|
2017-03-09 22:25:51 -06:00
|
|
|
if size_to == Pointer.size(self.infcx) => {
|
2016-06-10 05:00:21 -05:00
|
|
|
struct_span_err!(self.infcx.tcx.sess, span, E0591,
|
|
|
|
"`{}` is zero-sized and can't be transmuted to `{}`",
|
|
|
|
from, to)
|
|
|
|
.span_note(span, &format!("cast with `as` to a pointer instead"))
|
|
|
|
.emit();
|
2016-04-19 09:03:30 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
2014-12-22 19:57:14 -06:00
|
|
|
|
2016-04-19 09:03:30 -05:00
|
|
|
// Try to display a sensible error with as much information as possible.
|
2016-05-10 20:14:41 -05:00
|
|
|
let skeleton_string = |ty: Ty<'gcx>, sk| {
|
2016-04-19 09:03:30 -05:00
|
|
|
match sk {
|
|
|
|
Ok(SizeSkeleton::Known(size)) => {
|
|
|
|
format!("{} bits", size.bits())
|
|
|
|
}
|
|
|
|
Ok(SizeSkeleton::Pointer { tail, .. }) => {
|
|
|
|
format!("pointer to {}", tail)
|
|
|
|
}
|
|
|
|
Err(LayoutError::Unknown(bad)) => {
|
|
|
|
if bad == ty {
|
|
|
|
format!("size can vary")
|
|
|
|
} else {
|
|
|
|
format!("size can vary because of {}", bad)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => err.to_string()
|
2014-12-22 19:57:14 -06:00
|
|
|
}
|
2016-04-19 09:03:30 -05:00
|
|
|
};
|
2014-12-22 19:57:14 -06:00
|
|
|
|
2016-09-26 14:49:22 -05:00
|
|
|
struct_span_err!(self.infcx.tcx.sess, span, E0512,
|
2016-04-19 09:03:30 -05:00
|
|
|
"transmute called with differently sized types: \
|
|
|
|
{} ({}) to {} ({})",
|
|
|
|
from, skeleton_string(from, sk_from),
|
2016-09-26 14:49:22 -05:00
|
|
|
to, skeleton_string(to, sk_to))
|
|
|
|
.span_label(span,
|
|
|
|
&format!("transmuting between {} and {}",
|
|
|
|
skeleton_string(from, sk_from),
|
|
|
|
skeleton_string(to, sk_to)))
|
|
|
|
.emit();
|
2016-04-19 09:03:30 -05:00
|
|
|
}
|
|
|
|
}
|
2014-12-22 19:57:14 -06:00
|
|
|
|
2016-10-28 15:58:32 -05:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for ItemVisitor<'a, 'tcx> {
|
2016-11-28 13:00:26 -06:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2017-01-06 13:54:24 -06:00
|
|
|
NestedVisitorMap::None
|
2016-10-28 15:58:32 -05:00
|
|
|
}
|
|
|
|
|
2017-01-06 13:54:24 -06:00
|
|
|
fn visit_nested_body(&mut self, body_id: hir::BodyId) {
|
2017-01-25 18:41:06 -06:00
|
|
|
let body = self.tcx.hir.body(body_id);
|
2017-01-06 13:54:24 -06:00
|
|
|
self.tcx.infer_ctxt(body_id, Reveal::All).enter(|infcx| {
|
2016-03-24 22:22:44 -05:00
|
|
|
let mut visitor = ExprVisitor {
|
|
|
|
infcx: &infcx
|
|
|
|
};
|
2016-12-21 04:32:59 -06:00
|
|
|
visitor.visit_body(body);
|
2016-03-24 22:22:44 -05:00
|
|
|
});
|
2017-01-06 13:54:24 -06:00
|
|
|
self.visit_body(body);
|
2014-12-22 19:57:14 -06:00
|
|
|
}
|
2016-04-19 09:03:30 -05:00
|
|
|
}
|
2014-12-22 19:57:14 -06:00
|
|
|
|
2016-10-28 15:58:32 -05:00
|
|
|
impl<'a, 'gcx, 'tcx> Visitor<'gcx> for ExprVisitor<'a, 'gcx, 'tcx> {
|
2016-11-28 13:00:26 -06:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
|
2017-01-06 13:54:24 -06:00
|
|
|
NestedVisitorMap::None
|
2016-10-28 15:58:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, expr: &'gcx hir::Expr) {
|
2016-11-25 05:21:19 -06:00
|
|
|
let def = if let hir::ExprPath(ref qpath) = expr.node {
|
2017-01-06 13:54:24 -06:00
|
|
|
self.infcx.tables.borrow().qpath_def(qpath, expr.id)
|
2016-11-25 05:21:19 -06:00
|
|
|
} else {
|
|
|
|
Def::Err
|
2016-10-26 21:17:42 -05:00
|
|
|
};
|
|
|
|
match def {
|
|
|
|
Def::Fn(did) if self.def_id_is_transmute(did) => {
|
2017-01-06 13:54:24 -06:00
|
|
|
let typ = self.infcx.tables.borrow().node_id_to_type(expr.id);
|
|
|
|
let typ = self.infcx.tcx.lift_to_global(&typ).unwrap();
|
2016-10-26 21:17:42 -05:00
|
|
|
match typ.sty {
|
2017-02-13 02:51:06 -06:00
|
|
|
ty::TyFnDef(.., sig) if sig.abi() == RustIntrinsic => {
|
|
|
|
let from = sig.inputs().skip_binder()[0];
|
|
|
|
let to = *sig.output().skip_binder();
|
2016-06-10 05:00:21 -05:00
|
|
|
self.check_transmute(expr.span, from, to);
|
2016-10-26 21:17:42 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
span_bug!(expr.span, "transmute wasn't a bare fn?!");
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-26 21:17:42 -05:00
|
|
|
_ => {}
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
|
|
|
|
2015-11-17 16:51:44 -06:00
|
|
|
intravisit::walk_expr(self, expr);
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
|
|
|
}
|