2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::freevars::freevar_entry;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::freevars;
|
|
|
|
use middle::ty;
|
|
|
|
use middle::typeck;
|
2013-04-10 15:11:27 -05:00
|
|
|
use util::ppaux::{Repr, ty_to_str};
|
2013-05-07 16:30:21 -05:00
|
|
|
use util::ppaux::UserString;
|
2012-12-13 15:05:22 -06:00
|
|
|
|
|
|
|
use syntax::ast::*;
|
2013-07-19 06:51:37 -05:00
|
|
|
use syntax::attr;
|
2013-03-26 15:38:07 -05:00
|
|
|
use syntax::codemap::span;
|
2013-07-24 15:52:57 -05:00
|
|
|
use syntax::opt_vec;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::print::pprust::expr_to_str;
|
2013-08-13 07:11:34 -05:00
|
|
|
use syntax::{visit,ast_util};
|
|
|
|
use syntax::visit::Visitor;
|
2011-11-15 11:15:35 -06:00
|
|
|
|
2012-05-24 16:33:21 -05:00
|
|
|
// Kind analysis pass.
|
2011-11-18 10:09:14 -06:00
|
|
|
//
|
2012-05-24 16:33:21 -05:00
|
|
|
// There are several kinds defined by various operations. The most restrictive
|
|
|
|
// kind is noncopyable. The noncopyable kind can be extended with any number
|
|
|
|
// of the following attributes.
|
|
|
|
//
|
|
|
|
// send: Things that can be sent on channels or included in spawned closures.
|
2013-06-05 16:52:27 -05:00
|
|
|
// freeze: Things thare are deeply immutable. They are guaranteed never to
|
2012-05-24 16:33:21 -05:00
|
|
|
// change, and can be safely shared without copying between tasks.
|
2013-06-05 13:33:14 -05:00
|
|
|
// 'static: Things that do not contain borrowed pointers.
|
2012-05-24 16:33:21 -05:00
|
|
|
//
|
2012-06-24 17:09:57 -05:00
|
|
|
// Send includes scalar types as well as classes and unique types containing
|
|
|
|
// only sendable types.
|
2012-05-24 16:33:21 -05:00
|
|
|
//
|
2013-06-05 16:52:27 -05:00
|
|
|
// Freeze include scalar types, things without non-const fields, and pointers
|
|
|
|
// to freezable things.
|
2011-11-18 10:09:14 -06:00
|
|
|
//
|
|
|
|
// This pass ensures that type parameters are only instantiated with types
|
|
|
|
// whose kinds are equal or less general than the way the type parameter was
|
2013-07-10 19:31:53 -05:00
|
|
|
// annotated (with the `Send` or `Freeze` bound).
|
2011-11-18 10:09:14 -06:00
|
|
|
//
|
|
|
|
// It also verifies that noncopyable kinds are not copied. Sendability is not
|
|
|
|
// applied, since none of our language primitives send. Instead, the sending
|
|
|
|
// primitives in the stdlib are explicitly annotated to only take sendable
|
|
|
|
// types.
|
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone)]
|
2013-02-19 01:40:42 -06:00
|
|
|
pub struct Context {
|
2013-01-30 15:44:24 -06:00
|
|
|
tcx: ty::ctxt,
|
|
|
|
method_map: typeck::method_map,
|
2013-07-27 03:25:59 -05:00
|
|
|
current_item: NodeId
|
2013-02-19 01:40:42 -06:00
|
|
|
}
|
2011-11-15 11:15:35 -06:00
|
|
|
|
2013-08-13 07:11:34 -05:00
|
|
|
struct KindAnalysisVisitor;
|
|
|
|
|
|
|
|
impl Visitor<Context> for KindAnalysisVisitor {
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, ex:@expr, e:Context) {
|
|
|
|
check_expr(self, ex, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_fn(&mut self, fk:&visit::fn_kind, fd:&fn_decl, b:&Block, s:span, n:NodeId, e:Context) {
|
|
|
|
check_fn(self, fk, fd, b, s, n, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_ty(&mut self, t:&Ty, e:Context) {
|
|
|
|
check_ty(self, t, e);
|
|
|
|
}
|
|
|
|
fn visit_item(&mut self, i:@item, e:Context) {
|
|
|
|
check_item(self, i, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn check_crate(tcx: ty::ctxt,
|
|
|
|
method_map: typeck::method_map,
|
2013-07-19 00:38:55 -05:00
|
|
|
crate: &Crate) {
|
2013-02-19 01:40:42 -06:00
|
|
|
let ctx = Context {
|
|
|
|
tcx: tcx,
|
|
|
|
method_map: method_map,
|
2013-03-15 14:24:24 -05:00
|
|
|
current_item: -1
|
2013-02-19 01:40:42 -06:00
|
|
|
};
|
2013-08-13 07:11:34 -05:00
|
|
|
let mut visit = KindAnalysisVisitor;
|
|
|
|
visit::walk_crate(&mut visit, crate, ctx);
|
2011-11-15 11:15:35 -06:00
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
}
|
|
|
|
|
2013-03-20 20:18:57 -05:00
|
|
|
fn check_struct_safe_for_destructor(cx: Context,
|
|
|
|
span: span,
|
|
|
|
struct_did: def_id) {
|
|
|
|
let struct_tpt = ty::lookup_item_type(cx.tcx, struct_did);
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
if !struct_tpt.generics.has_type_params() {
|
2013-03-20 20:18:57 -05:00
|
|
|
let struct_ty = ty::mk_struct(cx.tcx, struct_did, ty::substs {
|
2013-07-24 15:52:57 -05:00
|
|
|
regions: ty::NonerasedRegions(opt_vec::Empty),
|
2013-03-20 20:18:57 -05:00
|
|
|
self_ty: None,
|
|
|
|
tps: ~[]
|
|
|
|
});
|
2013-06-05 13:33:14 -05:00
|
|
|
if !ty::type_is_sendable(cx.tcx, struct_ty) {
|
2013-03-20 20:18:57 -05:00
|
|
|
cx.tcx.sess.span_err(span,
|
2013-06-05 13:33:14 -05:00
|
|
|
"cannot implement a destructor on a \
|
|
|
|
structure that does not satisfy Send");
|
2013-03-20 20:18:57 -05:00
|
|
|
cx.tcx.sess.span_note(span,
|
2013-04-30 11:47:52 -05:00
|
|
|
"use \"#[unsafe_destructor]\" on the \
|
|
|
|
implementation to force the compiler to \
|
|
|
|
allow this");
|
2013-03-20 20:18:57 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cx.tcx.sess.span_err(span,
|
2013-06-05 13:33:14 -05:00
|
|
|
"cannot implement a destructor on a structure \
|
2013-04-30 11:47:52 -05:00
|
|
|
with type parameters");
|
2013-03-20 20:18:57 -05:00
|
|
|
cx.tcx.sess.span_note(span,
|
2013-04-30 11:47:52 -05:00
|
|
|
"use \"#[unsafe_destructor]\" on the \
|
|
|
|
implementation to force the compiler to \
|
|
|
|
allow this");
|
2013-03-20 20:18:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-16 15:57:42 -05:00
|
|
|
fn check_impl_of_trait(cx: Context, it: @item, trait_ref: &trait_ref, self_type: &Ty) {
|
|
|
|
let ast_trait_def = cx.tcx.def_map.find(&trait_ref.ref_id)
|
|
|
|
.expect("trait ref not in def map!");
|
|
|
|
let trait_def_id = ast_util::def_id_of_def(*ast_trait_def);
|
|
|
|
let trait_def = cx.tcx.trait_defs.find(&trait_def_id)
|
|
|
|
.expect("trait def not in trait-defs map!");
|
|
|
|
|
|
|
|
// If this trait has builtin-kind supertraits, meet them.
|
|
|
|
let self_ty: ty::t = ty::node_id_to_type(cx.tcx, it.id);
|
2013-08-28 13:34:54 -05:00
|
|
|
debug!("checking impl with self type %?", ty::get(self_ty).sty);
|
2013-08-16 15:57:42 -05:00
|
|
|
do check_builtin_bounds(cx, self_ty, trait_def.bounds) |missing| {
|
|
|
|
cx.tcx.sess.span_err(self_type.span,
|
|
|
|
fmt!("the type `%s', which does not fulfill `%s`, cannot implement this \
|
|
|
|
trait", ty_to_str(cx.tcx, self_ty), missing.user_string(cx.tcx)));
|
|
|
|
cx.tcx.sess.span_note(self_type.span,
|
|
|
|
fmt!("types implementing this trait must fulfill `%s`",
|
|
|
|
trait_def.bounds.user_string(cx.tcx)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a destructor, check kinds.
|
|
|
|
if cx.tcx.lang_items.drop_trait() == Some(trait_def_id) {
|
|
|
|
match self_type.node {
|
|
|
|
ty_path(_, ref bounds, path_node_id) => {
|
|
|
|
assert!(bounds.is_none());
|
|
|
|
let struct_def = cx.tcx.def_map.get_copy(&path_node_id);
|
|
|
|
let struct_did = ast_util::def_id_of_def(struct_def);
|
|
|
|
check_struct_safe_for_destructor(cx, self_type.span, struct_did);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
cx.tcx.sess.span_bug(self_type.span,
|
|
|
|
"the self type for the Drop trait impl is not a path");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-20 20:18:57 -05:00
|
|
|
}
|
|
|
|
|
2013-08-13 07:11:34 -05:00
|
|
|
fn check_item(visitor: &mut KindAnalysisVisitor, item: @item, cx: Context) {
|
2013-07-19 06:51:37 -05:00
|
|
|
if !attr::contains_name(item.attrs, "unsafe_destructor") {
|
2013-03-20 20:18:57 -05:00
|
|
|
match item.node {
|
2013-07-05 23:57:11 -05:00
|
|
|
item_impl(_, Some(ref trait_ref), ref self_type, _) => {
|
2013-08-16 15:57:42 -05:00
|
|
|
check_impl_of_trait(cx, item, trait_ref, self_type);
|
2013-03-20 20:18:57 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let cx = Context { current_item: item.id, ..cx };
|
2013-08-13 07:11:34 -05:00
|
|
|
visit::walk_item(visitor, item, cx);
|
2013-03-20 20:18:57 -05:00
|
|
|
}
|
|
|
|
|
2011-12-20 21:39:33 -06:00
|
|
|
// Yields the appropriate function to check the kind of closed over
|
2013-07-27 03:25:59 -05:00
|
|
|
// variables. `id` is the NodeId for some expression that creates the
|
2011-12-20 21:39:33 -06:00
|
|
|
// closure.
|
2013-07-27 03:25:59 -05:00
|
|
|
fn with_appropriate_checker(cx: Context, id: NodeId,
|
2013-06-13 18:37:18 -05:00
|
|
|
b: &fn(checker: &fn(Context, @freevar_entry))) {
|
2013-06-27 08:04:22 -05:00
|
|
|
fn check_for_uniq(cx: Context, fv: &freevar_entry, bounds: ty::BuiltinBounds) {
|
2013-02-07 21:33:12 -06:00
|
|
|
// all captured data must be owned, regardless of whether it is
|
|
|
|
// moved in or copied in.
|
2013-01-10 12:59:58 -06:00
|
|
|
let id = ast_util::def_id_of_def(fv.def).node;
|
|
|
|
let var_t = ty::node_id_to_type(cx.tcx, id);
|
2013-06-13 18:37:18 -05:00
|
|
|
|
2012-05-07 13:31:57 -05:00
|
|
|
// check that only immutable variables are implicitly copied in
|
2013-01-10 12:59:58 -06:00
|
|
|
check_imm_free_var(cx, fv.def, fv.span);
|
2013-06-13 18:37:18 -05:00
|
|
|
|
2013-06-21 16:58:21 -05:00
|
|
|
check_freevar_bounds(cx, fv.span, var_t, bounds, None);
|
2012-05-07 13:31:57 -05:00
|
|
|
}
|
|
|
|
|
2013-06-27 08:04:22 -05:00
|
|
|
fn check_for_box(cx: Context, fv: &freevar_entry, bounds: ty::BuiltinBounds) {
|
2012-07-16 22:17:57 -05:00
|
|
|
// all captured data must be owned
|
2013-01-10 12:59:58 -06:00
|
|
|
let id = ast_util::def_id_of_def(fv.def).node;
|
|
|
|
let var_t = ty::node_id_to_type(cx.tcx, id);
|
2013-06-13 18:37:18 -05:00
|
|
|
|
2012-05-07 13:31:57 -05:00
|
|
|
// check that only immutable variables are implicitly copied in
|
2013-01-10 12:59:58 -06:00
|
|
|
check_imm_free_var(cx, fv.def, fv.span);
|
2013-06-13 18:37:18 -05:00
|
|
|
|
2013-06-21 16:58:21 -05:00
|
|
|
check_freevar_bounds(cx, fv.span, var_t, bounds, None);
|
2012-05-07 13:31:57 -05:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:58:21 -05:00
|
|
|
fn check_for_block(cx: Context, fv: &freevar_entry,
|
|
|
|
bounds: ty::BuiltinBounds, region: ty::Region) {
|
2013-06-13 18:37:18 -05:00
|
|
|
let id = ast_util::def_id_of_def(fv.def).node;
|
|
|
|
let var_t = ty::node_id_to_type(cx.tcx, id);
|
2013-06-21 16:58:21 -05:00
|
|
|
// FIXME(#3569): Figure out whether the implicit borrow is actually
|
|
|
|
// mutable. Currently we assume all upvars are referenced mutably.
|
|
|
|
let implicit_borrowed_type = ty::mk_mut_rptr(cx.tcx, region, var_t);
|
|
|
|
check_freevar_bounds(cx, fv.span, implicit_borrowed_type,
|
|
|
|
bounds, Some(var_t));
|
2012-05-07 13:31:57 -05:00
|
|
|
}
|
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
fn check_for_bare(cx: Context, fv: @freevar_entry) {
|
2013-01-10 12:59:58 -06:00
|
|
|
cx.tcx.sess.span_err(
|
|
|
|
fv.span,
|
2013-07-01 12:47:43 -05:00
|
|
|
"can't capture dynamic environment in a fn item; \
|
|
|
|
use the || { ... } closure form instead");
|
|
|
|
} // same check is done in resolve.rs, but shouldn't be done
|
2012-05-07 13:31:57 -05:00
|
|
|
|
2012-01-30 10:28:30 -06:00
|
|
|
let fty = ty::node_id_to_type(cx.tcx, id);
|
2013-01-31 19:12:29 -06:00
|
|
|
match ty::get(fty).sty {
|
2013-06-13 18:37:18 -05:00
|
|
|
ty::ty_closure(ty::ClosureTy {sigil: OwnedSigil, bounds: bounds, _}) => {
|
|
|
|
b(|cx, fv| check_for_uniq(cx, fv, bounds))
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
2013-06-13 18:37:18 -05:00
|
|
|
ty::ty_closure(ty::ClosureTy {sigil: ManagedSigil, bounds: bounds, _}) => {
|
|
|
|
b(|cx, fv| check_for_box(cx, fv, bounds))
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
2013-06-21 16:58:21 -05:00
|
|
|
ty::ty_closure(ty::ClosureTy {sigil: BorrowedSigil, bounds: bounds,
|
|
|
|
region: region, _}) => {
|
|
|
|
b(|cx, fv| check_for_block(cx, fv, bounds, region))
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
|
|
|
ty::ty_bare_fn(_) => {
|
|
|
|
b(check_for_bare)
|
|
|
|
}
|
|
|
|
ref s => {
|
|
|
|
cx.tcx.sess.bug(
|
|
|
|
fmt!("expect fn type in kind checker, not %?", s));
|
|
|
|
}
|
2011-12-20 21:39:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the free variables used in a shared/sendable closure conform
|
|
|
|
// to the copy/move kind bounds. Then recursively check the function body.
|
2013-02-18 00:20:36 -06:00
|
|
|
fn check_fn(
|
2013-08-13 07:11:34 -05:00
|
|
|
v: &mut KindAnalysisVisitor,
|
|
|
|
fk: &visit::fn_kind,
|
2013-02-18 00:20:36 -06:00
|
|
|
decl: &fn_decl,
|
2013-07-19 00:38:55 -05:00
|
|
|
body: &Block,
|
2013-02-18 00:20:36 -06:00
|
|
|
sp: span,
|
2013-07-27 03:25:59 -05:00
|
|
|
fn_id: NodeId,
|
2013-08-13 07:11:34 -05:00
|
|
|
cx: Context) {
|
2012-05-07 13:31:57 -05:00
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
// Check kinds on free variables:
|
|
|
|
do with_appropriate_checker(cx, fn_id) |chk| {
|
2013-06-24 17:34:20 -05:00
|
|
|
let r = freevars::get_freevars(cx.tcx, fn_id);
|
2013-08-03 11:45:23 -05:00
|
|
|
for fv in r.iter() {
|
2013-01-10 12:59:58 -06:00
|
|
|
chk(cx, *fv);
|
2011-12-09 18:56:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 07:11:34 -05:00
|
|
|
visit::walk_fn(v, fk, decl, body, sp, fn_id, cx);
|
2011-12-20 21:39:33 -06:00
|
|
|
}
|
|
|
|
|
2013-08-13 07:11:34 -05:00
|
|
|
pub fn check_expr(v: &mut KindAnalysisVisitor, e: @expr, cx: Context) {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("kind::check_expr(%s)", expr_to_str(e, cx.tcx.sess.intr()));
|
2013-01-10 12:59:58 -06:00
|
|
|
|
|
|
|
// Handle any kind bounds on type parameters
|
2013-06-01 17:31:56 -05:00
|
|
|
let type_parameter_id = match e.get_callee_id() {
|
|
|
|
Some(callee_id) => callee_id,
|
|
|
|
None => e.id,
|
2012-09-06 17:42:59 -05:00
|
|
|
};
|
2013-06-10 16:50:12 -05:00
|
|
|
{
|
|
|
|
let r = cx.tcx.node_type_substs.find(&type_parameter_id);
|
2013-08-03 11:45:23 -05:00
|
|
|
for ts in r.iter() {
|
2013-06-10 16:50:12 -05:00
|
|
|
let type_param_defs = match e.node {
|
|
|
|
expr_path(_) => {
|
|
|
|
let did = ast_util::def_id_of_def(cx.tcx.def_map.get_copy(&e.id));
|
|
|
|
ty::lookup_item_type(cx.tcx, did).generics.type_param_defs
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Type substitutions should only occur on paths and
|
|
|
|
// method calls, so this needs to be a method call.
|
2012-09-06 17:42:59 -05:00
|
|
|
|
2013-06-10 16:50:12 -05:00
|
|
|
// Even though the callee_id may have been the id with
|
|
|
|
// node_type_substs, e.id is correct here.
|
|
|
|
ty::method_call_type_param_defs(cx.tcx, cx.method_map, e.id).expect(
|
|
|
|
"non path/method call expr has type substs??")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if ts.len() != type_param_defs.len() {
|
|
|
|
// Fail earlier to make debugging easier
|
|
|
|
fail!("internal error: in kind::check_expr, length \
|
|
|
|
mismatch between actual and declared bounds: actual = \
|
|
|
|
%s, declared = %s",
|
|
|
|
ts.repr(cx.tcx),
|
|
|
|
type_param_defs.repr(cx.tcx));
|
|
|
|
}
|
2013-08-03 11:45:23 -05:00
|
|
|
for (&ty, type_param_def) in ts.iter().zip(type_param_defs.iter()) {
|
2013-06-13 18:37:18 -05:00
|
|
|
check_typaram_bounds(cx, type_parameter_id, e.span, ty, type_param_def)
|
2013-06-10 16:50:12 -05:00
|
|
|
}
|
2012-08-16 18:44:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
match e.node {
|
2013-07-18 19:12:46 -05:00
|
|
|
expr_unary(_, box(_), interior) => {
|
|
|
|
let interior_type = ty::expr_ty(cx.tcx, interior);
|
|
|
|
let _ = check_durable(cx.tcx, interior_type, interior.span);
|
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
expr_cast(source, _) => {
|
|
|
|
check_cast_for_escaping_regions(cx, source, e);
|
2013-06-19 16:58:04 -05:00
|
|
|
match ty::get(ty::expr_ty(cx.tcx, e)).sty {
|
2013-06-19 21:06:50 -05:00
|
|
|
ty::ty_trait(_, _, _, _, bounds) => {
|
2013-06-19 16:58:04 -05:00
|
|
|
let source_ty = ty::expr_ty(cx.tcx, source);
|
2013-06-19 21:06:50 -05:00
|
|
|
check_trait_cast_bounds(cx, e.span, source_ty, bounds)
|
2013-06-19 16:58:04 -05:00
|
|
|
}
|
|
|
|
_ => { }
|
|
|
|
}
|
2011-11-22 06:27:40 -06:00
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
expr_repeat(element, count_expr, _) => {
|
2013-07-16 03:27:54 -05:00
|
|
|
let count = ty::eval_repeat_count(&cx.tcx, count_expr);
|
2013-01-10 12:59:58 -06:00
|
|
|
if count > 1 {
|
|
|
|
let element_ty = ty::expr_ty(cx.tcx, element);
|
|
|
|
check_copy(cx, element_ty, element.span,
|
|
|
|
"repeated element will be copied");
|
2011-11-15 11:15:35 -06:00
|
|
|
}
|
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
_ => {}
|
2011-11-15 11:15:35 -06:00
|
|
|
}
|
2013-08-13 07:11:34 -05:00
|
|
|
visit::walk_expr(v, e, cx);
|
2011-11-15 11:15:35 -06:00
|
|
|
}
|
|
|
|
|
2013-08-13 07:11:34 -05:00
|
|
|
fn check_ty(v: &mut KindAnalysisVisitor, aty: &Ty, cx: Context) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match aty.node {
|
2013-06-17 14:16:30 -05:00
|
|
|
ty_path(_, _, id) => {
|
2013-06-10 16:50:12 -05:00
|
|
|
let r = cx.tcx.node_type_substs.find(&id);
|
2013-08-03 11:45:23 -05:00
|
|
|
for ts in r.iter() {
|
2013-06-10 16:50:12 -05:00
|
|
|
let did = ast_util::def_id_of_def(cx.tcx.def_map.get_copy(&id));
|
|
|
|
let type_param_defs =
|
|
|
|
ty::lookup_item_type(cx.tcx, did).generics.type_param_defs;
|
2013-08-03 11:45:23 -05:00
|
|
|
for (&ty, type_param_def) in ts.iter().zip(type_param_defs.iter()) {
|
2013-06-13 18:37:18 -05:00
|
|
|
check_typaram_bounds(cx, aty.id, aty.span, ty, type_param_def)
|
2013-06-10 16:50:12 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-19 04:45:29 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {}
|
2012-03-19 04:45:29 -05:00
|
|
|
}
|
2013-08-13 07:11:34 -05:00
|
|
|
visit::walk_ty(v, aty, cx);
|
2012-03-19 04:45:29 -05:00
|
|
|
}
|
|
|
|
|
2013-06-19 16:58:04 -05:00
|
|
|
// Calls "any_missing" if any bounds were missing.
|
|
|
|
pub fn check_builtin_bounds(cx: Context, ty: ty::t, bounds: ty::BuiltinBounds,
|
|
|
|
any_missing: &fn(ty::BuiltinBounds))
|
2013-02-07 21:33:12 -06:00
|
|
|
{
|
|
|
|
let kind = ty::type_contents(cx.tcx, ty);
|
2013-05-07 16:30:21 -05:00
|
|
|
let mut missing = ty::EmptyBuiltinBounds();
|
2013-07-25 23:53:29 -05:00
|
|
|
for bound in bounds.iter() {
|
2013-05-07 16:30:21 -05:00
|
|
|
if !kind.meets_bound(cx.tcx, bound) {
|
|
|
|
missing.add(bound);
|
2012-05-29 18:22:22 -05:00
|
|
|
}
|
2013-07-25 23:53:29 -05:00
|
|
|
}
|
2013-06-19 16:58:04 -05:00
|
|
|
if !missing.is_empty() {
|
|
|
|
any_missing(missing);
|
|
|
|
}
|
2013-06-13 18:37:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_typaram_bounds(cx: Context,
|
2013-07-27 03:25:59 -05:00
|
|
|
_type_parameter_id: NodeId,
|
2013-06-13 18:37:18 -05:00
|
|
|
sp: span,
|
|
|
|
ty: ty::t,
|
|
|
|
type_param_def: &ty::TypeParameterDef)
|
|
|
|
{
|
2013-06-19 16:58:04 -05:00
|
|
|
do check_builtin_bounds(cx, ty, type_param_def.bounds.builtin_bounds) |missing| {
|
2013-02-07 21:33:12 -06:00
|
|
|
cx.tcx.sess.span_err(
|
|
|
|
sp,
|
|
|
|
fmt!("instantiating a type parameter with an incompatible type \
|
|
|
|
`%s`, which does not fulfill `%s`",
|
|
|
|
ty_to_str(cx.tcx, ty),
|
2013-05-07 16:30:21 -05:00
|
|
|
missing.user_string(cx.tcx)));
|
2013-02-07 21:33:12 -06:00
|
|
|
}
|
2012-03-19 04:45:29 -05:00
|
|
|
}
|
|
|
|
|
2013-06-13 18:37:18 -05:00
|
|
|
pub fn check_freevar_bounds(cx: Context, sp: span, ty: ty::t,
|
2013-06-21 16:58:21 -05:00
|
|
|
bounds: ty::BuiltinBounds, referenced_ty: Option<ty::t>)
|
2013-06-13 18:37:18 -05:00
|
|
|
{
|
2013-06-19 16:58:04 -05:00
|
|
|
do check_builtin_bounds(cx, ty, bounds) |missing| {
|
2013-06-21 16:58:21 -05:00
|
|
|
// Will be Some if the freevar is implicitly borrowed (stack closure).
|
|
|
|
// Emit a less mysterious error message in this case.
|
|
|
|
match referenced_ty {
|
|
|
|
Some(rty) => cx.tcx.sess.span_err(sp,
|
|
|
|
fmt!("cannot implicitly borrow variable of type `%s` in a bounded \
|
|
|
|
stack closure (implicit reference does not fulfill `%s`)",
|
|
|
|
ty_to_str(cx.tcx, rty), missing.user_string(cx.tcx))),
|
|
|
|
None => cx.tcx.sess.span_err(sp,
|
|
|
|
fmt!("cannot capture variable of type `%s`, which does \
|
|
|
|
not fulfill `%s`, in a bounded closure",
|
|
|
|
ty_to_str(cx.tcx, ty), missing.user_string(cx.tcx))),
|
|
|
|
}
|
2013-06-13 18:37:18 -05:00
|
|
|
cx.tcx.sess.span_note(
|
|
|
|
sp,
|
|
|
|
fmt!("this closure's environment must satisfy `%s`",
|
|
|
|
bounds.user_string(cx.tcx)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-19 16:58:04 -05:00
|
|
|
pub fn check_trait_cast_bounds(cx: Context, sp: span, ty: ty::t,
|
2013-06-19 21:06:50 -05:00
|
|
|
bounds: ty::BuiltinBounds) {
|
2013-06-19 16:58:04 -05:00
|
|
|
do check_builtin_bounds(cx, ty, bounds) |missing| {
|
|
|
|
cx.tcx.sess.span_err(sp,
|
|
|
|
fmt!("cannot pack type `%s`, which does not fulfill \
|
|
|
|
`%s`, as a trait bounded by %s",
|
|
|
|
ty_to_str(cx.tcx, ty), missing.user_string(cx.tcx),
|
|
|
|
bounds.user_string(cx.tcx)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
fn is_nullary_variant(cx: Context, ex: @expr) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match ex.node {
|
2012-08-03 21:59:04 -05:00
|
|
|
expr_path(_) => {
|
2013-05-05 11:17:59 -05:00
|
|
|
match cx.tcx.def_map.get_copy(&ex.id) {
|
2012-08-03 21:59:04 -05:00
|
|
|
def_variant(edid, vdid) => {
|
2013-06-08 20:38:47 -05:00
|
|
|
ty::enum_variant_with_id(cx.tcx, edid, vdid).args.is_empty()
|
2012-01-31 05:34:22 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => false
|
2012-01-31 05:34:22 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => false
|
2012-01-31 05:34:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
fn check_imm_free_var(cx: Context, def: def, sp: span) {
|
2013-01-10 12:59:58 -06:00
|
|
|
match def {
|
|
|
|
def_local(_, is_mutbl) => {
|
|
|
|
if is_mutbl {
|
|
|
|
cx.tcx.sess.span_err(
|
|
|
|
sp,
|
2013-04-30 11:47:52 -05:00
|
|
|
"mutable variables cannot be implicitly captured");
|
2012-12-04 17:38:04 -06:00
|
|
|
}
|
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
def_arg(*) => { /* ok */ }
|
|
|
|
def_upvar(_, def1, _, _) => { check_imm_free_var(cx, *def1, sp); }
|
|
|
|
def_binding(*) | def_self(*) => { /*ok*/ }
|
|
|
|
_ => {
|
|
|
|
cx.tcx.sess.span_bug(
|
|
|
|
sp,
|
|
|
|
fmt!("unknown def for free variable: %?", def));
|
2012-05-07 13:31:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
fn check_copy(cx: Context, ty: ty::t, sp: span, reason: &str) {
|
2013-02-07 21:33:12 -06:00
|
|
|
debug!("type_contents(%s)=%s",
|
|
|
|
ty_to_str(cx.tcx, ty),
|
|
|
|
ty::type_contents(cx.tcx, ty).to_str());
|
2013-07-10 18:00:11 -05:00
|
|
|
if ty::type_moves_by_default(cx.tcx, ty) {
|
2013-02-07 21:33:12 -06:00
|
|
|
cx.tcx.sess.span_err(
|
|
|
|
sp, fmt!("copying a value of non-copyable type `%s`",
|
|
|
|
ty_to_str(cx.tcx, ty)));
|
2013-01-10 12:59:58 -06:00
|
|
|
cx.tcx.sess.span_note(sp, fmt!("%s", reason));
|
2011-11-15 11:15:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 13:33:14 -05:00
|
|
|
pub fn check_send(cx: Context, ty: ty::t, sp: span) -> bool {
|
|
|
|
if !ty::type_is_sendable(cx.tcx, ty) {
|
2013-02-07 21:33:12 -06:00
|
|
|
cx.tcx.sess.span_err(
|
2013-06-05 13:33:14 -05:00
|
|
|
sp, fmt!("value has non-sendable type `%s`",
|
2013-02-07 21:33:12 -06:00
|
|
|
ty_to_str(cx.tcx, ty)));
|
2012-07-16 22:17:57 -05:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-18 13:01:54 -05:00
|
|
|
// note: also used from middle::typeck::regionck!
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn check_durable(tcx: ty::ctxt, ty: ty::t, sp: span) -> bool {
|
2013-05-07 16:30:21 -05:00
|
|
|
if !ty::type_is_static(tcx, ty) {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(ty).sty {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_param(*) => {
|
2013-04-30 11:47:52 -05:00
|
|
|
tcx.sess.span_err(sp, "value may contain borrowed \
|
2013-05-08 14:52:07 -05:00
|
|
|
pointers; add `'static` bound");
|
2012-07-18 13:01:54 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2013-04-30 11:47:52 -05:00
|
|
|
tcx.sess.span_err(sp, "value may contain borrowed \
|
|
|
|
pointers");
|
2012-07-18 13:01:54 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-16 22:17:57 -05:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
2011-12-09 18:56:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-02 00:32:37 -05:00
|
|
|
/// This is rather subtle. When we are casting a value to a instantiated
|
|
|
|
/// trait like `a as trait<'r>`, regionck already ensures that any borrowed
|
2013-04-21 10:58:53 -05:00
|
|
|
/// pointers that appear in the type of `a` are bounded by `'r` (ed.: rem
|
2013-04-02 00:32:37 -05:00
|
|
|
/// FIXME(#5723)). However, it is possible that there are *type parameters*
|
|
|
|
/// in the type of `a`, and those *type parameters* may have borrowed pointers
|
|
|
|
/// within them. We have to guarantee that the regions which appear in those
|
|
|
|
/// type parameters are not obscured.
|
2012-07-18 13:01:54 -05:00
|
|
|
///
|
|
|
|
/// Therefore, we ensure that one of three conditions holds:
|
|
|
|
///
|
2012-07-30 16:51:21 -05:00
|
|
|
/// (1) The trait instance cannot escape the current fn. This is
|
2012-07-18 13:01:54 -05:00
|
|
|
/// guaranteed if the region bound `&r` is some scope within the fn
|
|
|
|
/// itself. This case is safe because whatever borrowed pointers are
|
|
|
|
/// found within the type parameter, they must enclose the fn body
|
|
|
|
/// itself.
|
|
|
|
///
|
2012-07-30 16:51:21 -05:00
|
|
|
/// (2) The type parameter appears in the type of the trait. For
|
|
|
|
/// example, if the type parameter is `T` and the trait type is
|
2012-07-18 13:01:54 -05:00
|
|
|
/// `deque<T>`, then whatever borrowed ptrs may appear in `T` also
|
|
|
|
/// appear in `deque<T>`.
|
|
|
|
///
|
2013-06-05 13:33:14 -05:00
|
|
|
/// (3) The type parameter is sendable (and therefore does not contain
|
2012-07-18 13:01:54 -05:00
|
|
|
/// borrowed ptrs).
|
2013-04-02 00:32:37 -05:00
|
|
|
///
|
|
|
|
/// FIXME(#5723)---This code should probably move into regionck.
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn check_cast_for_escaping_regions(
|
2013-02-19 01:40:42 -06:00
|
|
|
cx: Context,
|
2013-06-27 08:04:22 -05:00
|
|
|
source: &expr,
|
|
|
|
target: &expr)
|
2013-02-07 21:33:12 -06:00
|
|
|
{
|
2012-07-30 16:51:21 -05:00
|
|
|
// Determine what type we are casting to; if it is not an trait, then no
|
2012-07-18 13:01:54 -05:00
|
|
|
// worries.
|
|
|
|
let target_ty = ty::expr_ty(cx.tcx, target);
|
2013-04-02 00:32:37 -05:00
|
|
|
match ty::get(target_ty).sty {
|
|
|
|
ty::ty_trait(*) => {}
|
|
|
|
_ => { return; }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect up the regions that appear in the target type. We want to
|
|
|
|
// ensure that these lifetimes are shorter than all lifetimes that are in
|
|
|
|
// the source type. See test `src/test/compile-fail/regions-trait-2.rs`
|
|
|
|
let mut target_regions = ~[];
|
|
|
|
ty::walk_regions_and_ty(
|
|
|
|
cx.tcx,
|
|
|
|
target_ty,
|
|
|
|
|r| {
|
|
|
|
if !r.is_bound() {
|
|
|
|
target_regions.push(r);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|_| true);
|
2012-07-18 13:01:54 -05:00
|
|
|
|
2012-07-30 16:51:21 -05:00
|
|
|
// Check, based on the region associated with the trait, whether it can
|
2012-07-18 13:01:54 -05:00
|
|
|
// possibly escape the enclosing fn item (note that all type parameters
|
2013-04-02 00:32:37 -05:00
|
|
|
// must have been declared on the enclosing fn item).
|
2013-07-04 21:13:26 -05:00
|
|
|
if target_regions.iter().any(|r| is_re_scope(*r)) {
|
2013-04-02 00:32:37 -05:00
|
|
|
return; /* case (1) */
|
2012-07-18 13:01:54 -05:00
|
|
|
}
|
|
|
|
|
2012-07-30 16:51:21 -05:00
|
|
|
// Assuming the trait instance can escape, then ensure that each parameter
|
2013-06-05 13:33:14 -05:00
|
|
|
// either appears in the trait type or is sendable.
|
2012-07-18 13:01:54 -05:00
|
|
|
let target_params = ty::param_tys_in_type(target_ty);
|
|
|
|
let source_ty = ty::expr_ty(cx.tcx, source);
|
2013-04-02 00:32:37 -05:00
|
|
|
ty::walk_regions_and_ty(
|
|
|
|
cx.tcx,
|
|
|
|
source_ty,
|
|
|
|
|
|
|
|
|_r| {
|
|
|
|
// FIXME(#5723) --- turn this check on once &Objects are usable
|
|
|
|
//
|
2013-07-04 21:13:26 -05:00
|
|
|
// if !target_regions.iter().any(|t_r| is_subregion_of(cx, *t_r, r)) {
|
2013-04-02 00:32:37 -05:00
|
|
|
// cx.tcx.sess.span_err(
|
|
|
|
// source.span,
|
|
|
|
// fmt!("source contains borrowed pointer with lifetime \
|
|
|
|
// not found in the target type `%s`",
|
|
|
|
// ty_to_str(cx.tcx, target_ty)));
|
|
|
|
// note_and_explain_region(
|
|
|
|
// cx.tcx, "source data is only valid for ", r, "");
|
|
|
|
// }
|
|
|
|
},
|
|
|
|
|
|
|
|
|ty| {
|
|
|
|
match ty::get(ty).sty {
|
|
|
|
ty::ty_param(source_param) => {
|
2013-07-04 21:13:26 -05:00
|
|
|
if target_params.iter().any(|x| x == &source_param) {
|
2013-04-02 00:32:37 -05:00
|
|
|
/* case (2) */
|
|
|
|
} else {
|
|
|
|
check_durable(cx.tcx, ty, source.span); /* case (3) */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
2012-07-18 13:01:54 -05:00
|
|
|
}
|
2013-04-02 00:32:37 -05:00
|
|
|
true
|
|
|
|
});
|
|
|
|
|
2013-04-17 11:15:37 -05:00
|
|
|
fn is_re_scope(r: ty::Region) -> bool {
|
2013-04-02 00:32:37 -05:00
|
|
|
match r {
|
|
|
|
ty::re_scope(*) => true,
|
|
|
|
_ => false
|
2012-07-18 13:01:54 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-02 00:32:37 -05:00
|
|
|
|
|
|
|
fn is_subregion_of(cx: Context, r_sub: ty::Region, r_sup: ty::Region) -> bool {
|
|
|
|
cx.tcx.region_maps.is_subregion_of(r_sub, r_sup)
|
|
|
|
}
|
2012-07-18 13:01:54 -05:00
|
|
|
}
|