1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use hir::def_id::DefId;
15 use ty::{self, Ty, TyCtxt};
16 use ty::layout::{LayoutError, Pointer, SizeSkeleton};
18 use syntax::abi::Abi::RustIntrinsic;
20 use hir::intravisit::{self, Visitor, NestedVisitorMap};
23 pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
24 let mut visitor = ItemVisitor {
27 tcx.hir.krate().visit_all_item_likes(&mut visitor.as_deep_visitor());
30 struct ItemVisitor<'a, 'tcx: 'a> {
31 tcx: TyCtxt<'a, 'tcx, 'tcx>
34 struct ExprVisitor<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
35 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>
38 /// If the type is `Option<T>`, it will return `T`, otherwise
39 /// the type itself. Works on most `Option`-like types.
40 fn unpack_option_like<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
43 let (def, substs) = match ty.sty {
44 ty::TyAdt(def, substs) => (def, substs),
48 if def.variants.len() == 2 && !def.repr.c() && def.repr.int.is_none() {
51 if def.variants[0].fields.is_empty() {
53 } else if def.variants[1].fields.is_empty() {
59 if def.variants[data_idx].fields.len() == 1 {
60 return def.variants[data_idx].fields[0].ty(tcx, substs);
67 impl<'a, 'gcx, 'tcx> ExprVisitor<'a, 'gcx, 'tcx> {
68 fn def_id_is_transmute(&self, def_id: DefId) -> bool {
69 let intrinsic = match self.infcx.tcx.type_of(def_id).sty {
70 ty::TyFnDef(.., bfty) => bfty.abi() == RustIntrinsic,
73 intrinsic && self.infcx.tcx.item_name(def_id) == "transmute"
76 fn check_transmute(&self, span: Span, from: Ty<'gcx>, to: Ty<'gcx>) {
77 let sk_from = SizeSkeleton::compute(from, self.infcx);
78 let sk_to = SizeSkeleton::compute(to, self.infcx);
80 // Check for same size using the skeletons.
81 if let (Ok(sk_from), Ok(sk_to)) = (sk_from, sk_to) {
82 if sk_from.same_size(sk_to) {
86 // Special-case transmutting from `typeof(function)` and
87 // `Option<typeof(function)>` to present a clearer error.
88 let from = unpack_option_like(self.infcx.tcx.global_tcx(), from);
89 match (&from.sty, sk_to) {
90 (&ty::TyFnDef(..), SizeSkeleton::Known(size_to))
91 if size_to == Pointer.size(self.infcx) => {
92 struct_span_err!(self.infcx.tcx.sess, span, E0591,
93 "`{}` is zero-sized and can't be transmuted to `{}`",
95 .span_note(span, &format!("cast with `as` to a pointer instead"))
103 // Try to display a sensible error with as much information as possible.
104 let skeleton_string = |ty: Ty<'gcx>, sk| {
106 Ok(SizeSkeleton::Known(size)) => {
107 format!("{} bits", size.bits())
109 Ok(SizeSkeleton::Pointer { tail, .. }) => {
110 format!("pointer to {}", tail)
112 Err(LayoutError::Unknown(bad)) => {
114 format!("size can vary")
116 format!("size can vary because of {}", bad)
119 Err(err) => err.to_string()
123 struct_span_err!(self.infcx.tcx.sess, span, E0512,
124 "transmute called with differently sized types: \
126 from, skeleton_string(from, sk_from),
127 to, skeleton_string(to, sk_to))
129 &format!("transmuting between {} and {}",
130 skeleton_string(from, sk_from),
131 skeleton_string(to, sk_to)))
136 impl<'a, 'tcx> Visitor<'tcx> for ItemVisitor<'a, 'tcx> {
137 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
138 NestedVisitorMap::None
141 fn visit_nested_body(&mut self, body_id: hir::BodyId) {
142 let body = self.tcx.hir.body(body_id);
143 self.tcx.infer_ctxt(body_id, Reveal::All).enter(|infcx| {
144 let mut visitor = ExprVisitor {
147 visitor.visit_body(body);
149 self.visit_body(body);
153 impl<'a, 'gcx, 'tcx> Visitor<'gcx> for ExprVisitor<'a, 'gcx, 'tcx> {
154 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
155 NestedVisitorMap::None
158 fn visit_expr(&mut self, expr: &'gcx hir::Expr) {
159 let def = if let hir::ExprPath(ref qpath) = expr.node {
160 self.infcx.tables.borrow().qpath_def(qpath, expr.id)
165 Def::Fn(did) if self.def_id_is_transmute(did) => {
166 let typ = self.infcx.tables.borrow().node_id_to_type(expr.id);
167 let typ = self.infcx.tcx.lift_to_global(&typ).unwrap();
169 ty::TyFnDef(.., sig) if sig.abi() == RustIntrinsic => {
170 let from = sig.inputs().skip_binder()[0];
171 let to = *sig.output().skip_binder();
172 self.check_transmute(expr.span, from, to);
175 span_bug!(expr.span, "transmute wasn't a bare fn?!");
182 intravisit::walk_expr(self, expr);