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-04-19 09:03:30 -05:00
|
|
|
use traits::ProjectionMode;
|
|
|
|
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;
|
2014-06-12 16:08:44 -05:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::codemap::Span;
|
2016-03-29 00:50:44 -05:00
|
|
|
use hir::intravisit::{self, Visitor, FnKind};
|
|
|
|
use hir;
|
2014-06-12 16:08:44 -05:00
|
|
|
|
2016-02-29 17:36:51 -06:00
|
|
|
pub fn check_crate(tcx: &TyCtxt) {
|
2016-04-19 09:03:30 -05:00
|
|
|
let mut visitor = ItemVisitor {
|
|
|
|
tcx: tcx
|
2014-12-22 19:57:14 -06:00
|
|
|
};
|
2015-12-22 15:35:02 -06:00
|
|
|
tcx.visit_all_items_in_krate(DepNode::IntrinsicCheck, &mut visitor);
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
|
|
|
|
2016-04-19 09:03:30 -05:00
|
|
|
struct ItemVisitor<'a, 'tcx: 'a> {
|
|
|
|
tcx: &'a TyCtxt<'tcx>
|
|
|
|
}
|
2014-12-22 19:57:14 -06:00
|
|
|
|
2016-04-19 09:03:30 -05:00
|
|
|
impl<'a, 'tcx> ItemVisitor<'a, 'tcx> {
|
|
|
|
fn visit_const(&mut self, item_id: ast::NodeId, expr: &hir::Expr) {
|
|
|
|
let param_env = ty::ParameterEnvironment::for_item(self.tcx, item_id);
|
2016-03-10 18:31:38 -06:00
|
|
|
let infcx = InferCtxt::new(self.tcx, &self.tcx.tables,
|
2016-04-19 09:03:30 -05:00
|
|
|
Some(param_env),
|
|
|
|
ProjectionMode::Any);
|
|
|
|
let mut visitor = ExprVisitor {
|
|
|
|
infcx: &infcx
|
|
|
|
};
|
|
|
|
visitor.visit_expr(expr);
|
|
|
|
}
|
|
|
|
}
|
2014-12-22 19:57:14 -06:00
|
|
|
|
2016-04-19 09:03:30 -05:00
|
|
|
struct ExprVisitor<'a, 'tcx: 'a> {
|
|
|
|
infcx: &'a InferCtxt<'a, 'tcx>
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
|
|
|
|
2016-04-19 09:03:30 -05:00
|
|
|
impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
|
2014-06-12 16:08:44 -05:00
|
|
|
fn def_id_is_transmute(&self, def_id: DefId) -> bool {
|
2016-04-19 09:03:30 -05:00
|
|
|
let intrinsic = match self.infcx.tcx.lookup_item_type(def_id).ty.sty {
|
2016-02-16 10:36:41 -06:00
|
|
|
ty::TyFnDef(_, _, ref bfty) => bfty.abi == RustIntrinsic,
|
2014-08-25 13:45:19 -05:00
|
|
|
_ => return false
|
|
|
|
};
|
2016-04-19 09:03:30 -05:00
|
|
|
intrinsic && self.infcx.tcx.item_name(def_id).as_str() == "transmute"
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>, id: ast::NodeId) {
|
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-04-19 09:03:30 -05:00
|
|
|
match (&from.sty, sk_to) {
|
|
|
|
(&ty::TyFnDef(..), SizeSkeleton::Known(size_to))
|
|
|
|
if size_to == Pointer.size(&self.infcx.tcx.data_layout) => {
|
|
|
|
// FIXME #19925 Remove this warning after a release cycle.
|
|
|
|
let msg = format!("`{}` is now zero-sized and has to be cast \
|
|
|
|
to a pointer before transmuting to `{}`",
|
|
|
|
from, to);
|
|
|
|
self.infcx.tcx.sess.add_lint(
|
|
|
|
::lint::builtin::TRANSMUTE_FROM_FN_ITEM_TYPES, id, span, msg);
|
|
|
|
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.
|
|
|
|
let skeleton_string = |ty: Ty<'tcx>, sk| {
|
|
|
|
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-04-19 09:03:30 -05:00
|
|
|
span_err!(self.infcx.tcx.sess, span, E0512,
|
|
|
|
"transmute called with differently sized types: \
|
|
|
|
{} ({}) to {} ({})",
|
|
|
|
from, skeleton_string(from, sk_from),
|
|
|
|
to, skeleton_string(to, sk_to));
|
|
|
|
}
|
|
|
|
}
|
2014-12-22 19:57:14 -06:00
|
|
|
|
2016-04-19 09:03:30 -05:00
|
|
|
impl<'a, 'tcx, 'v> Visitor<'v> for ItemVisitor<'a, 'tcx> {
|
|
|
|
// const, static and N in [T; N].
|
|
|
|
fn visit_expr(&mut self, expr: &hir::Expr) {
|
2016-03-10 18:31:38 -06:00
|
|
|
let infcx = InferCtxt::new(self.tcx, &self.tcx.tables,
|
2016-04-19 09:03:30 -05:00
|
|
|
None, ProjectionMode::Any);
|
|
|
|
let mut visitor = ExprVisitor {
|
|
|
|
infcx: &infcx
|
|
|
|
};
|
|
|
|
visitor.visit_expr(expr);
|
|
|
|
}
|
2014-12-22 19:57:14 -06:00
|
|
|
|
2016-04-19 09:03:30 -05:00
|
|
|
fn visit_trait_item(&mut self, item: &hir::TraitItem) {
|
|
|
|
if let hir::ConstTraitItem(_, Some(ref expr)) = item.node {
|
|
|
|
self.visit_const(item.id, expr);
|
|
|
|
} else {
|
|
|
|
intravisit::walk_trait_item(self, item);
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
2014-12-22 19:57:14 -06:00
|
|
|
}
|
2014-06-12 16:08:44 -05:00
|
|
|
|
2016-04-19 09:03:30 -05:00
|
|
|
fn visit_impl_item(&mut self, item: &hir::ImplItem) {
|
|
|
|
if let hir::ImplItemKind::Const(_, ref expr) = item.node {
|
|
|
|
self.visit_const(item.id, expr);
|
|
|
|
} else {
|
|
|
|
intravisit::walk_impl_item(self, item);
|
|
|
|
}
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v hir::FnDecl,
|
|
|
|
b: &'v hir::Block, s: Span, id: ast::NodeId) {
|
2014-12-22 19:57:14 -06:00
|
|
|
match fk {
|
2015-08-26 05:00:14 -05:00
|
|
|
FnKind::ItemFn(..) | FnKind::Method(..) => {
|
2014-12-22 19:57:14 -06:00
|
|
|
let param_env = ty::ParameterEnvironment::for_item(self.tcx, id);
|
2016-03-10 18:31:38 -06:00
|
|
|
let infcx = InferCtxt::new(self.tcx, &self.tcx.tables,
|
2016-04-19 09:03:30 -05:00
|
|
|
Some(param_env),
|
|
|
|
ProjectionMode::Any);
|
|
|
|
let mut visitor = ExprVisitor {
|
|
|
|
infcx: &infcx
|
|
|
|
};
|
|
|
|
visitor.visit_fn(fk, fd, b, s, id);
|
2014-12-22 19:57:14 -06:00
|
|
|
}
|
2016-01-25 07:11:51 -06:00
|
|
|
FnKind::Closure(..) => {
|
2016-04-19 09:03:30 -05:00
|
|
|
span_bug!(s, "intrinsicck: closure outside of function")
|
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-04-19 09:03:30 -05:00
|
|
|
impl<'a, 'tcx, 'v> Visitor<'v> for ExprVisitor<'a, 'tcx> {
|
2015-07-31 02:04:06 -05:00
|
|
|
fn visit_expr(&mut self, expr: &hir::Expr) {
|
|
|
|
if let hir::ExprPath(..) = expr.node {
|
2016-04-19 09:03:30 -05:00
|
|
|
match self.infcx.tcx.resolve_expr(expr) {
|
2016-01-20 13:31:10 -06:00
|
|
|
Def::Fn(did) if self.def_id_is_transmute(did) => {
|
2016-04-19 09:03:30 -05:00
|
|
|
let typ = self.infcx.tcx.node_id_to_type(expr.id);
|
2014-11-29 15:41:21 -06:00
|
|
|
match typ.sty {
|
2016-02-16 10:36:41 -06:00
|
|
|
ty::TyFnDef(_, _, ref bare_fn_ty) if bare_fn_ty.abi == RustIntrinsic => {
|
2014-12-12 10:28:35 -06:00
|
|
|
if let ty::FnConverging(to) = bare_fn_ty.sig.0.output {
|
|
|
|
let from = bare_fn_ty.sig.0.inputs[0];
|
2014-11-29 15:41:21 -06:00
|
|
|
self.check_transmute(expr.span, from, to, expr.id);
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
|
|
|
}
|
2014-11-29 15:41:21 -06:00
|
|
|
_ => {
|
2016-03-25 12:31:27 -05:00
|
|
|
span_bug!(expr.span, "transmute wasn't a bare fn?!");
|
2014-11-29 15:41:21 -06:00
|
|
|
}
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
|
|
|
}
|
2014-11-29 15:41:21 -06: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
|
|
|
}
|
|
|
|
}
|