]> git.proxmox.com Git - rustc.git/blame - src/librustc/middle/intrinsicck.rs
New upstream version 1.29.0+dfsg1
[rustc.git] / src / librustc / middle / intrinsicck.rs
CommitLineData
1a4d82fc
JJ
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.
4//
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.
10
54a0048b
SL
11use hir::def::Def;
12use hir::def_id::DefId;
54a0048b
SL
13use ty::{self, Ty, TyCtxt};
14use ty::layout::{LayoutError, Pointer, SizeSkeleton};
1a4d82fc 15
83c7162d 16use rustc_target::spec::abi::Abi::RustIntrinsic;
3157f602 17use syntax_pos::Span;
32a655c1 18use hir::intravisit::{self, Visitor, NestedVisitorMap};
54a0048b 19use hir;
1a4d82fc 20
a7813a04 21pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
54a0048b 22 let mut visitor = ItemVisitor {
041b39d2 23 tcx,
1a4d82fc 24 };
cc61c64b 25 tcx.hir.krate().visit_all_item_likes(&mut visitor.as_deep_visitor());
1a4d82fc
JJ
26}
27
54a0048b 28struct ItemVisitor<'a, 'tcx: 'a> {
a7813a04 29 tcx: TyCtxt<'a, 'tcx, 'tcx>
54a0048b 30}
1a4d82fc 31
7cac9316
XL
32struct ExprVisitor<'a, 'tcx: 'a> {
33 tcx: TyCtxt<'a, 'tcx, 'tcx>,
34 tables: &'tcx ty::TypeckTables<'tcx>,
35 param_env: ty::ParamEnv<'tcx>,
1a4d82fc
JJ
36}
37
8bb4bdeb
XL
38/// If the type is `Option<T>`, it will return `T`, otherwise
39/// the type itself. Works on most `Option`-like types.
40fn unpack_option_like<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
41 ty: Ty<'tcx>)
42 -> Ty<'tcx> {
43 let (def, substs) = match ty.sty {
44 ty::TyAdt(def, substs) => (def, substs),
45 _ => return ty
46 };
47
cc61c64b 48 if def.variants.len() == 2 && !def.repr.c() && def.repr.int.is_none() {
8bb4bdeb
XL
49 let data_idx;
50
51 if def.variants[0].fields.is_empty() {
52 data_idx = 1;
53 } else if def.variants[1].fields.is_empty() {
54 data_idx = 0;
55 } else {
56 return ty;
57 }
58
59 if def.variants[data_idx].fields.len() == 1 {
60 return def.variants[data_idx].fields[0].ty(tcx, substs);
61 }
62 }
63
64 ty
65}
66
7cac9316 67impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
1a4d82fc 68 fn def_id_is_transmute(&self, def_id: DefId) -> bool {
041b39d2
XL
69 self.tcx.fn_sig(def_id).abi() == RustIntrinsic &&
70 self.tcx.item_name(def_id) == "transmute"
1a4d82fc
JJ
71 }
72
7cac9316
XL
73 fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>) {
74 let sk_from = SizeSkeleton::compute(from, self.tcx, self.param_env);
75 let sk_to = SizeSkeleton::compute(to, self.tcx, self.param_env);
1a4d82fc 76
54a0048b
SL
77 // Check for same size using the skeletons.
78 if let (Ok(sk_from), Ok(sk_to)) = (sk_from, sk_to) {
79 if sk_from.same_size(sk_to) {
80 return;
1a4d82fc 81 }
1a4d82fc 82
8bb4bdeb
XL
83 // Special-case transmutting from `typeof(function)` and
84 // `Option<typeof(function)>` to present a clearer error.
7cac9316 85 let from = unpack_option_like(self.tcx.global_tcx(), from);
041b39d2
XL
86 if let (&ty::TyFnDef(..), SizeSkeleton::Known(size_to)) = (&from.sty, sk_to) {
87 if size_to == Pointer.size(self.tcx) {
7cac9316 88 struct_span_err!(self.tcx.sess, span, E0591,
041b39d2
XL
89 "can't transmute zero-sized type")
90 .note(&format!("source type: {}", from))
91 .note(&format!("target type: {}", to))
92 .help("cast with `as` to a pointer instead")
8bb4bdeb 93 .emit();
54a0048b
SL
94 return;
95 }
54a0048b 96 }
1a4d82fc
JJ
97 }
98
54a0048b 99 // Try to display a sensible error with as much information as possible.
7cac9316 100 let skeleton_string = |ty: Ty<'tcx>, sk| {
54a0048b
SL
101 match sk {
102 Ok(SizeSkeleton::Known(size)) => {
103 format!("{} bits", size.bits())
104 }
105 Ok(SizeSkeleton::Pointer { tail, .. }) => {
106 format!("pointer to {}", tail)
107 }
108 Err(LayoutError::Unknown(bad)) => {
109 if bad == ty {
8faf50e0 110 "this type's size can vary".to_string()
54a0048b
SL
111 } else {
112 format!("size can vary because of {}", bad)
113 }
114 }
115 Err(err) => err.to_string()
1a4d82fc 116 }
54a0048b 117 };
1a4d82fc 118
7cac9316 119 struct_span_err!(self.tcx.sess, span, E0512,
041b39d2
XL
120 "transmute called with types of different sizes")
121 .note(&format!("source type: {} ({})", from, skeleton_string(from, sk_from)))
122 .note(&format!("target type: {} ({})", to, skeleton_string(to, sk_to)))
9e0c209e 123 .emit();
54a0048b
SL
124 }
125}
1a4d82fc 126
476ff2be
SL
127impl<'a, 'tcx> Visitor<'tcx> for ItemVisitor<'a, 'tcx> {
128 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
32a655c1 129 NestedVisitorMap::None
1a4d82fc
JJ
130 }
131
32a655c1 132 fn visit_nested_body(&mut self, body_id: hir::BodyId) {
7cac9316 133 let owner_def_id = self.tcx.hir.body_owner_def_id(body_id);
32a655c1 134 let body = self.tcx.hir.body(body_id);
7cac9316
XL
135 let param_env = self.tcx.param_env(owner_def_id);
136 let tables = self.tcx.typeck_tables_of(owner_def_id);
137 ExprVisitor { tcx: self.tcx, param_env, tables }.visit_body(body);
32a655c1 138 self.visit_body(body);
1a4d82fc 139 }
54a0048b 140}
1a4d82fc 141
7cac9316
XL
142impl<'a, 'tcx> Visitor<'tcx> for ExprVisitor<'a, 'tcx> {
143 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
32a655c1 144 NestedVisitorMap::None
476ff2be
SL
145 }
146
7cac9316 147 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
8faf50e0 148 let def = if let hir::ExprKind::Path(ref qpath) = expr.node {
3b2f2976 149 self.tables.qpath_def(qpath, expr.hir_id)
476ff2be
SL
150 } else {
151 Def::Err
152 };
041b39d2
XL
153 if let Def::Fn(did) = def {
154 if self.def_id_is_transmute(did) {
3b2f2976 155 let typ = self.tables.node_id_to_type(expr.hir_id);
041b39d2
XL
156 let sig = typ.fn_sig(self.tcx);
157 let from = sig.inputs().skip_binder()[0];
158 let to = *sig.output().skip_binder();
159 self.check_transmute(expr.span, from, to);
1a4d82fc
JJ
160 }
161 }
162
92a42be0 163 intravisit::walk_expr(self, expr);
1a4d82fc
JJ
164 }
165}