2014-06-12 14:08:44 -07: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.
|
|
|
|
|
2016-03-29 12:54:26 +03:00
|
|
|
use hir::def::Def;
|
|
|
|
use hir::def_id::DefId;
|
2016-04-19 17:03:30 +03:00
|
|
|
use ty::{self, Ty, TyCtxt};
|
|
|
|
use ty::layout::{LayoutError, Pointer, SizeSkeleton};
|
2015-06-18 08:51:23 +03:00
|
|
|
|
2018-04-25 19:30:39 +03:00
|
|
|
use rustc_target::spec::abi::Abi::RustIntrinsic;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2017-01-06 21:54:24 +02:00
|
|
|
use hir::intravisit::{self, Visitor, NestedVisitorMap};
|
2016-03-29 08:50:44 +03:00
|
|
|
use hir;
|
2014-06-12 14:08:44 -07:00
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
|
2016-04-19 17:03:30 +03:00
|
|
|
let mut visitor = ItemVisitor {
|
2017-07-03 11:19:51 -07:00
|
|
|
tcx,
|
2014-12-22 20:57:14 -05:00
|
|
|
};
|
2017-04-18 05:57:39 -04:00
|
|
|
tcx.hir.krate().visit_all_item_likes(&mut visitor.as_deep_visitor());
|
2014-06-12 14:08:44 -07:00
|
|
|
}
|
|
|
|
|
2016-04-19 17:03:30 +03:00
|
|
|
struct ItemVisitor<'a, 'tcx: 'a> {
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>
|
2016-04-19 17:03:30 +03:00
|
|
|
}
|
2014-12-22 20:57:14 -05:00
|
|
|
|
2017-05-19 17:27:25 -04:00
|
|
|
struct ExprVisitor<'a, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
tables: &'tcx ty::TypeckTables<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2014-06-12 14:08:44 -07:00
|
|
|
}
|
|
|
|
|
2016-06-10 13:00:21 +03: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 {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Adt(def, substs) => (def, substs),
|
2016-06-10 13:00:21 +03:00
|
|
|
_ => return ty
|
|
|
|
};
|
|
|
|
|
2017-04-16 16:17:13 +03:00
|
|
|
if def.variants.len() == 2 && !def.repr.c() && def.repr.int.is_none() {
|
2016-06-10 13:00:21 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-19 17:27:25 -04:00
|
|
|
impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
|
2014-06-12 14:08:44 -07:00
|
|
|
fn def_id_is_transmute(&self, def_id: DefId) -> bool {
|
2017-05-13 17:11:52 +03:00
|
|
|
self.tcx.fn_sig(def_id).abi() == RustIntrinsic &&
|
|
|
|
self.tcx.item_name(def_id) == "transmute"
|
2014-06-12 14:08:44 -07:00
|
|
|
}
|
|
|
|
|
2017-05-19 17:27:25 -04:00
|
|
|
fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>) {
|
|
|
|
let sk_from = SizeSkeleton::compute(from, self.tcx, self.param_env);
|
|
|
|
let sk_to = SizeSkeleton::compute(to, self.tcx, self.param_env);
|
2014-12-22 20:57:14 -05:00
|
|
|
|
2016-04-19 17:03:30 +03: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 20:57:14 -05:00
|
|
|
}
|
|
|
|
|
2016-06-10 13:00:21 +03:00
|
|
|
// Special-case transmutting from `typeof(function)` and
|
|
|
|
// `Option<typeof(function)>` to present a clearer error.
|
2017-05-19 17:27:25 -04:00
|
|
|
let from = unpack_option_like(self.tcx.global_tcx(), from);
|
2018-08-22 01:35:02 +01:00
|
|
|
if let (&ty::FnDef(..), SizeSkeleton::Known(size_to)) = (&from.sty, sk_to) {
|
2017-06-04 07:02:05 -06:00
|
|
|
if size_to == Pointer.size(self.tcx) {
|
2017-05-19 17:27:25 -04:00
|
|
|
struct_span_err!(self.tcx.sess, span, E0591,
|
2017-05-29 17:22:41 -06:00
|
|
|
"can't transmute zero-sized type")
|
|
|
|
.note(&format!("source type: {}", from))
|
|
|
|
.note(&format!("target type: {}", to))
|
|
|
|
.help("cast with `as` to a pointer instead")
|
2016-06-10 13:00:21 +03:00
|
|
|
.emit();
|
2016-04-19 17:03:30 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-06-12 14:08:44 -07:00
|
|
|
}
|
2014-12-22 20:57:14 -05:00
|
|
|
|
2016-04-19 17:03:30 +03:00
|
|
|
// Try to display a sensible error with as much information as possible.
|
2017-05-19 17:27:25 -04:00
|
|
|
let skeleton_string = |ty: Ty<'tcx>, sk| {
|
2016-04-19 17:03:30 +03: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 {
|
2018-10-02 18:05:06 +02:00
|
|
|
"this type's size can vary".to_owned()
|
2016-04-19 17:03:30 +03:00
|
|
|
} else {
|
|
|
|
format!("size can vary because of {}", bad)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => err.to_string()
|
2014-12-22 20:57:14 -05:00
|
|
|
}
|
2016-04-19 17:03:30 +03:00
|
|
|
};
|
2014-12-22 20:57:14 -05:00
|
|
|
|
2017-05-19 17:27:25 -04:00
|
|
|
struct_span_err!(self.tcx.sess, span, E0512,
|
2018-10-02 18:29:48 +02:00
|
|
|
"transmute called with types of different sizes")
|
2017-05-29 17:22:41 -06:00
|
|
|
.note(&format!("source type: {} ({})", from, skeleton_string(from, sk_from)))
|
|
|
|
.note(&format!("target type: {} ({})", to, skeleton_string(to, sk_to)))
|
2016-09-26 22:49:22 +03:00
|
|
|
.emit();
|
2016-04-19 17:03:30 +03:00
|
|
|
}
|
|
|
|
}
|
2014-12-22 20:57:14 -05:00
|
|
|
|
2016-10-28 22:58:32 +02:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for ItemVisitor<'a, 'tcx> {
|
2016-11-28 14:00:26 -05:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2017-01-06 21:54:24 +02:00
|
|
|
NestedVisitorMap::None
|
2016-10-28 22:58:32 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 21:54:24 +02:00
|
|
|
fn visit_nested_body(&mut self, body_id: hir::BodyId) {
|
2017-05-19 17:27:25 -04:00
|
|
|
let owner_def_id = self.tcx.hir.body_owner_def_id(body_id);
|
2017-01-26 02:41:06 +02:00
|
|
|
let body = self.tcx.hir.body(body_id);
|
2017-05-19 17:27:25 -04:00
|
|
|
let param_env = self.tcx.param_env(owner_def_id);
|
|
|
|
let tables = self.tcx.typeck_tables_of(owner_def_id);
|
|
|
|
ExprVisitor { tcx: self.tcx, param_env, tables }.visit_body(body);
|
2017-01-06 21:54:24 +02:00
|
|
|
self.visit_body(body);
|
2014-12-22 20:57:14 -05:00
|
|
|
}
|
2016-04-19 17:03:30 +03:00
|
|
|
}
|
2014-12-22 20:57:14 -05:00
|
|
|
|
2017-05-19 17:27:25 -04:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for ExprVisitor<'a, 'tcx> {
|
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2017-01-06 21:54:24 +02:00
|
|
|
NestedVisitorMap::None
|
2016-10-28 22:58:32 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 17:27:25 -04:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
|
2018-07-11 20:05:29 +08:00
|
|
|
let def = if let hir::ExprKind::Path(ref qpath) = expr.node {
|
2017-08-04 09:49:40 +02:00
|
|
|
self.tables.qpath_def(qpath, expr.hir_id)
|
2016-11-25 13:21:19 +02:00
|
|
|
} else {
|
|
|
|
Def::Err
|
2016-10-27 05:17:42 +03:00
|
|
|
};
|
2017-05-13 17:11:52 +03:00
|
|
|
if let Def::Fn(did) = def {
|
|
|
|
if self.def_id_is_transmute(did) {
|
2017-08-07 14:43:43 +02:00
|
|
|
let typ = self.tables.node_id_to_type(expr.hir_id);
|
2017-05-13 17:11:52 +03:00
|
|
|
let sig = typ.fn_sig(self.tcx);
|
|
|
|
let from = sig.inputs().skip_binder()[0];
|
|
|
|
let to = *sig.output().skip_binder();
|
|
|
|
self.check_transmute(expr.span, from, to);
|
2014-06-12 14:08:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 17:51:44 -05:00
|
|
|
intravisit::walk_expr(self, expr);
|
2014-06-12 14:08:44 -07:00
|
|
|
}
|
|
|
|
}
|