|
| 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 | + |
| 11 | +use metadata::csearch; |
| 12 | +use middle::def::DefFn; |
| 13 | +use middle::subst::Subst; |
| 14 | +use middle::ty::{TransmuteRestriction, ctxt, ty_bare_fn}; |
| 15 | +use middle::ty; |
| 16 | + |
| 17 | +use syntax::abi::RustIntrinsic; |
| 18 | +use syntax::ast::DefId; |
| 19 | +use syntax::ast; |
| 20 | +use syntax::ast_map::NodeForeignItem; |
| 21 | +use syntax::codemap::Span; |
| 22 | +use syntax::parse::token; |
| 23 | +use syntax::visit::Visitor; |
| 24 | +use syntax::visit; |
| 25 | + |
| 26 | +fn type_size_is_affected_by_type_parameters(tcx: &ty::ctxt, typ: ty::t) |
| 27 | + -> bool { |
| 28 | + let mut result = false; |
| 29 | + ty::maybe_walk_ty(typ, |typ| { |
| 30 | + match ty::get(typ).sty { |
| 31 | + ty::ty_box(_) | ty::ty_uniq(_) | ty::ty_ptr(_) | |
| 32 | + ty::ty_rptr(..) | ty::ty_bare_fn(..) | ty::ty_closure(..) => { |
| 33 | + false |
| 34 | + } |
| 35 | + ty::ty_param(_) => { |
| 36 | + result = true; |
| 37 | + // No need to continue; we now know the result. |
| 38 | + false |
| 39 | + } |
| 40 | + ty::ty_enum(did, ref substs) => { |
| 41 | + for enum_variant in (*ty::enum_variants(tcx, did)).iter() { |
| 42 | + for argument_type in enum_variant.args.iter() { |
| 43 | + let argument_type = argument_type.subst(tcx, substs); |
| 44 | + result = result || |
| 45 | + type_size_is_affected_by_type_parameters( |
| 46 | + tcx, |
| 47 | + argument_type); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Don't traverse substitutions. |
| 52 | + false |
| 53 | + } |
| 54 | + ty::ty_struct(did, ref substs) => { |
| 55 | + for field in ty::struct_fields(tcx, did, substs).iter() { |
| 56 | + result = result || |
| 57 | + type_size_is_affected_by_type_parameters(tcx, |
| 58 | + field.mt.ty); |
| 59 | + } |
| 60 | + |
| 61 | + // Don't traverse substitutions. |
| 62 | + false |
| 63 | + } |
| 64 | + _ => true, |
| 65 | + } |
| 66 | + }); |
| 67 | + result |
| 68 | +} |
| 69 | + |
| 70 | +struct IntrinsicCheckingVisitor<'a> { |
| 71 | + tcx: &'a ctxt, |
| 72 | +} |
| 73 | + |
| 74 | +impl<'a> IntrinsicCheckingVisitor<'a> { |
| 75 | + fn def_id_is_transmute(&self, def_id: DefId) -> bool { |
| 76 | + if def_id.krate == ast::LOCAL_CRATE { |
| 77 | + match self.tcx.map.get(def_id.node) { |
| 78 | + NodeForeignItem(ref item) => { |
| 79 | + token::get_ident(item.ident) == |
| 80 | + token::intern_and_get_ident("transmute") |
| 81 | + } |
| 82 | + _ => false, |
| 83 | + } |
| 84 | + } else { |
| 85 | + match csearch::get_item_path(self.tcx, def_id).last() { |
| 86 | + None => false, |
| 87 | + Some(ref last) => { |
| 88 | + token::get_name(last.name()) == |
| 89 | + token::intern_and_get_ident("transmute") |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + fn check_transmute(&self, span: Span, from: ty::t, to: ty::t) { |
| 96 | + if type_size_is_affected_by_type_parameters(self.tcx, from) { |
| 97 | + self.tcx.sess.span_err(span, |
| 98 | + "cannot transmute from a type that \ |
| 99 | + contains type parameters"); |
| 100 | + } |
| 101 | + if type_size_is_affected_by_type_parameters(self.tcx, to) { |
| 102 | + self.tcx.sess.span_err(span, |
| 103 | + "cannot transmute to a type that contains \ |
| 104 | + type parameters"); |
| 105 | + } |
| 106 | + |
| 107 | + let restriction = TransmuteRestriction { |
| 108 | + span: span, |
| 109 | + from: from, |
| 110 | + to: to, |
| 111 | + }; |
| 112 | + self.tcx.transmute_restrictions.borrow_mut().push(restriction); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl<'a> Visitor<()> for IntrinsicCheckingVisitor<'a> { |
| 117 | + fn visit_expr(&mut self, expr: &ast::Expr, (): ()) { |
| 118 | + match expr.node { |
| 119 | + ast::ExprPath(..) => { |
| 120 | + match ty::resolve_expr(self.tcx, expr) { |
| 121 | + DefFn(did, _) if self.def_id_is_transmute(did) => { |
| 122 | + let typ = ty::node_id_to_type(self.tcx, expr.id); |
| 123 | + match ty::get(typ).sty { |
| 124 | + ty_bare_fn(ref bare_fn_ty) |
| 125 | + if bare_fn_ty.abi == RustIntrinsic => { |
| 126 | + let from = *bare_fn_ty.sig.inputs.get(0); |
| 127 | + let to = bare_fn_ty.sig.output; |
| 128 | + self.check_transmute(expr.span, from, to); |
| 129 | + } |
| 130 | + _ => { |
| 131 | + self.tcx |
| 132 | + .sess |
| 133 | + .span_bug(expr.span, |
| 134 | + "transmute wasn't a bare fn?!"); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + _ => {} |
| 139 | + } |
| 140 | + } |
| 141 | + _ => {} |
| 142 | + } |
| 143 | + |
| 144 | + visit::walk_expr(self, expr, ()); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +pub fn check_crate(tcx: &ctxt, krate: &ast::Crate) { |
| 149 | + let mut visitor = IntrinsicCheckingVisitor { |
| 150 | + tcx: tcx, |
| 151 | + }; |
| 152 | + |
| 153 | + visit::walk_crate(&mut visitor, krate, ()); |
| 154 | +} |
| 155 | + |
0 commit comments