2014-02-10 08:36:31 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/*!
|
|
|
|
* # Categorization
|
|
|
|
*
|
|
|
|
* The job of the categorization module is to analyze an expression to
|
|
|
|
* determine what kind of memory is used in evaluating it (for example,
|
|
|
|
* where dereferences occur and what kind of pointer is dereferenced;
|
|
|
|
* whether the memory is mutable; etc)
|
|
|
|
*
|
|
|
|
* Categorization effectively transforms all of our expressions into
|
|
|
|
* expressions of the following forms (the actual enum has many more
|
|
|
|
* possibilities, naturally, but they are all variants of these base
|
|
|
|
* forms):
|
|
|
|
*
|
|
|
|
* E = rvalue // some computed rvalue
|
2014-02-07 13:41:58 -06:00
|
|
|
* | x // address of a local variable or argument
|
2012-07-04 16:53:12 -05:00
|
|
|
* | *E // deref of a ptr
|
|
|
|
* | E.comp // access to an interior component
|
|
|
|
*
|
|
|
|
* Imagine a routine ToAddr(Expr) that evaluates an expression and returns an
|
|
|
|
* address where the result is to be found. If Expr is an lvalue, then this
|
|
|
|
* is the address of the lvalue. If Expr is an rvalue, this is the address of
|
|
|
|
* some temporary spot in memory where the result is stored.
|
|
|
|
*
|
2014-06-08 23:00:52 -05:00
|
|
|
* Now, cat_expr() classifies the expression Expr and the address A=ToAddr(Expr)
|
2012-07-04 16:53:12 -05:00
|
|
|
* as follows:
|
|
|
|
*
|
|
|
|
* - cat: what kind of expression was this? This is a subset of the
|
|
|
|
* full expression forms which only includes those that we care about
|
|
|
|
* for the purpose of the analysis.
|
|
|
|
* - mutbl: mutability of the address A
|
|
|
|
* - ty: the type of data found at the address A
|
|
|
|
*
|
|
|
|
* The resulting categorization tree differs somewhat from the expressions
|
|
|
|
* themselves. For example, auto-derefs are explicit. Also, an index a[b] is
|
2014-06-08 23:00:52 -05:00
|
|
|
* decomposed into two operations: a dereference to reach the array data and
|
2012-07-04 16:53:12 -05:00
|
|
|
* then an index to jump forward to the relevant item.
|
2014-02-07 13:41:58 -06:00
|
|
|
*
|
|
|
|
* ## By-reference upvars
|
|
|
|
*
|
|
|
|
* One part of the translation which may be non-obvious is that we translate
|
2014-02-11 15:55:03 -06:00
|
|
|
* closure upvars into the dereference of a borrowed pointer; this more closely
|
2014-02-07 13:41:58 -06:00
|
|
|
* resembles the runtime translation. So, for example, if we had:
|
|
|
|
*
|
|
|
|
* let mut x = 3;
|
|
|
|
* let y = 5;
|
|
|
|
* let inc = || x += y;
|
|
|
|
*
|
|
|
|
* Then when we categorize `x` (*within* the closure) we would yield a
|
|
|
|
* result of `*x'`, effectively, where `x'` is a `cat_upvar` reference
|
|
|
|
* tied to `x`. The type of `x'` will be a borrowed pointer.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2014-03-21 20:05:05 -05:00
|
|
|
#![allow(non_camel_case_types)]
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2014-05-14 14:31:30 -05:00
|
|
|
use middle::def;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::ty;
|
2014-03-06 11:24:11 -06:00
|
|
|
use middle::typeck;
|
2014-07-30 00:08:39 -05:00
|
|
|
use util::nodemap::{DefIdMap, NodeMap};
|
2014-06-21 05:39:03 -05:00
|
|
|
use util::ppaux::{ty_to_string, Repr};
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
use syntax::ast::{MutImmutable, MutMutable};
|
2012-12-23 16:41:37 -06:00
|
|
|
use syntax::ast;
|
2014-09-17 09:28:19 -05:00
|
|
|
use syntax::ast_map;
|
2013-08-31 11:13:04 -05:00
|
|
|
use syntax::codemap::Span;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::print::pprust;
|
2013-07-10 15:44:58 -05:00
|
|
|
use syntax::parse::token;
|
2012-08-08 10:15:32 -05:00
|
|
|
|
2014-04-10 08:26:26 -05:00
|
|
|
use std::cell::RefCell;
|
2014-04-15 05:15:56 -05:00
|
|
|
use std::rc::Rc;
|
2014-04-10 08:26:26 -05:00
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
#[deriving(Clone, PartialEq)]
|
2013-01-30 15:44:24 -06:00
|
|
|
pub enum categorization {
|
2014-01-15 13:39:08 -06:00
|
|
|
cat_rvalue(ty::Region), // temporary val, argument is its scope
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_static_item,
|
2014-04-09 08:29:29 -05:00
|
|
|
cat_copied_upvar(CopiedUpvar), // upvar copied into proc env
|
2014-10-04 16:06:08 -05:00
|
|
|
cat_upvar(ty::UpvarId, ty::UpvarBorrow,
|
|
|
|
Option<ty::UnboxedClosureKind>), // by ref upvar from stack or unboxed closure
|
2013-08-20 16:37:49 -05:00
|
|
|
cat_local(ast::NodeId), // local variable
|
|
|
|
cat_deref(cmt, uint, PointerKind), // deref of a ptr
|
2013-05-17 20:12:50 -05:00
|
|
|
cat_interior(cmt, InteriorKind), // something interior: field, tuple, etc
|
2014-02-07 13:41:58 -06:00
|
|
|
cat_downcast(cmt), // selects a particular enum variant (*1)
|
2013-08-20 16:37:49 -05:00
|
|
|
cat_discr(cmt, ast::NodeId), // match discriminant (see preserve())
|
2013-05-17 20:12:50 -05:00
|
|
|
|
2014-02-07 13:41:58 -06:00
|
|
|
// (*1) downcast is only required if the enum has more than one variant
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
|
2014-10-04 16:04:10 -05:00
|
|
|
#[deriving(Clone, PartialEq)]
|
|
|
|
pub enum CopiedUpvarKind {
|
|
|
|
Boxed(ast::Onceness),
|
|
|
|
Unboxed(ty::UnboxedClosureKind)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CopiedUpvarKind {
|
|
|
|
pub fn onceness(&self) -> ast::Onceness {
|
|
|
|
match *self {
|
|
|
|
Boxed(onceness) => onceness,
|
|
|
|
Unboxed(ty::FnUnboxedClosureKind) |
|
|
|
|
Unboxed(ty::FnMutUnboxedClosureKind) => ast::Many,
|
|
|
|
Unboxed(ty::FnOnceUnboxedClosureKind) => ast::Once
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
#[deriving(Clone, PartialEq)]
|
2013-06-17 18:06:36 -05:00
|
|
|
pub struct CopiedUpvar {
|
2014-03-28 12:05:27 -05:00
|
|
|
pub upvar_id: ast::NodeId,
|
2014-10-04 16:04:10 -05:00
|
|
|
pub kind: CopiedUpvarKind,
|
Ensure dataflow of a proc never looks at blocks from closed-over context.
Details: in a program like:
```
type T = proc(int) -> int; /* 4 */
pub fn outer(captured /* pat 16 */: T) -> T {
(proc(x /* pat 23 */) {
((captured /* 29 */).foo((x /* 30 */)) /* 28 */)
} /* block 27 */ /* 20 */)
} /* block 19 */ /* 12 */
```
the `captured` arg is moved from the outer fn into the inner proc (id=20).
The old dataflow analysis for flowed_move_data_moves, when looking at
the inner proc, would attempt to add a kill bit for `captured` at the
end of its scope; the problem is that it thought the end of the
`captured` arg's scope was the outer fn (id=12), even though at that
point in the analysis, the `captured` arg's scope should now be
restricted to the proc itself (id=20).
This patch fixes handling of upvars so that dataflow of a fn/proc
should never attempts to add a gen or kill bit to any NodeId outside
of the current fn/proc. It accomplishes this by adding an `LpUpvar`
variant to `borrowck::LoanPath`, so for cases like `captured` above
will carry both their original `var_id`, as before, as well as the
`NodeId` for the closure that is capturing them.
As a drive-by fix to another occurrence of a similar bug that
nikomatsakis pointed out to me earlier, this also fixes
`gather_loans::compute_kill_scope` so that it computes the kill scope
of the `captured` arg to be block 27; that is, the block for the proc
itself (id=20).
(This is an updated version that generalizes the new loan path variant
to cover all upvars, and thus renamed the variant from `LpCopiedUpvar`
to just `LpUpvar`.)
2014-06-13 10:19:29 -05:00
|
|
|
pub capturing_proc: ast::NodeId,
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// different kinds of pointers:
|
2014-05-31 12:43:52 -05:00
|
|
|
#[deriving(Clone, PartialEq, Eq, Hash)]
|
2013-08-20 16:37:49 -05:00
|
|
|
pub enum PointerKind {
|
2014-02-07 13:41:58 -06:00
|
|
|
OwnedPtr,
|
|
|
|
BorrowedPtr(ty::BorrowKind, ty::Region),
|
2014-07-14 02:43:21 -05:00
|
|
|
Implicit(ty::BorrowKind, ty::Region), // Implicit deref of a borrowed ptr.
|
|
|
|
UnsafePtr(ast::Mutability)
|
2012-08-27 18:26:35 -05:00
|
|
|
}
|
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
// We use the term "interior" to mean "something reachable from the
|
|
|
|
// base without a pointer dereference", e.g. a field
|
2014-05-31 12:43:52 -05:00
|
|
|
#[deriving(Clone, PartialEq, Eq, Hash)]
|
2013-05-17 20:12:50 -05:00
|
|
|
pub enum InteriorKind {
|
|
|
|
InteriorField(FieldName),
|
2013-05-22 05:54:35 -05:00
|
|
|
InteriorElement(ElementKind),
|
2013-05-17 20:12:50 -05:00
|
|
|
}
|
|
|
|
|
2014-05-31 12:43:52 -05:00
|
|
|
#[deriving(Clone, PartialEq, Eq, Hash)]
|
2013-05-17 20:12:50 -05:00
|
|
|
pub enum FieldName {
|
2013-07-10 15:44:58 -05:00
|
|
|
NamedField(ast::Name),
|
2013-05-17 20:12:50 -05:00
|
|
|
PositionalField(uint)
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2014-05-31 12:43:52 -05:00
|
|
|
#[deriving(Clone, PartialEq, Eq, Hash)]
|
2013-05-22 05:54:35 -05:00
|
|
|
pub enum ElementKind {
|
|
|
|
VecElement,
|
|
|
|
OtherElement,
|
|
|
|
}
|
|
|
|
|
2014-05-31 12:43:52 -05:00
|
|
|
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
|
2013-02-09 00:21:45 -06:00
|
|
|
pub enum MutabilityCategory {
|
|
|
|
McImmutable, // Immutable.
|
|
|
|
McDeclared, // Directly declared as mutable.
|
2014-02-07 13:41:58 -06:00
|
|
|
McInherited, // Inherited from the fact that owner is mutable.
|
2013-02-09 00:21:45 -06:00
|
|
|
}
|
|
|
|
|
2013-05-03 11:11:15 -05:00
|
|
|
// `cmt`: "Category, Mutability, and Type".
|
|
|
|
//
|
2012-08-08 10:15:32 -05:00
|
|
|
// a complete categorization of a value indicating where it originated
|
|
|
|
// and how it is located, as well as the mutability of the memory in
|
|
|
|
// which the value is stored.
|
2013-01-24 18:24:45 -06:00
|
|
|
//
|
2013-05-03 11:11:15 -05:00
|
|
|
// *WARNING* The field `cmt.type` is NOT necessarily the same as the
|
|
|
|
// result of `node_id_to_type(cmt.id)`. This is because the `id` is
|
|
|
|
// always the `id` of the node producing the type; in an expression
|
|
|
|
// like `*x`, the type of this deref node is the deref'd type (`T`),
|
|
|
|
// but in a pattern like `@x`, the `@x` pattern is again a
|
|
|
|
// dereference, but its type is the type *before* the dereference
|
2014-09-01 22:55:07 -05:00
|
|
|
// (`@T`). So use `cmt.ty` to find the type of the value in a consistent
|
2013-05-03 11:11:15 -05:00
|
|
|
// fashion. For more details, see the method `cat_pattern`
|
2014-05-29 19:45:07 -05:00
|
|
|
#[deriving(Clone, PartialEq)]
|
2013-01-30 15:44:24 -06:00
|
|
|
pub struct cmt_ {
|
2014-03-28 12:05:27 -05:00
|
|
|
pub id: ast::NodeId, // id of expr/pat producing this value
|
|
|
|
pub span: Span, // span of same expr/pat
|
|
|
|
pub cat: categorization, // categorization of expr
|
|
|
|
pub mutbl: MutabilityCategory, // mutability of expr as lvalue
|
|
|
|
pub ty: ty::t // type of the expr (*see WARNING above*)
|
2013-01-25 18:57:39 -06:00
|
|
|
}
|
2012-08-08 10:15:32 -05:00
|
|
|
|
2014-04-15 05:15:56 -05:00
|
|
|
pub type cmt = Rc<cmt_>;
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-08-08 10:15:32 -05:00
|
|
|
// We pun on *T to mean both actual deref of a ptr as well
|
|
|
|
// as accessing of components:
|
2013-05-17 20:12:50 -05:00
|
|
|
pub enum deref_kind {
|
2013-08-20 16:37:49 -05:00
|
|
|
deref_ptr(PointerKind),
|
2013-05-17 20:12:50 -05:00
|
|
|
deref_interior(InteriorKind),
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
|
|
|
|
// Categorizes a derefable type. Note that we include vectors and strings as
|
|
|
|
// derefable (we model an index as the combination of a deref and then a
|
|
|
|
// pointer adjustment).
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(t).sty {
|
2013-08-11 12:56:30 -05:00
|
|
|
ty::ty_uniq(_) |
|
2014-05-05 20:56:44 -05:00
|
|
|
ty::ty_closure(box ty::ClosureTy {store: ty::UniqTraitStore, ..}) => {
|
2014-02-07 13:41:58 -06:00
|
|
|
Some(deref_ptr(OwnedPtr))
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2014-04-11 01:01:31 -05:00
|
|
|
ty::ty_rptr(r, mt) => {
|
2014-02-07 13:41:58 -06:00
|
|
|
let kind = ty::BorrowKind::from_mutbl(mt.mutbl);
|
|
|
|
Some(deref_ptr(BorrowedPtr(kind, r)))
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
2013-08-11 12:56:30 -05:00
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
ty::ty_closure(box ty::ClosureTy {
|
|
|
|
store: ty::RegionTraitStore(r, _),
|
|
|
|
..
|
|
|
|
}) => {
|
2014-02-07 13:41:58 -06:00
|
|
|
Some(deref_ptr(BorrowedPtr(ty::ImmBorrow, r)))
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
2013-01-11 23:01:42 -06:00
|
|
|
|
2013-08-11 12:56:30 -05:00
|
|
|
ty::ty_ptr(ref mt) => {
|
2014-02-07 13:41:58 -06:00
|
|
|
Some(deref_ptr(UnsafePtr(mt.mutbl)))
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2013-11-28 14:22:53 -06:00
|
|
|
ty::ty_enum(..) |
|
|
|
|
ty::ty_struct(..) => { // newtype
|
2013-05-17 20:12:50 -05:00
|
|
|
Some(deref_interior(InteriorField(PositionalField(0))))
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
2012-06-04 18:21:14 -05:00
|
|
|
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
ty::ty_vec(_, _) | ty::ty_str => {
|
2013-05-22 05:54:35 -05:00
|
|
|
Some(deref_interior(InteriorElement(element_kind(t))))
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
2012-06-04 18:21:14 -05:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
_ => None
|
2012-06-01 17:46:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-05 21:07:47 -06:00
|
|
|
pub fn deref_kind(tcx: &ty::ctxt, t: ty::t) -> deref_kind {
|
2014-08-06 04:59:40 -05:00
|
|
|
debug!("deref_kind {}", ty_to_string(tcx, t));
|
2012-08-06 14:34:08 -05:00
|
|
|
match opt_deref_kind(t) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(k) => k,
|
|
|
|
None => {
|
2012-06-01 12:46:17 -05:00
|
|
|
tcx.sess.bug(
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
format!("deref_kind() invoked on non-derefable type {}",
|
2014-06-21 05:39:03 -05:00
|
|
|
ty_to_string(tcx, t)).as_slice());
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-19 14:30:07 -05:00
|
|
|
pub trait ast_node {
|
2013-07-27 03:25:59 -05:00
|
|
|
fn id(&self) -> ast::NodeId;
|
2013-08-31 11:13:04 -05:00
|
|
|
fn span(&self) -> Span;
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2014-01-06 06:00:46 -06:00
|
|
|
impl ast_node for ast::Expr {
|
2013-07-27 03:25:59 -05:00
|
|
|
fn id(&self) -> ast::NodeId { self.id }
|
2013-08-31 11:13:04 -05:00
|
|
|
fn span(&self) -> Span { self.span }
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2014-01-06 06:00:46 -06:00
|
|
|
impl ast_node for ast::Pat {
|
2013-07-27 03:25:59 -05:00
|
|
|
fn id(&self) -> ast::NodeId { self.id }
|
2013-08-31 11:13:04 -05:00
|
|
|
fn span(&self) -> Span { self.span }
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2014-08-27 20:46:52 -05:00
|
|
|
pub struct MemCategorizationContext<'t,TYPER:'t> {
|
|
|
|
typer: &'t TYPER
|
|
|
|
}
|
|
|
|
|
2014-02-07 13:41:58 -06:00
|
|
|
pub type McResult<T> = Result<T, ()>;
|
|
|
|
|
|
|
|
/**
|
2014-02-11 15:55:03 -06:00
|
|
|
* The `Typer` trait provides the interface for the mem-categorization
|
2014-02-07 13:41:58 -06:00
|
|
|
* module to the results of the type check. It can be used to query
|
|
|
|
* the type assigned to an expression node, to inquire after adjustments,
|
|
|
|
* and so on.
|
|
|
|
*
|
|
|
|
* This interface is needed because mem-categorization is used from
|
|
|
|
* two places: `regionck` and `borrowck`. `regionck` executes before
|
|
|
|
* type inference is complete, and hence derives types and so on from
|
|
|
|
* intermediate tables. This also implies that type errors can occur,
|
|
|
|
* and hence `node_ty()` and friends return a `Result` type -- any
|
|
|
|
* error will propagate back up through the mem-categorization
|
|
|
|
* routines.
|
|
|
|
*
|
|
|
|
* In the borrow checker, in contrast, type checking is complete and we
|
|
|
|
* know that no errors have occurred, so we simply consult the tcx and we
|
|
|
|
* can be sure that only `Ok` results will occur.
|
|
|
|
*/
|
2014-04-22 07:56:37 -05:00
|
|
|
pub trait Typer<'tcx> {
|
|
|
|
fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx>;
|
2014-04-10 08:43:26 -05:00
|
|
|
fn node_ty(&self, id: ast::NodeId) -> McResult<ty::t>;
|
2014-03-05 21:07:47 -06:00
|
|
|
fn node_method_ty(&self, method_call: typeck::MethodCall) -> Option<ty::t>;
|
2014-04-10 08:26:26 -05:00
|
|
|
fn adjustments<'a>(&'a self) -> &'a RefCell<NodeMap<ty::AutoAdjustment>>;
|
2014-04-10 08:43:26 -05:00
|
|
|
fn is_method_call(&self, id: ast::NodeId) -> bool;
|
|
|
|
fn temporary_scope(&self, rvalue_id: ast::NodeId) -> Option<ast::NodeId>;
|
|
|
|
fn upvar_borrow(&self, upvar_id: ty::UpvarId) -> ty::UpvarBorrow;
|
2014-07-23 14:43:29 -05:00
|
|
|
fn capture_mode(&self, closure_expr_id: ast::NodeId)
|
2014-09-14 15:55:25 -05:00
|
|
|
-> ast::CaptureClause;
|
2014-07-30 00:08:39 -05:00
|
|
|
fn unboxed_closures<'a>(&'a self)
|
|
|
|
-> &'a RefCell<DefIdMap<ty::UnboxedClosure>>;
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl MutabilityCategory {
|
2013-09-01 20:45:37 -05:00
|
|
|
pub fn from_mutbl(m: ast::Mutability) -> MutabilityCategory {
|
2013-02-09 00:21:45 -06:00
|
|
|
match m {
|
2013-09-01 20:45:37 -05:00
|
|
|
MutImmutable => McImmutable,
|
|
|
|
MutMutable => McDeclared
|
2013-02-09 00:21:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 13:41:58 -06:00
|
|
|
pub fn from_borrow_kind(borrow_kind: ty::BorrowKind) -> MutabilityCategory {
|
|
|
|
match borrow_kind {
|
|
|
|
ty::ImmBorrow => McImmutable,
|
|
|
|
ty::UniqueImmBorrow => McImmutable,
|
|
|
|
ty::MutBorrow => McDeclared,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_pointer_kind(base_mutbl: MutabilityCategory,
|
|
|
|
ptr: PointerKind) -> MutabilityCategory {
|
|
|
|
match ptr {
|
|
|
|
OwnedPtr => {
|
|
|
|
base_mutbl.inherit()
|
|
|
|
}
|
2014-07-14 02:43:21 -05:00
|
|
|
BorrowedPtr(borrow_kind, _) | Implicit(borrow_kind, _) => {
|
2014-02-07 13:41:58 -06:00
|
|
|
MutabilityCategory::from_borrow_kind(borrow_kind)
|
|
|
|
}
|
|
|
|
UnsafePtr(m) => {
|
|
|
|
MutabilityCategory::from_mutbl(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-17 10:17:09 -05:00
|
|
|
fn from_local(tcx: &ty::ctxt, id: ast::NodeId) -> MutabilityCategory {
|
|
|
|
match tcx.map.get(id) {
|
|
|
|
ast_map::NodeLocal(p) | ast_map::NodeArg(p) => match p.node {
|
|
|
|
ast::PatIdent(bind_mode, _, _) => {
|
|
|
|
if bind_mode == ast::BindByValue(ast::MutMutable) {
|
|
|
|
McDeclared
|
|
|
|
} else {
|
|
|
|
McImmutable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => tcx.sess.span_bug(p.span, "expected identifier pattern")
|
2014-08-04 15:10:08 -05:00
|
|
|
},
|
2014-09-17 10:17:09 -05:00
|
|
|
_ => tcx.sess.span_bug(tcx.map.span(id), "expected identifier pattern")
|
2014-08-04 15:10:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn inherit(&self) -> MutabilityCategory {
|
2013-02-09 00:21:45 -06:00
|
|
|
match *self {
|
|
|
|
McImmutable => McImmutable,
|
|
|
|
McDeclared => McInherited,
|
2014-02-07 13:41:58 -06:00
|
|
|
McInherited => McInherited,
|
2013-02-09 00:21:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn is_mutable(&self) -> bool {
|
2013-02-09 00:21:45 -06:00
|
|
|
match *self {
|
2013-08-02 23:41:06 -05:00
|
|
|
McImmutable => false,
|
2014-02-07 13:41:58 -06:00
|
|
|
McInherited => true,
|
|
|
|
McDeclared => true,
|
2013-02-09 00:21:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn is_immutable(&self) -> bool {
|
2013-02-09 00:21:45 -06:00
|
|
|
match *self {
|
|
|
|
McImmutable => true,
|
2013-08-02 23:41:06 -05:00
|
|
|
McDeclared | McInherited => false
|
2013-02-09 00:21:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_user_str(&self) -> &'static str {
|
2013-02-09 00:21:45 -06:00
|
|
|
match *self {
|
2013-03-15 14:24:24 -05:00
|
|
|
McDeclared | McInherited => "mutable",
|
|
|
|
McImmutable => "immutable",
|
2013-04-12 00:09:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 13:41:58 -06:00
|
|
|
macro_rules! if_ok(
|
|
|
|
($inp: expr) => (
|
|
|
|
match $inp {
|
|
|
|
Ok(v) => { v }
|
|
|
|
Err(e) => { return Err(e); }
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
|
2014-04-21 18:21:53 -05:00
|
|
|
pub fn new(typer: &'t TYPER) -> MemCategorizationContext<'t,TYPER> {
|
|
|
|
MemCategorizationContext { typer: typer }
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
fn tcx(&self) -> &'t ty::ctxt<'tcx> {
|
2014-02-07 13:41:58 -06:00
|
|
|
self.typer.tcx()
|
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
fn expr_ty(&self, expr: &ast::Expr) -> McResult<ty::t> {
|
2014-02-07 13:41:58 -06:00
|
|
|
self.typer.node_ty(expr.id)
|
2013-05-03 11:11:15 -05:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
fn expr_ty_adjusted(&self, expr: &ast::Expr) -> McResult<ty::t> {
|
2014-02-07 13:41:58 -06:00
|
|
|
let unadjusted_ty = if_ok!(self.expr_ty(expr));
|
2014-04-10 08:26:26 -05:00
|
|
|
Ok(ty::adjust_ty(self.tcx(), expr.span, expr.id, unadjusted_ty,
|
|
|
|
self.typer.adjustments().borrow().find(&expr.id),
|
2014-03-06 11:24:11 -06:00
|
|
|
|method_call| self.typer.node_method_ty(method_call)))
|
2013-05-03 11:11:15 -05:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
fn node_ty(&self, id: ast::NodeId) -> McResult<ty::t> {
|
2014-02-07 13:41:58 -06:00
|
|
|
self.typer.node_ty(id)
|
|
|
|
}
|
|
|
|
|
2014-04-15 05:15:56 -05:00
|
|
|
fn pat_ty(&self, pat: &ast::Pat) -> McResult<ty::t> {
|
2014-02-07 13:41:58 -06:00
|
|
|
self.typer.node_ty(pat.id)
|
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
pub fn cat_expr(&self, expr: &ast::Expr) -> McResult<cmt> {
|
2014-04-10 08:26:26 -05:00
|
|
|
match self.typer.adjustments().borrow().find(&expr.id) {
|
2012-09-11 23:25:01 -05:00
|
|
|
None => {
|
|
|
|
// No adjustments.
|
|
|
|
self.cat_expr_unadjusted(expr)
|
|
|
|
}
|
|
|
|
|
2014-01-03 17:08:48 -06:00
|
|
|
Some(adjustment) => {
|
2014-02-07 13:41:58 -06:00
|
|
|
match *adjustment {
|
2014-09-11 00:07:49 -05:00
|
|
|
ty::AdjustAddEnv(..) => {
|
2014-09-23 18:07:21 -05:00
|
|
|
debug!("cat_expr(AdjustAddEnv): {}",
|
|
|
|
expr.repr(self.tcx()));
|
2014-01-03 17:08:48 -06:00
|
|
|
// Convert a bare fn to a closure by adding NULL env.
|
|
|
|
// Result is an rvalue.
|
2014-02-07 13:41:58 -06:00
|
|
|
let expr_ty = if_ok!(self.expr_ty_adjusted(expr));
|
|
|
|
Ok(self.cat_rvalue_node(expr.id(), expr.span(), expr_ty))
|
2014-01-03 17:08:48 -06:00
|
|
|
}
|
2013-02-27 18:28:37 -06:00
|
|
|
|
2014-09-11 00:07:49 -05:00
|
|
|
ty::AdjustDerefRef(
|
2014-02-07 13:41:58 -06:00
|
|
|
ty::AutoDerefRef {
|
|
|
|
autoref: Some(_), ..}) => {
|
2014-09-23 18:07:21 -05:00
|
|
|
debug!("cat_expr(AdjustDerefRef): {}",
|
|
|
|
expr.repr(self.tcx()));
|
2014-01-03 17:08:48 -06:00
|
|
|
// Equivalent to &*expr or something similar.
|
|
|
|
// Result is an rvalue.
|
2014-02-07 13:41:58 -06:00
|
|
|
let expr_ty = if_ok!(self.expr_ty_adjusted(expr));
|
|
|
|
Ok(self.cat_rvalue_node(expr.id(), expr.span(), expr_ty))
|
2014-01-03 17:08:48 -06:00
|
|
|
}
|
2013-02-27 18:28:37 -06:00
|
|
|
|
2014-09-11 00:07:49 -05:00
|
|
|
ty::AdjustDerefRef(
|
2014-02-07 13:41:58 -06:00
|
|
|
ty::AutoDerefRef {
|
|
|
|
autoref: None, autoderefs: autoderefs}) => {
|
2014-01-03 17:08:48 -06:00
|
|
|
// Equivalent to *expr or something similar.
|
|
|
|
self.cat_expr_autoderefd(expr, autoderefs)
|
|
|
|
}
|
|
|
|
}
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-20 18:37:45 -05:00
|
|
|
|
2014-04-21 18:21:53 -05:00
|
|
|
pub fn cat_expr_autoderefd(&self,
|
|
|
|
expr: &ast::Expr,
|
|
|
|
autoderefs: uint)
|
2014-02-07 13:41:58 -06:00
|
|
|
-> McResult<cmt> {
|
|
|
|
let mut cmt = if_ok!(self.cat_expr_unadjusted(expr));
|
2014-09-23 18:07:21 -05:00
|
|
|
debug!("cat_expr_autoderefd: autoderefs={}, cmt={}",
|
|
|
|
autoderefs,
|
|
|
|
cmt.repr(self.tcx()));
|
2013-08-03 11:45:23 -05:00
|
|
|
for deref in range(1u, autoderefs + 1) {
|
2014-07-14 02:43:21 -05:00
|
|
|
cmt = self.cat_deref(expr, cmt, deref, false);
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
2014-02-07 13:41:58 -06:00
|
|
|
return Ok(cmt);
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
pub fn cat_expr_unadjusted(&self, expr: &ast::Expr) -> McResult<cmt> {
|
2014-02-13 23:07:09 -06:00
|
|
|
debug!("cat_expr: id={} expr={}", expr.id, expr.repr(self.tcx()));
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2014-02-07 13:41:58 -06:00
|
|
|
let expr_ty = if_ok!(self.expr_ty(expr));
|
2012-08-06 14:34:08 -05:00
|
|
|
match expr.node {
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::ExprUnary(ast::UnDeref, ref e_base) => {
|
|
|
|
let base_cmt = if_ok!(self.cat_expr(&**e_base));
|
2014-07-14 02:43:21 -05:00
|
|
|
Ok(self.cat_deref(expr, base_cmt, 0, false))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::ExprField(ref base, f_name, _) => {
|
|
|
|
let base_cmt = if_ok!(self.cat_expr(&**base));
|
2014-09-23 18:07:21 -05:00
|
|
|
debug!("cat_expr(cat_field): id={} expr={} base={}",
|
|
|
|
expr.id,
|
|
|
|
expr.repr(self.tcx()),
|
|
|
|
base_cmt.repr(self.tcx()));
|
2014-06-13 16:56:42 -05:00
|
|
|
Ok(self.cat_field(expr, base_cmt, f_name.node, expr_ty))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2014-08-09 22:54:33 -05:00
|
|
|
ast::ExprTupField(ref base, idx, _) => {
|
|
|
|
let base_cmt = if_ok!(self.cat_expr(&**base));
|
|
|
|
Ok(self.cat_tup_field(expr, base_cmt, idx.node, expr_ty))
|
|
|
|
}
|
|
|
|
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::ExprIndex(ref base, _) => {
|
2014-07-14 02:43:21 -05:00
|
|
|
let method_call = typeck::MethodCall::expr(expr.id());
|
|
|
|
match self.typer.node_method_ty(method_call) {
|
|
|
|
Some(method_ty) => {
|
|
|
|
// If this is an index implemented by a method call, then it will
|
|
|
|
// include an implicit deref of the result.
|
|
|
|
let ret_ty = ty::ty_fn_ret(method_ty);
|
|
|
|
Ok(self.cat_deref(expr,
|
|
|
|
self.cat_rvalue_node(expr.id(),
|
|
|
|
expr.span(),
|
|
|
|
ret_ty), 1, true))
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let base_cmt = if_ok!(self.cat_expr(&**base));
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
Ok(self.cat_index(expr, base_cmt))
|
2014-07-14 02:43:21 -05:00
|
|
|
}
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2014-01-27 06:18:36 -06:00
|
|
|
ast::ExprPath(_) => {
|
2014-09-07 12:09:06 -05:00
|
|
|
let def = *self.tcx().def_map.borrow().get(&expr.id);
|
2012-06-01 12:46:17 -05:00
|
|
|
self.cat_def(expr.id, expr.span, expr_ty, def)
|
|
|
|
}
|
|
|
|
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::ExprParen(ref e) => {
|
|
|
|
self.cat_expr(&**e)
|
2014-04-21 18:21:53 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::ExprAddrOf(..) | ast::ExprCall(..) |
|
|
|
|
ast::ExprAssign(..) | ast::ExprAssignOp(..) |
|
2014-05-29 00:26:56 -05:00
|
|
|
ast::ExprFnBlock(..) | ast::ExprProc(..) |
|
|
|
|
ast::ExprUnboxedFn(..) | ast::ExprRet(..) |
|
2014-09-15 03:48:58 -05:00
|
|
|
ast::ExprUnary(..) | ast::ExprSlice(..) |
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
ast::ExprMethodCall(..) | ast::ExprCast(..) |
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::ExprVec(..) | ast::ExprTup(..) | ast::ExprIf(..) |
|
2014-03-09 00:36:10 -06:00
|
|
|
ast::ExprBinary(..) | ast::ExprWhile(..) |
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::ExprBlock(..) | ast::ExprLoop(..) | ast::ExprMatch(..) |
|
|
|
|
ast::ExprLit(..) | ast::ExprBreak(..) | ast::ExprMac(..) |
|
|
|
|
ast::ExprAgain(..) | ast::ExprStruct(..) | ast::ExprRepeat(..) |
|
2014-07-21 22:54:28 -05:00
|
|
|
ast::ExprInlineAsm(..) | ast::ExprBox(..) |
|
|
|
|
ast::ExprForLoop(..) => {
|
2014-02-07 13:41:58 -06:00
|
|
|
Ok(self.cat_rvalue_node(expr.id(), expr.span(), expr_ty))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
2014-08-24 21:08:48 -05:00
|
|
|
|
2014-08-27 23:34:03 -05:00
|
|
|
ast::ExprIfLet(..) => {
|
|
|
|
self.tcx().sess.span_bug(expr.span, "non-desugared ExprIfLet");
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
pub fn cat_def(&self,
|
2013-07-27 03:25:59 -05:00
|
|
|
id: ast::NodeId,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-05-31 17:17:22 -05:00
|
|
|
expr_ty: ty::t,
|
2014-05-14 14:31:30 -05:00
|
|
|
def: def::Def)
|
2014-02-07 13:41:58 -06:00
|
|
|
-> McResult<cmt> {
|
2014-04-09 02:15:31 -05:00
|
|
|
debug!("cat_def: id={} expr={} def={:?}",
|
|
|
|
id, expr_ty.repr(self.tcx()), def);
|
2014-01-24 13:48:10 -06:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match def {
|
2014-07-24 17:27:01 -05:00
|
|
|
def::DefStruct(..) | def::DefVariant(..) | def::DefFn(..) |
|
|
|
|
def::DefStaticMethod(..) => {
|
2014-02-07 13:41:58 -06:00
|
|
|
Ok(self.cat_rvalue_node(id, span, expr_ty))
|
2014-01-24 13:48:10 -06:00
|
|
|
}
|
2014-08-04 15:10:08 -05:00
|
|
|
def::DefMod(_) | def::DefForeignMod(_) | def::DefUse(_) |
|
2014-09-15 16:13:00 -05:00
|
|
|
def::DefTrait(_) | def::DefTy(..) | def::DefPrimTy(_) |
|
2014-05-14 14:31:30 -05:00
|
|
|
def::DefTyParam(..) | def::DefTyParamBinder(..) | def::DefRegion(_) |
|
2014-08-05 21:44:21 -05:00
|
|
|
def::DefLabel(_) | def::DefSelfTy(..) | def::DefMethod(..) |
|
|
|
|
def::DefAssociatedTy(..) => {
|
2014-04-15 05:15:56 -05:00
|
|
|
Ok(Rc::new(cmt_ {
|
2013-06-21 20:46:34 -05:00
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_static_item,
|
|
|
|
mutbl: McImmutable,
|
|
|
|
ty:expr_ty
|
2014-04-15 05:15:56 -05:00
|
|
|
}))
|
2013-06-21 20:46:34 -05:00
|
|
|
}
|
|
|
|
|
2014-09-17 10:17:09 -05:00
|
|
|
def::DefStatic(_, mutbl) => {
|
2014-04-15 05:15:56 -05:00
|
|
|
Ok(Rc::new(cmt_ {
|
2013-06-21 20:46:34 -05:00
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_static_item,
|
2014-09-17 10:17:09 -05:00
|
|
|
mutbl: if mutbl { McDeclared } else { McImmutable},
|
2013-06-21 20:46:34 -05:00
|
|
|
ty:expr_ty
|
2014-04-15 05:15:56 -05:00
|
|
|
}))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2014-09-14 17:22:50 -05:00
|
|
|
def::DefUpvar(var_id, fn_node_id, _) => {
|
2014-02-07 13:41:58 -06:00
|
|
|
let ty = if_ok!(self.node_ty(fn_node_id));
|
2013-03-15 14:24:24 -05:00
|
|
|
match ty::get(ty).sty {
|
|
|
|
ty::ty_closure(ref closure_ty) => {
|
2013-06-19 14:53:05 -05:00
|
|
|
// Decide whether to use implicit reference or by copy/move
|
|
|
|
// capture for the upvar. This, combined with the onceness,
|
|
|
|
// determines whether the closure can move out of it.
|
2014-04-11 10:03:10 -05:00
|
|
|
let var_is_refd = match (closure_ty.store, closure_ty.onceness) {
|
2013-06-19 14:53:05 -05:00
|
|
|
// Many-shot stack closures can never move out.
|
2014-04-11 10:03:10 -05:00
|
|
|
(ty::RegionTraitStore(..), ast::Many) => true,
|
2013-10-15 00:21:54 -05:00
|
|
|
// 1-shot stack closures can move out.
|
2014-04-11 10:03:10 -05:00
|
|
|
(ty::RegionTraitStore(..), ast::Once) => false,
|
2013-06-19 14:53:05 -05:00
|
|
|
// Heap closures always capture by copy/move, and can
|
2013-10-15 00:21:54 -05:00
|
|
|
// move out if they are once.
|
2014-04-11 10:03:10 -05:00
|
|
|
(ty::UniqTraitStore, _) => false,
|
2013-06-19 14:53:05 -05:00
|
|
|
|
|
|
|
};
|
|
|
|
if var_is_refd {
|
2014-10-04 16:04:10 -05:00
|
|
|
self.cat_upvar(id, span, var_id, fn_node_id, None)
|
2013-06-19 14:53:05 -05:00
|
|
|
} else {
|
2014-04-15 05:15:56 -05:00
|
|
|
Ok(Rc::new(cmt_ {
|
2013-06-19 14:53:05 -05:00
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_copied_upvar(CopiedUpvar {
|
2014-02-07 13:41:58 -06:00
|
|
|
upvar_id: var_id,
|
2014-10-04 16:04:10 -05:00
|
|
|
kind: Boxed(closure_ty.onceness),
|
Ensure dataflow of a proc never looks at blocks from closed-over context.
Details: in a program like:
```
type T = proc(int) -> int; /* 4 */
pub fn outer(captured /* pat 16 */: T) -> T {
(proc(x /* pat 23 */) {
((captured /* 29 */).foo((x /* 30 */)) /* 28 */)
} /* block 27 */ /* 20 */)
} /* block 19 */ /* 12 */
```
the `captured` arg is moved from the outer fn into the inner proc (id=20).
The old dataflow analysis for flowed_move_data_moves, when looking at
the inner proc, would attempt to add a kill bit for `captured` at the
end of its scope; the problem is that it thought the end of the
`captured` arg's scope was the outer fn (id=12), even though at that
point in the analysis, the `captured` arg's scope should now be
restricted to the proc itself (id=20).
This patch fixes handling of upvars so that dataflow of a fn/proc
should never attempts to add a gen or kill bit to any NodeId outside
of the current fn/proc. It accomplishes this by adding an `LpUpvar`
variant to `borrowck::LoanPath`, so for cases like `captured` above
will carry both their original `var_id`, as before, as well as the
`NodeId` for the closure that is capturing them.
As a drive-by fix to another occurrence of a similar bug that
nikomatsakis pointed out to me earlier, this also fixes
`gather_loans::compute_kill_scope` so that it computes the kill scope
of the `captured` arg to be block 27; that is, the block for the proc
itself (id=20).
(This is an updated version that generalizes the new loan path variant
to cover all upvars, and thus renamed the variant from `LpCopiedUpvar`
to just `LpUpvar`.)
2014-06-13 10:19:29 -05:00
|
|
|
capturing_proc: fn_node_id,
|
|
|
|
}),
|
2014-09-17 10:17:09 -05:00
|
|
|
mutbl: MutabilityCategory::from_local(self.tcx(), var_id),
|
2013-06-19 14:53:05 -05:00
|
|
|
ty:expr_ty
|
2014-04-15 05:15:56 -05:00
|
|
|
}))
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
}
|
2014-07-30 00:08:39 -05:00
|
|
|
ty::ty_unboxed_closure(closure_id, _) => {
|
|
|
|
let unboxed_closures = self.typer
|
|
|
|
.unboxed_closures()
|
|
|
|
.borrow();
|
|
|
|
let kind = unboxed_closures.get(&closure_id).kind;
|
2014-10-02 01:52:19 -05:00
|
|
|
if self.typer.capture_mode(fn_node_id) == ast::CaptureByRef {
|
2014-10-04 16:04:10 -05:00
|
|
|
self.cat_upvar(id, span, var_id, fn_node_id, Some(kind))
|
2014-10-02 01:52:19 -05:00
|
|
|
} else {
|
|
|
|
Ok(Rc::new(cmt_ {
|
|
|
|
id: id,
|
|
|
|
span: span,
|
|
|
|
cat: cat_copied_upvar(CopiedUpvar {
|
|
|
|
upvar_id: var_id,
|
2014-10-04 16:04:10 -05:00
|
|
|
kind: Unboxed(kind),
|
2014-10-02 01:52:19 -05:00
|
|
|
capturing_proc: fn_node_id,
|
|
|
|
}),
|
|
|
|
mutbl: MutabilityCategory::from_local(self.tcx(), var_id),
|
|
|
|
ty: expr_ty
|
|
|
|
}))
|
|
|
|
}
|
2014-05-29 00:26:56 -05:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
_ => {
|
2014-02-07 13:41:58 -06:00
|
|
|
self.tcx().sess.span_bug(
|
2013-03-15 14:24:24 -05:00
|
|
|
span,
|
2014-02-07 13:41:58 -06:00
|
|
|
format!("Upvar of non-closure {} - {}",
|
2014-05-16 12:45:16 -05:00
|
|
|
fn_node_id,
|
|
|
|
ty.repr(self.tcx())).as_slice());
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2014-09-17 10:17:09 -05:00
|
|
|
def::DefLocal(vid) => {
|
2014-04-15 05:15:56 -05:00
|
|
|
Ok(Rc::new(cmt_ {
|
2013-10-20 07:31:23 -05:00
|
|
|
id: id,
|
|
|
|
span: span,
|
|
|
|
cat: cat_local(vid),
|
2014-09-17 10:17:09 -05:00
|
|
|
mutbl: MutabilityCategory::from_local(self.tcx(), vid),
|
2013-10-20 07:31:23 -05:00
|
|
|
ty: expr_ty
|
2014-04-15 05:15:56 -05:00
|
|
|
}))
|
2012-08-02 18:00:45 -05:00
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
fn cat_upvar(&self,
|
2014-02-07 13:41:58 -06:00
|
|
|
id: ast::NodeId,
|
|
|
|
span: Span,
|
|
|
|
var_id: ast::NodeId,
|
2014-10-04 16:04:10 -05:00
|
|
|
fn_node_id: ast::NodeId,
|
|
|
|
kind: Option<ty::UnboxedClosureKind>)
|
2014-02-07 13:41:58 -06:00
|
|
|
-> McResult<cmt> {
|
|
|
|
/*!
|
|
|
|
* Upvars through a closure are in fact indirect
|
|
|
|
* references. That is, when a closure refers to a
|
|
|
|
* variable from a parent stack frame like `x = 10`,
|
|
|
|
* that is equivalent to `*x_ = 10` where `x_` is a
|
|
|
|
* borrowed pointer (`&mut x`) created when the closure
|
|
|
|
* was created and store in the environment. This
|
|
|
|
* equivalence is expose in the mem-categorization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
let upvar_id = ty::UpvarId { var_id: var_id,
|
|
|
|
closure_expr_id: fn_node_id };
|
|
|
|
|
|
|
|
let upvar_borrow = self.typer.upvar_borrow(upvar_id);
|
|
|
|
|
|
|
|
let var_ty = if_ok!(self.node_ty(var_id));
|
|
|
|
|
|
|
|
// We can't actually represent the types of all upvars
|
|
|
|
// as user-describable types, since upvars support const
|
|
|
|
// and unique-imm borrows! Therefore, we cheat, and just
|
|
|
|
// give err type. Nobody should be inspecting this type anyhow.
|
|
|
|
let upvar_ty = ty::mk_err();
|
|
|
|
|
2014-04-15 05:15:56 -05:00
|
|
|
let base_cmt = Rc::new(cmt_ {
|
2014-02-07 13:41:58 -06:00
|
|
|
id:id,
|
|
|
|
span:span,
|
2014-10-04 16:04:10 -05:00
|
|
|
cat:cat_upvar(upvar_id, upvar_borrow, kind),
|
2014-02-07 13:41:58 -06:00
|
|
|
mutbl:McImmutable,
|
|
|
|
ty:upvar_ty,
|
2014-04-15 05:15:56 -05:00
|
|
|
});
|
2014-02-07 13:41:58 -06:00
|
|
|
|
|
|
|
let ptr = BorrowedPtr(upvar_borrow.kind, upvar_borrow.region);
|
|
|
|
|
2014-04-15 05:15:56 -05:00
|
|
|
let deref_cmt = Rc::new(cmt_ {
|
2014-02-07 13:41:58 -06:00
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_deref(base_cmt, 0, ptr),
|
|
|
|
mutbl:MutabilityCategory::from_borrow_kind(upvar_borrow.kind),
|
|
|
|
ty:var_ty,
|
2014-04-15 05:15:56 -05:00
|
|
|
});
|
2014-02-07 13:41:58 -06:00
|
|
|
|
|
|
|
Ok(deref_cmt)
|
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
pub fn cat_rvalue_node(&self,
|
2014-02-07 13:41:58 -06:00
|
|
|
id: ast::NodeId,
|
|
|
|
span: Span,
|
|
|
|
expr_ty: ty::t)
|
|
|
|
-> cmt {
|
|
|
|
match self.typer.temporary_scope(id) {
|
2014-01-15 13:39:08 -06:00
|
|
|
Some(scope) => {
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
match ty::get(expr_ty).sty {
|
|
|
|
ty::ty_vec(_, Some(0)) => self.cat_rvalue(id, span, ty::ReStatic, expr_ty),
|
|
|
|
_ => self.cat_rvalue(id, span, ty::ReScope(scope), expr_ty)
|
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
None => {
|
2014-01-24 13:48:10 -06:00
|
|
|
self.cat_rvalue(id, span, ty::ReStatic, expr_ty)
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
}
|
2013-06-20 14:25:52 -05:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
pub fn cat_rvalue(&self,
|
2013-07-27 03:25:59 -05:00
|
|
|
cmt_id: ast::NodeId,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2014-01-15 13:39:08 -06:00
|
|
|
temp_scope: ty::Region,
|
2013-06-20 14:25:52 -05:00
|
|
|
expr_ty: ty::t) -> cmt {
|
2014-04-15 05:15:56 -05:00
|
|
|
Rc::new(cmt_ {
|
2013-06-20 14:25:52 -05:00
|
|
|
id:cmt_id,
|
|
|
|
span:span,
|
2014-01-15 13:39:08 -06:00
|
|
|
cat:cat_rvalue(temp_scope),
|
2013-05-14 08:26:21 -05:00
|
|
|
mutbl:McDeclared,
|
2013-01-25 18:57:39 -06:00
|
|
|
ty:expr_ty
|
2014-04-15 05:15:56 -05:00
|
|
|
})
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
pub fn cat_field<N:ast_node>(&self,
|
2014-01-06 06:00:46 -06:00
|
|
|
node: &N,
|
2013-05-31 17:17:22 -05:00
|
|
|
base_cmt: cmt,
|
2013-09-01 19:50:59 -05:00
|
|
|
f_name: ast::Ident,
|
2013-05-31 17:17:22 -05:00
|
|
|
f_ty: ty::t)
|
|
|
|
-> cmt {
|
2014-04-15 05:15:56 -05:00
|
|
|
Rc::new(cmt_ {
|
2013-01-25 18:57:39 -06:00
|
|
|
id: node.id(),
|
|
|
|
span: node.span(),
|
2013-05-09 14:58:41 -05:00
|
|
|
mutbl: base_cmt.mutbl.inherit(),
|
2014-04-15 05:15:56 -05:00
|
|
|
cat: cat_interior(base_cmt, InteriorField(NamedField(f_name.name))),
|
2013-05-03 11:11:15 -05:00
|
|
|
ty: f_ty
|
2014-04-15 05:15:56 -05:00
|
|
|
})
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2014-08-09 22:54:33 -05:00
|
|
|
pub fn cat_tup_field<N:ast_node>(&self,
|
|
|
|
node: &N,
|
|
|
|
base_cmt: cmt,
|
|
|
|
f_idx: uint,
|
|
|
|
f_ty: ty::t)
|
|
|
|
-> cmt {
|
|
|
|
Rc::new(cmt_ {
|
|
|
|
id: node.id(),
|
|
|
|
span: node.span(),
|
|
|
|
mutbl: base_cmt.mutbl.inherit(),
|
|
|
|
cat: cat_interior(base_cmt, InteriorField(PositionalField(f_idx))),
|
|
|
|
ty: f_ty
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
fn cat_deref<N:ast_node>(&self,
|
2014-03-06 11:24:11 -06:00
|
|
|
node: &N,
|
|
|
|
base_cmt: cmt,
|
2014-07-14 02:43:21 -05:00
|
|
|
deref_cnt: uint,
|
|
|
|
implicit: bool)
|
2014-03-06 11:24:11 -06:00
|
|
|
-> cmt {
|
2014-06-11 17:01:48 -05:00
|
|
|
let adjustment = match self.typer.adjustments().borrow().find(&node.id()) {
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
Some(adj) if ty::adjust_is_object(adj) => typeck::AutoObject,
|
2014-06-11 17:01:48 -05:00
|
|
|
_ if deref_cnt != 0 => typeck::AutoDeref(deref_cnt),
|
|
|
|
_ => typeck::NoAdjustment
|
|
|
|
};
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
|
2014-03-06 11:24:11 -06:00
|
|
|
let method_call = typeck::MethodCall {
|
|
|
|
expr_id: node.id(),
|
2014-06-11 17:01:48 -05:00
|
|
|
adjustment: adjustment
|
2014-03-06 11:24:11 -06:00
|
|
|
};
|
|
|
|
let method_ty = self.typer.node_method_ty(method_call);
|
|
|
|
|
|
|
|
debug!("cat_deref: method_call={:?} method_ty={}",
|
|
|
|
method_call, method_ty.map(|ty| ty.repr(self.tcx())));
|
|
|
|
|
|
|
|
let base_cmt = match method_ty {
|
|
|
|
Some(method_ty) => {
|
|
|
|
let ref_ty = ty::ty_fn_ret(method_ty);
|
|
|
|
self.cat_rvalue_node(node.id(), node.span(), ref_ty)
|
|
|
|
}
|
|
|
|
None => base_cmt
|
|
|
|
};
|
|
|
|
match ty::deref(base_cmt.ty, true) {
|
2014-07-14 02:43:21 -05:00
|
|
|
Some(mt) => self.cat_deref_common(node, base_cmt, deref_cnt, mt.ty, implicit),
|
2012-09-11 23:25:01 -05:00
|
|
|
None => {
|
2014-02-07 13:41:58 -06:00
|
|
|
self.tcx().sess.span_bug(
|
2012-09-11 23:25:01 -05:00
|
|
|
node.span(),
|
2014-02-07 13:41:58 -06:00
|
|
|
format!("Explicit deref of non-derefable type: {}",
|
2014-05-16 12:45:16 -05:00
|
|
|
base_cmt.ty.repr(self.tcx())).as_slice());
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
2014-03-06 11:24:11 -06:00
|
|
|
}
|
2012-11-04 22:41:00 -06:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
fn cat_deref_common<N:ast_node>(&self,
|
2014-03-06 11:24:11 -06:00
|
|
|
node: &N,
|
|
|
|
base_cmt: cmt,
|
|
|
|
deref_cnt: uint,
|
2014-07-14 02:43:21 -05:00
|
|
|
deref_ty: ty::t,
|
|
|
|
implicit: bool)
|
2014-03-06 11:24:11 -06:00
|
|
|
-> cmt {
|
|
|
|
let (m, cat) = match deref_kind(self.tcx(), base_cmt.ty) {
|
2012-09-11 23:25:01 -05:00
|
|
|
deref_ptr(ptr) => {
|
2014-07-14 02:43:21 -05:00
|
|
|
let ptr = if implicit {
|
|
|
|
match ptr {
|
|
|
|
BorrowedPtr(bk, r) => Implicit(bk, r),
|
|
|
|
_ => self.tcx().sess.span_bug(node.span(),
|
|
|
|
"Implicit deref of non-borrowed pointer")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ptr
|
|
|
|
};
|
2012-07-20 18:37:45 -05:00
|
|
|
// for unique ptrs, we inherit mutability from the
|
|
|
|
// owning reference.
|
2014-03-06 11:24:11 -06:00
|
|
|
(MutabilityCategory::from_pointer_kind(base_cmt.mutbl, ptr),
|
|
|
|
cat_deref(base_cmt, deref_cnt, ptr))
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
deref_interior(interior) => {
|
2014-03-06 11:24:11 -06:00
|
|
|
(base_cmt.mutbl.inherit(), cat_interior(base_cmt, interior))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
2014-03-06 11:24:11 -06:00
|
|
|
};
|
2014-04-15 05:15:56 -05:00
|
|
|
Rc::new(cmt_ {
|
2014-03-06 11:24:11 -06:00
|
|
|
id: node.id(),
|
|
|
|
span: node.span(),
|
|
|
|
cat: cat,
|
|
|
|
mutbl: m,
|
|
|
|
ty: deref_ty
|
2014-04-15 05:15:56 -05:00
|
|
|
})
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
pub fn cat_index<N:ast_node>(&self,
|
2014-01-06 06:00:46 -06:00
|
|
|
elt: &N,
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
mut base_cmt: cmt)
|
2013-05-31 17:17:22 -05:00
|
|
|
-> cmt {
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
//! Creates a cmt for an indexing operation (`[]`).
|
2013-05-09 14:58:02 -05:00
|
|
|
//!
|
|
|
|
//! One subtle aspect of indexing that may not be
|
|
|
|
//! immediately obvious: for anything other than a fixed-length
|
|
|
|
//! vector, an operation like `x[y]` actually consists of two
|
|
|
|
//! disjoint (from the point of view of borrowck) operations.
|
|
|
|
//! The first is a deref of `x` to create a pointer `p` that points
|
|
|
|
//! at the first element in the array. The second operation is
|
|
|
|
//! an index which adds `y*sizeof(T)` to `p` to obtain the
|
|
|
|
//! pointer to `x[y]`. `cat_index` will produce a resulting
|
|
|
|
//! cmt containing both this deref and the indexing,
|
|
|
|
//! presuming that `base_cmt` is not of fixed-length type.
|
|
|
|
//!
|
|
|
|
//! # Parameters
|
|
|
|
//! - `elt`: the AST node being indexed
|
|
|
|
//! - `base_cmt`: the cmt of `elt`
|
|
|
|
|
2014-07-03 16:32:41 -05:00
|
|
|
let method_call = typeck::MethodCall::expr(elt.id());
|
|
|
|
let method_ty = self.typer.node_method_ty(method_call);
|
|
|
|
|
|
|
|
let element_ty = match method_ty {
|
|
|
|
Some(method_ty) => {
|
|
|
|
let ref_ty = ty::ty_fn_ret(method_ty);
|
|
|
|
base_cmt = self.cat_rvalue_node(elt.id(), elt.span(), ref_ty);
|
|
|
|
*ty::ty_fn_args(method_ty).get(0)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
match ty::array_element_ty(base_cmt.ty) {
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
Some(ty) => ty,
|
2014-07-03 16:32:41 -05:00
|
|
|
None => {
|
|
|
|
self.tcx().sess.span_bug(
|
|
|
|
elt.span(),
|
|
|
|
format!("Explicit index of non-index type `{}`",
|
|
|
|
base_cmt.ty.repr(self.tcx())).as_slice());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
};
|
|
|
|
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
let m = base_cmt.mutbl.inherit();
|
|
|
|
return interior(elt, base_cmt.clone(), base_cmt.ty, m, element_ty);
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2014-01-06 06:00:46 -06:00
|
|
|
fn interior<N: ast_node>(elt: &N,
|
2013-05-17 20:12:50 -05:00
|
|
|
of_cmt: cmt,
|
|
|
|
vec_ty: ty::t,
|
|
|
|
mutbl: MutabilityCategory,
|
2013-08-11 12:56:30 -05:00
|
|
|
element_ty: ty::t) -> cmt
|
2013-01-24 21:33:48 -06:00
|
|
|
{
|
2014-04-15 05:15:56 -05:00
|
|
|
Rc::new(cmt_ {
|
2013-01-25 18:57:39 -06:00
|
|
|
id:elt.id(),
|
|
|
|
span:elt.span(),
|
2013-05-22 05:54:35 -05:00
|
|
|
cat:cat_interior(of_cmt, InteriorElement(element_kind(vec_ty))),
|
2013-01-25 18:57:39 -06:00
|
|
|
mutbl:mutbl,
|
2013-08-11 12:56:30 -05:00
|
|
|
ty:element_ty
|
2014-04-15 05:15:56 -05:00
|
|
|
})
|
2012-06-04 18:21:14 -05:00
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
// Takes either a vec or a reference to a vec and returns the cmt for the
|
|
|
|
// underlying vec.
|
|
|
|
fn deref_vec<N:ast_node>(&self,
|
|
|
|
elt: &N,
|
|
|
|
base_cmt: cmt)
|
|
|
|
-> cmt {
|
|
|
|
match deref_kind(self.tcx(), base_cmt.ty) {
|
|
|
|
deref_ptr(ptr) => {
|
|
|
|
// for unique ptrs, we inherit mutability from the
|
|
|
|
// owning reference.
|
|
|
|
let m = MutabilityCategory::from_pointer_kind(base_cmt.mutbl, ptr);
|
|
|
|
|
|
|
|
// the deref is explicit in the resulting cmt
|
|
|
|
Rc::new(cmt_ {
|
|
|
|
id:elt.id(),
|
|
|
|
span:elt.span(),
|
|
|
|
cat:cat_deref(base_cmt.clone(), 0, ptr),
|
|
|
|
mutbl:m,
|
|
|
|
ty: match ty::deref(base_cmt.ty, false) {
|
|
|
|
Some(mt) => mt.ty,
|
|
|
|
None => self.tcx().sess.bug("Found non-derefable type")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
deref_interior(_) => {
|
|
|
|
base_cmt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
pub fn cat_slice_pattern(&self,
|
2014-02-07 13:41:58 -06:00
|
|
|
vec_cmt: cmt,
|
2014-04-15 05:15:56 -05:00
|
|
|
slice_pat: &ast::Pat)
|
2014-02-07 13:41:58 -06:00
|
|
|
-> McResult<(cmt, ast::Mutability, ty::Region)> {
|
|
|
|
/*!
|
|
|
|
* Given a pattern P like: `[_, ..Q, _]`, where `vec_cmt` is
|
|
|
|
* the cmt for `P`, `slice_pat` is the pattern `Q`, returns:
|
|
|
|
* - a cmt for `Q`
|
|
|
|
* - the mutability and region of the slice `Q`
|
|
|
|
*
|
|
|
|
* These last two bits of info happen to be things that
|
|
|
|
* borrowck needs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
let slice_ty = if_ok!(self.node_ty(slice_pat.id));
|
|
|
|
let (slice_mutbl, slice_r) = vec_slice_info(self.tcx(),
|
|
|
|
slice_pat,
|
|
|
|
slice_ty);
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
let cmt_slice = self.cat_index(slice_pat, self.deref_vec(slice_pat, vec_cmt));
|
2014-02-07 13:41:58 -06:00
|
|
|
return Ok((cmt_slice, slice_mutbl, slice_r));
|
|
|
|
|
2014-03-05 21:07:47 -06:00
|
|
|
fn vec_slice_info(tcx: &ty::ctxt,
|
2014-04-15 05:15:56 -05:00
|
|
|
pat: &ast::Pat,
|
2014-02-07 13:41:58 -06:00
|
|
|
slice_ty: ty::t)
|
|
|
|
-> (ast::Mutability, ty::Region) {
|
|
|
|
/*!
|
|
|
|
* In a pattern like [a, b, ..c], normally `c` has slice type,
|
|
|
|
* but if you have [a, b, ..ref c], then the type of `ref c`
|
|
|
|
* will be `&&[]`, so to extract the slice details we have
|
|
|
|
* to recurse through rptrs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
match ty::get(slice_ty).sty {
|
2014-04-09 02:15:31 -05:00
|
|
|
ty::ty_rptr(r, ref mt) => match ty::get(mt.ty).sty {
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
ty::ty_vec(_, None) => (mt.mutbl, r),
|
2014-04-09 02:15:31 -05:00
|
|
|
_ => vec_slice_info(tcx, pat, mt.ty),
|
|
|
|
},
|
2014-02-07 13:41:58 -06:00
|
|
|
|
|
|
|
_ => {
|
2014-05-16 12:45:16 -05:00
|
|
|
tcx.sess.span_bug(pat.span,
|
|
|
|
"type of slice pattern is not a slice");
|
2014-02-07 13:41:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
pub fn cat_imm_interior<N:ast_node>(&self,
|
2014-01-06 06:00:46 -06:00
|
|
|
node: &N,
|
2013-05-31 17:17:22 -05:00
|
|
|
base_cmt: cmt,
|
|
|
|
interior_ty: ty::t,
|
|
|
|
interior: InteriorKind)
|
|
|
|
-> cmt {
|
2014-04-15 05:15:56 -05:00
|
|
|
Rc::new(cmt_ {
|
2013-05-03 11:11:15 -05:00
|
|
|
id: node.id(),
|
|
|
|
span: node.span(),
|
|
|
|
mutbl: base_cmt.mutbl.inherit(),
|
2014-04-15 05:15:56 -05:00
|
|
|
cat: cat_interior(base_cmt, interior),
|
2013-05-03 11:11:15 -05:00
|
|
|
ty: interior_ty
|
2014-04-15 05:15:56 -05:00
|
|
|
})
|
2012-11-02 11:56:09 -05:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
pub fn cat_downcast<N:ast_node>(&self,
|
2014-01-06 06:00:46 -06:00
|
|
|
node: &N,
|
2013-05-31 17:17:22 -05:00
|
|
|
base_cmt: cmt,
|
|
|
|
downcast_ty: ty::t)
|
|
|
|
-> cmt {
|
2014-04-15 05:15:56 -05:00
|
|
|
Rc::new(cmt_ {
|
2013-05-17 20:12:50 -05:00
|
|
|
id: node.id(),
|
|
|
|
span: node.span(),
|
|
|
|
mutbl: base_cmt.mutbl.inherit(),
|
2014-04-15 05:15:56 -05:00
|
|
|
cat: cat_downcast(base_cmt),
|
2013-05-17 20:12:50 -05:00
|
|
|
ty: downcast_ty
|
2014-04-15 05:15:56 -05:00
|
|
|
})
|
2013-05-17 20:12:50 -05:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:43:26 -05:00
|
|
|
pub fn cat_pattern(&self,
|
2013-05-31 17:17:22 -05:00
|
|
|
cmt: cmt,
|
2014-04-10 08:43:26 -05:00
|
|
|
pat: &ast::Pat,
|
|
|
|
op: |&MemCategorizationContext<TYPER>,
|
2014-02-07 13:41:58 -06:00
|
|
|
cmt,
|
2014-04-10 08:43:26 -05:00
|
|
|
&ast::Pat|)
|
2014-02-07 13:41:58 -06:00
|
|
|
-> McResult<()> {
|
2012-08-08 10:15:32 -05:00
|
|
|
// Here, `cmt` is the categorization for the value being
|
|
|
|
// matched and pat is the pattern it is being matched against.
|
|
|
|
//
|
|
|
|
// In general, the way that this works is that we walk down
|
|
|
|
// the pattern, constructing a cmt that represents the path
|
|
|
|
// that will be taken to reach the value being matched.
|
|
|
|
//
|
|
|
|
// When we encounter named bindings, we take the cmt that has
|
|
|
|
// been built up and pass it off to guarantee_valid() so that
|
|
|
|
// we can be sure that the binding will remain valid for the
|
|
|
|
// duration of the arm.
|
|
|
|
//
|
2014-02-07 13:41:58 -06:00
|
|
|
// (*2) There is subtlety concerning the correspondence between
|
2013-05-03 11:11:15 -05:00
|
|
|
// pattern ids and types as compared to *expression* ids and
|
|
|
|
// types. This is explained briefly. on the definition of the
|
|
|
|
// type `cmt`, so go off and read what it says there, then
|
|
|
|
// come back and I'll dive into a bit more detail here. :) OK,
|
|
|
|
// back?
|
2012-08-08 10:15:32 -05:00
|
|
|
//
|
2013-05-03 11:11:15 -05:00
|
|
|
// In general, the id of the cmt should be the node that
|
|
|
|
// "produces" the value---patterns aren't executable code
|
|
|
|
// exactly, but I consider them to "execute" when they match a
|
2014-02-07 13:41:58 -06:00
|
|
|
// value, and I consider them to produce the value that was
|
|
|
|
// matched. So if you have something like:
|
2012-08-08 10:15:32 -05:00
|
|
|
//
|
|
|
|
// let x = @@3;
|
|
|
|
// match x {
|
|
|
|
// @@y { ... }
|
|
|
|
// }
|
|
|
|
//
|
2013-05-03 11:11:15 -05:00
|
|
|
// In this case, the cmt and the relevant ids would be:
|
|
|
|
//
|
|
|
|
// CMT Id Type of Id Type of cmt
|
2012-08-08 10:15:32 -05:00
|
|
|
//
|
|
|
|
// local(x)->@->@
|
2013-05-03 11:11:15 -05:00
|
|
|
// ^~~~~~~^ `x` from discr @@int @@int
|
|
|
|
// ^~~~~~~~~~^ `@@y` pattern node @@int @int
|
|
|
|
// ^~~~~~~~~~~~~^ `@y` pattern node @int int
|
2012-08-08 10:15:32 -05:00
|
|
|
//
|
2013-05-03 11:11:15 -05:00
|
|
|
// You can see that the types of the id and the cmt are in
|
|
|
|
// sync in the first line, because that id is actually the id
|
|
|
|
// of an expression. But once we get to pattern ids, the types
|
|
|
|
// step out of sync again. So you'll see below that we always
|
|
|
|
// get the type of the *subpattern* and use that.
|
2012-07-18 18:18:02 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("cat_pattern: id={} pat={} cmt={}",
|
2014-06-21 05:39:03 -05:00
|
|
|
pat.id, pprust::pat_to_string(pat),
|
2014-03-05 21:07:47 -06:00
|
|
|
cmt.repr(self.tcx()));
|
2012-08-08 10:15:32 -05:00
|
|
|
|
2014-04-15 05:15:56 -05:00
|
|
|
op(self, cmt.clone(), pat);
|
2013-01-24 21:33:48 -06:00
|
|
|
|
|
|
|
match pat.node {
|
2014-08-06 10:04:44 -05:00
|
|
|
ast::PatWild(_) => {
|
2012-08-08 10:15:32 -05:00
|
|
|
// _
|
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::PatEnum(_, None) => {
|
2013-11-28 14:22:53 -06:00
|
|
|
// variant(..)
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::PatEnum(_, Some(ref subpats)) => {
|
2014-03-20 21:49:20 -05:00
|
|
|
match self.tcx().def_map.borrow().find(&pat.id) {
|
2014-05-14 14:31:30 -05:00
|
|
|
Some(&def::DefVariant(enum_did, _, _)) => {
|
2012-11-02 11:56:09 -05:00
|
|
|
// variant(x, y, z)
|
2013-05-17 20:12:50 -05:00
|
|
|
|
|
|
|
let downcast_cmt = {
|
2014-02-07 13:41:58 -06:00
|
|
|
if ty::enum_is_univariant(self.tcx(), enum_did) {
|
2013-05-17 20:12:50 -05:00
|
|
|
cmt // univariant, no downcast needed
|
|
|
|
} else {
|
2014-04-15 05:15:56 -05:00
|
|
|
self.cat_downcast(pat, cmt.clone(), cmt.ty)
|
2013-05-17 20:12:50 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-16 12:15:33 -05:00
|
|
|
for (i, subpat) in subpats.iter().enumerate() {
|
|
|
|
let subpat_ty = if_ok!(self.pat_ty(&**subpat)); // see (*2)
|
2013-05-17 20:12:50 -05:00
|
|
|
|
2013-05-03 11:11:15 -05:00
|
|
|
let subcmt =
|
2013-05-17 20:12:50 -05:00
|
|
|
self.cat_imm_interior(
|
2014-04-15 05:15:56 -05:00
|
|
|
pat, downcast_cmt.clone(), subpat_ty,
|
2013-05-17 20:12:50 -05:00
|
|
|
InteriorField(PositionalField(i)));
|
|
|
|
|
2014-05-16 12:15:33 -05:00
|
|
|
if_ok!(self.cat_pattern(subcmt, &**subpat, |x,y,z| op(x,y,z)));
|
2012-11-02 11:56:09 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-14 14:31:30 -05:00
|
|
|
Some(&def::DefStruct(..)) => {
|
2014-05-16 12:15:33 -05:00
|
|
|
for (i, subpat) in subpats.iter().enumerate() {
|
|
|
|
let subpat_ty = if_ok!(self.pat_ty(&**subpat)); // see (*2)
|
2013-05-03 11:11:15 -05:00
|
|
|
let cmt_field =
|
2013-05-17 20:12:50 -05:00
|
|
|
self.cat_imm_interior(
|
2014-04-15 05:15:56 -05:00
|
|
|
pat, cmt.clone(), subpat_ty,
|
2013-05-17 20:12:50 -05:00
|
|
|
InteriorField(PositionalField(i)));
|
2014-05-16 12:15:33 -05:00
|
|
|
if_ok!(self.cat_pattern(cmt_field, &**subpat,
|
|
|
|
|x,y,z| op(x,y,z)));
|
2012-11-02 11:56:09 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-14 14:31:30 -05:00
|
|
|
Some(&def::DefStatic(..)) => {
|
2014-05-16 12:15:33 -05:00
|
|
|
for subpat in subpats.iter() {
|
|
|
|
if_ok!(self.cat_pattern(cmt.clone(), &**subpat, |x,y,z| op(x,y,z)));
|
2013-03-21 02:27:26 -05:00
|
|
|
}
|
|
|
|
}
|
2012-11-02 11:56:09 -05:00
|
|
|
_ => {
|
2014-02-07 13:41:58 -06:00
|
|
|
self.tcx().sess.span_bug(
|
2012-11-02 11:56:09 -05:00
|
|
|
pat.span,
|
2013-04-30 11:47:52 -05:00
|
|
|
"enum pattern didn't resolve to enum or struct");
|
2012-11-02 11:56:09 -05:00
|
|
|
}
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::PatIdent(_, _, Some(ref subpat)) => {
|
|
|
|
if_ok!(self.cat_pattern(cmt, &**subpat, op));
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::PatIdent(_, _, None) => {
|
2012-08-08 10:15:32 -05:00
|
|
|
// nullary variant or identifier: ignore
|
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::PatStruct(_, ref field_pats, _) => {
|
2012-08-08 10:15:32 -05:00
|
|
|
// {f1: p1, ..., fN: pN}
|
2013-08-03 11:45:23 -05:00
|
|
|
for fp in field_pats.iter() {
|
2014-05-16 12:15:33 -05:00
|
|
|
let field_ty = if_ok!(self.pat_ty(&*fp.pat)); // see (*2)
|
2014-04-15 05:15:56 -05:00
|
|
|
let cmt_field = self.cat_field(pat, cmt.clone(), fp.ident, field_ty);
|
2014-05-16 12:15:33 -05:00
|
|
|
if_ok!(self.cat_pattern(cmt_field, &*fp.pat, |x,y,z| op(x,y,z)));
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::PatTup(ref subpats) => {
|
2012-08-08 10:15:32 -05:00
|
|
|
// (p1, ..., pN)
|
2014-05-16 12:15:33 -05:00
|
|
|
for (i, subpat) in subpats.iter().enumerate() {
|
|
|
|
let subpat_ty = if_ok!(self.pat_ty(&**subpat)); // see (*2)
|
2013-05-17 20:12:50 -05:00
|
|
|
let subcmt =
|
|
|
|
self.cat_imm_interior(
|
2014-04-15 05:15:56 -05:00
|
|
|
pat, cmt.clone(), subpat_ty,
|
2013-05-17 20:12:50 -05:00
|
|
|
InteriorField(PositionalField(i)));
|
2014-05-16 12:15:33 -05:00
|
|
|
if_ok!(self.cat_pattern(subcmt, &**subpat, |x,y,z| op(x,y,z)));
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::PatBox(ref subpat) | ast::PatRegion(ref subpat) => {
|
2014-07-21 22:54:28 -05:00
|
|
|
// @p1, ~p1, ref p1
|
2014-07-14 02:43:21 -05:00
|
|
|
let subcmt = self.cat_deref(pat, cmt, 0, false);
|
2014-05-16 12:15:33 -05:00
|
|
|
if_ok!(self.cat_pattern(subcmt, &**subpat, op));
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
ast::PatVec(ref before, ref slice, ref after) => {
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
let elt_cmt = self.cat_index(pat, self.deref_vec(pat, cmt));
|
2014-05-16 12:15:33 -05:00
|
|
|
for before_pat in before.iter() {
|
|
|
|
if_ok!(self.cat_pattern(elt_cmt.clone(), &**before_pat,
|
|
|
|
|x,y,z| op(x,y,z)));
|
2013-01-24 21:33:48 -06:00
|
|
|
}
|
2014-05-16 12:15:33 -05:00
|
|
|
for slice_pat in slice.iter() {
|
|
|
|
let slice_ty = if_ok!(self.pat_ty(&**slice_pat));
|
2014-01-24 13:48:10 -06:00
|
|
|
let slice_cmt = self.cat_rvalue_node(pat.id(), pat.span(), slice_ty);
|
2014-05-16 12:15:33 -05:00
|
|
|
if_ok!(self.cat_pattern(slice_cmt, &**slice_pat, |x,y,z| op(x,y,z)));
|
2013-02-26 12:58:46 -06:00
|
|
|
}
|
2014-05-16 12:15:33 -05:00
|
|
|
for after_pat in after.iter() {
|
|
|
|
if_ok!(self.cat_pattern(elt_cmt.clone(), &**after_pat, |x,y,z| op(x,y,z)));
|
2013-01-24 21:33:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::PatLit(_) | ast::PatRange(_, _) => {
|
2012-12-08 14:22:43 -06:00
|
|
|
/*always ok*/
|
|
|
|
}
|
2014-05-19 15:29:41 -05:00
|
|
|
|
|
|
|
ast::PatMac(_) => {
|
|
|
|
self.tcx().sess.span_bug(pat.span, "unexpanded macro");
|
|
|
|
}
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
2014-02-07 13:41:58 -06:00
|
|
|
|
|
|
|
Ok(())
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2014-06-21 05:39:03 -05:00
|
|
|
pub fn cmt_to_string(&self, cmt: &cmt_) -> String {
|
2012-08-08 10:15:32 -05:00
|
|
|
match cmt.cat {
|
2013-05-17 20:12:50 -05:00
|
|
|
cat_static_item => {
|
2014-05-25 05:17:19 -05:00
|
|
|
"static item".to_string()
|
2013-05-17 20:12:50 -05:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_copied_upvar(_) => {
|
2014-05-25 05:17:19 -05:00
|
|
|
"captured outer variable in a proc".to_string()
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
cat_rvalue(..) => {
|
2014-05-25 05:17:19 -05:00
|
|
|
"non-lvalue".to_string()
|
2013-05-17 20:12:50 -05:00
|
|
|
}
|
2014-09-17 09:28:19 -05:00
|
|
|
cat_local(vid) => {
|
|
|
|
match self.tcx().map.find(vid) {
|
|
|
|
Some(ast_map::NodeArg(_)) => {
|
|
|
|
"argument".to_string()
|
|
|
|
}
|
|
|
|
_ => "local variable".to_string()
|
|
|
|
}
|
2013-05-17 20:12:50 -05:00
|
|
|
}
|
2014-04-15 05:15:56 -05:00
|
|
|
cat_deref(ref base, _, pk) => {
|
2014-02-07 13:41:58 -06:00
|
|
|
match base.cat {
|
|
|
|
cat_upvar(..) => {
|
2014-05-25 05:17:19 -05:00
|
|
|
"captured outer variable".to_string()
|
2014-02-07 13:41:58 -06:00
|
|
|
}
|
|
|
|
_ => {
|
2014-06-21 00:08:58 -05:00
|
|
|
match pk {
|
2014-07-14 02:43:21 -05:00
|
|
|
Implicit(..) => {
|
|
|
|
"dereference (dereference is implicit, due to indexing)".to_string()
|
|
|
|
}
|
2014-09-30 17:26:04 -05:00
|
|
|
OwnedPtr => format!("dereference of `{}`", ptr_sigil(pk)),
|
2014-06-21 00:08:58 -05:00
|
|
|
_ => format!("dereference of `{}`-pointer", ptr_sigil(pk))
|
|
|
|
}
|
2014-02-07 13:41:58 -06:00
|
|
|
}
|
|
|
|
}
|
2013-05-17 20:12:50 -05:00
|
|
|
}
|
|
|
|
cat_interior(_, InteriorField(NamedField(_))) => {
|
2014-05-25 05:17:19 -05:00
|
|
|
"field".to_string()
|
2013-05-17 20:12:50 -05:00
|
|
|
}
|
|
|
|
cat_interior(_, InteriorField(PositionalField(_))) => {
|
2014-05-25 05:17:19 -05:00
|
|
|
"anonymous field".to_string()
|
2013-05-17 20:12:50 -05:00
|
|
|
}
|
2013-05-22 05:54:35 -05:00
|
|
|
cat_interior(_, InteriorElement(VecElement)) => {
|
2014-05-25 05:17:19 -05:00
|
|
|
"vec content".to_string()
|
2013-05-22 05:54:35 -05:00
|
|
|
}
|
|
|
|
cat_interior(_, InteriorElement(OtherElement)) => {
|
2014-05-25 05:17:19 -05:00
|
|
|
"indexed content".to_string()
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
2014-02-07 13:41:58 -06:00
|
|
|
cat_upvar(..) => {
|
2014-05-25 05:17:19 -05:00
|
|
|
"captured outer variable".to_string()
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2014-04-15 05:15:56 -05:00
|
|
|
cat_discr(ref cmt, _) => {
|
2014-06-21 05:39:03 -05:00
|
|
|
self.cmt_to_string(&**cmt)
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
2014-04-15 05:15:56 -05:00
|
|
|
cat_downcast(ref cmt) => {
|
2014-06-21 05:39:03 -05:00
|
|
|
self.cmt_to_string(&**cmt)
|
2013-05-17 20:12:50 -05:00
|
|
|
}
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
2013-01-11 23:01:42 -06:00
|
|
|
|
2014-03-12 19:02:31 -05:00
|
|
|
pub enum InteriorSafety {
|
|
|
|
InteriorUnsafe,
|
|
|
|
InteriorSafe
|
|
|
|
}
|
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
pub enum AliasableReason {
|
2014-02-07 13:41:58 -06:00
|
|
|
AliasableBorrowed,
|
2014-10-04 16:06:08 -05:00
|
|
|
AliasableClosure(ast::NodeId), // Aliasable due to capture by unboxed closure expr
|
2014-02-07 16:28:56 -06:00
|
|
|
AliasableOther,
|
2014-03-12 19:02:31 -05:00
|
|
|
AliasableStatic(InteriorSafety),
|
|
|
|
AliasableStaticMut(InteriorSafety),
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl cmt_ {
|
2014-04-15 05:15:56 -05:00
|
|
|
pub fn guarantor(&self) -> cmt {
|
2013-03-15 14:24:24 -05:00
|
|
|
//! Returns `self` after stripping away any owned pointer derefs or
|
|
|
|
//! interior content. The return value is basically the `cmt` which
|
|
|
|
//! determines how long the value in `self` remains live.
|
|
|
|
|
|
|
|
match self.cat {
|
2013-11-28 14:22:53 -06:00
|
|
|
cat_rvalue(..) |
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_static_item |
|
2013-11-28 14:22:53 -06:00
|
|
|
cat_copied_upvar(..) |
|
|
|
|
cat_local(..) |
|
2014-02-07 13:41:58 -06:00
|
|
|
cat_deref(_, _, UnsafePtr(..)) |
|
|
|
|
cat_deref(_, _, BorrowedPtr(..)) |
|
2014-07-14 02:43:21 -05:00
|
|
|
cat_deref(_, _, Implicit(..)) |
|
2014-02-07 13:41:58 -06:00
|
|
|
cat_upvar(..) => {
|
2014-04-15 05:15:56 -05:00
|
|
|
Rc::new((*self).clone())
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
2014-04-15 05:15:56 -05:00
|
|
|
cat_downcast(ref b) |
|
|
|
|
cat_discr(ref b, _) |
|
|
|
|
cat_interior(ref b, _) |
|
|
|
|
cat_deref(ref b, _, OwnedPtr) => {
|
2013-03-15 14:24:24 -05:00
|
|
|
b.guarantor()
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 19:02:31 -05:00
|
|
|
pub fn freely_aliasable(&self, ctxt: &ty::ctxt) -> Option<AliasableReason> {
|
2013-08-27 14:44:55 -05:00
|
|
|
/*!
|
|
|
|
* Returns `Some(_)` if this lvalue represents a freely aliasable
|
|
|
|
* pointer type.
|
|
|
|
*/
|
2013-03-15 14:24:24 -05:00
|
|
|
|
|
|
|
// Maybe non-obvious: copied upvars can only be considered
|
|
|
|
// non-aliasable in once closures, since any other kind can be
|
|
|
|
// aliased and eventually recused.
|
|
|
|
|
|
|
|
match self.cat {
|
2014-04-15 05:15:56 -05:00
|
|
|
cat_deref(ref b, _, BorrowedPtr(ty::MutBorrow, _)) |
|
2014-07-14 02:43:21 -05:00
|
|
|
cat_deref(ref b, _, Implicit(ty::MutBorrow, _)) |
|
2014-04-15 05:15:56 -05:00
|
|
|
cat_deref(ref b, _, BorrowedPtr(ty::UniqueImmBorrow, _)) |
|
2014-07-14 02:43:21 -05:00
|
|
|
cat_deref(ref b, _, Implicit(ty::UniqueImmBorrow, _)) |
|
2014-04-15 05:15:56 -05:00
|
|
|
cat_downcast(ref b) |
|
|
|
|
cat_deref(ref b, _, OwnedPtr) |
|
|
|
|
cat_interior(ref b, _) |
|
|
|
|
cat_discr(ref b, _) => {
|
2014-02-07 16:28:56 -06:00
|
|
|
// Aliasability depends on base cmt
|
2014-03-12 19:02:31 -05:00
|
|
|
b.freely_aliasable(ctxt)
|
2014-02-07 16:28:56 -06:00
|
|
|
}
|
|
|
|
|
2013-11-28 14:22:53 -06:00
|
|
|
cat_rvalue(..) |
|
|
|
|
cat_local(..) |
|
2014-02-07 13:41:58 -06:00
|
|
|
cat_deref(_, _, UnsafePtr(..)) => { // yes, it's aliasable, but...
|
2013-03-15 14:24:24 -05:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2014-10-04 16:04:10 -05:00
|
|
|
cat_copied_upvar(CopiedUpvar {kind: kind, capturing_proc: id, ..}) => {
|
|
|
|
match kind {
|
|
|
|
Boxed(ast::Once) |
|
|
|
|
Unboxed(ty::FnOnceUnboxedClosureKind) |
|
|
|
|
Unboxed(ty::FnMutUnboxedClosureKind) => None,
|
|
|
|
Boxed(_) => Some(AliasableOther),
|
|
|
|
Unboxed(_) => Some(AliasableClosure(id))
|
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
|
2014-10-04 16:06:08 -05:00
|
|
|
cat_upvar(ty::UpvarId { closure_expr_id: id, .. }, _,
|
|
|
|
Some(ty::FnUnboxedClosureKind)) => {
|
|
|
|
Some(AliasableClosure(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
cat_upvar(..) => None,
|
|
|
|
|
2014-02-07 16:28:56 -06:00
|
|
|
cat_static_item(..) => {
|
2014-03-12 19:02:31 -05:00
|
|
|
let int_safe = if ty::type_interior_is_unsafe(ctxt, self.ty) {
|
|
|
|
InteriorUnsafe
|
|
|
|
} else {
|
|
|
|
InteriorSafe
|
|
|
|
};
|
|
|
|
|
2014-02-07 16:28:56 -06:00
|
|
|
if self.mutbl.is_mutable() {
|
2014-03-12 19:02:31 -05:00
|
|
|
Some(AliasableStaticMut(int_safe))
|
2014-02-07 16:28:56 -06:00
|
|
|
} else {
|
2014-03-12 19:02:31 -05:00
|
|
|
Some(AliasableStatic(int_safe))
|
2014-02-07 16:28:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 02:43:21 -05:00
|
|
|
cat_deref(_, _, BorrowedPtr(ty::ImmBorrow, _)) |
|
|
|
|
cat_deref(_, _, Implicit(ty::ImmBorrow, _)) => {
|
2014-02-07 13:41:58 -06:00
|
|
|
Some(AliasableBorrowed)
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-08 18:54:28 -05:00
|
|
|
impl Repr for cmt_ {
|
2014-05-28 11:24:28 -05:00
|
|
|
fn repr(&self, tcx: &ty::ctxt) -> String {
|
|
|
|
format!("{{{} id:{} m:{:?} ty:{}}}",
|
|
|
|
self.cat.repr(tcx),
|
|
|
|
self.id,
|
|
|
|
self.mutbl,
|
|
|
|
self.ty.repr(tcx))
|
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Repr for categorization {
|
2014-05-22 18:57:53 -05:00
|
|
|
fn repr(&self, tcx: &ty::ctxt) -> String {
|
2013-03-15 14:24:24 -05:00
|
|
|
match *self {
|
|
|
|
cat_static_item |
|
2013-11-28 14:22:53 -06:00
|
|
|
cat_rvalue(..) |
|
|
|
|
cat_copied_upvar(..) |
|
|
|
|
cat_local(..) |
|
2014-09-17 09:28:19 -05:00
|
|
|
cat_upvar(..) => {
|
2014-05-27 22:44:58 -05:00
|
|
|
format!("{:?}", *self)
|
2013-06-20 14:25:52 -05:00
|
|
|
}
|
2014-04-15 05:15:56 -05:00
|
|
|
cat_deref(ref cmt, derefs, ptr) => {
|
2014-05-27 22:44:58 -05:00
|
|
|
format!("{}-{}{}->", cmt.cat.repr(tcx), ptr_sigil(ptr), derefs)
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2014-04-15 05:15:56 -05:00
|
|
|
cat_interior(ref cmt, interior) => {
|
2014-05-27 22:44:58 -05:00
|
|
|
format!("{}.{}", cmt.cat.repr(tcx), interior.repr(tcx))
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
2014-04-15 05:15:56 -05:00
|
|
|
cat_downcast(ref cmt) => {
|
2014-05-27 22:44:58 -05:00
|
|
|
format!("{}->(enum)", cmt.cat.repr(tcx))
|
2013-05-17 20:12:50 -05:00
|
|
|
}
|
2014-04-15 05:15:56 -05:00
|
|
|
cat_discr(ref cmt, _) => {
|
2013-06-20 14:25:52 -05:00
|
|
|
cmt.cat.repr(tcx)
|
|
|
|
}
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
|
2014-02-07 13:41:58 -06:00
|
|
|
pub fn ptr_sigil(ptr: PointerKind) -> &'static str {
|
2013-03-15 14:24:24 -05:00
|
|
|
match ptr {
|
2014-06-21 00:08:58 -05:00
|
|
|
OwnedPtr => "Box",
|
2014-07-14 02:43:21 -05:00
|
|
|
BorrowedPtr(ty::ImmBorrow, _) |
|
|
|
|
Implicit(ty::ImmBorrow, _) => "&",
|
|
|
|
BorrowedPtr(ty::MutBorrow, _) |
|
|
|
|
Implicit(ty::MutBorrow, _) => "&mut",
|
|
|
|
BorrowedPtr(ty::UniqueImmBorrow, _) |
|
|
|
|
Implicit(ty::UniqueImmBorrow, _) => "&unique",
|
2014-02-07 13:41:58 -06:00
|
|
|
UnsafePtr(_) => "*"
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
}
|
2013-01-11 23:01:42 -06:00
|
|
|
|
2013-05-17 20:12:50 -05:00
|
|
|
impl Repr for InteriorKind {
|
2014-05-22 18:57:53 -05:00
|
|
|
fn repr(&self, _tcx: &ty::ctxt) -> String {
|
2013-01-11 23:01:42 -06:00
|
|
|
match *self {
|
2014-01-31 18:03:55 -06:00
|
|
|
InteriorField(NamedField(fld)) => {
|
2014-06-21 05:39:03 -05:00
|
|
|
token::get_name(fld).get().to_string()
|
2014-01-31 18:03:55 -06:00
|
|
|
}
|
2014-05-28 11:24:28 -05:00
|
|
|
InteriorField(PositionalField(i)) => format!("#{:?}", i),
|
2014-05-25 05:17:19 -05:00
|
|
|
InteriorElement(_) => "[]".to_string(),
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 05:54:35 -05:00
|
|
|
|
|
|
|
fn element_kind(t: ty::t) -> ElementKind {
|
|
|
|
match ty::get(t).sty {
|
2014-04-09 02:15:31 -05:00
|
|
|
ty::ty_rptr(_, ty::mt{ty:ty, ..}) |
|
|
|
|
ty::ty_uniq(ty) => match ty::get(ty).sty {
|
|
|
|
ty::ty_vec(_, None) => VecElement,
|
|
|
|
_ => OtherElement
|
|
|
|
},
|
2013-12-31 20:09:50 -06:00
|
|
|
ty::ty_vec(..) => VecElement,
|
2013-05-22 05:54:35 -05:00
|
|
|
_ => OtherElement
|
|
|
|
}
|
|
|
|
}
|