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-01-20 13:31:10 -06:00
|
|
|
use middle::def::Def;
|
2015-08-16 08:06:23 -05:00
|
|
|
use middle::def_id::DefId;
|
2014-12-22 19:57:14 -06:00
|
|
|
use middle::subst::{Subst, Substs, EnumeratedItems};
|
2015-06-11 18:21:46 -05:00
|
|
|
use middle::ty::{TransmuteRestriction, ctxt, TyBareFn};
|
2015-12-18 04:07:06 -06:00
|
|
|
use middle::ty::{self, Ty, TypeFoldable};
|
2014-06-12 16:08:44 -05:00
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
use std::fmt;
|
|
|
|
|
2014-06-12 16:08:44 -05:00
|
|
|
use syntax::abi::RustIntrinsic;
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::codemap::Span;
|
2015-11-17 16:51:44 -06:00
|
|
|
use rustc_front::intravisit::{self, Visitor, FnKind};
|
2015-07-31 02:04:06 -05:00
|
|
|
use rustc_front::hir;
|
2014-06-12 16:08:44 -05:00
|
|
|
|
2014-12-22 19:57:14 -06:00
|
|
|
pub fn check_crate(tcx: &ctxt) {
|
|
|
|
let mut visitor = IntrinsicCheckingVisitor {
|
|
|
|
tcx: tcx,
|
|
|
|
param_envs: Vec::new(),
|
2015-03-25 19:06:52 -05:00
|
|
|
dummy_sized_ty: tcx.types.isize,
|
2015-06-24 20:09:46 -05:00
|
|
|
dummy_unsized_ty: tcx.mk_slice(tcx.types.isize),
|
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
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
struct IntrinsicCheckingVisitor<'a, 'tcx: 'a> {
|
|
|
|
tcx: &'a ctxt<'tcx>,
|
2014-12-22 19:57:14 -06:00
|
|
|
|
|
|
|
// As we traverse the AST, we keep a stack of the parameter
|
|
|
|
// environments for each function we encounter. When we find a
|
|
|
|
// call to `transmute`, we can check it in the context of the top
|
|
|
|
// of the stack (which ought not to be empty).
|
2015-01-02 03:09:35 -06:00
|
|
|
param_envs: Vec<ty::ParameterEnvironment<'a,'tcx>>,
|
2014-12-22 19:57:14 -06:00
|
|
|
|
|
|
|
// Dummy sized/unsized types that use to substitute for type
|
|
|
|
// parameters in order to estimate how big a type will be for any
|
|
|
|
// possible instantiation of the type parameters in scope. See
|
|
|
|
// `check_transmute` for more details.
|
|
|
|
dummy_sized_ty: Ty<'tcx>,
|
|
|
|
dummy_unsized_ty: Ty<'tcx>,
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'a, 'tcx> IntrinsicCheckingVisitor<'a, 'tcx> {
|
2014-06-12 16:08:44 -05:00
|
|
|
fn def_id_is_transmute(&self, def_id: DefId) -> bool {
|
2015-06-25 15:42:17 -05:00
|
|
|
let intrinsic = match self.tcx.lookup_item_type(def_id).ty.sty {
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyBareFn(_, ref bfty) => bfty.abi == RustIntrinsic,
|
2014-08-25 13:45:19 -05:00
|
|
|
_ => return false
|
|
|
|
};
|
2015-09-24 15:05:02 -05:00
|
|
|
intrinsic && self.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) {
|
2014-12-22 19:57:14 -06:00
|
|
|
// Find the parameter environment for the most recent function that
|
|
|
|
// we entered.
|
|
|
|
|
|
|
|
let param_env = match self.param_envs.last() {
|
|
|
|
Some(p) => p,
|
|
|
|
None => {
|
|
|
|
self.tcx.sess.span_bug(
|
|
|
|
span,
|
|
|
|
"transmute encountered outside of any fn");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Simple case: no type parameters involved.
|
|
|
|
if
|
2015-06-23 18:54:32 -05:00
|
|
|
!from.has_param_types() && !from.has_self_ty() &&
|
|
|
|
!to.has_param_types() && !to.has_self_ty()
|
2014-12-22 19:57:14 -06:00
|
|
|
{
|
|
|
|
let restriction = TransmuteRestriction {
|
|
|
|
span: span,
|
|
|
|
original_from: from,
|
|
|
|
original_to: to,
|
|
|
|
substituted_from: from,
|
|
|
|
substituted_to: to,
|
|
|
|
id: id,
|
|
|
|
};
|
|
|
|
self.push_transmute_restriction(restriction);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The rules around type parameters are a bit subtle. We are
|
|
|
|
// checking these rules before monomorphization, so there may
|
|
|
|
// be unsubstituted type parameters present in the
|
|
|
|
// types. Obviously we cannot create LLVM types for those.
|
|
|
|
// However, if a type parameter appears only indirectly (i.e.,
|
|
|
|
// through a pointer), it does not necessarily affect the
|
|
|
|
// size, so that should be allowed. The only catch is that we
|
|
|
|
// DO want to be careful around unsized type parameters, since
|
|
|
|
// fat pointers have a different size than a thin pointer, and
|
|
|
|
// hence `&T` and `&U` have different sizes if `T : Sized` but
|
|
|
|
// `U : Sized` does not hold.
|
|
|
|
//
|
|
|
|
// However, it's not as simple as checking whether `T :
|
|
|
|
// Sized`, because even if `T : Sized` does not hold, that
|
|
|
|
// just means that `T` *may* not be sized. After all, even a
|
2015-01-05 15:16:49 -06:00
|
|
|
// type parameter `T: ?Sized` could be bound to a sized
|
2014-12-22 19:57:14 -06:00
|
|
|
// type. (Issue #20116)
|
|
|
|
//
|
|
|
|
// To handle this, we first check for "interior" type
|
|
|
|
// parameters, which are always illegal. If there are none of
|
|
|
|
// those, then we know that the only way that all type
|
|
|
|
// parameters `T` are referenced indirectly, e.g. via a
|
|
|
|
// pointer type like `&T`. In that case, we only care whether
|
|
|
|
// `T` is sized or not, because that influences whether `&T`
|
|
|
|
// is a thin or fat pointer.
|
|
|
|
//
|
|
|
|
// One could imagine establishing a sophisticated constraint
|
|
|
|
// system to ensure that the transmute is legal, but instead
|
|
|
|
// we do something brutally dumb. We just substitute dummy
|
|
|
|
// sized or unsized types for every type parameter in scope,
|
|
|
|
// exhaustively checking all possible combinations. Here are some examples:
|
|
|
|
//
|
|
|
|
// ```
|
2015-01-05 15:16:49 -06:00
|
|
|
// fn foo<T, U>() {
|
2014-12-22 19:57:14 -06:00
|
|
|
// // T=int, U=int
|
|
|
|
// }
|
|
|
|
//
|
2015-01-05 15:16:49 -06:00
|
|
|
// fn bar<T: ?Sized, U>() {
|
2014-12-22 19:57:14 -06:00
|
|
|
// // T=int, U=int
|
|
|
|
// // T=[int], U=int
|
|
|
|
// }
|
|
|
|
//
|
2015-01-05 15:16:49 -06:00
|
|
|
// fn baz<T: ?Sized, U: ?Sized>() {
|
2014-12-22 19:57:14 -06:00
|
|
|
// // T=int, U=int
|
|
|
|
// // T=[int], U=int
|
|
|
|
// // T=int, U=[int]
|
|
|
|
// // T=[int], U=[int]
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// In all cases, we keep the original unsubstituted types
|
|
|
|
// around for error reporting.
|
|
|
|
|
2015-06-25 15:42:17 -05:00
|
|
|
let from_tc = from.type_contents(self.tcx);
|
|
|
|
let to_tc = to.type_contents(self.tcx);
|
2014-12-22 19:57:14 -06:00
|
|
|
if from_tc.interior_param() || to_tc.interior_param() {
|
2014-07-17 12:56:37 -05:00
|
|
|
span_err!(self.tcx.sess, span, E0139,
|
2014-12-22 19:57:14 -06:00
|
|
|
"cannot transmute to or from a type that contains \
|
2015-07-19 09:18:09 -05:00
|
|
|
unsubstituted type parameters");
|
2014-12-22 19:57:14 -06:00
|
|
|
return;
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
2014-12-22 19:57:14 -06:00
|
|
|
|
|
|
|
let mut substs = param_env.free_substs.clone();
|
|
|
|
self.with_each_combination(
|
2015-01-02 03:01:30 -06:00
|
|
|
span,
|
2014-12-22 19:57:14 -06:00
|
|
|
param_env,
|
|
|
|
param_env.free_substs.types.iter_enumerated(),
|
|
|
|
&mut substs,
|
|
|
|
&mut |substs| {
|
|
|
|
let restriction = TransmuteRestriction {
|
|
|
|
span: span,
|
|
|
|
original_from: from,
|
|
|
|
original_to: to,
|
|
|
|
substituted_from: from.subst(self.tcx, substs),
|
|
|
|
substituted_to: to.subst(self.tcx, substs),
|
|
|
|
id: id,
|
|
|
|
};
|
|
|
|
self.push_transmute_restriction(restriction);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_each_combination(&self,
|
2015-01-02 03:01:30 -06:00
|
|
|
span: Span,
|
|
|
|
param_env: &ty::ParameterEnvironment<'a,'tcx>,
|
2014-12-22 19:57:14 -06:00
|
|
|
mut types_in_scope: EnumeratedItems<Ty<'tcx>>,
|
|
|
|
substs: &mut Substs<'tcx>,
|
|
|
|
callback: &mut FnMut(&Substs<'tcx>))
|
|
|
|
{
|
|
|
|
// This parameter invokes `callback` many times with different
|
|
|
|
// substitutions that replace all the parameters in scope with
|
|
|
|
// either `int` or `[int]`, depending on whether the type
|
|
|
|
// parameter is known to be sized. See big comment above for
|
|
|
|
// an explanation of why this is a reasonable thing to do.
|
|
|
|
|
|
|
|
match types_in_scope.next() {
|
|
|
|
None => {
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("with_each_combination(substs={:?})",
|
|
|
|
substs);
|
2014-12-22 19:57:14 -06:00
|
|
|
|
2015-01-06 11:24:46 -06:00
|
|
|
callback(substs);
|
2014-12-22 19:57:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Some((space, index, ¶m_ty)) => {
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("with_each_combination: space={:?}, index={}, param_ty={:?}",
|
|
|
|
space, index, param_ty);
|
2014-12-22 19:57:14 -06:00
|
|
|
|
2015-06-25 15:42:17 -05:00
|
|
|
if !param_ty.is_sized(param_env, span) {
|
2014-12-22 19:57:14 -06:00
|
|
|
debug!("with_each_combination: param_ty is not known to be sized");
|
|
|
|
|
|
|
|
substs.types.get_mut_slice(space)[index] = self.dummy_unsized_ty;
|
2015-01-02 03:01:30 -06:00
|
|
|
self.with_each_combination(span, param_env, types_in_scope.clone(),
|
|
|
|
substs, callback);
|
2014-12-22 19:57:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
substs.types.get_mut_slice(space)[index] = self.dummy_sized_ty;
|
2015-01-02 03:01:30 -06:00
|
|
|
self.with_each_combination(span, param_env, types_in_scope,
|
|
|
|
substs, callback);
|
2014-12-22 19:57:14 -06:00
|
|
|
}
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|
2014-12-22 19:57:14 -06:00
|
|
|
}
|
2014-06-12 16:08:44 -05:00
|
|
|
|
2014-12-22 19:57:14 -06:00
|
|
|
fn push_transmute_restriction(&self, restriction: TransmuteRestriction<'tcx>) {
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("Pushing transmute restriction: {:?}", restriction);
|
2014-06-12 16:08:44 -05:00
|
|
|
self.tcx.transmute_restrictions.borrow_mut().push(restriction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-09 17:54:36 -05:00
|
|
|
impl<'a, 'tcx, 'v> Visitor<'v> for IntrinsicCheckingVisitor<'a, 'tcx> {
|
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);
|
|
|
|
self.param_envs.push(param_env);
|
2015-11-17 16:51:44 -06:00
|
|
|
intravisit::walk_fn(self, fk, fd, b, s);
|
2014-12-22 19:57:14 -06:00
|
|
|
self.param_envs.pop();
|
|
|
|
}
|
2015-10-26 13:10:41 -05:00
|
|
|
FnKind::Closure => {
|
2015-11-17 16:51:44 -06:00
|
|
|
intravisit::walk_fn(self, fk, fd, b, s);
|
2014-12-22 19:57:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn visit_expr(&mut self, expr: &hir::Expr) {
|
|
|
|
if let hir::ExprPath(..) = expr.node {
|
2015-06-25 15:42:17 -05:00
|
|
|
match self.tcx.resolve_expr(expr) {
|
2016-01-20 13:31:10 -06:00
|
|
|
Def::Fn(did) if self.def_id_is_transmute(did) => {
|
2015-06-25 15:42:17 -05:00
|
|
|
let typ = self.tcx.node_id_to_type(expr.id);
|
2014-11-29 15:41:21 -06:00
|
|
|
match typ.sty {
|
2015-06-11 18:21:46 -05:00
|
|
|
TyBareFn(_, 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
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.span_bug(expr.span, "transmute wasn't a bare fn?!");
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
impl<'tcx> fmt::Debug for TransmuteRestriction<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "TransmuteRestriction(id={}, original=({:?},{:?}), substituted=({:?},{:?}))",
|
|
|
|
self.id,
|
|
|
|
self.original_from,
|
|
|
|
self.original_to,
|
|
|
|
self.substituted_from,
|
|
|
|
self.substituted_to)
|
2014-12-22 19:57:14 -06:00
|
|
|
}
|
2014-06-12 16:08:44 -05:00
|
|
|
}
|