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.
|
|
|
|
|
2014-11-25 20:17:11 -06: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
|
|
|
|
//! | x // address of a local variable or argument
|
|
|
|
//! | *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.
|
|
|
|
//!
|
|
|
|
//! Now, cat_expr() classifies the expression Expr and the address A=ToAddr(Expr)
|
|
|
|
//! 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
|
|
|
|
//! decomposed into two operations: a dereference to reach the array data and
|
|
|
|
//! then an index to jump forward to the relevant item.
|
|
|
|
//!
|
|
|
|
//! ## By-reference upvars
|
|
|
|
//!
|
|
|
|
//! One part of the translation which may be non-obvious is that we translate
|
|
|
|
//! closure upvars into the dereference of a borrowed pointer; this more closely
|
|
|
|
//! 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-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-11-06 02:05:53 -06:00
|
|
|
pub use self::PointerKind::*;
|
|
|
|
pub use self::InteriorKind::*;
|
|
|
|
pub use self::FieldName::*;
|
|
|
|
pub use self::ElementKind::*;
|
|
|
|
pub use self::MutabilityCategory::*;
|
|
|
|
pub use self::InteriorSafety::*;
|
|
|
|
pub use self::AliasableReason::*;
|
|
|
|
pub use self::Note::*;
|
|
|
|
pub use self::deref_kind::*;
|
|
|
|
pub use self::categorization::*;
|
|
|
|
|
2014-05-14 14:31:30 -05:00
|
|
|
use middle::def;
|
2014-11-18 07:22:59 -06:00
|
|
|
use middle::region;
|
2014-09-13 13:09:25 -05:00
|
|
|
use middle::ty::{mod, Ty};
|
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-10-15 01:25:34 -05:00
|
|
|
#[deriving(Clone, PartialEq, Show)]
|
2014-09-29 14:11:30 -05:00
|
|
|
pub enum categorization<'tcx> {
|
|
|
|
cat_rvalue(ty::Region), // temporary val, argument is its scope
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_static_item,
|
2014-09-29 14:11:30 -05:00
|
|
|
cat_upvar(Upvar), // upvar referenced by closure env
|
|
|
|
cat_local(ast::NodeId), // local variable
|
|
|
|
cat_deref(cmt<'tcx>, uint, PointerKind), // deref of a ptr
|
|
|
|
cat_interior(cmt<'tcx>, InteriorKind), // something interior: field, tuple, etc
|
2014-09-18 00:58:26 -05:00
|
|
|
cat_downcast(cmt<'tcx>, ast::DefId), // selects a particular enum variant (*1)
|
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-07 22:04:45 -05:00
|
|
|
// Represents any kind of upvar
|
2014-10-15 01:25:34 -05:00
|
|
|
#[deriving(Clone, PartialEq, Show)]
|
2014-10-07 22:04:45 -05:00
|
|
|
pub struct Upvar {
|
|
|
|
pub id: ty::UpvarId,
|
|
|
|
// Unboxed closure kinds are used even for old-style closures for simplicity
|
|
|
|
pub kind: ty::UnboxedClosureKind,
|
|
|
|
// Is this from an unboxed closure? Used only for diagnostics.
|
|
|
|
pub is_unboxed: bool
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for Upvar {}
|
|
|
|
|
2012-08-08 10:15:32 -05:00
|
|
|
// different kinds of pointers:
|
2014-10-15 01:25:34 -05:00
|
|
|
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
|
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
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for PointerKind {}
|
|
|
|
|
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-10-15 01:25:34 -05:00
|
|
|
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
|
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
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for InteriorKind {}
|
|
|
|
|
2014-10-15 01:25:34 -05:00
|
|
|
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
|
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
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for FieldName {}
|
|
|
|
|
2014-10-15 01:25:34 -05:00
|
|
|
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
|
2013-05-22 05:54:35 -05:00
|
|
|
pub enum ElementKind {
|
|
|
|
VecElement,
|
|
|
|
OtherElement,
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for ElementKind {}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for MutabilityCategory {}
|
|
|
|
|
2014-10-07 22:04:45 -05:00
|
|
|
// A note about the provenance of a `cmt`. This is used for
|
|
|
|
// special-case handling of upvars such as mutability inference.
|
|
|
|
// Upvar categorization can generate a variable number of nested
|
|
|
|
// derefs. The note allows detecting them without deep pattern
|
|
|
|
// matching on the categorization.
|
|
|
|
#[deriving(Clone, PartialEq, Show)]
|
|
|
|
pub enum Note {
|
|
|
|
NoteClosureEnv(ty::UpvarId), // Deref through closure env
|
|
|
|
NoteUpvarRef(ty::UpvarId), // Deref through by-ref upvar
|
|
|
|
NoteNone // Nothing special
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for Note {}
|
|
|
|
|
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-10-15 01:25:34 -05:00
|
|
|
#[deriving(Clone, PartialEq, Show)]
|
2014-09-29 14:11:30 -05:00
|
|
|
pub struct cmt_<'tcx> {
|
|
|
|
pub id: ast::NodeId, // id of expr/pat producing this value
|
2014-03-28 12:05:27 -05:00
|
|
|
pub span: Span, // span of same expr/pat
|
2014-09-29 14:11:30 -05:00
|
|
|
pub cat: categorization<'tcx>, // categorization of expr
|
2014-03-28 12:05:27 -05:00
|
|
|
pub mutbl: MutabilityCategory, // mutability of expr as lvalue
|
2014-09-29 14:11:30 -05:00
|
|
|
pub ty: Ty<'tcx>, // type of the expr (*see WARNING above*)
|
2014-10-07 22:04:45 -05:00
|
|
|
pub note: Note, // Note about the provenance of this cmt
|
2013-01-25 18:57:39 -06:00
|
|
|
}
|
2012-08-08 10:15:32 -05:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub type cmt<'tcx> = Rc<cmt_<'tcx>>;
|
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
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for deref_kind {}
|
|
|
|
|
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).
|
2014-09-13 13:09:25 -05:00
|
|
|
pub fn opt_deref_kind(t: Ty) -> Option<deref_kind> {
|
2014-10-31 03:51:16 -05:00
|
|
|
match 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-09-29 14:11:30 -05:00
|
|
|
pub fn deref_kind<'tcx>(tcx: &ty::ctxt<'tcx>, t: Ty<'tcx>) -> 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
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl<'t,TYPER:'t> Copy for MemCategorizationContext<'t,TYPER> {}
|
|
|
|
|
2014-02-07 13:41:58 -06:00
|
|
|
pub type McResult<T> = Result<T, ()>;
|
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `Typer` trait provides the interface for the mem-categorization
|
|
|
|
/// 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-09-29 14:11:30 -05:00
|
|
|
fn node_ty(&self, id: ast::NodeId) -> McResult<Ty<'tcx>>;
|
2014-11-25 13:21:20 -06:00
|
|
|
fn node_method_ty(&self, method_call: ty::MethodCall) -> Option<Ty<'tcx>>;
|
2014-09-29 14:11:30 -05:00
|
|
|
fn adjustments<'a>(&'a self) -> &'a RefCell<NodeMap<ty::AutoAdjustment<'tcx>>>;
|
2014-04-10 08:43:26 -05:00
|
|
|
fn is_method_call(&self, id: ast::NodeId) -> bool;
|
2014-11-18 07:22:59 -06:00
|
|
|
fn temporary_scope(&self, rvalue_id: ast::NodeId) -> Option<region::CodeExtent>;
|
2014-04-10 08:43:26 -05:00
|
|
|
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)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> &'a RefCell<DefIdMap<ty::UnboxedClosure<'tcx>>>;
|
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-09-29 14:11:30 -05:00
|
|
|
fn expr_ty(&self, expr: &ast::Expr) -> McResult<Ty<'tcx>> {
|
2014-02-07 13:41:58 -06:00
|
|
|
self.typer.node_ty(expr.id)
|
2013-05-03 11:11:15 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn expr_ty_adjusted(&self, expr: &ast::Expr) -> McResult<Ty<'tcx>> {
|
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,
|
2014-11-06 11:25:16 -06:00
|
|
|
self.typer.adjustments().borrow().get(&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-09-29 14:11:30 -05:00
|
|
|
fn node_ty(&self, id: ast::NodeId) -> McResult<Ty<'tcx>> {
|
2014-02-07 13:41:58 -06:00
|
|
|
self.typer.node_ty(id)
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn pat_ty(&self, pat: &ast::Pat) -> McResult<Ty<'tcx>> {
|
2014-10-13 20:21:54 -05:00
|
|
|
let tcx = self.typer.tcx();
|
|
|
|
let base_ty = self.typer.node_ty(pat.id);
|
|
|
|
// FIXME (Issue #18207): This code detects whether we are
|
|
|
|
// looking at a `ref x`, and if so, figures out what the type
|
|
|
|
// *being borrowed* is. But ideally we would put in a more
|
|
|
|
// fundamental fix to this conflated use of the node id.
|
|
|
|
let ret_ty = match pat.node {
|
|
|
|
ast::PatIdent(ast::BindByRef(_), _, _) => {
|
|
|
|
// a bind-by-ref means that the base_ty will be the type of the ident itself,
|
|
|
|
// but what we want here is the type of the underlying value being borrowed.
|
|
|
|
// So peel off one-level, turning the &T into T.
|
|
|
|
base_ty.map(|t| {
|
|
|
|
ty::deref(t, false).unwrap_or_else(|| {
|
|
|
|
panic!("encountered BindByRef with non &-type");
|
|
|
|
}).ty
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => base_ty,
|
|
|
|
};
|
|
|
|
debug!("pat_ty(pat={}) base_ty={} ret_ty={}",
|
|
|
|
pat.repr(tcx), base_ty.repr(tcx), ret_ty.repr(tcx));
|
|
|
|
ret_ty
|
2014-02-07 13:41:58 -06:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn cat_expr(&self, expr: &ast::Expr) -> McResult<cmt<'tcx>> {
|
2014-11-06 11:25:16 -06:00
|
|
|
match self.typer.adjustments().borrow().get(&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 {
|
2014-10-05 19:36:53 -05:00
|
|
|
autoref: None, 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-09-29 14:11:30 -05:00
|
|
|
-> McResult<cmt<'tcx>> {
|
2014-02-07 13:41:58 -06:00
|
|
|
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-09-29 14:11:30 -05:00
|
|
|
pub fn cat_expr_unadjusted(&self, expr: &ast::Expr) -> McResult<cmt<'tcx>> {
|
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-11-23 05:14:35 -06:00
|
|
|
ast::ExprField(ref base, f_name) => {
|
2014-05-16 12:15:33 -05:00
|
|
|
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-09-30 19:11:34 -05:00
|
|
|
Ok(self.cat_field(expr, base_cmt, f_name.node.name, expr_ty))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2014-11-23 05:14:35 -06:00
|
|
|
ast::ExprTupField(ref base, idx) => {
|
2014-08-09 22:54:33 -05:00
|
|
|
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-11-25 13:21:20 -06:00
|
|
|
let method_call = ty::MethodCall::expr(expr.id());
|
2014-07-14 02:43:21 -05:00
|
|
|
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.
|
2014-10-24 14:14:37 -05:00
|
|
|
let ret_ty = ty::ty_fn_ret(method_ty).unwrap();
|
2014-07-14 02:43:21 -05:00
|
|
|
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-10-15 01:05:01 -05:00
|
|
|
let def = (*self.tcx().def_map.borrow())[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-11-26 09:07:22 -06:00
|
|
|
ast::ExprClosure(..) | 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");
|
|
|
|
}
|
2014-10-02 23:41:24 -05:00
|
|
|
ast::ExprWhileLet(..) => {
|
|
|
|
self.tcx().sess.span_bug(expr.span, "non-desugared ExprWhileLet");
|
|
|
|
}
|
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,
|
2014-09-29 14:11:30 -05:00
|
|
|
expr_ty: Ty<'tcx>,
|
2014-05-14 14:31:30 -05:00
|
|
|
def: def::Def)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> McResult<cmt<'tcx>> {
|
2014-10-15 01:25:34 -05:00
|
|
|
debug!("cat_def: id={} expr={} def={}",
|
2014-04-09 02:15:31 -05:00
|
|
|
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(..) |
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 10:17:01 -05:00
|
|
|
def::DefStaticMethod(..) | def::DefConst(..) => {
|
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,
|
2014-10-07 22:04:45 -05:00
|
|
|
ty:expr_ty,
|
|
|
|
note: NoteNone
|
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},
|
2014-10-07 22:04:45 -05:00
|
|
|
ty:expr_ty,
|
|
|
|
note: NoteNone
|
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));
|
2014-10-31 03:51:16 -05:00
|
|
|
match ty.sty {
|
2013-03-15 14:24:24 -05:00
|
|
|
ty::ty_closure(ref closure_ty) => {
|
2014-10-07 22:04:45 -05:00
|
|
|
// Translate old closure type info into unboxed
|
|
|
|
// closure kind/capture mode
|
|
|
|
let (mode, kind) = match (closure_ty.store, closure_ty.onceness) {
|
|
|
|
// stack closure
|
|
|
|
(ty::RegionTraitStore(..), ast::Many) => {
|
|
|
|
(ast::CaptureByRef, ty::FnMutUnboxedClosureKind)
|
|
|
|
}
|
|
|
|
// proc or once closure
|
|
|
|
(_, ast::Once) => {
|
|
|
|
(ast::CaptureByValue, ty::FnOnceUnboxedClosureKind)
|
|
|
|
}
|
|
|
|
// There should be no such old closure type
|
|
|
|
(ty::UniqTraitStore, ast::Many) => {
|
|
|
|
self.tcx().sess.span_bug(span, "Impossible closure type");
|
|
|
|
}
|
2013-06-19 14:53:05 -05:00
|
|
|
};
|
2014-10-07 22:04:45 -05:00
|
|
|
self.cat_upvar(id, span, var_id, fn_node_id, kind, mode, false)
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2014-10-18 12:46:57 -05:00
|
|
|
ty::ty_unboxed_closure(closure_id, _, _) => {
|
2014-10-07 22:04:45 -05:00
|
|
|
let unboxed_closures = self.typer.unboxed_closures().borrow();
|
2014-10-15 01:05:01 -05:00
|
|
|
let kind = (*unboxed_closures)[closure_id].kind;
|
2014-10-07 22:04:45 -05:00
|
|
|
let mode = self.typer.capture_mode(fn_node_id);
|
|
|
|
self.cat_upvar(id, span, var_id, fn_node_id, kind, mode, true)
|
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),
|
2014-10-07 22:04:45 -05:00
|
|
|
ty: expr_ty,
|
|
|
|
note: NoteNone
|
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-10-07 22:04:45 -05:00
|
|
|
// Categorize an upvar, complete with invisible derefs of closure
|
|
|
|
// environment and upvar reference as appropriate.
|
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,
|
2014-10-07 22:04:45 -05:00
|
|
|
kind: ty::UnboxedClosureKind,
|
|
|
|
mode: ast::CaptureClause,
|
|
|
|
is_unboxed: bool)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> McResult<cmt<'tcx>> {
|
2014-10-07 22:04:45 -05:00
|
|
|
// An upvar can have up to 3 components. The base is a
|
|
|
|
// `cat_upvar`. Next, we add a deref through the implicit
|
|
|
|
// environment pointer with an anonymous free region 'env and
|
|
|
|
// appropriate borrow kind for closure kinds that take self by
|
|
|
|
// reference. Finally, if the upvar was captured
|
|
|
|
// by-reference, we add a deref through that reference. The
|
|
|
|
// region of this reference is an inference variable 'up that
|
|
|
|
// was previously generated and recorded in the upvar borrow
|
|
|
|
// map. The borrow kind bk is inferred by based on how the
|
|
|
|
// upvar is used.
|
|
|
|
//
|
|
|
|
// This results in the following table for concrete closure
|
|
|
|
// types:
|
|
|
|
//
|
|
|
|
// | move | ref
|
|
|
|
// ---------------+----------------------+-------------------------------
|
|
|
|
// Fn | copied -> &'env | upvar -> &'env -> &'up bk
|
|
|
|
// FnMut | copied -> &'env mut | upvar -> &'env mut -> &'up bk
|
|
|
|
// FnOnce | copied | upvar -> &'up bk
|
|
|
|
// old stack | N/A | upvar -> &'env mut -> &'up bk
|
|
|
|
// old proc/once | copied | N/A
|
2014-10-25 23:40:25 -05:00
|
|
|
let var_ty = if_ok!(self.node_ty(var_id));
|
|
|
|
|
2014-02-07 13:41:58 -06:00
|
|
|
let upvar_id = ty::UpvarId { var_id: var_id,
|
|
|
|
closure_expr_id: fn_node_id };
|
|
|
|
|
2014-10-07 22:04:45 -05:00
|
|
|
// Mutability of original variable itself
|
|
|
|
let var_mutbl = MutabilityCategory::from_local(self.tcx(), var_id);
|
2014-02-07 13:41:58 -06:00
|
|
|
|
2014-10-25 23:40:25 -05:00
|
|
|
// Construct information about env pointer dereference, if any
|
|
|
|
let mutbl = match kind {
|
|
|
|
ty::FnOnceUnboxedClosureKind => None, // None, env is by-value
|
|
|
|
ty::FnMutUnboxedClosureKind => match mode { // Depends on capture type
|
|
|
|
ast::CaptureByValue => Some(var_mutbl), // Mutable if the original var is
|
|
|
|
ast::CaptureByRef => Some(McDeclared) // Mutable regardless
|
|
|
|
},
|
|
|
|
ty::FnUnboxedClosureKind => Some(McImmutable) // Never mutable
|
2014-10-07 22:04:45 -05:00
|
|
|
};
|
2014-10-25 23:40:25 -05:00
|
|
|
let env_info = mutbl.map(|env_mutbl| {
|
|
|
|
// Look up the node ID of the closure body so we can construct
|
|
|
|
// a free region within it
|
|
|
|
let fn_body_id = {
|
|
|
|
let fn_expr = match self.tcx().map.find(fn_node_id) {
|
|
|
|
Some(ast_map::NodeExpr(e)) => e,
|
|
|
|
_ => unreachable!()
|
|
|
|
};
|
2014-02-07 13:41:58 -06:00
|
|
|
|
2014-10-25 23:40:25 -05:00
|
|
|
match fn_expr.node {
|
2014-11-19 10:18:17 -06:00
|
|
|
ast::ExprClosure(_, _, _, ref body) => body.id,
|
2014-10-25 23:40:25 -05:00
|
|
|
_ => unreachable!()
|
|
|
|
}
|
2014-10-07 22:04:45 -05:00
|
|
|
};
|
|
|
|
|
2014-10-25 23:40:25 -05:00
|
|
|
// Region of environment pointer
|
|
|
|
let env_region = ty::ReFree(ty::FreeRegion {
|
2014-11-18 07:22:59 -06:00
|
|
|
scope: region::CodeExtent::from_node_id(fn_body_id),
|
2014-10-25 23:40:25 -05:00
|
|
|
bound_region: ty::BrEnv
|
|
|
|
});
|
2014-02-07 13:41:58 -06:00
|
|
|
|
2014-10-25 23:40:25 -05:00
|
|
|
let env_ptr = BorrowedPtr(if env_mutbl.is_mutable() {
|
|
|
|
ty::MutBorrow
|
|
|
|
} else {
|
|
|
|
ty::ImmBorrow
|
|
|
|
}, env_region);
|
2014-02-07 13:41:58 -06:00
|
|
|
|
2014-10-25 23:40:25 -05:00
|
|
|
(env_mutbl, env_ptr)
|
|
|
|
});
|
2014-10-07 22:04:45 -05:00
|
|
|
|
|
|
|
// First, switch by capture mode
|
|
|
|
Ok(match mode {
|
|
|
|
ast::CaptureByValue => {
|
|
|
|
let mut base = cmt_ {
|
|
|
|
id: id,
|
|
|
|
span: span,
|
|
|
|
cat: cat_upvar(Upvar {
|
|
|
|
id: upvar_id,
|
|
|
|
kind: kind,
|
|
|
|
is_unboxed: is_unboxed
|
|
|
|
}),
|
|
|
|
mutbl: var_mutbl,
|
|
|
|
ty: var_ty,
|
|
|
|
note: NoteNone
|
|
|
|
};
|
2014-02-07 13:41:58 -06:00
|
|
|
|
2014-10-25 23:40:25 -05:00
|
|
|
match env_info {
|
|
|
|
Some((env_mutbl, env_ptr)) => {
|
|
|
|
// We need to add the env deref. This means
|
|
|
|
// that the above is actually immutable and
|
|
|
|
// has a ref type. However, nothing should
|
|
|
|
// actually look at the type, so we can get
|
|
|
|
// away with stuffing a `ty_err` in there
|
|
|
|
// instead of bothering to construct a proper
|
|
|
|
// one.
|
|
|
|
base.mutbl = McImmutable;
|
|
|
|
base.ty = ty::mk_err();
|
|
|
|
Rc::new(cmt_ {
|
|
|
|
id: id,
|
|
|
|
span: span,
|
|
|
|
cat: cat_deref(Rc::new(base), 0, env_ptr),
|
|
|
|
mutbl: env_mutbl,
|
|
|
|
ty: var_ty,
|
|
|
|
note: NoteClosureEnv(upvar_id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
None => Rc::new(base)
|
2014-10-07 22:04:45 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
ast::CaptureByRef => {
|
|
|
|
// The type here is actually a ref (or ref of a ref),
|
|
|
|
// but we can again get away with not constructing one
|
|
|
|
// properly since it will never be used.
|
|
|
|
let mut base = cmt_ {
|
|
|
|
id: id,
|
|
|
|
span: span,
|
|
|
|
cat: cat_upvar(Upvar {
|
|
|
|
id: upvar_id,
|
|
|
|
kind: kind,
|
|
|
|
is_unboxed: is_unboxed
|
|
|
|
}),
|
|
|
|
mutbl: McImmutable,
|
|
|
|
ty: ty::mk_err(),
|
|
|
|
note: NoteNone
|
|
|
|
};
|
|
|
|
|
2014-10-25 23:40:25 -05:00
|
|
|
match env_info {
|
|
|
|
Some((env_mutbl, env_ptr)) => {
|
|
|
|
base = cmt_ {
|
|
|
|
id: id,
|
|
|
|
span: span,
|
|
|
|
cat: cat_deref(Rc::new(base), 0, env_ptr),
|
|
|
|
mutbl: env_mutbl,
|
|
|
|
ty: ty::mk_err(),
|
|
|
|
note: NoteClosureEnv(upvar_id)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
None => {}
|
2014-10-07 22:04:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look up upvar borrow so we can get its region
|
|
|
|
let upvar_borrow = self.typer.upvar_borrow(upvar_id);
|
|
|
|
let ptr = BorrowedPtr(upvar_borrow.kind, upvar_borrow.region);
|
|
|
|
|
|
|
|
Rc::new(cmt_ {
|
|
|
|
id: id,
|
|
|
|
span: span,
|
|
|
|
cat: cat_deref(Rc::new(base), 0, ptr),
|
|
|
|
mutbl: MutabilityCategory::from_borrow_kind(upvar_borrow.kind),
|
|
|
|
ty: var_ty,
|
|
|
|
note: NoteUpvarRef(upvar_id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2014-02-07 13:41:58 -06:00
|
|
|
}
|
|
|
|
|
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,
|
2014-09-29 14:11:30 -05:00
|
|
|
expr_ty: Ty<'tcx>)
|
|
|
|
-> cmt<'tcx> {
|
2014-02-07 13:41:58 -06:00
|
|
|
match self.typer.temporary_scope(id) {
|
2014-01-15 13:39:08 -06:00
|
|
|
Some(scope) => {
|
2014-10-31 03:51:16 -05:00
|
|
|
match expr_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(_, 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,
|
2014-09-29 14:11:30 -05:00
|
|
|
expr_ty: Ty<'tcx>) -> cmt<'tcx> {
|
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,
|
2014-10-07 22:04:45 -05:00
|
|
|
ty:expr_ty,
|
|
|
|
note: NoteNone
|
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,
|
2014-09-29 14:11:30 -05:00
|
|
|
base_cmt: cmt<'tcx>,
|
2014-09-30 19:11:34 -05:00
|
|
|
f_name: ast::Name,
|
2014-09-29 14:11:30 -05:00
|
|
|
f_ty: Ty<'tcx>)
|
|
|
|
-> cmt<'tcx> {
|
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-09-30 19:11:34 -05:00
|
|
|
cat: cat_interior(base_cmt, InteriorField(NamedField(f_name))),
|
2014-10-07 22:04:45 -05:00
|
|
|
ty: f_ty,
|
|
|
|
note: NoteNone
|
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,
|
2014-09-29 14:11:30 -05:00
|
|
|
base_cmt: cmt<'tcx>,
|
2014-08-09 22:54:33 -05:00
|
|
|
f_idx: uint,
|
2014-09-29 14:11:30 -05:00
|
|
|
f_ty: Ty<'tcx>)
|
|
|
|
-> cmt<'tcx> {
|
2014-08-09 22:54:33 -05:00
|
|
|
Rc::new(cmt_ {
|
|
|
|
id: node.id(),
|
|
|
|
span: node.span(),
|
|
|
|
mutbl: base_cmt.mutbl.inherit(),
|
|
|
|
cat: cat_interior(base_cmt, InteriorField(PositionalField(f_idx))),
|
2014-10-07 22:04:45 -05:00
|
|
|
ty: f_ty,
|
|
|
|
note: NoteNone
|
2014-08-09 22:54:33 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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,
|
2014-09-29 14:11:30 -05:00
|
|
|
base_cmt: cmt<'tcx>,
|
2014-07-14 02:43:21 -05:00
|
|
|
deref_cnt: uint,
|
|
|
|
implicit: bool)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> cmt<'tcx> {
|
2014-11-06 11:25:16 -06:00
|
|
|
let adjustment = match self.typer.adjustments().borrow().get(&node.id()) {
|
2014-11-25 13:21:20 -06:00
|
|
|
Some(adj) if ty::adjust_is_object(adj) => ty::AutoObject,
|
|
|
|
_ if deref_cnt != 0 => ty::AutoDeref(deref_cnt),
|
|
|
|
_ => ty::NoAdjustment
|
2014-06-11 17:01:48 -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
|
|
|
|
2014-11-25 13:21:20 -06:00
|
|
|
let method_call = ty::MethodCall {
|
2014-03-06 11:24:11 -06:00
|
|
|
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);
|
|
|
|
|
2014-10-15 01:25:34 -05:00
|
|
|
debug!("cat_deref: method_call={} method_ty={}",
|
2014-03-06 11:24:11 -06:00
|
|
|
method_call, method_ty.map(|ty| ty.repr(self.tcx())));
|
|
|
|
|
|
|
|
let base_cmt = match method_ty {
|
|
|
|
Some(method_ty) => {
|
2014-10-24 14:14:37 -05:00
|
|
|
let ref_ty = ty::ty_fn_ret(method_ty).unwrap();
|
2014-03-06 11:24:11 -06:00
|
|
|
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,
|
2014-09-29 14:11:30 -05:00
|
|
|
base_cmt: cmt<'tcx>,
|
2014-03-06 11:24:11 -06:00
|
|
|
deref_cnt: uint,
|
2014-09-29 14:11:30 -05:00
|
|
|
deref_ty: Ty<'tcx>,
|
2014-07-14 02:43:21 -05:00
|
|
|
implicit: bool)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> cmt<'tcx> {
|
2014-03-06 11:24:11 -06:00
|
|
|
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,
|
2014-10-07 22:04:45 -05:00
|
|
|
ty: deref_ty,
|
|
|
|
note: NoteNone
|
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,
|
2014-09-29 14:11:30 -05:00
|
|
|
mut base_cmt: cmt<'tcx>)
|
|
|
|
-> cmt<'tcx> {
|
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-11-25 13:21:20 -06:00
|
|
|
let method_call = ty::MethodCall::expr(elt.id());
|
2014-07-03 16:32:41 -05:00
|
|
|
let method_ty = self.typer.node_method_ty(method_call);
|
|
|
|
|
|
|
|
let element_ty = match method_ty {
|
|
|
|
Some(method_ty) => {
|
2014-10-24 14:14:37 -05:00
|
|
|
let ref_ty = ty::ty_fn_ret(method_ty).unwrap();
|
2014-07-03 16:32:41 -05:00
|
|
|
base_cmt = self.cat_rvalue_node(elt.id(), elt.span(), ref_ty);
|
2014-10-15 01:05:01 -05:00
|
|
|
ty::ty_fn_args(method_ty)[0]
|
2014-07-03 16:32:41 -05:00
|
|
|
}
|
|
|
|
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-09-29 14:11:30 -05:00
|
|
|
fn interior<'tcx, N: ast_node>(elt: &N,
|
|
|
|
of_cmt: cmt<'tcx>,
|
|
|
|
vec_ty: Ty<'tcx>,
|
|
|
|
mutbl: MutabilityCategory,
|
|
|
|
element_ty: Ty<'tcx>) -> cmt<'tcx>
|
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,
|
2014-10-07 22:04:45 -05:00
|
|
|
ty:element_ty,
|
|
|
|
note: NoteNone
|
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,
|
2014-09-29 14:11:30 -05:00
|
|
|
base_cmt: cmt<'tcx>)
|
|
|
|
-> cmt<'tcx> {
|
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 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")
|
2014-10-07 22:04:45 -05:00
|
|
|
},
|
|
|
|
note: NoteNone
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
deref_interior(_) => {
|
|
|
|
base_cmt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// 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.
|
2014-04-10 08:43:26 -05:00
|
|
|
pub fn cat_slice_pattern(&self,
|
2014-09-29 14:11:30 -05:00
|
|
|
vec_cmt: cmt<'tcx>,
|
2014-04-15 05:15:56 -05:00
|
|
|
slice_pat: &ast::Pat)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> McResult<(cmt<'tcx>, ast::Mutability, ty::Region)> {
|
2014-02-07 13:41:58 -06:00
|
|
|
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-11-25 20:17:11 -06:00
|
|
|
/// 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.
|
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-09-13 13:09:25 -05:00
|
|
|
slice_ty: Ty)
|
2014-02-07 13:41:58 -06:00
|
|
|
-> (ast::Mutability, ty::Region) {
|
2014-10-31 03:51:16 -05:00
|
|
|
match slice_ty.sty {
|
|
|
|
ty::ty_rptr(r, ref mt) => match 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,
|
2014-09-29 14:11:30 -05:00
|
|
|
base_cmt: cmt<'tcx>,
|
|
|
|
interior_ty: Ty<'tcx>,
|
2013-05-31 17:17:22 -05:00
|
|
|
interior: InteriorKind)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> cmt<'tcx> {
|
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),
|
2014-10-07 22:04:45 -05:00
|
|
|
ty: interior_ty,
|
|
|
|
note: NoteNone
|
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,
|
2014-09-29 14:11:30 -05:00
|
|
|
base_cmt: cmt<'tcx>,
|
2014-09-16 07:14:59 -05:00
|
|
|
downcast_ty: Ty<'tcx>,
|
|
|
|
variant_did: ast::DefId)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> cmt<'tcx> {
|
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-09-16 07:14:59 -05:00
|
|
|
cat: cat_downcast(base_cmt, variant_did),
|
2014-10-07 22:04:45 -05:00
|
|
|
ty: downcast_ty,
|
|
|
|
note: NoteNone
|
2014-04-15 05:15:56 -05:00
|
|
|
})
|
2013-05-17 20:12:50 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 19:26:43 -06:00
|
|
|
// FIXME(#19596) unbox `op`
|
2014-04-10 08:43:26 -05:00
|
|
|
pub fn cat_pattern(&self,
|
2014-09-29 14:11:30 -05:00
|
|
|
cmt: cmt<'tcx>,
|
2014-04-10 08:43:26 -05:00
|
|
|
pat: &ast::Pat,
|
2014-12-08 19:26:43 -06:00
|
|
|
op: |&MemCategorizationContext<'t, TYPER>, cmt<'tcx>, &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
|
|
|
|
2014-09-16 07:14:59 -05:00
|
|
|
let def_map = self.tcx().def_map.borrow();
|
|
|
|
let opt_def = def_map.get(&pat.id);
|
|
|
|
|
|
|
|
// Note: This goes up here (rather than within the PatEnum arm
|
|
|
|
// alone) because struct patterns can refer to struct types or
|
|
|
|
// to struct variants within enums.
|
|
|
|
let cmt = match opt_def {
|
|
|
|
Some(&def::DefVariant(enum_did, variant_did, _))
|
|
|
|
// univariant enums do not need downcasts
|
|
|
|
if !ty::enum_is_univariant(self.tcx(), enum_did) => {
|
|
|
|
self.cat_downcast(pat, cmt.clone(), cmt.ty, variant_did)
|
|
|
|
}
|
|
|
|
_ => cmt
|
|
|
|
};
|
|
|
|
|
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-09-16 07:14:59 -05:00
|
|
|
match opt_def {
|
|
|
|
Some(&def::DefVariant(..)) => {
|
2012-11-02 11:56:09 -05:00
|
|
|
// variant(x, y, z)
|
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-09-16 07:14:59 -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-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
|
|
|
}
|
|
|
|
}
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 10:17:01 -05:00
|
|
|
Some(&def::DefConst(..)) => {
|
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-10-05 19:36:53 -05:00
|
|
|
let field_ty = if_ok!(self.pat_ty(&*fp.node.pat)); // see (*2)
|
|
|
|
let cmt_field = self.cat_field(pat, cmt.clone(), fp.node.ident.name, field_ty);
|
|
|
|
if_ok!(self.cat_pattern(cmt_field, &*fp.node.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-09-29 14:11:30 -05:00
|
|
|
pub fn cmt_to_string(&self, cmt: &cmt_<'tcx>) -> String {
|
2014-10-07 22:04:45 -05:00
|
|
|
fn upvar_to_string(upvar: &Upvar, is_copy: bool) -> String {
|
|
|
|
if upvar.is_unboxed {
|
|
|
|
let kind = match upvar.kind {
|
|
|
|
ty::FnUnboxedClosureKind => "Fn",
|
|
|
|
ty::FnMutUnboxedClosureKind => "FnMut",
|
|
|
|
ty::FnOnceUnboxedClosureKind => "FnOnce"
|
|
|
|
};
|
|
|
|
format!("captured outer variable in an `{}` closure", kind)
|
|
|
|
} else {
|
|
|
|
(match (upvar.kind, is_copy) {
|
|
|
|
(ty::FnOnceUnboxedClosureKind, true) => "captured outer variable in a proc",
|
|
|
|
_ => "captured outer variable"
|
|
|
|
}).to_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-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-10-07 22:04:45 -05:00
|
|
|
cat_deref(_, _, pk) => {
|
|
|
|
let upvar = cmt.upvar();
|
|
|
|
match upvar.as_ref().map(|i| &i.cat) {
|
|
|
|
Some(&cat_upvar(ref var)) => {
|
|
|
|
upvar_to_string(var, false)
|
2014-02-07 13:41:58 -06:00
|
|
|
}
|
2014-10-07 22:04:45 -05:00
|
|
|
Some(_) => unreachable!(),
|
|
|
|
None => {
|
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-10-07 22:04:45 -05:00
|
|
|
cat_upvar(ref var) => {
|
|
|
|
upvar_to_string(var, true)
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2014-09-16 07:14:59 -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
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for InteriorSafety {}
|
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
pub enum AliasableReason {
|
2014-02-07 13:41:58 -06:00
|
|
|
AliasableBorrowed,
|
2014-10-07 22:04:45 -05:00
|
|
|
AliasableClosure(ast::NodeId), // Aliasable due to capture Fn closure env
|
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
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for AliasableReason {}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> cmt_<'tcx> {
|
|
|
|
pub fn guarantor(&self) -> cmt<'tcx> {
|
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_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-09-16 07:14:59 -05:00
|
|
|
cat_downcast(ref b, _) |
|
2014-04-15 05:15:56 -05:00
|
|
|
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-11-25 20:17:11 -06:00
|
|
|
/// Returns `Some(_)` if this lvalue represents a freely aliasable pointer type.
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn freely_aliasable(&self, ctxt: &ty::ctxt<'tcx>)
|
|
|
|
-> Option<AliasableReason> {
|
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-09-16 07:14:59 -05:00
|
|
|
cat_downcast(ref b, _) |
|
2014-04-15 05:15:56 -05:00
|
|
|
cat_deref(ref b, _, OwnedPtr) |
|
2014-10-09 06:58:45 -05:00
|
|
|
cat_interior(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-10-07 22:04:45 -05:00
|
|
|
cat_upvar(..) |
|
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-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-10-07 22:04:45 -05:00
|
|
|
cat_deref(ref base, _, BorrowedPtr(ty::ImmBorrow, _)) |
|
|
|
|
cat_deref(ref base, _, Implicit(ty::ImmBorrow, _)) => {
|
|
|
|
match base.cat {
|
|
|
|
cat_upvar(Upvar{ id, .. }) => Some(AliasableClosure(id.closure_expr_id)),
|
|
|
|
_ => Some(AliasableBorrowed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Digs down through one or two layers of deref and grabs the cmt
|
|
|
|
// for the upvar if a note indicates there is one.
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn upvar(&self) -> Option<cmt<'tcx>> {
|
2014-10-07 22:04:45 -05:00
|
|
|
match self.note {
|
|
|
|
NoteClosureEnv(..) | NoteUpvarRef(..) => {
|
|
|
|
Some(match self.cat {
|
|
|
|
cat_deref(ref inner, _, _) => {
|
|
|
|
match inner.cat {
|
|
|
|
cat_deref(ref inner, _, _) => inner.clone(),
|
|
|
|
cat_upvar(..) => inner.clone(),
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unreachable!()
|
|
|
|
})
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2014-10-07 22:04:45 -05:00
|
|
|
NoteNone => None
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> Repr<'tcx> for cmt_<'tcx> {
|
|
|
|
fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
|
2014-10-15 01:25:34 -05:00
|
|
|
format!("{{{} id:{} m:{} ty:{}}}",
|
2014-05-28 11:24:28 -05:00
|
|
|
self.cat.repr(tcx),
|
|
|
|
self.id,
|
|
|
|
self.mutbl,
|
|
|
|
self.ty.repr(tcx))
|
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> Repr<'tcx> for categorization<'tcx> {
|
|
|
|
fn repr(&self, tcx: &ty::ctxt<'tcx>) -> 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_local(..) |
|
2014-09-17 09:28:19 -05:00
|
|
|
cat_upvar(..) => {
|
2014-10-15 01:25:34 -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-09-16 07:14:59 -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
|
|
|
}
|
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
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> Repr<'tcx> 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-10-15 01:25:34 -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
|
|
|
|
2014-09-13 13:09:25 -05:00
|
|
|
fn element_kind(t: Ty) -> ElementKind {
|
2014-10-31 03:51:16 -05:00
|
|
|
match t.sty {
|
2014-10-05 19:36:53 -05:00
|
|
|
ty::ty_rptr(_, ty::mt{ty, ..}) |
|
2014-10-31 03:51:16 -05:00
|
|
|
ty::ty_uniq(ty) => match ty.sty {
|
2014-04-09 02:15:31 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|