2015-09-06 21:51:58 +03:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08: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-06 00:05:53 -08:00
|
|
|
pub use self::Variance::*;
|
|
|
|
pub use self::DtorKind::*;
|
2016-11-10 02:06:34 +02:00
|
|
|
pub use self::AssociatedItemContainer::*;
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::BorrowKind::*;
|
|
|
|
pub use self::IntVarValue::*;
|
2015-09-03 13:10:34 -04:00
|
|
|
pub use self::LvaluePreference::*;
|
2015-12-18 10:07:06 +00:00
|
|
|
pub use self::fold::TypeFoldable;
|
2014-11-06 00:05:53 -08:00
|
|
|
|
2015-12-22 16:39:33 -05:00
|
|
|
use dep_graph::{self, DepNode};
|
2016-03-29 08:50:44 +03:00
|
|
|
use hir::map as ast_map;
|
2014-12-07 11:10:48 -05:00
|
|
|
use middle;
|
2016-09-15 00:51:46 +03:00
|
|
|
use hir::def::{Def, CtorKind, PathResolution, ExportMap};
|
2016-09-17 13:34:55 +03:00
|
|
|
use hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
|
2015-03-15 04:01:57 +02:00
|
|
|
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
|
2016-05-02 23:11:19 +03:00
|
|
|
use middle::region::{CodeExtent, ROOT_CODE_EXTENT};
|
2016-10-28 13:55:49 +03:00
|
|
|
use mir::Mir;
|
2016-03-22 17:30:57 +02:00
|
|
|
use traits;
|
|
|
|
use ty;
|
2016-08-17 06:32:00 +03:00
|
|
|
use ty::subst::{Subst, Substs};
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::walk::TypeWalker;
|
2015-12-22 16:39:33 -05:00
|
|
|
use util::common::MemoizationMap;
|
2016-03-29 13:14:01 +03:00
|
|
|
use util::nodemap::NodeSet;
|
2016-11-08 14:02:55 +11:00
|
|
|
use util::nodemap::FxHashMap;
|
2014-12-07 11:10:48 -05:00
|
|
|
|
2016-08-30 14:24:14 +03:00
|
|
|
use serialize::{self, Encodable, Encoder};
|
2016-04-12 22:58:55 +09:00
|
|
|
use std::borrow::Cow;
|
2016-10-28 13:55:49 +03:00
|
|
|
use std::cell::{Cell, RefCell, Ref};
|
2015-09-14 14:55:56 +03:00
|
|
|
use std::hash::{Hash, Hasher};
|
2015-07-22 21:53:52 +03:00
|
|
|
use std::iter;
|
2016-09-02 11:08:16 +03:00
|
|
|
use std::ops::Deref;
|
2014-02-01 15:57:59 +11:00
|
|
|
use std::rc::Rc;
|
2015-07-22 21:53:52 +03:00
|
|
|
use std::slice;
|
2015-02-28 23:24:05 -08:00
|
|
|
use std::vec::IntoIter;
|
2016-10-24 18:23:29 -06:00
|
|
|
use std::mem;
|
2016-08-31 14:00:29 +03:00
|
|
|
use syntax::ast::{self, Name, NodeId};
|
2016-08-23 03:54:53 +00:00
|
|
|
use syntax::attr;
|
2016-09-17 13:34:55 +03:00
|
|
|
use syntax::parse::token::{self, InternedString};
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::{DUMMY_SP, Span};
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-03-15 12:33:13 +01:00
|
|
|
use rustc_const_math::ConstInt;
|
2015-12-16 18:44:15 +01:00
|
|
|
|
2016-03-29 08:50:44 +03:00
|
|
|
use hir;
|
2016-11-02 18:22:59 -04:00
|
|
|
use hir::itemlikevisit::ItemLikeVisitor;
|
2012-06-06 14:19:52 -07:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
pub use self::sty::{Binder, DebruijnIndex};
|
2016-08-04 15:52:57 +03:00
|
|
|
pub use self::sty::{BuiltinBound, BuiltinBounds};
|
2016-07-31 22:33:41 +08:00
|
|
|
pub use self::sty::{BareFnTy, FnSig, PolyFnSig};
|
2016-08-04 15:52:57 +03:00
|
|
|
pub use self::sty::{ClosureTy, InferTy, ParamTy, ProjectionTy, TraitObject};
|
2015-09-06 21:51:58 +03:00
|
|
|
pub use self::sty::{ClosureSubsts, TypeAndMut};
|
|
|
|
pub use self::sty::{TraitRef, TypeVariants, PolyTraitRef};
|
2016-08-04 15:52:57 +03:00
|
|
|
pub use self::sty::{ExistentialTraitRef, PolyExistentialTraitRef};
|
|
|
|
pub use self::sty::{ExistentialProjection, PolyExistentialProjection};
|
2015-09-06 21:51:58 +03:00
|
|
|
pub use self::sty::{BoundRegion, EarlyBoundRegion, FreeRegion, Region};
|
2016-04-21 05:10:10 -04:00
|
|
|
pub use self::sty::Issue32330;
|
2015-09-06 21:51:58 +03:00
|
|
|
pub use self::sty::{TyVid, IntVid, FloatVid, RegionVid, SkolemizedRegionVid};
|
|
|
|
pub use self::sty::BoundRegion::*;
|
|
|
|
pub use self::sty::InferTy::*;
|
|
|
|
pub use self::sty::Region::*;
|
|
|
|
pub use self::sty::TypeVariants::*;
|
|
|
|
|
|
|
|
pub use self::sty::BuiltinBound::Send as BoundSend;
|
|
|
|
pub use self::sty::BuiltinBound::Sized as BoundSized;
|
|
|
|
pub use self::sty::BuiltinBound::Copy as BoundCopy;
|
|
|
|
pub use self::sty::BuiltinBound::Sync as BoundSync;
|
|
|
|
|
|
|
|
pub use self::contents::TypeContents;
|
2016-02-29 23:36:51 +00:00
|
|
|
pub use self::context::{TyCtxt, tls};
|
2015-09-06 21:51:58 +03:00
|
|
|
pub use self::context::{CtxtArenas, Lift, Tables};
|
|
|
|
|
2015-12-22 16:39:33 -05:00
|
|
|
pub use self::trait_def::{TraitDef, TraitFlags};
|
|
|
|
|
2015-09-14 14:55:56 +03:00
|
|
|
pub mod adjustment;
|
2015-09-06 18:32:34 +03:00
|
|
|
pub mod cast;
|
2015-09-06 21:51:58 +03:00
|
|
|
pub mod error;
|
2015-09-06 18:32:34 +03:00
|
|
|
pub mod fast_reject;
|
|
|
|
pub mod fold;
|
2016-03-16 05:57:03 -04:00
|
|
|
pub mod item_path;
|
2016-04-18 16:03:16 +03:00
|
|
|
pub mod layout;
|
2015-09-06 18:32:34 +03:00
|
|
|
pub mod _match;
|
2015-12-22 16:39:33 -05:00
|
|
|
pub mod maps;
|
2015-09-06 18:32:34 +03:00
|
|
|
pub mod outlives;
|
|
|
|
pub mod relate;
|
2016-03-22 16:21:46 +02:00
|
|
|
pub mod subst;
|
2015-12-22 16:39:33 -05:00
|
|
|
pub mod trait_def;
|
2015-09-06 18:32:34 +03:00
|
|
|
pub mod walk;
|
|
|
|
pub mod wf;
|
2015-09-14 14:55:56 +03:00
|
|
|
pub mod util;
|
2015-09-06 18:32:34 +03:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
mod contents;
|
|
|
|
mod context;
|
|
|
|
mod flags;
|
|
|
|
mod ivar;
|
|
|
|
mod structural_impls;
|
|
|
|
mod sty;
|
|
|
|
|
2015-12-16 18:44:15 +01:00
|
|
|
pub type Disr = ConstInt;
|
2013-07-10 17:28:28 +02:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
// Data types
|
2011-05-09 12:27:03 -07:00
|
|
|
|
2014-11-27 07:21:26 -05:00
|
|
|
/// The complete set of all analyses described in this module. This is
|
|
|
|
/// produced by the driver and fed to trans and later passes.
|
2016-05-05 08:54:18 +00:00
|
|
|
#[derive(Clone)]
|
2015-10-20 08:54:19 +13:00
|
|
|
pub struct CrateAnalysis<'a> {
|
2014-12-19 00:03:00 +02:00
|
|
|
pub export_map: ExportMap,
|
2015-11-19 14:16:35 +03:00
|
|
|
pub access_levels: middle::privacy::AccessLevels,
|
2014-11-27 07:21:26 -05:00
|
|
|
pub reachable: NodeSet,
|
2015-10-20 08:54:19 +13:00
|
|
|
pub name: &'a str,
|
2016-03-29 13:14:01 +03:00
|
|
|
pub glob_map: Option<hir::GlobMap>,
|
2014-11-27 07:21:26 -05:00
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum DtorKind {
|
|
|
|
NoDtor,
|
2016-08-23 10:39:30 +03:00
|
|
|
TraitDtor
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DtorKind {
|
|
|
|
pub fn is_present(&self) -> bool {
|
|
|
|
match *self {
|
2016-08-23 10:39:30 +03:00
|
|
|
TraitDtor => true,
|
2015-06-25 23:42:17 +03:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 08:52:36 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
2016-11-10 02:06:34 +02:00
|
|
|
pub enum AssociatedItemContainer {
|
2015-08-16 06:32:28 -04:00
|
|
|
TraitContainer(DefId),
|
|
|
|
ImplContainer(DefId),
|
2013-08-23 14:34:00 -07:00
|
|
|
}
|
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
impl AssociatedItemContainer {
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn id(&self) -> DefId {
|
2014-08-04 13:56:56 -07:00
|
|
|
match *self {
|
|
|
|
TraitContainer(id) => id,
|
|
|
|
ImplContainer(id) => id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 10:51:10 -08:00
|
|
|
/// The "header" of an impl is everything outside the body: a Self type, a trait
|
|
|
|
/// ref (in the case of a trait impl), and a set of predicates (from the
|
|
|
|
/// bounds/where clauses).
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub struct ImplHeader<'tcx> {
|
|
|
|
pub impl_def_id: DefId,
|
|
|
|
pub self_ty: Ty<'tcx>,
|
|
|
|
pub trait_ref: Option<TraitRef<'tcx>>,
|
|
|
|
pub predicates: Vec<Predicate<'tcx>>,
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> ImplHeader<'tcx> {
|
|
|
|
pub fn with_fresh_ty_vars(selcx: &mut traits::SelectionContext<'a, 'gcx, 'tcx>,
|
2016-05-03 04:56:42 +03:00
|
|
|
impl_def_id: DefId)
|
|
|
|
-> ImplHeader<'tcx>
|
2016-02-26 10:51:10 -08:00
|
|
|
{
|
|
|
|
let tcx = selcx.tcx();
|
2016-08-08 23:39:49 +03:00
|
|
|
let impl_substs = selcx.infcx().fresh_substs_for_item(DUMMY_SP, impl_def_id);
|
2016-02-26 10:51:10 -08:00
|
|
|
|
|
|
|
let header = ImplHeader {
|
|
|
|
impl_def_id: impl_def_id,
|
2016-11-10 16:49:53 +02:00
|
|
|
self_ty: tcx.item_type(impl_def_id),
|
2016-02-26 10:51:10 -08:00
|
|
|
trait_ref: tcx.impl_trait_ref(impl_def_id),
|
2016-11-10 16:49:53 +02:00
|
|
|
predicates: tcx.item_predicates(impl_def_id).predicates
|
2016-08-08 23:39:49 +03:00
|
|
|
}.subst(tcx, impl_substs);
|
2016-02-26 10:51:10 -08:00
|
|
|
|
2016-02-26 13:21:44 -08:00
|
|
|
let traits::Normalized { value: mut header, obligations } =
|
|
|
|
traits::normalize(selcx, traits::ObligationCause::dummy(), &header);
|
2016-02-26 10:51:10 -08:00
|
|
|
|
|
|
|
header.predicates.extend(obligations.into_iter().map(|o| o.predicate));
|
|
|
|
header
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct AssociatedItem {
|
|
|
|
pub def_id: DefId,
|
|
|
|
pub name: Name,
|
|
|
|
pub kind: AssociatedKind,
|
|
|
|
pub vis: Visibility,
|
|
|
|
pub defaultness: hir::Defaultness,
|
|
|
|
pub has_value: bool,
|
|
|
|
pub container: AssociatedItemContainer,
|
2014-08-04 13:56:56 -07:00
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
/// Whether this is a method with an explicit self
|
|
|
|
/// as its first argument, allowing method calls.
|
|
|
|
pub method_has_self_argument: bool,
|
|
|
|
}
|
2015-03-14 12:05:00 -06:00
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
pub enum AssociatedKind {
|
|
|
|
Const,
|
|
|
|
Method,
|
|
|
|
Type
|
|
|
|
}
|
2014-10-17 09:07:45 -04:00
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
impl AssociatedItem {
|
|
|
|
pub fn def(&self) -> Def {
|
|
|
|
match self.kind {
|
|
|
|
AssociatedKind::Const => Def::AssociatedConst(self.def_id),
|
|
|
|
AssociatedKind::Method => Def::Method(self.def_id),
|
|
|
|
AssociatedKind::Type => Def::AssociatedTy(self.def_id),
|
2014-10-17 09:07:45 -04:00
|
|
|
}
|
|
|
|
}
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
|
|
|
|
2016-09-19 23:49:01 +03:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Copy, RustcEncodable, RustcDecodable)]
|
2016-03-25 06:08:11 +00:00
|
|
|
pub enum Visibility {
|
|
|
|
/// Visible everywhere (including in other crates).
|
|
|
|
Public,
|
|
|
|
/// Visible only in the given crate-local module.
|
|
|
|
Restricted(NodeId),
|
|
|
|
/// Not visible anywhere in the local crate. This is the visibility of private external items.
|
|
|
|
PrivateExternal,
|
|
|
|
}
|
|
|
|
|
2016-04-27 02:29:59 +00:00
|
|
|
pub trait NodeIdTree {
|
|
|
|
fn is_descendant_of(&self, node: NodeId, ancestor: NodeId) -> bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> NodeIdTree for ast_map::Map<'a> {
|
|
|
|
fn is_descendant_of(&self, node: NodeId, ancestor: NodeId) -> bool {
|
|
|
|
let mut node_ancestor = node;
|
2016-04-27 19:30:16 +00:00
|
|
|
while node_ancestor != ancestor {
|
2016-04-27 02:29:59 +00:00
|
|
|
let node_ancestor_parent = self.get_module_parent(node_ancestor);
|
2016-04-27 19:30:16 +00:00
|
|
|
if node_ancestor_parent == node_ancestor {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-27 02:29:59 +00:00
|
|
|
node_ancestor = node_ancestor_parent;
|
|
|
|
}
|
2016-04-27 19:30:16 +00:00
|
|
|
true
|
2016-04-27 02:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 06:08:11 +00:00
|
|
|
impl Visibility {
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn from_hir(visibility: &hir::Visibility, id: NodeId, tcx: TyCtxt) -> Self {
|
2016-03-25 06:08:11 +00:00
|
|
|
match *visibility {
|
|
|
|
hir::Public => Visibility::Public,
|
2016-04-02 20:24:02 +00:00
|
|
|
hir::Visibility::Crate => Visibility::Restricted(ast::CRATE_NODE_ID),
|
2016-06-03 23:15:00 +03:00
|
|
|
hir::Visibility::Restricted { id, .. } => match tcx.expect_def(id) {
|
2016-04-02 20:24:02 +00:00
|
|
|
// If there is no resolution, `resolve` will have already reported an error, so
|
|
|
|
// assume that the visibility is public to avoid reporting more privacy errors.
|
2016-06-03 23:15:00 +03:00
|
|
|
Def::Err => Visibility::Public,
|
|
|
|
def => Visibility::Restricted(tcx.map.as_local_node_id(def.def_id()).unwrap()),
|
2016-04-02 20:24:02 +00:00
|
|
|
},
|
2016-03-25 06:08:11 +00:00
|
|
|
hir::Inherited => Visibility::Restricted(tcx.map.get_module_parent(id)),
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 07:03:00 +00:00
|
|
|
|
|
|
|
/// Returns true if an item with this visibility is accessible from the given block.
|
2016-04-27 02:29:59 +00:00
|
|
|
pub fn is_accessible_from<T: NodeIdTree>(self, block: NodeId, tree: &T) -> bool {
|
2016-03-31 07:03:00 +00:00
|
|
|
let restriction = match self {
|
|
|
|
// Public items are visible everywhere.
|
|
|
|
Visibility::Public => return true,
|
|
|
|
// Private items from other crates are visible nowhere.
|
|
|
|
Visibility::PrivateExternal => return false,
|
|
|
|
// Restricted items are visible in an arbitrary local module.
|
|
|
|
Visibility::Restricted(module) => module,
|
|
|
|
};
|
|
|
|
|
2016-04-27 02:29:59 +00:00
|
|
|
tree.is_descendant_of(block, restriction)
|
2016-03-31 07:03:00 +00:00
|
|
|
}
|
2016-04-01 20:16:31 +00:00
|
|
|
|
|
|
|
/// Returns true if this visibility is at least as accessible as the given visibility
|
2016-04-27 02:29:59 +00:00
|
|
|
pub fn is_at_least<T: NodeIdTree>(self, vis: Visibility, tree: &T) -> bool {
|
2016-04-01 20:16:31 +00:00
|
|
|
let vis_restriction = match vis {
|
|
|
|
Visibility::Public => return self == Visibility::Public,
|
|
|
|
Visibility::PrivateExternal => return true,
|
|
|
|
Visibility::Restricted(module) => module,
|
|
|
|
};
|
|
|
|
|
2016-04-27 02:29:59 +00:00
|
|
|
self.is_accessible_from(vis_restriction, tree)
|
2016-04-01 20:16:31 +00:00
|
|
|
}
|
2016-03-25 06:08:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 08:51:23 +03:00
|
|
|
#[derive(Clone, PartialEq, RustcDecodable, RustcEncodable, Copy)]
|
2013-10-29 06:08:34 -04:00
|
|
|
pub enum Variance {
|
2013-11-08 15:52:36 -05:00
|
|
|
Covariant, // T<A> <: T<B> iff A <: B -- e.g., function return type
|
|
|
|
Invariant, // T<A> <: T<B> iff B == A -- e.g., type of mutable cell
|
|
|
|
Contravariant, // T<A> <: T<B> iff B <: A -- e.g., function param type
|
|
|
|
Bivariant, // T<A> <: T<B> -- e.g., unused type parameter
|
2013-07-02 12:47:32 -07:00
|
|
|
}
|
2012-08-09 09:59:50 -07:00
|
|
|
|
2016-08-31 14:00:29 +03:00
|
|
|
#[derive(Clone, Copy, Debug, RustcDecodable, RustcEncodable)]
|
2014-11-25 14:21:20 -05:00
|
|
|
pub struct MethodCallee<'tcx> {
|
2015-07-02 08:52:36 +03:00
|
|
|
/// Impl method ID, for inherent methods, or trait method ID, otherwise.
|
2015-08-16 06:32:28 -04:00
|
|
|
pub def_id: DefId,
|
2014-11-25 14:21:20 -05:00
|
|
|
pub ty: Ty<'tcx>,
|
2016-08-08 23:39:49 +03:00
|
|
|
pub substs: &'tcx Substs<'tcx>
|
2014-11-25 14:21:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// With method calls, we store some extra information in
|
|
|
|
/// side tables (i.e method_map). We use
|
|
|
|
/// MethodCall as a key to index into these tables instead of
|
|
|
|
/// just directly using the expression's NodeId. The reason
|
|
|
|
/// for this being that we may apply adjustments (coercions)
|
|
|
|
/// with the resulting expression also needing to use the
|
|
|
|
/// side tables. The problem with this is that we don't
|
|
|
|
/// assign a separate NodeId to this new expression
|
|
|
|
/// and so it would clash with the base expression if both
|
|
|
|
/// needed to add to the side tables. Thus to disambiguate
|
|
|
|
/// we also keep track of whether there's an adjustment in
|
|
|
|
/// our key.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
2014-11-25 14:21:20 -05:00
|
|
|
pub struct MethodCall {
|
2015-07-31 00:04:06 -07:00
|
|
|
pub expr_id: NodeId,
|
2015-03-16 18:45:01 +02:00
|
|
|
pub autoderef: u32
|
2014-11-25 14:21:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MethodCall {
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn expr(id: NodeId) -> MethodCall {
|
2014-11-25 14:21:20 -05:00
|
|
|
MethodCall {
|
|
|
|
expr_id: id,
|
2015-03-16 18:45:01 +02:00
|
|
|
autoderef: 0
|
2014-11-25 14:21:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn autoderef(expr_id: NodeId, autoderef: u32) -> MethodCall {
|
2014-11-25 14:21:20 -05:00
|
|
|
MethodCall {
|
|
|
|
expr_id: expr_id,
|
2015-03-16 18:45:01 +02:00
|
|
|
autoderef: 1 + autoderef
|
2014-11-25 14:21:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// maps from an expression id that corresponds to a method call to the details
|
|
|
|
// of the method to be invoked
|
2016-11-08 14:02:55 +11:00
|
|
|
pub type MethodMap<'tcx> = FxHashMap<MethodCall, MethodCallee<'tcx>>;
|
2014-11-25 14:21:20 -05:00
|
|
|
|
2015-05-05 19:36:47 +03:00
|
|
|
// Contains information needed to resolve types and (in the future) look up
|
|
|
|
// the types of AST nodes.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
2015-07-08 12:27:32 -07:00
|
|
|
pub struct CReaderCacheKey {
|
2015-05-05 19:36:47 +03:00
|
|
|
pub cnum: CrateNum,
|
|
|
|
pub pos: usize,
|
|
|
|
}
|
|
|
|
|
2015-06-07 09:16:55 +02:00
|
|
|
/// Describes the fragment-state associated with a NodeId.
|
|
|
|
///
|
|
|
|
/// Currently only unfragmented paths have entries in the table,
|
|
|
|
/// but longer-term this enum is expected to expand to also
|
|
|
|
/// include data for fragmented paths.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum FragmentInfo {
|
|
|
|
Moved { var: NodeId, move_expr: NodeId },
|
|
|
|
Assigned { var: NodeId, assign_expr: NodeId, assignee_id: NodeId },
|
2013-01-30 13:14:58 -08:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2014-10-22 11:35:53 -04:00
|
|
|
// Flags that we track on types. These flags are propagated upwards
|
|
|
|
// through the type during type construction, so that we can quickly
|
|
|
|
// check whether the type has various kinds of types in it without
|
|
|
|
// recursing over the type itself.
|
2014-10-24 10:20:02 -04:00
|
|
|
bitflags! {
|
|
|
|
flags TypeFlags: u32 {
|
2015-06-06 02:06:14 +03:00
|
|
|
const HAS_PARAMS = 1 << 0,
|
|
|
|
const HAS_SELF = 1 << 1,
|
|
|
|
const HAS_TY_INFER = 1 << 2,
|
|
|
|
const HAS_RE_INFER = 1 << 3,
|
2016-03-15 08:23:23 -04:00
|
|
|
const HAS_RE_SKOL = 1 << 4,
|
|
|
|
const HAS_RE_EARLY_BOUND = 1 << 5,
|
|
|
|
const HAS_FREE_REGIONS = 1 << 6,
|
|
|
|
const HAS_TY_ERR = 1 << 7,
|
|
|
|
const HAS_PROJECTION = 1 << 8,
|
|
|
|
const HAS_TY_CLOSURE = 1 << 9,
|
Add a (somewhat hacky) cache to the tcx that tracks "global" trait refs
that are known to have been satisfied *somewhere*. This means that if
one fn finds that `SomeType: Foo`, then every other fn can just consider
that to hold.
Unfortunately, there are some complications:
1. If `SomeType: Foo` includes dependent conditions, those conditions
may trigger an error. This error will be repored in the first fn
where `SomeType: Foo` is evaluated, but not in the other fns, which
can lead to uneven error reporting (which is sometimes confusing).
2. This kind of caching can be unsound in the presence of
unsatisfiable where clauses. For example, suppose that the first fn
has a where-clause like `i32: Bar<u32>`, which in fact does not
hold. This will "fool" trait resolution into thinking that `i32:
Bar<u32>` holds. This is ok currently, because it means that the
first fn can never be calle (since its where clauses cannot be
satisfied), but if the first fn's successful resolution is cached, it
can allow other fns to compile that should not. This problem is fixed
in the next commit.
2015-06-09 17:02:18 -04:00
|
|
|
|
|
|
|
// true if there are "names" of types and regions and so forth
|
|
|
|
// that are local to a particular fn
|
2016-03-15 08:23:23 -04:00
|
|
|
const HAS_LOCAL_NAMES = 1 << 10,
|
Add a (somewhat hacky) cache to the tcx that tracks "global" trait refs
that are known to have been satisfied *somewhere*. This means that if
one fn finds that `SomeType: Foo`, then every other fn can just consider
that to hold.
Unfortunately, there are some complications:
1. If `SomeType: Foo` includes dependent conditions, those conditions
may trigger an error. This error will be repored in the first fn
where `SomeType: Foo` is evaluated, but not in the other fns, which
can lead to uneven error reporting (which is sometimes confusing).
2. This kind of caching can be unsound in the presence of
unsatisfiable where clauses. For example, suppose that the first fn
has a where-clause like `i32: Bar<u32>`, which in fact does not
hold. This will "fool" trait resolution into thinking that `i32:
Bar<u32>` holds. This is ok currently, because it means that the
first fn can never be calle (since its where clauses cannot be
satisfied), but if the first fn's successful resolution is cached, it
can allow other fns to compile that should not. This problem is fixed
in the next commit.
2015-06-09 17:02:18 -04:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
// Present if the type belongs in a local type context.
|
|
|
|
// Only set for TyInfer other than Fresh.
|
2016-04-21 05:10:10 -04:00
|
|
|
const KEEP_IN_LOCAL_TCX = 1 << 11,
|
2016-05-11 04:14:41 +03:00
|
|
|
|
2016-09-30 18:15:11 -04:00
|
|
|
// Is there a projection that does not involve a bound region?
|
|
|
|
// Currently we can't normalize projections w/ bound regions.
|
|
|
|
const HAS_NORMALIZABLE_PROJECTION = 1 << 12,
|
|
|
|
|
2015-06-06 02:06:14 +03:00
|
|
|
const NEEDS_SUBST = TypeFlags::HAS_PARAMS.bits |
|
|
|
|
TypeFlags::HAS_SELF.bits |
|
|
|
|
TypeFlags::HAS_RE_EARLY_BOUND.bits,
|
2015-06-05 03:50:49 +03:00
|
|
|
|
|
|
|
// Flags representing the nominal content of a type,
|
2015-06-06 02:06:14 +03:00
|
|
|
// computed by FlagsComputation. If you add a new nominal
|
|
|
|
// flag, it should be added here too.
|
2015-06-05 03:50:49 +03:00
|
|
|
const NOMINAL_FLAGS = TypeFlags::HAS_PARAMS.bits |
|
|
|
|
TypeFlags::HAS_SELF.bits |
|
|
|
|
TypeFlags::HAS_TY_INFER.bits |
|
|
|
|
TypeFlags::HAS_RE_INFER.bits |
|
2016-10-19 18:39:49 -04:00
|
|
|
TypeFlags::HAS_RE_SKOL.bits |
|
2015-06-06 02:06:14 +03:00
|
|
|
TypeFlags::HAS_RE_EARLY_BOUND.bits |
|
|
|
|
TypeFlags::HAS_FREE_REGIONS.bits |
|
2015-06-05 03:50:49 +03:00
|
|
|
TypeFlags::HAS_TY_ERR.bits |
|
2015-06-06 02:06:14 +03:00
|
|
|
TypeFlags::HAS_PROJECTION.bits |
|
Add a (somewhat hacky) cache to the tcx that tracks "global" trait refs
that are known to have been satisfied *somewhere*. This means that if
one fn finds that `SomeType: Foo`, then every other fn can just consider
that to hold.
Unfortunately, there are some complications:
1. If `SomeType: Foo` includes dependent conditions, those conditions
may trigger an error. This error will be repored in the first fn
where `SomeType: Foo` is evaluated, but not in the other fns, which
can lead to uneven error reporting (which is sometimes confusing).
2. This kind of caching can be unsound in the presence of
unsatisfiable where clauses. For example, suppose that the first fn
has a where-clause like `i32: Bar<u32>`, which in fact does not
hold. This will "fool" trait resolution into thinking that `i32:
Bar<u32>` holds. This is ok currently, because it means that the
first fn can never be calle (since its where clauses cannot be
satisfied), but if the first fn's successful resolution is cached, it
can allow other fns to compile that should not. This problem is fixed
in the next commit.
2015-06-09 17:02:18 -04:00
|
|
|
TypeFlags::HAS_TY_CLOSURE.bits |
|
2016-05-11 04:14:41 +03:00
|
|
|
TypeFlags::HAS_LOCAL_NAMES.bits |
|
|
|
|
TypeFlags::KEEP_IN_LOCAL_TCX.bits,
|
2015-06-05 03:50:49 +03:00
|
|
|
|
|
|
|
// Caches for type_is_sized, type_moves_by_default
|
|
|
|
const SIZEDNESS_CACHED = 1 << 16,
|
|
|
|
const IS_SIZED = 1 << 17,
|
|
|
|
const MOVENESS_CACHED = 1 << 18,
|
|
|
|
const MOVES_BY_DEFAULT = 1 << 19,
|
2014-10-24 10:20:02 -04:00
|
|
|
}
|
2012-05-09 06:09:58 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub struct TyS<'tcx> {
|
2015-06-11 16:21:46 -07:00
|
|
|
pub sty: TypeVariants<'tcx>,
|
2015-06-05 03:50:49 +03:00
|
|
|
pub flags: Cell<TypeFlags>,
|
2014-11-15 16:47:59 -05:00
|
|
|
|
|
|
|
// the maximal depth of any bound regions appearing in this type.
|
2014-12-04 12:06:42 -08:00
|
|
|
region_depth: u32,
|
2014-10-24 10:20:02 -04:00
|
|
|
}
|
|
|
|
|
2015-01-21 20:02:52 +01:00
|
|
|
impl<'tcx> PartialEq for TyS<'tcx> {
|
2015-04-21 19:00:12 +03:00
|
|
|
#[inline]
|
2015-01-21 20:02:52 +01:00
|
|
|
fn eq(&self, other: &TyS<'tcx>) -> bool {
|
|
|
|
// (self as *const _) == (other as *const _)
|
|
|
|
(self as *const TyS<'tcx>) == (other as *const TyS<'tcx>)
|
|
|
|
}
|
|
|
|
}
|
2014-09-29 22:11:30 +03:00
|
|
|
impl<'tcx> Eq for TyS<'tcx> {}
|
2011-05-11 17:05:39 -07:00
|
|
|
|
2015-02-17 20:48:07 -08:00
|
|
|
impl<'tcx> Hash for TyS<'tcx> {
|
|
|
|
fn hash<H: Hasher>(&self, s: &mut H) {
|
Add trivial cast lints.
This permits all coercions to be performed in casts, but adds lints to warn in those cases.
Part of this patch moves cast checking to a later stage of type checking. We acquire obligations to check casts as part of type checking where we previously checked them. Once we have type checked a function or module, then we check any cast obligations which have been acquired. That means we have more type information available to check casts (this was crucial to making coercions work properly in place of some casts), but it means that casts cannot feed input into type inference.
[breaking change]
* Adds two new lints for trivial casts and trivial numeric casts, these are warn by default, but can cause errors if you build with warnings as errors. Previously, trivial numeric casts and casts to trait objects were allowed.
* The unused casts lint has gone.
* Interactions between casting and type inference have changed in subtle ways. Two ways this might manifest are:
- You may need to 'direct' casts more with extra type information, for example, in some cases where `foo as _ as T` succeeded, you may now need to specify the type for `_`
- Casts do not influence inference of integer types. E.g., the following used to type check:
```
let x = 42;
let y = &x as *const u32;
```
Because the cast would inform inference that `x` must have type `u32`. This no longer applies and the compiler will fallback to `i32` for `x` and thus there will be a type error in the cast. The solution is to add more type information:
```
let x: u32 = 42;
let y = &x as *const u32;
```
2015-03-20 17:15:27 +13:00
|
|
|
(self as *const TyS).hash(s)
|
2015-02-17 20:48:07 -08:00
|
|
|
}
|
|
|
|
}
|
2014-02-19 18:56:33 -08:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
|
2014-09-28 18:31:04 +03:00
|
|
|
|
2016-09-19 23:46:31 +03:00
|
|
|
impl<'tcx> serialize::UseSpecializedEncodable for Ty<'tcx> {}
|
|
|
|
impl<'tcx> serialize::UseSpecializedDecodable for Ty<'tcx> {}
|
2015-12-01 16:07:15 +01:00
|
|
|
|
2016-10-05 20:34:11 -07:00
|
|
|
/// A wrapper for slices with the additional invariant
|
2016-09-02 11:08:16 +03:00
|
|
|
/// that the slice is interned and no other slice with
|
|
|
|
/// the same contents can exist in the same context.
|
|
|
|
/// This means we can use pointer + length for both
|
|
|
|
/// equality comparisons and hashing.
|
|
|
|
#[derive(Debug, RustcEncodable)]
|
|
|
|
pub struct Slice<T>([T]);
|
|
|
|
|
|
|
|
impl<T> PartialEq for Slice<T> {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &Slice<T>) -> bool {
|
|
|
|
(&self.0 as *const [T]) == (&other.0 as *const [T])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<T> Eq for Slice<T> {}
|
|
|
|
|
|
|
|
impl<T> Hash for Slice<T> {
|
|
|
|
fn hash<H: Hasher>(&self, s: &mut H) {
|
|
|
|
(self.as_ptr(), self.len()).hash(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Deref for Slice<T> {
|
|
|
|
type Target = [T];
|
|
|
|
fn deref(&self) -> &[T] {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> IntoIterator for &'a Slice<T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
type IntoIter = <&'a [T] as IntoIterator>::IntoIter;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self[..].iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> serialize::UseSpecializedDecodable for &'tcx Slice<Ty<'tcx>> {}
|
|
|
|
|
2016-10-24 18:23:29 -06:00
|
|
|
impl<T> Slice<T> {
|
|
|
|
pub fn empty<'a>() -> &'a Slice<T> {
|
|
|
|
unsafe {
|
|
|
|
mem::transmute(slice::from_raw_parts(0x1 as *const T, 0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:06:06 -05:00
|
|
|
/// Upvars do not get their own node-id. Instead, we use the pair of
|
|
|
|
/// the original var id (that is, the root variable that is referenced
|
|
|
|
/// by the upvar) and the id of the closure expression.
|
2015-06-18 08:51:23 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
2014-02-07 14:37:19 -05:00
|
|
|
pub struct UpvarId {
|
2015-07-31 00:04:06 -07:00
|
|
|
pub var_id: NodeId,
|
|
|
|
pub closure_expr_id: NodeId,
|
2014-02-07 14:37:19 -05:00
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, Copy)]
|
2014-02-07 14:37:19 -05:00
|
|
|
pub enum BorrowKind {
|
|
|
|
/// Data must be immutable and is aliasable.
|
|
|
|
ImmBorrow,
|
|
|
|
|
|
|
|
/// Data must be immutable but not aliasable. This kind of borrow
|
|
|
|
/// cannot currently be expressed by the user and is used only in
|
|
|
|
/// implicit closure bindings. It is needed when you the closure
|
|
|
|
/// is borrowing or mutating a mutable referent, e.g.:
|
|
|
|
///
|
2015-03-25 17:06:52 -07:00
|
|
|
/// let x: &mut isize = ...;
|
2014-02-07 14:37:19 -05:00
|
|
|
/// let y = || *x += 5;
|
|
|
|
///
|
|
|
|
/// If we were to try to translate this closure into a more explicit
|
|
|
|
/// form, we'd encounter an error with the code as written:
|
|
|
|
///
|
2015-03-25 17:06:52 -07:00
|
|
|
/// struct Env { x: & &mut isize }
|
|
|
|
/// let x: &mut isize = ...;
|
2014-02-07 14:37:19 -05:00
|
|
|
/// let y = (&mut Env { &x }, fn_ptr); // Closure is pair of env and fn
|
|
|
|
/// fn fn_ptr(env: &mut Env) { **env.x += 5; }
|
|
|
|
///
|
|
|
|
/// This is then illegal because you cannot mutate a `&mut` found
|
|
|
|
/// in an aliasable location. To solve, you'd have to translate with
|
|
|
|
/// an `&mut` borrow:
|
|
|
|
///
|
2015-03-25 17:06:52 -07:00
|
|
|
/// struct Env { x: & &mut isize }
|
|
|
|
/// let x: &mut isize = ...;
|
2014-02-07 14:37:19 -05:00
|
|
|
/// let y = (&mut Env { &mut x }, fn_ptr); // changed from &x to &mut x
|
|
|
|
/// fn fn_ptr(env: &mut Env) { **env.x += 5; }
|
|
|
|
///
|
|
|
|
/// Now the assignment to `**env.x` is legal, but creating a
|
|
|
|
/// mutable pointer to `x` is not because `x` is not mutable. We
|
|
|
|
/// could fix this by declaring `x` as `let mut x`. This is ok in
|
|
|
|
/// user code, if awkward, but extra weird for closures, since the
|
|
|
|
/// borrow is hidden.
|
|
|
|
///
|
|
|
|
/// So we introduce a "unique imm" borrow -- the referent is
|
|
|
|
/// immutable, but not aliasable. This solves the problem. For
|
|
|
|
/// simplicity, we don't give users the way to express this
|
|
|
|
/// borrow, it's just used when translating closures.
|
|
|
|
UniqueImmBorrow,
|
|
|
|
|
|
|
|
/// Data is mutable and not aliasable.
|
|
|
|
MutBorrow
|
|
|
|
}
|
|
|
|
|
2015-01-24 15:54:52 -05:00
|
|
|
/// Information describing the capture of an upvar. This is computed
|
|
|
|
/// during `typeck`, specifically by `regionck`.
|
2016-08-31 14:00:29 +03:00
|
|
|
#[derive(PartialEq, Clone, Debug, Copy, RustcEncodable, RustcDecodable)]
|
2016-08-25 23:58:52 +03:00
|
|
|
pub enum UpvarCapture<'tcx> {
|
2015-01-24 15:54:52 -05:00
|
|
|
/// Upvar is captured by value. This is always true when the
|
|
|
|
/// closure is labeled `move`, but can also be true in other cases
|
|
|
|
/// depending on inference.
|
|
|
|
ByValue,
|
|
|
|
|
|
|
|
/// Upvar is captured by reference.
|
2016-08-25 23:58:52 +03:00
|
|
|
ByRef(UpvarBorrow<'tcx>),
|
2015-01-24 15:54:52 -05:00
|
|
|
}
|
|
|
|
|
2016-08-31 14:00:29 +03:00
|
|
|
#[derive(PartialEq, Clone, Copy, RustcEncodable, RustcDecodable)]
|
2016-08-25 23:58:52 +03:00
|
|
|
pub struct UpvarBorrow<'tcx> {
|
2015-01-24 15:54:52 -05:00
|
|
|
/// The kind of borrow: by-ref upvars have access to shared
|
|
|
|
/// immutable borrows, which are not part of the normal language
|
|
|
|
/// syntax.
|
2014-03-28 10:05:27 -07:00
|
|
|
pub kind: BorrowKind,
|
2015-01-24 15:54:52 -05:00
|
|
|
|
|
|
|
/// Region of the resulting reference.
|
2016-08-25 23:58:52 +03:00
|
|
|
pub region: &'tcx ty::Region,
|
2014-02-07 14:37:19 -05:00
|
|
|
}
|
|
|
|
|
2016-11-08 14:02:55 +11:00
|
|
|
pub type UpvarCaptureMap<'tcx> = FxHashMap<UpvarId, UpvarCapture<'tcx>>;
|
2014-02-07 14:37:19 -05:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct ClosureUpvar<'tcx> {
|
2016-01-20 22:31:10 +03:00
|
|
|
pub def: Def,
|
2015-06-25 23:42:17 +03:00
|
|
|
pub span: Span,
|
|
|
|
pub ty: Ty<'tcx>,
|
|
|
|
}
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
2013-01-30 12:17:17 -08:00
|
|
|
pub enum IntVarValue {
|
2015-09-14 21:58:20 +12:00
|
|
|
IntType(ast::IntTy),
|
|
|
|
UintType(ast::UintTy),
|
2013-01-22 07:02:40 -08:00
|
|
|
}
|
|
|
|
|
2015-02-11 16:37:36 -05:00
|
|
|
/// Default region to use for the bound of objects that are
|
|
|
|
/// supplied as the value for this type parameter. This is derived
|
|
|
|
/// from `T:'a` annotations appearing in the type definition. If
|
|
|
|
/// this is `None`, then the default is inherited from the
|
|
|
|
/// surrounding context. See RFC #599 for details.
|
2016-09-02 00:49:29 +03:00
|
|
|
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
|
2016-08-25 23:58:52 +03:00
|
|
|
pub enum ObjectLifetimeDefault<'tcx> {
|
2015-02-11 16:37:36 -05:00
|
|
|
/// Require an explicit annotation. Occurs when multiple
|
|
|
|
/// `T:'a` constraints are found.
|
|
|
|
Ambiguous,
|
|
|
|
|
2015-06-17 10:02:32 -04:00
|
|
|
/// Use the base default, typically 'static, but in a fn body it is a fresh variable
|
|
|
|
BaseDefault,
|
|
|
|
|
2015-02-11 16:37:36 -05:00
|
|
|
/// Use the given region as the default.
|
2016-08-25 23:58:52 +03:00
|
|
|
Specific(&'tcx Region),
|
2015-02-11 16:37:36 -05:00
|
|
|
}
|
|
|
|
|
2016-09-02 00:49:29 +03:00
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub struct TypeParameterDef<'tcx> {
|
2015-07-31 00:04:06 -07:00
|
|
|
pub name: Name,
|
2015-08-16 06:32:28 -04:00
|
|
|
pub def_id: DefId,
|
2014-12-04 12:06:42 -08:00
|
|
|
pub index: u32,
|
2015-07-16 11:26:02 -07:00
|
|
|
pub default_def_id: DefId, // for use in error reporing about defaults
|
2014-09-29 22:11:30 +03:00
|
|
|
pub default: Option<Ty<'tcx>>,
|
2016-08-25 23:58:52 +03:00
|
|
|
pub object_lifetime_default: ObjectLifetimeDefault<'tcx>,
|
2016-10-11 16:06:43 +02:00
|
|
|
|
|
|
|
/// `pure_wrt_drop`, set by the (unsafe) `#[may_dangle]` attribute
|
|
|
|
/// on generic parameter `T`, asserts data behind the parameter
|
|
|
|
/// `T` won't be accessed during the parent type's `Drop` impl.
|
|
|
|
pub pure_wrt_drop: bool,
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-08 22:54:49 -07:00
|
|
|
}
|
|
|
|
|
2016-09-02 00:49:29 +03:00
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable)]
|
2016-08-25 23:58:52 +03:00
|
|
|
pub struct RegionParameterDef<'tcx> {
|
2015-07-31 00:04:06 -07:00
|
|
|
pub name: Name,
|
2015-08-16 06:32:28 -04:00
|
|
|
pub def_id: DefId,
|
2014-12-04 12:06:42 -08:00
|
|
|
pub index: u32,
|
2016-08-25 23:58:52 +03:00
|
|
|
pub bounds: Vec<&'tcx ty::Region>,
|
2016-10-11 16:06:43 +02:00
|
|
|
|
|
|
|
/// `pure_wrt_drop`, set by the (unsafe) `#[may_dangle]` attribute
|
|
|
|
/// on generic parameter `'a`, asserts data of lifetime `'a`
|
|
|
|
/// won't be accessed during the parent type's `Drop` impl.
|
|
|
|
pub pure_wrt_drop: bool,
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
|
|
|
|
2016-08-25 23:58:52 +03:00
|
|
|
impl<'tcx> RegionParameterDef<'tcx> {
|
2014-12-07 11:10:48 -05:00
|
|
|
pub fn to_early_bound_region(&self) -> ty::Region {
|
2016-08-30 00:03:29 +03:00
|
|
|
ty::ReEarlyBound(self.to_early_bound_region_data())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_early_bound_region_data(&self) -> ty::EarlyBoundRegion {
|
|
|
|
ty::EarlyBoundRegion {
|
2015-04-15 09:13:20 -04:00
|
|
|
index: self.index,
|
|
|
|
name: self.name,
|
2016-08-30 00:03:29 +03:00
|
|
|
}
|
2014-12-07 11:10:48 -05:00
|
|
|
}
|
2016-08-30 00:03:29 +03:00
|
|
|
|
Reject specialized Drop impls.
See Issue 8142 for discussion.
This makes it illegal for a Drop impl to be more specialized than the
original item.
So for example, all of the following are now rejected (when they would
have been blindly accepted before):
```rust
struct S<A> { ... };
impl Drop for S<i8> { ... } // error: specialized to concrete type
struct T<'a> { ... };
impl Drop for T<'static> { ... } // error: specialized to concrete region
struct U<A> { ... };
impl<A:Clone> Drop for U<A> { ... } // error: added extra type requirement
struct V<'a,'b>;
impl<'a,'b:a> Drop for V<'a,'b> { ... } // error: added extra region requirement
```
Due to examples like the above, this is a [breaking-change].
(The fix is to either remove the specialization from the `Drop` impl,
or to transcribe the requirements into the struct/enum definition;
examples of both are shown in the PR's fixed to `libstd`.)
----
This is likely to be the last thing blocking the removal of the
`#[unsafe_destructor]` attribute.
Includes two new error codes for the new dropck check.
Update run-pass tests to accommodate new dropck pass.
Update tests and docs to reflect new destructor restriction.
----
Implementation notes:
We identify Drop impl specialization by not being as parametric as the
struct/enum definition via unification.
More specifically:
1. Attempt unification of a skolemized instance of the struct/enum
with an instance of the Drop impl's type expression where all of
the impl's generics (i.e. the free variables of the type
expression) have been replaced with unification variables.
2. If unification fails, then reject Drop impl as specialized.
3. If unification succeeds, check if any of the skolemized
variables "leaked" into the constraint set for the inference
context; if so, then reject Drop impl as specialized.
4. Otherwise, unification succeeded without leaking skolemized
variables: accept the Drop impl.
We identify whether a Drop impl is injecting new predicates by simply
looking whether the predicate, after an appropriate substitution,
appears on the struct/enum definition.
2015-03-21 13:12:08 +01:00
|
|
|
pub fn to_bound_region(&self) -> ty::BoundRegion {
|
2016-04-21 05:10:10 -04:00
|
|
|
// this is an early bound region, so unaffected by #32330
|
|
|
|
ty::BoundRegion::BrNamed(self.def_id, self.name, Issue32330::WontChange)
|
Reject specialized Drop impls.
See Issue 8142 for discussion.
This makes it illegal for a Drop impl to be more specialized than the
original item.
So for example, all of the following are now rejected (when they would
have been blindly accepted before):
```rust
struct S<A> { ... };
impl Drop for S<i8> { ... } // error: specialized to concrete type
struct T<'a> { ... };
impl Drop for T<'static> { ... } // error: specialized to concrete region
struct U<A> { ... };
impl<A:Clone> Drop for U<A> { ... } // error: added extra type requirement
struct V<'a,'b>;
impl<'a,'b:a> Drop for V<'a,'b> { ... } // error: added extra region requirement
```
Due to examples like the above, this is a [breaking-change].
(The fix is to either remove the specialization from the `Drop` impl,
or to transcribe the requirements into the struct/enum definition;
examples of both are shown in the PR's fixed to `libstd`.)
----
This is likely to be the last thing blocking the removal of the
`#[unsafe_destructor]` attribute.
Includes two new error codes for the new dropck check.
Update run-pass tests to accommodate new dropck pass.
Update tests and docs to reflect new destructor restriction.
----
Implementation notes:
We identify Drop impl specialization by not being as parametric as the
struct/enum definition via unification.
More specifically:
1. Attempt unification of a skolemized instance of the struct/enum
with an instance of the Drop impl's type expression where all of
the impl's generics (i.e. the free variables of the type
expression) have been replaced with unification variables.
2. If unification fails, then reject Drop impl as specialized.
3. If unification succeeds, check if any of the skolemized
variables "leaked" into the constraint set for the inference
context; if so, then reject Drop impl as specialized.
4. Otherwise, unification succeeded without leaking skolemized
variables: accept the Drop impl.
We identify whether a Drop impl is injecting new predicates by simply
looking whether the predicate, after an appropriate substitution,
appears on the struct/enum definition.
2015-03-21 13:12:08 +01:00
|
|
|
}
|
2014-12-07 11:10:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Information about the formal type/lifetime parameters associated
|
2015-07-31 00:04:06 -07:00
|
|
|
/// with an item or method. Analogous to hir::Generics.
|
2016-09-02 00:49:29 +03:00
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub struct Generics<'tcx> {
|
2016-08-10 20:39:09 +03:00
|
|
|
pub parent: Option<DefId>,
|
|
|
|
pub parent_regions: u32,
|
|
|
|
pub parent_types: u32,
|
2016-08-25 23:58:52 +03:00
|
|
|
pub regions: Vec<RegionParameterDef<'tcx>>,
|
2016-08-10 20:39:09 +03:00
|
|
|
pub types: Vec<TypeParameterDef<'tcx>>,
|
2016-08-08 23:39:49 +03:00
|
|
|
pub has_self: bool,
|
2014-12-05 11:04:49 -05:00
|
|
|
}
|
|
|
|
|
2016-08-27 01:13:48 +03:00
|
|
|
impl<'tcx> Generics<'tcx> {
|
|
|
|
pub fn parent_count(&self) -> usize {
|
|
|
|
self.parent_regions as usize + self.parent_types as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn own_count(&self) -> usize {
|
|
|
|
self.regions.len() + self.types.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn count(&self) -> usize {
|
|
|
|
self.parent_count() + self.own_count()
|
|
|
|
}
|
2016-10-17 16:14:25 +02:00
|
|
|
|
|
|
|
pub fn region_param(&self, param: &EarlyBoundRegion) -> &RegionParameterDef<'tcx> {
|
|
|
|
&self.regions[param.index as usize - self.has_self as usize]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn type_param(&self, param: &ParamTy) -> &TypeParameterDef<'tcx> {
|
|
|
|
&self.types[param.idx as usize - self.has_self as usize - self.regions.len()]
|
|
|
|
}
|
2016-08-27 01:13:48 +03:00
|
|
|
}
|
|
|
|
|
2015-02-11 06:54:05 -05:00
|
|
|
/// Bounds on generics.
|
2015-06-18 08:51:23 +03:00
|
|
|
#[derive(Clone)]
|
2015-02-11 06:54:05 -05:00
|
|
|
pub struct GenericPredicates<'tcx> {
|
2016-08-11 09:19:42 +03:00
|
|
|
pub parent: Option<DefId>,
|
2016-06-13 20:10:32 +03:00
|
|
|
pub predicates: Vec<Predicate<'tcx>>,
|
2015-02-11 06:54:05 -05:00
|
|
|
}
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
impl<'tcx> serialize::UseSpecializedEncodable for GenericPredicates<'tcx> {}
|
|
|
|
impl<'tcx> serialize::UseSpecializedDecodable for GenericPredicates<'tcx> {}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> GenericPredicates<'tcx> {
|
|
|
|
pub fn instantiate(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, substs: &Substs<'tcx>)
|
2015-02-11 06:54:05 -05:00
|
|
|
-> InstantiatedPredicates<'tcx> {
|
2016-08-11 09:19:42 +03:00
|
|
|
let mut instantiated = InstantiatedPredicates::empty();
|
|
|
|
self.instantiate_into(tcx, &mut instantiated, substs);
|
|
|
|
instantiated
|
|
|
|
}
|
|
|
|
pub fn instantiate_own(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, substs: &Substs<'tcx>)
|
|
|
|
-> InstantiatedPredicates<'tcx> {
|
2015-02-11 06:54:05 -05:00
|
|
|
InstantiatedPredicates {
|
2016-08-11 09:19:42 +03:00
|
|
|
predicates: self.predicates.subst(tcx, substs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn instantiate_into(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
instantiated: &mut InstantiatedPredicates<'tcx>,
|
|
|
|
substs: &Substs<'tcx>) {
|
|
|
|
if let Some(def_id) = self.parent {
|
2016-11-10 16:49:53 +02:00
|
|
|
tcx.item_predicates(def_id).instantiate_into(tcx, instantiated, substs);
|
2014-12-07 11:10:48 -05:00
|
|
|
}
|
2016-08-11 09:19:42 +03:00
|
|
|
instantiated.predicates.extend(self.predicates.iter().map(|p| p.subst(tcx, substs)))
|
2014-12-07 11:10:48 -05:00
|
|
|
}
|
2015-02-24 09:24:42 -05:00
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn instantiate_supertrait(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2015-02-24 09:24:42 -05:00
|
|
|
poly_trait_ref: &ty::PolyTraitRef<'tcx>)
|
|
|
|
-> InstantiatedPredicates<'tcx>
|
|
|
|
{
|
2016-08-11 09:19:42 +03:00
|
|
|
assert_eq!(self.parent, None);
|
2015-02-24 09:24:42 -05:00
|
|
|
InstantiatedPredicates {
|
2016-06-13 20:10:32 +03:00
|
|
|
predicates: self.predicates.iter().map(|pred| {
|
2016-04-29 06:00:23 +03:00
|
|
|
pred.subst_supertrait(tcx, poly_trait_ref)
|
2016-06-13 20:10:32 +03:00
|
|
|
}).collect()
|
2015-02-24 09:24:42 -05:00
|
|
|
}
|
|
|
|
}
|
2014-12-07 11:10:48 -05:00
|
|
|
}
|
|
|
|
|
2016-09-02 00:49:29 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2014-12-07 11:10:48 -05:00
|
|
|
pub enum Predicate<'tcx> {
|
2014-12-11 04:35:51 -05:00
|
|
|
/// Corresponds to `where Foo : Bar<A,B,C>`. `Foo` here would be
|
|
|
|
/// the `Self` type of the trait reference and `A`, `B`, and `C`
|
2016-08-17 06:32:00 +03:00
|
|
|
/// would be the type parameters.
|
2014-12-17 14:16:28 -05:00
|
|
|
Trait(PolyTraitPredicate<'tcx>),
|
2014-12-07 11:10:48 -05:00
|
|
|
|
2014-12-11 04:35:51 -05:00
|
|
|
/// where `T1 == T2`.
|
2014-12-13 05:34:34 -05:00
|
|
|
Equate(PolyEquatePredicate<'tcx>),
|
2014-12-07 11:10:48 -05:00
|
|
|
|
|
|
|
/// where 'a : 'b
|
2016-08-25 23:58:52 +03:00
|
|
|
RegionOutlives(PolyRegionOutlivesPredicate<'tcx>),
|
2014-12-07 11:10:48 -05:00
|
|
|
|
|
|
|
/// where T : 'a
|
2014-12-13 05:34:34 -05:00
|
|
|
TypeOutlives(PolyTypeOutlivesPredicate<'tcx>),
|
2014-12-17 14:16:28 -05:00
|
|
|
|
2014-12-30 08:59:33 -05:00
|
|
|
/// where <T as TraitRef>::Name == X, approximately.
|
|
|
|
/// See `ProjectionPredicate` struct for details.
|
2014-12-17 14:16:28 -05:00
|
|
|
Projection(PolyProjectionPredicate<'tcx>),
|
2015-08-07 09:30:19 -04:00
|
|
|
|
|
|
|
/// no syntax: T WF
|
|
|
|
WellFormed(Ty<'tcx>),
|
|
|
|
|
|
|
|
/// trait must be object-safe
|
2015-08-16 06:32:28 -04:00
|
|
|
ObjectSafe(DefId),
|
2016-04-06 00:20:59 -07:00
|
|
|
|
2016-08-17 06:32:00 +03:00
|
|
|
/// No direct syntax. May be thought of as `where T : FnFoo<...>`
|
|
|
|
/// for some substitutions `...` and T being a closure type.
|
|
|
|
/// Satisfied (or refuted) once we know the closure's kind.
|
2016-04-06 00:20:59 -07:00
|
|
|
ClosureKind(DefId, ClosureKind),
|
2014-12-17 14:16:28 -05:00
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> Predicate<'tcx> {
|
2015-05-04 13:21:27 -04:00
|
|
|
/// Performs a substitution suitable for going from a
|
2015-02-24 09:24:42 -05:00
|
|
|
/// poly-trait-ref to supertraits that must hold if that
|
|
|
|
/// poly-trait-ref holds. This is slightly different from a normal
|
|
|
|
/// substitution in terms of what happens with bound regions. See
|
|
|
|
/// lengthy comment below for details.
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn subst_supertrait(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2015-02-24 09:24:42 -05:00
|
|
|
trait_ref: &ty::PolyTraitRef<'tcx>)
|
|
|
|
-> ty::Predicate<'tcx>
|
|
|
|
{
|
|
|
|
// The interaction between HRTB and supertraits is not entirely
|
|
|
|
// obvious. Let me walk you (and myself) through an example.
|
|
|
|
//
|
|
|
|
// Let's start with an easy case. Consider two traits:
|
|
|
|
//
|
|
|
|
// trait Foo<'a> : Bar<'a,'a> { }
|
|
|
|
// trait Bar<'b,'c> { }
|
|
|
|
//
|
|
|
|
// Now, if we have a trait reference `for<'x> T : Foo<'x>`, then
|
|
|
|
// we can deduce that `for<'x> T : Bar<'x,'x>`. Basically, if we
|
|
|
|
// knew that `Foo<'x>` (for any 'x) then we also know that
|
|
|
|
// `Bar<'x,'x>` (for any 'x). This more-or-less falls out from
|
|
|
|
// normal substitution.
|
|
|
|
//
|
|
|
|
// In terms of why this is sound, the idea is that whenever there
|
|
|
|
// is an impl of `T:Foo<'a>`, it must show that `T:Bar<'a,'a>`
|
|
|
|
// holds. So if there is an impl of `T:Foo<'a>` that applies to
|
|
|
|
// all `'a`, then we must know that `T:Bar<'a,'a>` holds for all
|
|
|
|
// `'a`.
|
|
|
|
//
|
|
|
|
// Another example to be careful of is this:
|
|
|
|
//
|
|
|
|
// trait Foo1<'a> : for<'b> Bar1<'a,'b> { }
|
|
|
|
// trait Bar1<'b,'c> { }
|
|
|
|
//
|
|
|
|
// Here, if we have `for<'x> T : Foo1<'x>`, then what do we know?
|
|
|
|
// The answer is that we know `for<'x,'b> T : Bar1<'x,'b>`. The
|
|
|
|
// reason is similar to the previous example: any impl of
|
|
|
|
// `T:Foo1<'x>` must show that `for<'b> T : Bar1<'x, 'b>`. So
|
|
|
|
// basically we would want to collapse the bound lifetimes from
|
|
|
|
// the input (`trait_ref`) and the supertraits.
|
|
|
|
//
|
|
|
|
// To achieve this in practice is fairly straightforward. Let's
|
|
|
|
// consider the more complicated scenario:
|
|
|
|
//
|
|
|
|
// - We start out with `for<'x> T : Foo1<'x>`. In this case, `'x`
|
|
|
|
// has a De Bruijn index of 1. We want to produce `for<'x,'b> T : Bar1<'x,'b>`,
|
|
|
|
// where both `'x` and `'b` would have a DB index of 1.
|
|
|
|
// The substitution from the input trait-ref is therefore going to be
|
|
|
|
// `'a => 'x` (where `'x` has a DB index of 1).
|
|
|
|
// - The super-trait-ref is `for<'b> Bar1<'a,'b>`, where `'a` is an
|
|
|
|
// early-bound parameter and `'b' is a late-bound parameter with a
|
|
|
|
// DB index of 1.
|
|
|
|
// - If we replace `'a` with `'x` from the input, it too will have
|
|
|
|
// a DB index of 1, and thus we'll have `for<'x,'b> Bar1<'x,'b>`
|
|
|
|
// just as we wanted.
|
|
|
|
//
|
|
|
|
// There is only one catch. If we just apply the substitution `'a
|
|
|
|
// => 'x` to `for<'b> Bar1<'a,'b>`, the substitution code will
|
|
|
|
// adjust the DB index because we substituting into a binder (it
|
|
|
|
// tries to be so smart...) resulting in `for<'x> for<'b>
|
|
|
|
// Bar1<'x,'b>` (we have no syntax for this, so use your
|
|
|
|
// imagination). Basically the 'x will have DB index of 2 and 'b
|
|
|
|
// will have DB index of 1. Not quite what we want. So we apply
|
|
|
|
// the substitution to the *contents* of the trait reference,
|
|
|
|
// rather than the trait reference itself (put another way, the
|
|
|
|
// substitution code expects equal binding levels in the values
|
|
|
|
// from the substitution and the value being substituted into, and
|
|
|
|
// this trick achieves that).
|
|
|
|
|
|
|
|
let substs = &trait_ref.0.substs;
|
|
|
|
match *self {
|
|
|
|
Predicate::Trait(ty::Binder(ref data)) =>
|
|
|
|
Predicate::Trait(ty::Binder(data.subst(tcx, substs))),
|
|
|
|
Predicate::Equate(ty::Binder(ref data)) =>
|
|
|
|
Predicate::Equate(ty::Binder(data.subst(tcx, substs))),
|
|
|
|
Predicate::RegionOutlives(ty::Binder(ref data)) =>
|
|
|
|
Predicate::RegionOutlives(ty::Binder(data.subst(tcx, substs))),
|
|
|
|
Predicate::TypeOutlives(ty::Binder(ref data)) =>
|
|
|
|
Predicate::TypeOutlives(ty::Binder(data.subst(tcx, substs))),
|
|
|
|
Predicate::Projection(ty::Binder(ref data)) =>
|
|
|
|
Predicate::Projection(ty::Binder(data.subst(tcx, substs))),
|
2015-08-07 09:30:19 -04:00
|
|
|
Predicate::WellFormed(data) =>
|
|
|
|
Predicate::WellFormed(data.subst(tcx, substs)),
|
|
|
|
Predicate::ObjectSafe(trait_def_id) =>
|
|
|
|
Predicate::ObjectSafe(trait_def_id),
|
2016-04-06 00:20:59 -07:00
|
|
|
Predicate::ClosureKind(closure_def_id, kind) =>
|
|
|
|
Predicate::ClosureKind(closure_def_id, kind),
|
2015-02-24 09:24:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 00:49:29 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2014-12-17 14:16:28 -05:00
|
|
|
pub struct TraitPredicate<'tcx> {
|
2015-04-21 18:59:58 +03:00
|
|
|
pub trait_ref: TraitRef<'tcx>
|
2014-12-17 14:16:28 -05:00
|
|
|
}
|
|
|
|
pub type PolyTraitPredicate<'tcx> = ty::Binder<TraitPredicate<'tcx>>;
|
|
|
|
|
|
|
|
impl<'tcx> TraitPredicate<'tcx> {
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn def_id(&self) -> DefId {
|
2014-12-17 14:16:28 -05:00
|
|
|
self.trait_ref.def_id
|
|
|
|
}
|
|
|
|
|
2016-01-20 16:56:29 -05:00
|
|
|
/// Creates the dep-node for selecting/evaluating this trait reference.
|
2016-03-28 17:37:34 -04:00
|
|
|
fn dep_node(&self) -> DepNode<DefId> {
|
2016-05-27 21:19:11 -04:00
|
|
|
// Ideally, the dep-node would just have all the input types
|
|
|
|
// in it. But they are limited to including def-ids. So as an
|
|
|
|
// approximation we include the def-ids for all nominal types
|
|
|
|
// found somewhere. This means that we will e.g. conflate the
|
|
|
|
// dep-nodes for `u32: SomeTrait` and `u64: SomeTrait`, but we
|
|
|
|
// would have distinct dep-nodes for `Vec<u32>: SomeTrait`,
|
|
|
|
// `Rc<u32>: SomeTrait`, and `(Vec<u32>, Rc<u32>): SomeTrait`.
|
2016-06-03 20:40:44 -04:00
|
|
|
// Note that it's always sound to conflate dep-nodes, it just
|
2016-05-27 21:19:11 -04:00
|
|
|
// leads to more recompilation.
|
|
|
|
let def_ids: Vec<_> =
|
|
|
|
self.input_types()
|
|
|
|
.flat_map(|t| t.walk())
|
|
|
|
.filter_map(|t| match t.sty {
|
2016-09-06 01:26:02 +03:00
|
|
|
ty::TyAdt(adt_def, _) =>
|
2016-05-27 21:19:11 -04:00
|
|
|
Some(adt_def.did),
|
|
|
|
_ =>
|
|
|
|
None
|
|
|
|
})
|
2016-08-08 09:40:12 -04:00
|
|
|
.chain(iter::once(self.def_id()))
|
2016-05-27 21:19:11 -04:00
|
|
|
.collect();
|
2016-08-08 09:40:12 -04:00
|
|
|
DepNode::TraitSelect(def_ids)
|
2016-01-20 16:56:29 -05:00
|
|
|
}
|
|
|
|
|
2016-08-27 01:13:48 +03:00
|
|
|
pub fn input_types<'a>(&'a self) -> impl DoubleEndedIterator<Item=Ty<'tcx>> + 'a {
|
2016-08-18 08:32:50 +03:00
|
|
|
self.trait_ref.input_types()
|
2014-12-17 14:16:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn self_ty(&self) -> Ty<'tcx> {
|
|
|
|
self.trait_ref.self_ty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> PolyTraitPredicate<'tcx> {
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn def_id(&self) -> DefId {
|
2016-01-20 16:56:29 -05:00
|
|
|
// ok to skip binder since trait def-id does not care about regions
|
2014-12-17 14:16:28 -05:00
|
|
|
self.0.def_id()
|
|
|
|
}
|
2016-01-20 16:56:29 -05:00
|
|
|
|
2016-03-28 17:37:34 -04:00
|
|
|
pub fn dep_node(&self) -> DepNode<DefId> {
|
2016-01-20 16:56:29 -05:00
|
|
|
// ok to skip binder since depnode does not care about regions
|
|
|
|
self.0.dep_node()
|
|
|
|
}
|
2014-12-13 05:34:34 -05:00
|
|
|
}
|
|
|
|
|
2016-09-02 00:49:29 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
|
2014-12-13 05:34:34 -05:00
|
|
|
pub struct EquatePredicate<'tcx>(pub Ty<'tcx>, pub Ty<'tcx>); // `0 == 1`
|
|
|
|
pub type PolyEquatePredicate<'tcx> = ty::Binder<EquatePredicate<'tcx>>;
|
|
|
|
|
2016-09-02 00:49:29 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
|
2014-12-13 05:34:34 -05:00
|
|
|
pub struct OutlivesPredicate<A,B>(pub A, pub B); // `A : B`
|
|
|
|
pub type PolyOutlivesPredicate<A,B> = ty::Binder<OutlivesPredicate<A,B>>;
|
2016-08-25 23:58:52 +03:00
|
|
|
pub type PolyRegionOutlivesPredicate<'tcx> = PolyOutlivesPredicate<&'tcx ty::Region,
|
|
|
|
&'tcx ty::Region>;
|
|
|
|
pub type PolyTypeOutlivesPredicate<'tcx> = PolyOutlivesPredicate<Ty<'tcx>, &'tcx ty::Region>;
|
2014-12-13 05:34:34 -05:00
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
/// This kind of predicate has no *direct* correspondent in the
|
|
|
|
/// syntax, but it roughly corresponds to the syntactic forms:
|
|
|
|
///
|
|
|
|
/// 1. `T : TraitRef<..., Item=Type>`
|
|
|
|
/// 2. `<T as TraitRef<...>>::Item == Type` (NYI)
|
|
|
|
///
|
|
|
|
/// In particular, form #1 is "desugared" to the combination of a
|
|
|
|
/// normal trait predicate (`T : TraitRef<...>`) and one of these
|
|
|
|
/// predicates. Form #2 is a broader form in that it also permits
|
|
|
|
/// equality between arbitrary types. Processing an instance of Form
|
2015-01-13 20:21:19 +01:00
|
|
|
/// #2 eventually yields one of these `ProjectionPredicate`
|
2014-12-17 14:16:28 -05:00
|
|
|
/// instances to normalize the LHS.
|
2016-09-02 00:49:29 +03:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2014-12-17 14:16:28 -05:00
|
|
|
pub struct ProjectionPredicate<'tcx> {
|
|
|
|
pub projection_ty: ProjectionTy<'tcx>,
|
|
|
|
pub ty: Ty<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type PolyProjectionPredicate<'tcx> = Binder<ProjectionPredicate<'tcx>>;
|
|
|
|
|
2014-12-26 04:36:04 -05:00
|
|
|
impl<'tcx> PolyProjectionPredicate<'tcx> {
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn item_name(&self) -> Name {
|
2015-01-06 13:41:14 -05:00
|
|
|
self.0.projection_ty.item_name // safe to skip the binder to access a name
|
|
|
|
}
|
2014-12-26 04:36:04 -05:00
|
|
|
}
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
pub trait ToPolyTraitRef<'tcx> {
|
|
|
|
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx>;
|
|
|
|
}
|
|
|
|
|
2015-04-21 18:59:58 +03:00
|
|
|
impl<'tcx> ToPolyTraitRef<'tcx> for TraitRef<'tcx> {
|
2014-12-17 14:16:28 -05:00
|
|
|
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx> {
|
|
|
|
assert!(!self.has_escaping_regions());
|
|
|
|
ty::Binder(self.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ToPolyTraitRef<'tcx> for PolyTraitPredicate<'tcx> {
|
|
|
|
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx> {
|
2016-02-15 19:08:53 +02:00
|
|
|
self.map_bound_ref(|trait_pred| trait_pred.trait_ref)
|
2014-12-17 14:16:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ToPolyTraitRef<'tcx> for PolyProjectionPredicate<'tcx> {
|
|
|
|
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx> {
|
|
|
|
// Note: unlike with TraitRef::to_poly_trait_ref(),
|
|
|
|
// self.0.trait_ref is permitted to have escaping regions.
|
|
|
|
// This is because here `self` has a `Binder` and so does our
|
|
|
|
// return value, so we are preserving the number of binding
|
|
|
|
// levels.
|
2016-02-15 19:08:53 +02:00
|
|
|
ty::Binder(self.0.projection_ty.trait_ref)
|
2014-12-17 14:16:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:50:50 -07:00
|
|
|
pub trait ToPredicate<'tcx> {
|
|
|
|
fn to_predicate(&self) -> Predicate<'tcx>;
|
2014-12-13 05:34:34 -05:00
|
|
|
}
|
|
|
|
|
2015-06-23 11:50:50 -07:00
|
|
|
impl<'tcx> ToPredicate<'tcx> for TraitRef<'tcx> {
|
|
|
|
fn to_predicate(&self) -> Predicate<'tcx> {
|
2014-12-17 14:16:28 -05:00
|
|
|
// we're about to add a binder, so let's check that we don't
|
|
|
|
// accidentally capture anything, or else that might be some
|
|
|
|
// weird debruijn accounting.
|
|
|
|
assert!(!self.has_escaping_regions());
|
|
|
|
|
|
|
|
ty::Predicate::Trait(ty::Binder(ty::TraitPredicate {
|
|
|
|
trait_ref: self.clone()
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:50:50 -07:00
|
|
|
impl<'tcx> ToPredicate<'tcx> for PolyTraitRef<'tcx> {
|
|
|
|
fn to_predicate(&self) -> Predicate<'tcx> {
|
2014-12-17 14:16:28 -05:00
|
|
|
ty::Predicate::Trait(self.to_poly_trait_predicate())
|
2014-12-13 05:34:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:50:50 -07:00
|
|
|
impl<'tcx> ToPredicate<'tcx> for PolyEquatePredicate<'tcx> {
|
|
|
|
fn to_predicate(&self) -> Predicate<'tcx> {
|
2014-12-13 05:34:34 -05:00
|
|
|
Predicate::Equate(self.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 23:58:52 +03:00
|
|
|
impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> {
|
2015-06-23 11:50:50 -07:00
|
|
|
fn to_predicate(&self) -> Predicate<'tcx> {
|
2014-12-13 05:34:34 -05:00
|
|
|
Predicate::RegionOutlives(self.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:50:50 -07:00
|
|
|
impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> {
|
|
|
|
fn to_predicate(&self) -> Predicate<'tcx> {
|
2014-12-13 05:34:34 -05:00
|
|
|
Predicate::TypeOutlives(self.clone())
|
|
|
|
}
|
2014-12-07 11:10:48 -05:00
|
|
|
}
|
|
|
|
|
2015-06-23 11:50:50 -07:00
|
|
|
impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
|
|
|
|
fn to_predicate(&self) -> Predicate<'tcx> {
|
2014-12-17 14:16:28 -05:00
|
|
|
Predicate::Projection(self.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-07 11:10:48 -05:00
|
|
|
impl<'tcx> Predicate<'tcx> {
|
2015-02-17 10:57:15 -05:00
|
|
|
/// Iterates over the types in this predicate. Note that in all
|
|
|
|
/// cases this is skipping over a binder, so late-bound regions
|
|
|
|
/// with depth 0 are bound by the predicate.
|
|
|
|
pub fn walk_tys(&self) -> IntoIter<Ty<'tcx>> {
|
|
|
|
let vec: Vec<_> = match *self {
|
|
|
|
ty::Predicate::Trait(ref data) => {
|
2016-08-27 01:13:48 +03:00
|
|
|
data.skip_binder().input_types().collect()
|
2015-02-17 10:57:15 -05:00
|
|
|
}
|
|
|
|
ty::Predicate::Equate(ty::Binder(ref data)) => {
|
|
|
|
vec![data.0, data.1]
|
|
|
|
}
|
|
|
|
ty::Predicate::TypeOutlives(ty::Binder(ref data)) => {
|
|
|
|
vec![data.0]
|
|
|
|
}
|
|
|
|
ty::Predicate::RegionOutlives(..) => {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
ty::Predicate::Projection(ref data) => {
|
2016-08-17 06:32:00 +03:00
|
|
|
let trait_inputs = data.0.projection_ty.trait_ref.input_types();
|
2016-08-27 01:13:48 +03:00
|
|
|
trait_inputs.chain(Some(data.0.ty)).collect()
|
2015-02-17 10:57:15 -05:00
|
|
|
}
|
2015-08-07 09:30:19 -04:00
|
|
|
ty::Predicate::WellFormed(data) => {
|
|
|
|
vec![data]
|
|
|
|
}
|
|
|
|
ty::Predicate::ObjectSafe(_trait_def_id) => {
|
|
|
|
vec![]
|
|
|
|
}
|
2016-04-06 00:20:59 -07:00
|
|
|
ty::Predicate::ClosureKind(_closure_def_id, _kind) => {
|
|
|
|
vec![]
|
|
|
|
}
|
2015-02-17 10:57:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// The only reason to collect into a vector here is that I was
|
|
|
|
// too lazy to make the full (somewhat complicated) iterator
|
|
|
|
// type that would be needed here. But I wanted this fn to
|
|
|
|
// return an iterator conceptually, rather than a `Vec`, so as
|
|
|
|
// to be closer to `Ty::walk`.
|
|
|
|
vec.into_iter()
|
|
|
|
}
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
pub fn to_opt_poly_trait_ref(&self) -> Option<PolyTraitRef<'tcx>> {
|
2014-12-07 11:10:48 -05:00
|
|
|
match *self {
|
|
|
|
Predicate::Trait(ref t) => {
|
2014-12-17 14:16:28 -05:00
|
|
|
Some(t.to_poly_trait_ref())
|
2014-12-07 11:10:48 -05:00
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
Predicate::Projection(..) |
|
2014-12-07 11:10:48 -05:00
|
|
|
Predicate::Equate(..) |
|
|
|
|
Predicate::RegionOutlives(..) |
|
2015-08-07 09:30:19 -04:00
|
|
|
Predicate::WellFormed(..) |
|
|
|
|
Predicate::ObjectSafe(..) |
|
2016-04-06 00:20:59 -07:00
|
|
|
Predicate::ClosureKind(..) |
|
2014-12-07 11:10:48 -05:00
|
|
|
Predicate::TypeOutlives(..) => {
|
|
|
|
None
|
|
|
|
}
|
2014-11-15 17:25:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:06:06 -05:00
|
|
|
/// Represents the bounds declared on a particular set of type
|
|
|
|
/// parameters. Should eventually be generalized into a flag list of
|
2015-02-11 06:54:05 -05:00
|
|
|
/// where clauses. You can obtain a `InstantiatedPredicates` list from a
|
|
|
|
/// `GenericPredicates` by using the `instantiate` method. Note that this method
|
|
|
|
/// reflects an important semantic invariant of `InstantiatedPredicates`: while
|
|
|
|
/// the `GenericPredicates` are expressed in terms of the bound type
|
|
|
|
/// parameters of the impl/trait/whatever, an `InstantiatedPredicates` instance
|
2014-11-24 20:06:06 -05:00
|
|
|
/// represented a set of bounds for some particular instantiation,
|
|
|
|
/// meaning that the generic parameters have been substituted with
|
|
|
|
/// their values.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// struct Foo<T,U:Bar<T>> { ... }
|
|
|
|
///
|
2015-02-11 06:54:05 -05:00
|
|
|
/// Here, the `GenericPredicates` for `Foo` would contain a list of bounds like
|
2015-01-07 11:58:31 -05:00
|
|
|
/// `[[], [U:Bar<T>]]`. Now if there were some particular reference
|
2015-03-25 17:06:52 -07:00
|
|
|
/// like `Foo<isize,usize>`, then the `InstantiatedPredicates` would be `[[],
|
|
|
|
/// [usize:Bar<isize>]]`.
|
2015-06-18 08:51:23 +03:00
|
|
|
#[derive(Clone)]
|
2015-02-11 06:54:05 -05:00
|
|
|
pub struct InstantiatedPredicates<'tcx> {
|
2016-06-13 20:10:32 +03:00
|
|
|
pub predicates: Vec<Predicate<'tcx>>,
|
2014-11-15 17:25:05 -05:00
|
|
|
}
|
|
|
|
|
2015-02-11 06:54:05 -05:00
|
|
|
impl<'tcx> InstantiatedPredicates<'tcx> {
|
|
|
|
pub fn empty() -> InstantiatedPredicates<'tcx> {
|
2016-06-13 20:10:32 +03:00
|
|
|
InstantiatedPredicates { predicates: vec![] }
|
2014-11-15 17:25:05 -05:00
|
|
|
}
|
|
|
|
|
2014-12-07 11:10:48 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.predicates.is_empty()
|
2014-11-15 17:25:05 -05:00
|
|
|
}
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-08 22:54:49 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
impl<'tcx> TraitRef<'tcx> {
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn new(def_id: DefId, substs: &'tcx Substs<'tcx>) -> TraitRef<'tcx> {
|
2014-10-17 09:07:45 -04:00
|
|
|
TraitRef { def_id: def_id, substs: substs }
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn self_ty(&self) -> Ty<'tcx> {
|
2016-08-18 08:32:50 +03:00
|
|
|
self.substs.type_at(0)
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
2014-10-21 11:49:06 -04:00
|
|
|
|
2016-08-27 01:13:48 +03:00
|
|
|
pub fn input_types<'a>(&'a self) -> impl DoubleEndedIterator<Item=Ty<'tcx>> + 'a {
|
2014-10-21 11:49:06 -04:00
|
|
|
// Select only the "input types" from a trait-reference. For
|
|
|
|
// now this is all the types that appear in the
|
|
|
|
// trait-reference, but it should eventually exclude
|
|
|
|
// associated types.
|
2016-08-18 08:32:50 +03:00
|
|
|
self.substs.types()
|
2014-10-21 11:49:06 -04:00
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2013-10-29 06:03:32 -04:00
|
|
|
/// When type checking, we use the `ParameterEnvironment` to track
|
|
|
|
/// details about the type/lifetime parameters that are in scope.
|
|
|
|
/// It primarily stores the bounds information.
|
|
|
|
///
|
|
|
|
/// Note: This information might seem to be redundant with the data in
|
|
|
|
/// `tcx.ty_param_defs`, but it is not. That table contains the
|
|
|
|
/// parameter definitions from an "outside" perspective, but this
|
|
|
|
/// struct will contain the bounds for a parameter as seen from inside
|
|
|
|
/// the function body. Currently the only real distinction is that
|
|
|
|
/// bound lifetime parameters are replaced with free ones, but in the
|
|
|
|
/// future I hope to refine the representation of types so as to make
|
|
|
|
/// more distinctions clearer.
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone)]
|
2016-03-25 05:22:52 +02:00
|
|
|
pub struct ParameterEnvironment<'tcx> {
|
2015-01-26 14:20:38 -05:00
|
|
|
/// See `construct_free_substs` for details.
|
2016-04-29 08:30:54 +03:00
|
|
|
pub free_substs: &'tcx Substs<'tcx>,
|
2013-10-29 06:03:32 -04:00
|
|
|
|
2014-08-27 21:46:52 -04:00
|
|
|
/// Each type parameter has an implicit region bound that
|
|
|
|
/// indicates it must outlive at least the function body (the user
|
|
|
|
/// may specify stronger requirements). This field indicates the
|
|
|
|
/// region of the callee.
|
2016-08-25 23:58:52 +03:00
|
|
|
pub implicit_region_bound: &'tcx ty::Region,
|
2014-09-12 10:53:35 -04:00
|
|
|
|
|
|
|
/// Obligations that the caller must satisfy. This is basically
|
|
|
|
/// the set of bounds on the in-scope type parameters, translated
|
2015-05-11 17:02:56 -04:00
|
|
|
/// into Obligations, and elaborated and normalized.
|
2015-01-26 16:37:45 -05:00
|
|
|
pub caller_bounds: Vec<ty::Predicate<'tcx>>,
|
2014-09-17 16:12:02 -04:00
|
|
|
|
2015-08-07 10:37:17 -04:00
|
|
|
/// Scope that is attached to free regions for this scope. This
|
|
|
|
/// is usually the id of the fn body, but for more abstract scopes
|
|
|
|
/// like structs we often use the node-id of the struct.
|
|
|
|
///
|
|
|
|
/// FIXME(#3696). It would be nice to refactor so that free
|
|
|
|
/// regions don't have this implicit scope and instead introduce
|
|
|
|
/// relationships in the environment.
|
2015-12-08 23:38:36 +01:00
|
|
|
pub free_id_outlive: CodeExtent,
|
2016-10-13 23:39:50 +03:00
|
|
|
|
|
|
|
/// A cache for `moves_by_default`.
|
2016-11-08 14:02:55 +11:00
|
|
|
pub is_copy_cache: RefCell<FxHashMap<Ty<'tcx>, bool>>,
|
2016-10-13 23:39:50 +03:00
|
|
|
|
|
|
|
/// A cache for `type_is_sized`
|
2016-11-08 14:02:55 +11:00
|
|
|
pub is_sized_cache: RefCell<FxHashMap<Ty<'tcx>, bool>>,
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
|
|
|
|
2016-03-25 05:22:52 +02:00
|
|
|
impl<'a, 'tcx> ParameterEnvironment<'tcx> {
|
2015-01-26 14:20:38 -05:00
|
|
|
pub fn with_caller_bounds(&self,
|
|
|
|
caller_bounds: Vec<ty::Predicate<'tcx>>)
|
2016-03-25 05:22:52 +02:00
|
|
|
-> ParameterEnvironment<'tcx>
|
2015-01-26 14:20:38 -05:00
|
|
|
{
|
|
|
|
ParameterEnvironment {
|
2016-04-29 08:30:54 +03:00
|
|
|
free_substs: self.free_substs,
|
2015-01-26 14:20:38 -05:00
|
|
|
implicit_region_bound: self.implicit_region_bound,
|
|
|
|
caller_bounds: caller_bounds,
|
2015-12-08 23:38:36 +01:00
|
|
|
free_id_outlive: self.free_id_outlive,
|
2016-11-08 14:02:55 +11:00
|
|
|
is_copy_cache: RefCell::new(FxHashMap()),
|
|
|
|
is_sized_cache: RefCell::new(FxHashMap()),
|
2015-01-26 14:20:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-27 06:04:50 +05:30
|
|
|
/// Construct a parameter environment given an item, impl item, or trait item
|
2016-03-25 05:22:52 +02:00
|
|
|
pub fn for_item(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: NodeId)
|
|
|
|
-> ParameterEnvironment<'tcx> {
|
2016-05-03 04:02:41 +03:00
|
|
|
match tcx.map.find(id) {
|
2014-08-11 17:12:01 -07:00
|
|
|
Some(ast_map::NodeImplItem(ref impl_item)) => {
|
2015-03-10 12:28:44 +02:00
|
|
|
match impl_item.node {
|
2016-08-26 19:23:42 +03:00
|
|
|
hir::ImplItemKind::Type(_) | hir::ImplItemKind::Const(..) => {
|
2015-08-07 10:37:17 -04:00
|
|
|
// associated types don't have their own entry (for some reason),
|
|
|
|
// so for now just grab environment for the impl
|
2016-05-03 04:02:41 +03:00
|
|
|
let impl_id = tcx.map.get_parent(id);
|
|
|
|
let impl_def_id = tcx.map.local_def_id(impl_id);
|
|
|
|
tcx.construct_parameter_environment(impl_item.span,
|
2016-08-08 23:39:49 +03:00
|
|
|
impl_def_id,
|
2016-05-03 04:02:41 +03:00
|
|
|
tcx.region_maps.item_extent(id))
|
2015-08-07 10:37:17 -04:00
|
|
|
}
|
2015-11-12 15:57:51 +01:00
|
|
|
hir::ImplItemKind::Method(_, ref body) => {
|
2016-11-10 02:06:34 +02:00
|
|
|
tcx.construct_parameter_environment(
|
|
|
|
impl_item.span,
|
|
|
|
tcx.map.local_def_id(id),
|
|
|
|
tcx.region_maps.call_site_extent(id, body.id))
|
2014-08-11 17:12:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-10 12:28:44 +02:00
|
|
|
Some(ast_map::NodeTraitItem(trait_item)) => {
|
|
|
|
match trait_item.node {
|
2016-05-12 00:30:08 +03:00
|
|
|
hir::TypeTraitItem(..) | hir::ConstTraitItem(..) => {
|
2015-08-07 10:37:17 -04:00
|
|
|
// associated types don't have their own entry (for some reason),
|
|
|
|
// so for now just grab environment for the trait
|
2016-05-03 04:02:41 +03:00
|
|
|
let trait_id = tcx.map.get_parent(id);
|
|
|
|
let trait_def_id = tcx.map.local_def_id(trait_id);
|
|
|
|
tcx.construct_parameter_environment(trait_item.span,
|
2016-08-08 23:39:49 +03:00
|
|
|
trait_def_id,
|
2016-05-03 04:02:41 +03:00
|
|
|
tcx.region_maps.item_extent(id))
|
2015-03-14 12:05:00 -06:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::MethodTraitItem(_, ref body) => {
|
2015-12-08 23:38:36 +01:00
|
|
|
// Use call-site for extent (unless this is a
|
|
|
|
// trait method with no default; then fallback
|
|
|
|
// to the method id).
|
2016-11-10 02:06:34 +02:00
|
|
|
let extent = if let Some(ref body) = *body {
|
|
|
|
// default impl: use call_site extent as free_id_outlive bound.
|
|
|
|
tcx.region_maps.call_site_extent(id, body.id)
|
|
|
|
} else {
|
|
|
|
// no default impl: use item extent as free_id_outlive bound.
|
|
|
|
tcx.region_maps.item_extent(id)
|
|
|
|
};
|
|
|
|
tcx.construct_parameter_environment(
|
|
|
|
trait_item.span,
|
|
|
|
tcx.map.local_def_id(id),
|
|
|
|
extent)
|
2014-08-11 17:12:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ast_map::NodeItem(item)) => {
|
|
|
|
match item.node {
|
2016-08-26 19:23:42 +03:00
|
|
|
hir::ItemFn(.., ref body) => {
|
2014-08-11 17:12:01 -07:00
|
|
|
// We assume this is a function.
|
2016-05-03 04:02:41 +03:00
|
|
|
let fn_def_id = tcx.map.local_def_id(id);
|
|
|
|
|
|
|
|
tcx.construct_parameter_environment(
|
|
|
|
item.span,
|
2016-08-08 23:39:49 +03:00
|
|
|
fn_def_id,
|
2016-05-03 04:02:41 +03:00
|
|
|
tcx.region_maps.call_site_extent(id, body.id))
|
2014-08-11 17:12:01 -07:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemEnum(..) |
|
|
|
|
hir::ItemStruct(..) |
|
2016-08-10 21:00:17 +03:00
|
|
|
hir::ItemUnion(..) |
|
2016-05-02 23:11:19 +03:00
|
|
|
hir::ItemTy(..) |
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemImpl(..) |
|
|
|
|
hir::ItemConst(..) |
|
|
|
|
hir::ItemStatic(..) => {
|
2016-05-03 04:02:41 +03:00
|
|
|
let def_id = tcx.map.local_def_id(id);
|
|
|
|
tcx.construct_parameter_environment(item.span,
|
2016-08-08 23:39:49 +03:00
|
|
|
def_id,
|
2016-05-03 04:02:41 +03:00
|
|
|
tcx.region_maps.item_extent(id))
|
2014-08-11 17:12:01 -07:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemTrait(..) => {
|
2016-05-03 04:02:41 +03:00
|
|
|
let def_id = tcx.map.local_def_id(id);
|
|
|
|
tcx.construct_parameter_environment(item.span,
|
2016-08-08 23:39:49 +03:00
|
|
|
def_id,
|
2016-05-03 04:02:41 +03:00
|
|
|
tcx.region_maps.item_extent(id))
|
2015-08-07 10:37:17 -04:00
|
|
|
}
|
2014-08-11 17:12:01 -07:00
|
|
|
_ => {
|
2016-03-25 18:31:27 +01:00
|
|
|
span_bug!(item.span,
|
|
|
|
"ParameterEnvironment::for_item():
|
|
|
|
can't create a parameter \
|
|
|
|
environment for this kind of item")
|
2014-08-11 17:12:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-28 18:27:11 +03:00
|
|
|
Some(ast_map::NodeExpr(expr)) => {
|
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 17:01:33 -08:00
|
|
|
// This is a convenience to allow closures to work.
|
2016-07-28 18:27:11 +03:00
|
|
|
if let hir::ExprClosure(..) = expr.node {
|
|
|
|
ParameterEnvironment::for_item(tcx, tcx.map.get_parent(id))
|
|
|
|
} else {
|
|
|
|
tcx.empty_parameter_environment()
|
|
|
|
}
|
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 17:01:33 -08:00
|
|
|
}
|
2016-05-02 23:11:19 +03:00
|
|
|
Some(ast_map::NodeForeignItem(item)) => {
|
2016-05-03 04:02:41 +03:00
|
|
|
let def_id = tcx.map.local_def_id(id);
|
|
|
|
tcx.construct_parameter_environment(item.span,
|
2016-08-08 23:39:49 +03:00
|
|
|
def_id,
|
2016-05-03 04:02:41 +03:00
|
|
|
ROOT_CODE_EXTENT)
|
2016-05-02 23:11:19 +03:00
|
|
|
}
|
2014-08-11 17:12:01 -07:00
|
|
|
_ => {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("ParameterEnvironment::from_item(): \
|
|
|
|
`{}` is not an item",
|
2016-05-03 04:02:41 +03:00
|
|
|
tcx.map.node_to_string(id))
|
2014-08-11 17:12:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 22:13:36 +03:00
|
|
|
bitflags! {
|
2015-08-07 14:41:33 +03:00
|
|
|
flags AdtFlags: u32 {
|
2015-07-20 22:13:36 +03:00
|
|
|
const NO_ADT_FLAGS = 0,
|
2015-08-06 18:25:15 +03:00
|
|
|
const IS_ENUM = 1 << 0,
|
|
|
|
const IS_DTORCK = 1 << 1, // is this a dtorck type?
|
|
|
|
const IS_DTORCK_VALID = 1 << 2,
|
|
|
|
const IS_PHANTOM_DATA = 1 << 3,
|
|
|
|
const IS_SIMD = 1 << 4,
|
|
|
|
const IS_FUNDAMENTAL = 1 << 5,
|
2016-08-26 19:23:42 +03:00
|
|
|
const IS_UNION = 1 << 6,
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:33 +03:00
|
|
|
pub type AdtDef<'tcx> = &'tcx AdtDefData<'tcx, 'static>;
|
|
|
|
pub type VariantDef<'tcx> = &'tcx VariantDefData<'tcx, 'static>;
|
|
|
|
pub type FieldDef<'tcx> = &'tcx FieldDefData<'tcx, 'static>;
|
2015-07-22 20:10:18 +03:00
|
|
|
|
2015-08-07 14:41:33 +03:00
|
|
|
// See comment on AdtDefData for explanation
|
|
|
|
pub type AdtDefMaster<'tcx> = &'tcx AdtDefData<'tcx, 'tcx>;
|
|
|
|
pub type VariantDefMaster<'tcx> = &'tcx VariantDefData<'tcx, 'tcx>;
|
|
|
|
pub type FieldDefMaster<'tcx> = &'tcx FieldDefData<'tcx, 'tcx>;
|
|
|
|
|
|
|
|
pub struct VariantDefData<'tcx, 'container: 'tcx> {
|
2015-10-07 20:30:48 +03:00
|
|
|
/// The variant's DefId. If this is a tuple-like struct,
|
|
|
|
/// this is the DefId of the struct's ctor.
|
2015-07-22 20:10:18 +03:00
|
|
|
pub did: DefId,
|
|
|
|
pub name: Name, // struct's name if this is a struct
|
|
|
|
pub disr_val: Disr,
|
2015-10-02 16:12:20 +03:00
|
|
|
pub fields: Vec<FieldDefData<'tcx, 'container>>,
|
2016-09-15 00:51:46 +03:00
|
|
|
pub ctor_kind: CtorKind,
|
2015-07-22 20:10:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:33 +03:00
|
|
|
pub struct FieldDefData<'tcx, 'container: 'tcx> {
|
2015-07-22 20:10:18 +03:00
|
|
|
pub did: DefId,
|
2015-07-22 21:53:52 +03:00
|
|
|
pub name: Name,
|
2016-03-25 06:08:11 +00:00
|
|
|
pub vis: Visibility,
|
2015-08-07 13:48:29 +03:00
|
|
|
/// TyIVar is used here to allow for variance (see the doc at
|
2015-08-07 14:41:33 +03:00
|
|
|
/// AdtDefData).
|
2015-12-22 16:39:33 -05:00
|
|
|
///
|
|
|
|
/// Note: direct accesses to `ty` must also add dep edges.
|
2015-09-06 21:51:58 +03:00
|
|
|
ty: ivar::TyIVar<'tcx, 'container>
|
2015-07-22 20:10:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 13:48:29 +03:00
|
|
|
/// The definition of an abstract data type - a struct or enum.
|
|
|
|
///
|
|
|
|
/// These are all interned (by intern_adt_def) into the adt_defs
|
|
|
|
/// table.
|
|
|
|
///
|
|
|
|
/// Because of the possibility of nested tcx-s, this type
|
|
|
|
/// needs 2 lifetimes: the traditional variant lifetime ('tcx)
|
|
|
|
/// bounding the lifetime of the inner types is of course necessary.
|
|
|
|
/// However, it is not sufficient - types from a child tcx must
|
2015-08-07 14:41:33 +03:00
|
|
|
/// not be leaked into the master tcx by being stored in an AdtDefData.
|
2015-08-07 13:48:29 +03:00
|
|
|
///
|
|
|
|
/// The 'container lifetime ensures that by outliving the container
|
|
|
|
/// tcx and preventing shorter-lived types from being inserted. When
|
|
|
|
/// write access is not needed, the 'container lifetime can be
|
2015-08-07 14:41:33 +03:00
|
|
|
/// erased to 'static, which can be done by the AdtDef wrapper.
|
|
|
|
pub struct AdtDefData<'tcx, 'container: 'tcx> {
|
2015-07-20 22:13:36 +03:00
|
|
|
pub did: DefId,
|
2015-08-07 14:41:33 +03:00
|
|
|
pub variants: Vec<VariantDefData<'tcx, 'container>>,
|
2015-08-25 21:52:15 +03:00
|
|
|
destructor: Cell<Option<DefId>>,
|
2015-08-07 14:41:33 +03:00
|
|
|
flags: Cell<AdtFlags>,
|
2016-04-14 15:49:39 +03:00
|
|
|
sized_constraint: ivar::TyIVar<'tcx, 'container>,
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:33 +03:00
|
|
|
impl<'tcx, 'container> PartialEq for AdtDefData<'tcx, 'container> {
|
|
|
|
// AdtDefData are always interned and this is part of TyS equality
|
2015-07-20 22:13:36 +03:00
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &Self) -> bool { self as *const _ == other as *const _ }
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:33 +03:00
|
|
|
impl<'tcx, 'container> Eq for AdtDefData<'tcx, 'container> {}
|
2015-07-20 22:13:36 +03:00
|
|
|
|
2015-08-07 14:41:33 +03:00
|
|
|
impl<'tcx, 'container> Hash for AdtDefData<'tcx, 'container> {
|
2015-07-20 22:13:36 +03:00
|
|
|
#[inline]
|
|
|
|
fn hash<H: Hasher>(&self, s: &mut H) {
|
2015-08-07 14:41:33 +03:00
|
|
|
(self as *const AdtDefData).hash(s)
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-17 23:31:03 +03:00
|
|
|
impl<'tcx> serialize::UseSpecializedEncodable for AdtDef<'tcx> {
|
|
|
|
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
2015-12-01 16:07:15 +01:00
|
|
|
self.did.encode(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 14:24:14 +03:00
|
|
|
impl<'tcx> serialize::UseSpecializedDecodable for AdtDef<'tcx> {}
|
2015-07-22 21:53:52 +03:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
2016-08-09 01:18:47 +03:00
|
|
|
pub enum AdtKind { Struct, Union, Enum }
|
2015-07-22 20:10:18 +03:00
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx, 'container> AdtDefData<'gcx, 'container> {
|
|
|
|
fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
2015-07-22 21:53:52 +03:00
|
|
|
did: DefId,
|
2015-08-07 14:41:33 +03:00
|
|
|
kind: AdtKind,
|
2016-04-29 06:00:23 +03:00
|
|
|
variants: Vec<VariantDefData<'gcx, 'container>>) -> Self {
|
2015-08-07 14:41:33 +03:00
|
|
|
let mut flags = AdtFlags::NO_ADT_FLAGS;
|
2015-08-06 18:25:15 +03:00
|
|
|
let attrs = tcx.get_attrs(did);
|
2015-07-13 11:35:00 -07:00
|
|
|
if attr::contains_name(&attrs, "fundamental") {
|
2015-08-07 14:41:33 +03:00
|
|
|
flags = flags | AdtFlags::IS_FUNDAMENTAL;
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
2015-07-13 11:35:00 -07:00
|
|
|
if tcx.lookup_simd(did) {
|
2015-08-07 14:41:33 +03:00
|
|
|
flags = flags | AdtFlags::IS_SIMD;
|
2015-08-06 18:25:15 +03:00
|
|
|
}
|
2015-07-20 22:13:36 +03:00
|
|
|
if Some(did) == tcx.lang_items.phantom_data() {
|
2015-08-07 14:41:33 +03:00
|
|
|
flags = flags | AdtFlags::IS_PHANTOM_DATA;
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
2016-08-09 01:18:47 +03:00
|
|
|
match kind {
|
|
|
|
AdtKind::Enum => flags = flags | AdtFlags::IS_ENUM,
|
|
|
|
AdtKind::Union => flags = flags | AdtFlags::IS_UNION,
|
|
|
|
AdtKind::Struct => {}
|
2015-07-22 20:10:18 +03:00
|
|
|
}
|
2015-08-07 14:41:33 +03:00
|
|
|
AdtDefData {
|
2015-07-20 22:13:36 +03:00
|
|
|
did: did,
|
2015-07-22 21:53:52 +03:00
|
|
|
variants: variants,
|
2015-07-20 22:13:36 +03:00
|
|
|
flags: Cell::new(flags),
|
2016-04-14 15:49:39 +03:00
|
|
|
destructor: Cell::new(None),
|
|
|
|
sized_constraint: ivar::TyIVar::new(),
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 18:07:47 +03:00
|
|
|
fn calculate_dtorck(&'gcx self, tcx: TyCtxt) {
|
2015-07-20 22:13:36 +03:00
|
|
|
if tcx.is_adt_dtorck(self) {
|
2015-08-07 14:41:33 +03:00
|
|
|
self.flags.set(self.flags.get() | AdtFlags::IS_DTORCK);
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
2015-08-07 14:41:33 +03:00
|
|
|
self.flags.set(self.flags.get() | AdtFlags::IS_DTORCK_VALID)
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
|
|
|
|
2016-09-06 01:26:02 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn is_struct(&self) -> bool {
|
|
|
|
!self.is_union() && !self.is_enum()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_union(&self) -> bool {
|
|
|
|
self.flags.get().intersects(AdtFlags::IS_UNION)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_enum(&self) -> bool {
|
|
|
|
self.flags.get().intersects(AdtFlags::IS_ENUM)
|
|
|
|
}
|
|
|
|
|
2015-08-07 13:48:29 +03:00
|
|
|
/// Returns the kind of the ADT - Struct or Enum.
|
2015-07-22 20:10:18 +03:00
|
|
|
#[inline]
|
2015-08-07 14:41:33 +03:00
|
|
|
pub fn adt_kind(&self) -> AdtKind {
|
2016-09-06 01:26:02 +03:00
|
|
|
if self.is_enum() {
|
2015-08-07 14:41:33 +03:00
|
|
|
AdtKind::Enum
|
2016-09-06 01:26:02 +03:00
|
|
|
} else if self.is_union() {
|
2016-08-09 01:18:47 +03:00
|
|
|
AdtKind::Union
|
2015-07-22 20:10:18 +03:00
|
|
|
} else {
|
2015-08-07 14:41:33 +03:00
|
|
|
AdtKind::Struct
|
2015-07-22 20:10:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 01:26:02 +03:00
|
|
|
pub fn descr(&self) -> &'static str {
|
|
|
|
match self.adt_kind() {
|
|
|
|
AdtKind::Struct => "struct",
|
|
|
|
AdtKind::Union => "union",
|
|
|
|
AdtKind::Enum => "enum",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn variant_descr(&self) -> &'static str {
|
|
|
|
match self.adt_kind() {
|
|
|
|
AdtKind::Struct => "struct",
|
|
|
|
AdtKind::Union => "union",
|
|
|
|
AdtKind::Enum => "variant",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 13:48:29 +03:00
|
|
|
/// Returns whether this is a dtorck type. If this returns
|
|
|
|
/// true, this type being safe for destruction requires it to be
|
|
|
|
/// alive; Otherwise, only the contents are required to be.
|
2015-07-20 22:13:36 +03:00
|
|
|
#[inline]
|
2016-05-02 18:07:47 +03:00
|
|
|
pub fn is_dtorck(&'gcx self, tcx: TyCtxt) -> bool {
|
2015-08-07 14:41:33 +03:00
|
|
|
if !self.flags.get().intersects(AdtFlags::IS_DTORCK_VALID) {
|
2015-07-20 22:13:36 +03:00
|
|
|
self.calculate_dtorck(tcx)
|
|
|
|
}
|
2015-08-07 14:41:33 +03:00
|
|
|
self.flags.get().intersects(AdtFlags::IS_DTORCK)
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 13:48:29 +03:00
|
|
|
/// Returns whether this type is #[fundamental] for the purposes
|
|
|
|
/// of coherence checking.
|
2015-07-20 22:13:36 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn is_fundamental(&self) -> bool {
|
2015-08-07 14:41:33 +03:00
|
|
|
self.flags.get().intersects(AdtFlags::IS_FUNDAMENTAL)
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
|
|
|
|
2015-08-06 18:25:15 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn is_simd(&self) -> bool {
|
2015-08-07 14:41:33 +03:00
|
|
|
self.flags.get().intersects(AdtFlags::IS_SIMD)
|
2015-08-06 18:25:15 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 13:48:29 +03:00
|
|
|
/// Returns true if this is PhantomData<T>.
|
2015-07-20 22:13:36 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn is_phantom_data(&self) -> bool {
|
2015-08-07 14:41:33 +03:00
|
|
|
self.flags.get().intersects(AdtFlags::IS_PHANTOM_DATA)
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 13:48:29 +03:00
|
|
|
/// Returns whether this type has a destructor.
|
2015-08-25 21:52:15 +03:00
|
|
|
pub fn has_dtor(&self) -> bool {
|
2016-08-23 10:39:30 +03:00
|
|
|
self.dtor_kind().is_present()
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 13:48:29 +03:00
|
|
|
/// Asserts this is a struct and returns the struct's unique
|
|
|
|
/// variant.
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn struct_variant(&self) -> &VariantDefData<'gcx, 'container> {
|
2016-09-06 01:26:02 +03:00
|
|
|
assert!(!self.is_enum());
|
2015-07-22 21:53:52 +03:00
|
|
|
&self.variants[0]
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn predicates(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> GenericPredicates<'gcx> {
|
2016-11-10 16:49:53 +02:00
|
|
|
tcx.item_predicates(self.did)
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
2015-07-22 21:53:52 +03:00
|
|
|
|
2015-08-07 13:48:29 +03:00
|
|
|
/// Returns an iterator over all fields contained
|
|
|
|
/// by this ADT.
|
2015-07-22 21:53:52 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn all_fields(&self) ->
|
|
|
|
iter::FlatMap<
|
2016-04-29 06:00:23 +03:00
|
|
|
slice::Iter<VariantDefData<'gcx, 'container>>,
|
|
|
|
slice::Iter<FieldDefData<'gcx, 'container>>,
|
|
|
|
for<'s> fn(&'s VariantDefData<'gcx, 'container>)
|
|
|
|
-> slice::Iter<'s, FieldDefData<'gcx, 'container>>
|
2015-07-22 21:53:52 +03:00
|
|
|
> {
|
2015-08-07 14:41:33 +03:00
|
|
|
self.variants.iter().flat_map(VariantDefData::fields_iter)
|
2015-07-22 21:53:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.variants.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_univariant(&self) -> bool {
|
|
|
|
self.variants.len() == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_payloadfree(&self) -> bool {
|
|
|
|
!self.variants.is_empty() &&
|
|
|
|
self.variants.iter().all(|v| v.fields.is_empty())
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn variant_with_id(&self, vid: DefId) -> &VariantDefData<'gcx, 'container> {
|
2015-07-22 21:53:52 +03:00
|
|
|
self.variants
|
|
|
|
.iter()
|
|
|
|
.find(|v| v.did == vid)
|
|
|
|
.expect("variant_with_id: unknown variant")
|
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
pub fn variant_index_with_id(&self, vid: DefId) -> usize {
|
|
|
|
self.variants
|
|
|
|
.iter()
|
|
|
|
.position(|v| v.did == vid)
|
|
|
|
.expect("variant_index_with_id: unknown variant")
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn variant_of_def(&self, def: Def) -> &VariantDefData<'gcx, 'container> {
|
2015-07-22 21:53:52 +03:00
|
|
|
match def {
|
2016-09-15 00:51:46 +03:00
|
|
|
Def::Variant(vid) | Def::VariantCtor(vid, ..) => self.variant_with_id(vid),
|
|
|
|
Def::Struct(..) | Def::StructCtor(..) | Def::Union(..) |
|
2016-09-15 00:51:46 +03:00
|
|
|
Def::TyAlias(..) | Def::AssociatedTy(..) | Def::SelfTy(..) => self.struct_variant(),
|
2016-03-26 19:59:04 +01:00
|
|
|
_ => bug!("unexpected def {:?} in variant_of_def", def)
|
2015-07-22 21:53:52 +03:00
|
|
|
}
|
|
|
|
}
|
2015-08-25 21:52:15 +03:00
|
|
|
|
|
|
|
pub fn destructor(&self) -> Option<DefId> {
|
|
|
|
self.destructor.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_destructor(&self, dtor: DefId) {
|
|
|
|
self.destructor.set(Some(dtor));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dtor_kind(&self) -> DtorKind {
|
|
|
|
match self.destructor.get() {
|
2016-08-23 10:39:30 +03:00
|
|
|
Some(_) => TraitDtor,
|
2015-08-25 21:52:15 +03:00
|
|
|
None => NoDtor,
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 06:00:23 +03:00
|
|
|
}
|
2016-04-14 15:49:39 +03:00
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx, 'container> AdtDefData<'tcx, 'container> {
|
2016-04-14 15:49:39 +03:00
|
|
|
/// Returns a simpler type such that `Self: Sized` if and only
|
|
|
|
/// if that type is Sized, or `TyErr` if this type is recursive.
|
2016-04-16 15:16:36 +03:00
|
|
|
///
|
2016-04-18 00:04:21 +03:00
|
|
|
/// HACK: instead of returning a list of types, this function can
|
|
|
|
/// return a tuple. In that case, the result is Sized only if
|
|
|
|
/// all elements of the tuple are Sized.
|
|
|
|
///
|
2016-04-16 15:16:36 +03:00
|
|
|
/// This is generally the `struct_tail` if this is a struct, or a
|
|
|
|
/// tuple of them if this is an enum.
|
|
|
|
///
|
|
|
|
/// Oddly enough, checking that the sized-constraint is Sized is
|
|
|
|
/// actually more expressive than checking all members:
|
|
|
|
/// the Sized trait is inductive, so an associated type that references
|
|
|
|
/// Self would prevent its containing ADT from being Sized.
|
|
|
|
///
|
|
|
|
/// Due to normalization being eager, this applies even if
|
|
|
|
/// the associated type is behind a pointer, e.g. issue #31299.
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn sized_constraint(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
|
2016-08-02 19:12:02 -04:00
|
|
|
match self.sized_constraint.get(DepNode::SizedConstraint(self.did)) {
|
2016-04-14 15:49:39 +03:00
|
|
|
None => {
|
2016-04-29 06:00:23 +03:00
|
|
|
let global_tcx = tcx.global_tcx();
|
|
|
|
let this = global_tcx.lookup_adt_def_master(self.did);
|
|
|
|
this.calculate_sized_constraint_inner(global_tcx, &mut Vec::new());
|
2016-04-18 00:04:21 +03:00
|
|
|
self.sized_constraint(tcx)
|
2016-04-14 15:49:39 +03:00
|
|
|
}
|
|
|
|
Some(ty) => ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
impl<'a, 'tcx> AdtDefData<'tcx, 'tcx> {
|
2016-04-22 16:38:17 +03:00
|
|
|
/// Calculates the Sized-constraint.
|
|
|
|
///
|
|
|
|
/// As the Sized-constraint of enums can be a *set* of types,
|
|
|
|
/// the Sized-constraint may need to be a set also. Because introducing
|
|
|
|
/// a new type of IVar is currently a complex affair, the Sized-constraint
|
|
|
|
/// may be a tuple.
|
|
|
|
///
|
|
|
|
/// In fact, there are only a few options for the constraint:
|
|
|
|
/// - `bool`, if the type is always Sized
|
|
|
|
/// - an obviously-unsized type
|
|
|
|
/// - a type parameter or projection whose Sizedness can't be known
|
|
|
|
/// - a tuple of type parameters or projections, if there are multiple
|
|
|
|
/// such.
|
|
|
|
/// - a TyError, if a type contained itself. The representability
|
|
|
|
/// check should catch this case.
|
2016-08-02 19:12:02 -04:00
|
|
|
fn calculate_sized_constraint_inner(&'tcx self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-04-22 16:38:17 +03:00
|
|
|
stack: &mut Vec<AdtDefMaster<'tcx>>)
|
2016-04-14 15:49:39 +03:00
|
|
|
{
|
2016-05-26 06:11:16 -04:00
|
|
|
let dep_node = || DepNode::SizedConstraint(self.did);
|
2016-08-02 19:12:02 -04:00
|
|
|
|
|
|
|
// Follow the memoization pattern: push the computation of
|
|
|
|
// DepNode::SizedConstraint as our current task.
|
|
|
|
let _task = tcx.dep_graph.in_task(dep_node());
|
|
|
|
if self.sized_constraint.untracked_get().is_some() {
|
|
|
|
// ---------------
|
|
|
|
// can skip the dep-graph read since we just pushed the task
|
2016-04-22 16:38:17 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if stack.contains(&self) {
|
|
|
|
debug!("calculate_sized_constraint: {:?} is recursive", self);
|
|
|
|
// This should be reported as an error by `check_representable`.
|
|
|
|
//
|
|
|
|
// Consider the type as Sized in the meanwhile to avoid
|
|
|
|
// further errors.
|
2016-05-26 06:11:16 -04:00
|
|
|
self.sized_constraint.fulfill(dep_node(), tcx.types.err);
|
2016-04-22 16:38:17 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stack.push(self);
|
|
|
|
|
|
|
|
let tys : Vec<_> =
|
|
|
|
self.variants.iter().flat_map(|v| {
|
|
|
|
v.fields.last()
|
|
|
|
}).flat_map(|f| {
|
|
|
|
self.sized_constraint_for_ty(tcx, stack, f.unsubst_ty())
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
let self_ = stack.pop().unwrap();
|
|
|
|
assert_eq!(self_, self);
|
|
|
|
|
|
|
|
let ty = match tys.len() {
|
2016-04-16 15:16:36 +03:00
|
|
|
_ if tys.references_error() => tcx.types.err,
|
2016-04-14 15:49:39 +03:00
|
|
|
0 => tcx.types.bool,
|
|
|
|
1 => tys[0],
|
2016-10-24 18:23:29 -06:00
|
|
|
_ => tcx.intern_tup(&tys[..])
|
2016-04-22 16:38:17 +03:00
|
|
|
};
|
|
|
|
|
2016-05-26 06:11:16 -04:00
|
|
|
match self.sized_constraint.get(dep_node()) {
|
2016-04-22 16:38:17 +03:00
|
|
|
Some(old_ty) => {
|
|
|
|
debug!("calculate_sized_constraint: {:?} recurred", self);
|
|
|
|
assert_eq!(old_ty, tcx.types.err)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
debug!("calculate_sized_constraint: {:?} => {:?}", self, ty);
|
2016-05-26 06:11:16 -04:00
|
|
|
self.sized_constraint.fulfill(dep_node(), ty)
|
2016-04-22 16:38:17 +03:00
|
|
|
}
|
2016-04-14 15:49:39 +03:00
|
|
|
}
|
|
|
|
}
|
2016-04-18 00:04:21 +03:00
|
|
|
|
2016-04-14 15:49:39 +03:00
|
|
|
fn sized_constraint_for_ty(
|
|
|
|
&'tcx self,
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-04-14 15:49:39 +03:00
|
|
|
stack: &mut Vec<AdtDefMaster<'tcx>>,
|
|
|
|
ty: Ty<'tcx>
|
2016-04-22 16:38:17 +03:00
|
|
|
) -> Vec<Ty<'tcx>> {
|
2016-04-14 15:49:39 +03:00
|
|
|
let result = match ty.sty {
|
|
|
|
TyBool | TyChar | TyInt(..) | TyUint(..) | TyFloat(..) |
|
|
|
|
TyBox(..) | TyRawPtr(..) | TyRef(..) | TyFnDef(..) | TyFnPtr(_) |
|
2016-08-02 15:56:20 +08:00
|
|
|
TyArray(..) | TyClosure(..) | TyNever => {
|
2016-04-22 16:38:17 +03:00
|
|
|
vec![]
|
2016-04-14 15:49:39 +03:00
|
|
|
}
|
|
|
|
|
2016-04-16 15:16:36 +03:00
|
|
|
TyStr | TyTrait(..) | TySlice(_) | TyError => {
|
2016-04-14 15:49:39 +03:00
|
|
|
// these are never sized - return the target type
|
2016-04-22 16:38:17 +03:00
|
|
|
vec![ty]
|
2016-04-14 15:49:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TyTuple(ref tys) => {
|
2016-09-01 13:34:56 +03:00
|
|
|
match tys.last() {
|
|
|
|
None => vec![],
|
|
|
|
Some(ty) => self.sized_constraint_for_ty(tcx, stack, ty)
|
|
|
|
}
|
2016-04-14 15:49:39 +03:00
|
|
|
}
|
|
|
|
|
2016-09-06 01:26:02 +03:00
|
|
|
TyAdt(adt, substs) => {
|
2016-04-14 15:49:39 +03:00
|
|
|
// recursive case
|
|
|
|
let adt = tcx.lookup_adt_def_master(adt.did);
|
|
|
|
adt.calculate_sized_constraint_inner(tcx, stack);
|
|
|
|
let adt_ty =
|
|
|
|
adt.sized_constraint
|
|
|
|
.unwrap(DepNode::SizedConstraint(adt.did))
|
|
|
|
.subst(tcx, substs);
|
|
|
|
debug!("sized_constraint_for_ty({:?}) intermediate = {:?}",
|
|
|
|
ty, adt_ty);
|
2016-04-22 16:38:17 +03:00
|
|
|
if let ty::TyTuple(ref tys) = adt_ty.sty {
|
|
|
|
tys.iter().flat_map(|ty| {
|
|
|
|
self.sized_constraint_for_ty(tcx, stack, ty)
|
|
|
|
}).collect()
|
|
|
|
} else {
|
|
|
|
self.sized_constraint_for_ty(tcx, stack, adt_ty)
|
|
|
|
}
|
2016-04-14 15:49:39 +03:00
|
|
|
}
|
|
|
|
|
2016-07-22 18:56:22 +03:00
|
|
|
TyProjection(..) | TyAnon(..) => {
|
2016-04-14 15:49:39 +03:00
|
|
|
// must calculate explicitly.
|
|
|
|
// FIXME: consider special-casing always-Sized projections
|
2016-04-22 16:38:17 +03:00
|
|
|
vec![ty]
|
2016-04-14 15:49:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TyParam(..) => {
|
2016-04-16 15:16:36 +03:00
|
|
|
// perf hack: if there is a `T: Sized` bound, then
|
|
|
|
// we know that `T` is Sized and do not need to check
|
|
|
|
// it on the impl.
|
|
|
|
|
2016-04-14 15:49:39 +03:00
|
|
|
let sized_trait = match tcx.lang_items.sized_trait() {
|
|
|
|
Some(x) => x,
|
2016-04-22 16:38:17 +03:00
|
|
|
_ => return vec![ty]
|
2016-04-14 15:49:39 +03:00
|
|
|
};
|
|
|
|
let sized_predicate = Binder(TraitRef {
|
|
|
|
def_id: sized_trait,
|
2016-10-24 18:23:29 -06:00
|
|
|
substs: tcx.mk_substs_trait(ty, &[])
|
2016-04-14 15:49:39 +03:00
|
|
|
}).to_predicate();
|
2016-11-10 16:49:53 +02:00
|
|
|
let predicates = tcx.item_predicates(self.did).predicates;
|
2016-04-14 15:49:39 +03:00
|
|
|
if predicates.into_iter().any(|p| p == sized_predicate) {
|
2016-04-22 16:38:17 +03:00
|
|
|
vec![]
|
2016-04-14 15:49:39 +03:00
|
|
|
} else {
|
2016-04-22 16:38:17 +03:00
|
|
|
vec![ty]
|
2016-04-14 15:49:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-16 15:16:36 +03:00
|
|
|
TyInfer(..) => {
|
2016-04-14 15:49:39 +03:00
|
|
|
bug!("unexpected type `{:?}` in sized_constraint_for_ty",
|
|
|
|
ty)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
debug!("sized_constraint_for_ty({:?}) = {:?}", ty, result);
|
|
|
|
result
|
|
|
|
}
|
2015-07-22 21:53:52 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:33 +03:00
|
|
|
impl<'tcx, 'container> VariantDefData<'tcx, 'container> {
|
2015-07-22 21:53:52 +03:00
|
|
|
#[inline]
|
2015-08-07 14:41:33 +03:00
|
|
|
fn fields_iter(&self) -> slice::Iter<FieldDefData<'tcx, 'container>> {
|
2015-07-22 21:53:52 +03:00
|
|
|
self.fields.iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-08-07 14:41:33 +03:00
|
|
|
pub fn find_field_named(&self,
|
|
|
|
name: ast::Name)
|
|
|
|
-> Option<&FieldDefData<'tcx, 'container>> {
|
2015-07-22 21:53:52 +03:00
|
|
|
self.fields.iter().find(|f| f.name == name)
|
|
|
|
}
|
|
|
|
|
2015-10-21 17:19:02 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn index_of_field_named(&self,
|
|
|
|
name: ast::Name)
|
|
|
|
-> Option<usize> {
|
|
|
|
self.fields.iter().position(|f| f.name == name)
|
|
|
|
}
|
|
|
|
|
2015-07-22 21:53:52 +03:00
|
|
|
#[inline]
|
2015-08-07 14:41:33 +03:00
|
|
|
pub fn field_named(&self, name: ast::Name) -> &FieldDefData<'tcx, 'container> {
|
2015-07-22 21:53:52 +03:00
|
|
|
self.find_field_named(name).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx, 'container> FieldDefData<'tcx, 'container> {
|
2015-07-22 21:53:52 +03:00
|
|
|
pub fn new(did: DefId,
|
|
|
|
name: Name,
|
2016-03-25 06:08:11 +00:00
|
|
|
vis: Visibility) -> Self {
|
2015-08-07 14:41:33 +03:00
|
|
|
FieldDefData {
|
2015-07-22 21:53:52 +03:00
|
|
|
did: did,
|
|
|
|
name: name,
|
|
|
|
vis: vis,
|
2015-09-06 21:51:58 +03:00
|
|
|
ty: ivar::TyIVar::new()
|
2015-07-22 21:53:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, subst: &Substs<'tcx>) -> Ty<'tcx> {
|
2015-07-22 21:53:52 +03:00
|
|
|
self.unsubst_ty().subst(tcx, subst)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unsubst_ty(&self) -> Ty<'tcx> {
|
2015-12-22 16:39:33 -05:00
|
|
|
self.ty.unwrap(DepNode::FieldTy(self.did))
|
2015-07-22 21:53:52 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 13:48:29 +03:00
|
|
|
pub fn fulfill_ty(&self, ty: Ty<'container>) {
|
2015-12-22 16:39:33 -05:00
|
|
|
self.ty.fulfill(DepNode::FieldTy(self.did), ty);
|
2015-07-22 21:53:52 +03:00
|
|
|
}
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
|
|
|
|
2014-05-07 07:20:15 -04:00
|
|
|
/// Records the substitutions used to translate the polytype for an
|
|
|
|
/// item into the monotype of an item reference.
|
2016-08-31 14:00:29 +03:00
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub struct ItemSubsts<'tcx> {
|
2016-04-29 08:30:54 +03:00
|
|
|
pub substs: &'tcx Substs<'tcx>,
|
2014-05-07 07:20:15 -04:00
|
|
|
}
|
|
|
|
|
2016-04-06 00:20:59 -07:00
|
|
|
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
|
2015-01-24 22:00:03 +02:00
|
|
|
pub enum ClosureKind {
|
2015-02-15 15:09:26 -05:00
|
|
|
// Warning: Ordering is significant here! The ordering is chosen
|
|
|
|
// because the trait Fn is a subtrait of FnMut and so in turn, and
|
|
|
|
// hence we order it so that Fn < FnMut < FnOnce.
|
2016-02-12 16:44:27 +01:00
|
|
|
Fn,
|
|
|
|
FnMut,
|
|
|
|
FnOnce,
|
2014-07-29 22:08:39 -07:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
impl<'a, 'tcx> ClosureKind {
|
|
|
|
pub fn trait_did(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> DefId {
|
2014-08-20 09:12:16 -07:00
|
|
|
let result = match *self {
|
2016-05-03 04:02:41 +03:00
|
|
|
ClosureKind::Fn => tcx.lang_items.require(FnTraitLangItem),
|
2016-02-12 16:44:27 +01:00
|
|
|
ClosureKind::FnMut => {
|
2016-05-03 04:02:41 +03:00
|
|
|
tcx.lang_items.require(FnMutTraitLangItem)
|
2014-08-20 09:12:16 -07:00
|
|
|
}
|
2016-02-12 16:44:27 +01:00
|
|
|
ClosureKind::FnOnce => {
|
2016-05-03 04:02:41 +03:00
|
|
|
tcx.lang_items.require(FnOnceTraitLangItem)
|
2014-08-20 09:12:16 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
match result {
|
|
|
|
Ok(trait_did) => trait_did,
|
2016-05-03 04:02:41 +03:00
|
|
|
Err(err) => tcx.sess.fatal(&err[..]),
|
2014-08-20 09:12:16 -07:00
|
|
|
}
|
|
|
|
}
|
2015-02-15 15:09:26 -05:00
|
|
|
|
|
|
|
/// True if this a type that impls this closure kind
|
|
|
|
/// must also implement `other`.
|
|
|
|
pub fn extends(self, other: ty::ClosureKind) -> bool {
|
|
|
|
match (self, other) {
|
2016-02-12 16:44:27 +01:00
|
|
|
(ClosureKind::Fn, ClosureKind::Fn) => true,
|
|
|
|
(ClosureKind::Fn, ClosureKind::FnMut) => true,
|
|
|
|
(ClosureKind::Fn, ClosureKind::FnOnce) => true,
|
|
|
|
(ClosureKind::FnMut, ClosureKind::FnMut) => true,
|
|
|
|
(ClosureKind::FnMut, ClosureKind::FnOnce) => true,
|
|
|
|
(ClosureKind::FnOnce, ClosureKind::FnOnce) => true,
|
2015-02-15 15:09:26 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2014-08-20 09:12:16 -07:00
|
|
|
}
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
impl<'tcx> TyS<'tcx> {
|
|
|
|
/// Iterator that walks `self` and any types reachable from
|
|
|
|
/// `self`, in depth-first order. Note that just walks the types
|
|
|
|
/// that appear in `self`, it does not descend into the fields of
|
|
|
|
/// structs or variants. For example:
|
|
|
|
///
|
|
|
|
/// ```notrust
|
|
|
|
/// isize => { isize }
|
|
|
|
/// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
|
|
|
|
/// [isize] => { [isize], isize }
|
|
|
|
/// ```
|
|
|
|
pub fn walk(&'tcx self) -> TypeWalker<'tcx> {
|
|
|
|
TypeWalker::new(self)
|
2014-11-15 16:47:59 -05:00
|
|
|
}
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// Iterator that walks the immediate children of `self`. Hence
|
|
|
|
/// `Foo<Bar<i32>, u32>` yields the sequence `[Bar<i32>, u32]`
|
|
|
|
/// (but not `i32`, like `walk`).
|
|
|
|
pub fn walk_shallow(&'tcx self) -> IntoIter<Ty<'tcx>> {
|
|
|
|
walk::walk_shallow(self)
|
2014-11-15 16:47:59 -05:00
|
|
|
}
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// Walks `ty` and any types appearing within `ty`, invoking the
|
|
|
|
/// callback `f` on each type. If the callback returns false, then the
|
|
|
|
/// children of the current type are ignored.
|
|
|
|
///
|
|
|
|
/// Note: prefer `ty.walk()` where possible.
|
|
|
|
pub fn maybe_walk<F>(&'tcx self, mut f: F)
|
|
|
|
where F : FnMut(Ty<'tcx>) -> bool
|
|
|
|
{
|
|
|
|
let mut walker = self.walk();
|
|
|
|
while let Some(ty) = walker.next() {
|
|
|
|
if !f(ty) {
|
|
|
|
walker.skip_current_subtree();
|
|
|
|
}
|
|
|
|
}
|
2013-10-29 16:08:49 -04:00
|
|
|
}
|
2014-11-14 09:18:10 -08:00
|
|
|
}
|
2013-10-29 16:08:49 -04:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
impl<'tcx> ItemSubsts<'tcx> {
|
|
|
|
pub fn is_noop(&self) -> bool {
|
|
|
|
self.substs.is_noop()
|
2014-11-17 11:29:38 -08:00
|
|
|
}
|
2012-07-16 20:17:57 -07:00
|
|
|
}
|
2014-02-26 19:22:41 +01:00
|
|
|
|
2015-09-03 13:10:34 -04:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum LvaluePreference {
|
|
|
|
PreferMutLvalue,
|
|
|
|
NoPreference
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LvaluePreference {
|
|
|
|
pub fn from_mutbl(m: hir::Mutability) -> Self {
|
|
|
|
match m {
|
|
|
|
hir::MutMutable => PreferMutLvalue,
|
|
|
|
hir::MutImmutable => NoPreference,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 12:29:48 +13:00
|
|
|
/// Helper for looking things up in the various maps that are populated during
|
2016-11-10 16:49:53 +02:00
|
|
|
/// typeck::collect (e.g., `tcx.associated_items`, `tcx.types`, etc). All of
|
2015-01-01 12:29:48 +13:00
|
|
|
/// these share the pattern that if the id is local, it should have been loaded
|
|
|
|
/// into the map by the `typeck::collect` phase. If the def-id is external,
|
|
|
|
/// then we have to go consult the crate loading code (and cache the result for
|
2014-11-25 21:17:11 -05:00
|
|
|
/// the future).
|
2015-12-22 16:39:33 -05:00
|
|
|
fn lookup_locally_or_in_crate_store<M, F>(descr: &str,
|
2015-08-16 06:32:28 -04:00
|
|
|
def_id: DefId,
|
2015-12-22 16:39:33 -05:00
|
|
|
map: &M,
|
|
|
|
load_external: F)
|
|
|
|
-> M::Value where
|
|
|
|
M: MemoizationMap<Key=DefId>,
|
|
|
|
F: FnOnce() -> M::Value,
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2015-12-22 16:39:33 -05:00
|
|
|
map.memoize(def_id, || {
|
|
|
|
if def_id.is_local() {
|
2016-03-26 19:59:04 +01:00
|
|
|
bug!("No def'n found for {:?} in tcx.{}", def_id, descr);
|
2015-12-22 16:39:33 -05:00
|
|
|
}
|
|
|
|
load_external()
|
|
|
|
})
|
2013-03-27 10:26:57 -04:00
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
impl BorrowKind {
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn from_mutbl(m: hir::Mutability) -> BorrowKind {
|
2015-06-25 23:42:17 +03:00
|
|
|
match m {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::MutMutable => MutBorrow,
|
|
|
|
hir::MutImmutable => ImmBorrow,
|
2014-08-05 19:44:21 -07:00
|
|
|
}
|
|
|
|
}
|
2013-03-27 10:26:57 -04:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Returns a mutability `m` such that an `&m T` pointer could be used to obtain this borrow
|
|
|
|
/// kind. Because borrow kinds are richer than mutabilities, we sometimes have to pick a
|
|
|
|
/// mutability that is stronger than necessary so that it at least *would permit* the borrow in
|
|
|
|
/// question.
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn to_mutbl_lossy(self) -> hir::Mutability {
|
2015-06-25 23:42:17 +03:00
|
|
|
match self {
|
2015-07-31 00:04:06 -07:00
|
|
|
MutBorrow => hir::MutMutable,
|
|
|
|
ImmBorrow => hir::MutImmutable,
|
2015-06-25 23:42:17 +03:00
|
|
|
|
|
|
|
// We have no type corresponding to a unique imm borrow, so
|
|
|
|
// use `&mut`. It gives all the capabilities of an `&uniq`
|
|
|
|
// and hence is a safe "over approximation".
|
2015-07-31 00:04:06 -07:00
|
|
|
UniqueImmBorrow => hir::MutMutable,
|
2012-03-07 12:54:00 +01:00
|
|
|
}
|
2015-05-26 00:41:27 +03:00
|
|
|
}
|
2012-01-05 10:57:19 +01:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
pub fn to_user_str(&self) -> &'static str {
|
|
|
|
match *self {
|
|
|
|
MutBorrow => "mutable",
|
|
|
|
ImmBorrow => "immutable",
|
|
|
|
UniqueImmBorrow => "uniquely immutable",
|
|
|
|
}
|
2012-03-08 16:10:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
2016-10-27 04:52:10 +03:00
|
|
|
pub fn tables(self) -> Ref<'a, Tables<'gcx>> {
|
|
|
|
self.tables.borrow()
|
2013-06-23 14:41:45 +12:00
|
|
|
}
|
2012-11-28 15:42:16 -08:00
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn expr_span(self, id: NodeId) -> Span {
|
2015-06-25 23:42:17 +03:00
|
|
|
match self.map.find(id) {
|
|
|
|
Some(ast_map::NodeExpr(e)) => {
|
|
|
|
e.span
|
|
|
|
}
|
|
|
|
Some(f) => {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("Node id {} is not an expr: {:?}", id, f);
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
None => {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("Node id {} is not present in the node map", id);
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2013-06-23 14:41:45 +12:00
|
|
|
}
|
2012-05-15 17:59:55 -07:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn local_var_name_str(self, id: NodeId) -> InternedString {
|
2015-06-25 23:42:17 +03:00
|
|
|
match self.map.find(id) {
|
|
|
|
Some(ast_map::NodeLocal(pat)) => {
|
|
|
|
match pat.node {
|
2016-09-19 23:49:01 +03:00
|
|
|
hir::PatKind::Binding(_, ref path1, _) => path1.node.as_str(),
|
2015-06-25 23:42:17 +03:00
|
|
|
_ => {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("Variable id {} maps to {:?}, not local", id, pat);
|
2015-07-28 18:07:20 +02:00
|
|
|
},
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2015-07-28 18:07:20 +02:00
|
|
|
},
|
2016-03-25 01:14:29 +01:00
|
|
|
r => bug!("Variable id {} maps to {:?}, not local", id, r),
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2012-02-10 06:01:32 -08:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn expr_is_lval(self, expr: &hir::Expr) -> bool {
|
2015-06-25 23:42:17 +03:00
|
|
|
match expr.node {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprPath(..) => {
|
2016-06-03 23:15:00 +03:00
|
|
|
// This function can be used during type checking when not all paths are
|
|
|
|
// fully resolved. Partially resolved paths in expressions can only legally
|
|
|
|
// refer to associated items which are always rvalues.
|
|
|
|
match self.expect_resolution(expr.id).base_def {
|
|
|
|
Def::Local(..) | Def::Upvar(..) | Def::Static(..) | Def::Err => true,
|
|
|
|
_ => false,
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-27 01:43:14 +01:00
|
|
|
|
2015-12-03 05:37:48 +03:00
|
|
|
hir::ExprType(ref e, _) => {
|
|
|
|
self.expr_is_lval(e)
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprUnary(hir::UnDeref, _) |
|
|
|
|
hir::ExprField(..) |
|
|
|
|
hir::ExprTupField(..) |
|
|
|
|
hir::ExprIndex(..) => {
|
2015-06-25 23:42:17 +03:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprCall(..) |
|
|
|
|
hir::ExprMethodCall(..) |
|
|
|
|
hir::ExprStruct(..) |
|
|
|
|
hir::ExprTup(..) |
|
|
|
|
hir::ExprIf(..) |
|
|
|
|
hir::ExprMatch(..) |
|
|
|
|
hir::ExprClosure(..) |
|
|
|
|
hir::ExprBlock(..) |
|
|
|
|
hir::ExprRepeat(..) |
|
2016-09-20 02:14:46 +02:00
|
|
|
hir::ExprArray(..) |
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprBreak(..) |
|
|
|
|
hir::ExprAgain(..) |
|
|
|
|
hir::ExprRet(..) |
|
|
|
|
hir::ExprWhile(..) |
|
|
|
|
hir::ExprLoop(..) |
|
|
|
|
hir::ExprAssign(..) |
|
|
|
|
hir::ExprInlineAsm(..) |
|
|
|
|
hir::ExprAssignOp(..) |
|
|
|
|
hir::ExprLit(_) |
|
|
|
|
hir::ExprUnary(..) |
|
2015-09-06 21:51:58 +03:00
|
|
|
hir::ExprBox(..) |
|
|
|
|
hir::ExprAddrOf(..) |
|
|
|
|
hir::ExprBinary(..) |
|
|
|
|
hir::ExprCast(..) => {
|
|
|
|
false
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2015-03-27 01:43:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
pub fn provided_trait_methods(self, id: DefId) -> Vec<AssociatedItem> {
|
|
|
|
self.associated_items(id)
|
|
|
|
.filter(|item| item.kind == AssociatedKind::Method && item.has_value)
|
|
|
|
.collect()
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
|
2016-09-19 23:49:01 +03:00
|
|
|
pub fn trait_impl_polarity(self, id: DefId) -> hir::ImplPolarity {
|
2015-09-04 13:52:28 -04:00
|
|
|
if let Some(id) = self.map.as_local_node_id(id) {
|
|
|
|
match self.map.expect_item(id).node {
|
2016-09-19 23:49:01 +03:00
|
|
|
hir::ItemImpl(_, polarity, ..) => polarity,
|
|
|
|
ref item => bug!("trait_impl_polarity: {:?} not an impl", item)
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
} else {
|
2015-11-20 14:51:18 +02:00
|
|
|
self.sess.cstore.impl_polarity(id)
|
2015-03-27 01:43:14 +01:00
|
|
|
}
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2015-03-27 01:43:14 +01:00
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn custom_coerce_unsized_kind(self, did: DefId) -> adjustment::CustomCoerceUnsized {
|
2015-12-22 16:39:33 -05:00
|
|
|
self.custom_coerce_unsized_kinds.memoize(did, || {
|
2015-08-16 06:32:28 -04:00
|
|
|
let (kind, src) = if did.krate != LOCAL_CRATE {
|
2015-11-20 14:51:18 +02:00
|
|
|
(self.sess.cstore.custom_coerce_unsized_kind(did), "external")
|
2015-06-25 23:42:17 +03:00
|
|
|
} else {
|
|
|
|
(None, "local")
|
|
|
|
};
|
2015-03-27 01:43:14 +01:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
match kind {
|
|
|
|
Some(kind) => kind,
|
|
|
|
None => {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("custom_coerce_unsized_kind: \
|
|
|
|
{} impl `{}` is missing its kind",
|
|
|
|
src, self.item_path_str(did));
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
pub fn associated_item(self, def_id: DefId) -> AssociatedItem {
|
|
|
|
self.associated_items.memoize(def_id, || {
|
|
|
|
if !def_id.is_local() {
|
|
|
|
return self.sess.cstore.associated_item(self.global_tcx(), def_id)
|
|
|
|
.expect("missing AssociatedItem in metadata");
|
|
|
|
}
|
|
|
|
|
2016-11-11 18:22:41 -05:00
|
|
|
// When the user asks for a given associated item, we
|
|
|
|
// always go ahead and convert all the associated items in
|
|
|
|
// the container. Note that we are also careful only to
|
|
|
|
// ever register a read on the *container* of the assoc
|
|
|
|
// item, not the assoc item itself. This prevents changes
|
|
|
|
// in the details of an item (for example, the type to
|
|
|
|
// which an associated type is bound) from contaminating
|
|
|
|
// those tasks that just need to scan the names of items
|
|
|
|
// and so forth.
|
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
let id = self.map.as_local_node_id(def_id).unwrap();
|
|
|
|
let parent_id = self.map.get_parent(id);
|
|
|
|
let parent_def_id = self.map.local_def_id(parent_id);
|
2016-11-11 18:22:41 -05:00
|
|
|
let parent_item = self.map.expect_item(parent_id);
|
|
|
|
match parent_item.node {
|
|
|
|
hir::ItemImpl(.., ref impl_trait_ref, _, ref impl_item_refs) => {
|
|
|
|
for impl_item_ref in impl_item_refs {
|
|
|
|
let assoc_item =
|
|
|
|
self.associated_item_from_impl_item_ref(parent_def_id,
|
|
|
|
impl_trait_ref.is_some(),
|
|
|
|
impl_item_ref);
|
|
|
|
self.associated_items.borrow_mut().insert(assoc_item.def_id, assoc_item);
|
2016-11-10 02:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-11 18:22:41 -05:00
|
|
|
|
|
|
|
hir::ItemTrait(.., ref trait_items) => {
|
|
|
|
for trait_item in trait_items {
|
|
|
|
let assoc_item =
|
|
|
|
self.associated_item_from_trait_item_ref(parent_def_id, trait_item);
|
|
|
|
self.associated_items.borrow_mut().insert(assoc_item.def_id, assoc_item);
|
2016-11-10 02:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-11 18:22:41 -05:00
|
|
|
|
|
|
|
ref r => {
|
|
|
|
panic!("unexpected container of associated items: {:?}", r)
|
|
|
|
}
|
2016-11-10 02:06:34 +02:00
|
|
|
}
|
2016-11-11 18:22:41 -05:00
|
|
|
|
|
|
|
// memoize wants us to return something, so return
|
|
|
|
// the one we generated for this def-id
|
|
|
|
*self.associated_items.borrow().get(&def_id).unwrap()
|
2016-11-10 02:06:34 +02:00
|
|
|
})
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
|
2016-11-11 18:22:41 -05:00
|
|
|
fn associated_item_from_trait_item_ref(self,
|
|
|
|
parent_def_id: DefId,
|
|
|
|
trait_item: &hir::TraitItem)
|
|
|
|
-> AssociatedItem {
|
|
|
|
let def_id = self.map.local_def_id(trait_item.id);
|
|
|
|
|
|
|
|
let (kind, has_self, has_value) = match trait_item.node {
|
|
|
|
hir::MethodTraitItem(ref sig, ref body) => {
|
|
|
|
(AssociatedKind::Method, sig.decl.get_self().is_some(),
|
|
|
|
body.is_some())
|
|
|
|
}
|
|
|
|
hir::ConstTraitItem(_, ref value) => {
|
|
|
|
(AssociatedKind::Const, false, value.is_some())
|
|
|
|
}
|
|
|
|
hir::TypeTraitItem(_, ref ty) => {
|
|
|
|
(AssociatedKind::Type, false, ty.is_some())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AssociatedItem {
|
|
|
|
name: trait_item.name,
|
|
|
|
kind: kind,
|
|
|
|
vis: Visibility::from_hir(&hir::Inherited, trait_item.id, self),
|
|
|
|
defaultness: hir::Defaultness::Default,
|
|
|
|
has_value: has_value,
|
|
|
|
def_id: def_id,
|
|
|
|
container: TraitContainer(parent_def_id),
|
|
|
|
method_has_self_argument: has_self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn associated_item_from_impl_item_ref(self,
|
|
|
|
parent_def_id: DefId,
|
|
|
|
from_trait_impl: bool,
|
|
|
|
impl_item_ref: &hir::ImplItemRef)
|
|
|
|
-> AssociatedItem {
|
|
|
|
let def_id = self.map.local_def_id(impl_item_ref.id.node_id);
|
|
|
|
let (kind, has_self) = match impl_item_ref.kind {
|
|
|
|
hir::AssociatedItemKind::Const => (ty::AssociatedKind::Const, false),
|
|
|
|
hir::AssociatedItemKind::Method { has_self } => {
|
|
|
|
(ty::AssociatedKind::Method, has_self)
|
|
|
|
}
|
|
|
|
hir::AssociatedItemKind::Type => (ty::AssociatedKind::Type, false),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Trait impl items are always public.
|
|
|
|
let public = hir::Public;
|
|
|
|
let vis = if from_trait_impl { &public } else { &impl_item_ref.vis };
|
|
|
|
|
|
|
|
ty::AssociatedItem {
|
|
|
|
name: impl_item_ref.name,
|
|
|
|
kind: kind,
|
|
|
|
vis: ty::Visibility::from_hir(vis, impl_item_ref.id.node_id, self),
|
|
|
|
defaultness: impl_item_ref.defaultness,
|
|
|
|
has_value: true,
|
|
|
|
def_id: def_id,
|
|
|
|
container: ImplContainer(parent_def_id),
|
|
|
|
method_has_self_argument: has_self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
pub fn associated_item_def_ids(self, def_id: DefId) -> Rc<Vec<DefId>> {
|
|
|
|
self.associated_item_def_ids.memoize(def_id, || {
|
|
|
|
if !def_id.is_local() {
|
|
|
|
return Rc::new(self.sess.cstore.associated_item_def_ids(def_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = self.map.as_local_node_id(def_id).unwrap();
|
|
|
|
let item = self.map.expect_item(id);
|
2016-11-11 18:22:41 -05:00
|
|
|
let vec: Vec<_> = match item.node {
|
2016-11-10 02:06:34 +02:00
|
|
|
hir::ItemTrait(.., ref trait_items) => {
|
2016-11-11 18:22:41 -05:00
|
|
|
trait_items.iter()
|
|
|
|
.map(|trait_item| trait_item.id)
|
|
|
|
.map(|id| self.map.local_def_id(id))
|
|
|
|
.collect()
|
2016-11-10 02:06:34 +02:00
|
|
|
}
|
2016-11-10 09:47:00 -05:00
|
|
|
hir::ItemImpl(.., ref impl_item_refs) => {
|
2016-11-11 18:22:41 -05:00
|
|
|
impl_item_refs.iter()
|
|
|
|
.map(|impl_item_ref| impl_item_ref.id)
|
|
|
|
.map(|id| self.map.local_def_id(id.node_id))
|
|
|
|
.collect()
|
2016-11-10 02:06:34 +02:00
|
|
|
}
|
|
|
|
_ => span_bug!(item.span, "associated_item_def_ids: not impl or trait")
|
2016-11-11 18:22:41 -05:00
|
|
|
};
|
|
|
|
Rc::new(vec)
|
2016-11-10 02:06:34 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline] // FIXME(#35870) Avoid closures being unexported due to impl Trait.
|
|
|
|
pub fn associated_items(self, def_id: DefId)
|
|
|
|
-> impl Iterator<Item = ty::AssociatedItem> + 'a {
|
|
|
|
let def_ids = self.associated_item_def_ids(def_id);
|
|
|
|
(0..def_ids.len()).map(move |i| self.associated_item(def_ids[i]))
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the trait-ref corresponding to a given impl, or None if it is
|
|
|
|
/// an inherent impl.
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn impl_trait_ref(self, id: DefId) -> Option<TraitRef<'gcx>> {
|
2015-06-25 23:42:17 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"impl_trait_refs", id, &self.impl_trait_refs,
|
2016-04-29 06:00:23 +03:00
|
|
|
|| self.sess.cstore.impl_trait_ref(self.global_tcx(), id))
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
|
2016-06-03 23:15:00 +03:00
|
|
|
/// Returns a path resolution for node id if it exists, panics otherwise.
|
|
|
|
pub fn expect_resolution(self, id: NodeId) -> PathResolution {
|
|
|
|
*self.def_map.borrow().get(&id).expect("no def-map entry for node id")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a fully resolved definition for node id if it exists, panics otherwise.
|
|
|
|
pub fn expect_def(self, id: NodeId) -> Def {
|
|
|
|
self.expect_resolution(id).full_def()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a fully resolved definition for node id if it exists, or none if no
|
|
|
|
/// definition exists, panics on partial resolutions to catch errors.
|
|
|
|
pub fn expect_def_or_none(self, id: NodeId) -> Option<Def> {
|
|
|
|
self.def_map.borrow().get(&id).map(|resolution| resolution.full_def())
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2015-03-27 01:43:14 +01:00
|
|
|
|
2016-06-11 18:47:47 +03:00
|
|
|
// Returns `ty::VariantDef` if `def` refers to a struct,
|
|
|
|
// or variant or their constructors, panics otherwise.
|
|
|
|
pub fn expect_variant_def(self, def: Def) -> VariantDef<'tcx> {
|
|
|
|
match def {
|
2016-09-15 00:51:46 +03:00
|
|
|
Def::Variant(did) | Def::VariantCtor(did, ..) => {
|
2016-09-08 19:05:50 +03:00
|
|
|
let enum_did = self.parent_def_id(did).unwrap();
|
2016-06-11 18:47:47 +03:00
|
|
|
self.lookup_adt_def(enum_did).variant_with_id(did)
|
|
|
|
}
|
2016-10-11 23:04:29 +03:00
|
|
|
Def::Struct(did) | Def::Union(did) => {
|
|
|
|
self.lookup_adt_def(did).struct_variant()
|
|
|
|
}
|
|
|
|
Def::StructCtor(ctor_did, ..) => {
|
|
|
|
let did = self.parent_def_id(ctor_did).expect("struct ctor has no parent");
|
2016-06-11 18:47:47 +03:00
|
|
|
self.lookup_adt_def(did).struct_variant()
|
|
|
|
}
|
|
|
|
_ => bug!("expect_variant_def used with unexpected def {:?}", def)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn def_key(self, id: DefId) -> ast_map::DefKey {
|
2016-03-16 05:57:03 -04:00
|
|
|
if id.is_local() {
|
|
|
|
self.map.def_key(id)
|
|
|
|
} else {
|
|
|
|
self.sess.cstore.def_key(id)
|
|
|
|
}
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
|
2016-09-02 18:38:54 -04:00
|
|
|
/// Convert a `DefId` into its fully expanded `DefPath` (every
|
|
|
|
/// `DefId` is really just an interned def-path).
|
|
|
|
///
|
|
|
|
/// Note that if `id` is not local to this crate -- or is
|
|
|
|
/// inlined into this crate -- the result will be a non-local
|
|
|
|
/// `DefPath`.
|
|
|
|
///
|
|
|
|
/// This function is only safe to use when you are sure that the
|
|
|
|
/// full def-path is accessible. Examples that are known to be
|
|
|
|
/// safe are local def-ids or items; see `opt_def_path` for more
|
|
|
|
/// details.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn def_path(self, id: DefId) -> ast_map::DefPath {
|
2016-09-02 18:38:54 -04:00
|
|
|
self.opt_def_path(id).unwrap_or_else(|| {
|
|
|
|
bug!("could not load def-path for {:?}", id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a `DefId` into its fully expanded `DefPath` (every
|
|
|
|
/// `DefId` is really just an interned def-path).
|
|
|
|
///
|
|
|
|
/// When going across crates, we do not save the full info for
|
|
|
|
/// every cross-crate def-id, and hence we may not always be able
|
|
|
|
/// to create a def-path. Therefore, this returns
|
|
|
|
/// `Option<DefPath>` to cover that possibility. It will always
|
|
|
|
/// return `Some` for local def-ids, however, as well as for
|
|
|
|
/// items. The problems arise with "minor" def-ids like those
|
|
|
|
/// associated with a pattern, `impl Trait`, or other internal
|
|
|
|
/// detail to a fn.
|
|
|
|
///
|
|
|
|
/// Note that if `id` is not local to this crate -- or is
|
|
|
|
/// inlined into this crate -- the result will be a non-local
|
|
|
|
/// `DefPath`.
|
|
|
|
pub fn opt_def_path(self, id: DefId) -> Option<ast_map::DefPath> {
|
2015-09-17 14:29:59 -04:00
|
|
|
if id.is_local() {
|
2016-09-02 18:38:54 -04:00
|
|
|
Some(self.map.def_path(id))
|
2015-09-17 14:29:59 -04:00
|
|
|
} else {
|
2016-03-16 05:50:38 -04:00
|
|
|
self.sess.cstore.relative_def_path(id)
|
2015-09-17 14:29:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn item_name(self, id: DefId) -> ast::Name {
|
2015-09-04 13:52:28 -04:00
|
|
|
if let Some(id) = self.map.as_local_node_id(id) {
|
2016-04-06 13:51:55 +03:00
|
|
|
self.map.name(id)
|
2016-09-17 13:34:55 +03:00
|
|
|
} else if id.index == CRATE_DEF_INDEX {
|
|
|
|
token::intern(&self.sess.cstore.original_crate_name(id.krate))
|
2015-08-12 20:22:25 +02:00
|
|
|
} else {
|
2016-09-17 13:34:55 +03:00
|
|
|
let def_key = self.sess.cstore.def_key(id);
|
|
|
|
// The name of a StructCtor is that of its struct parent.
|
|
|
|
if let ast_map::DefPathData::StructCtor = def_key.disambiguated_data.data {
|
|
|
|
self.item_name(DefId {
|
|
|
|
krate: id.krate,
|
|
|
|
index: def_key.parent.unwrap()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
def_key.disambiguated_data.data.get_opt_name().unwrap_or_else(|| {
|
|
|
|
bug!("item_name: no name for {:?}", self.def_path(id));
|
|
|
|
})
|
|
|
|
}
|
2015-08-12 20:22:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
// If the given item is in an external crate, looks up its type and adds it to
|
|
|
|
// the type cache. Returns the type parameters and type.
|
2016-11-10 16:49:53 +02:00
|
|
|
pub fn item_type(self, did: DefId) -> Ty<'gcx> {
|
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"item_types", did, &self.item_types,
|
|
|
|
|| self.sess.cstore.item_type(self.global_tcx(), did))
|
2016-05-29 19:27:05 +03:00
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Given the did of a trait, returns its canonical trait ref.
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn lookup_trait_def(self, did: DefId) -> &'gcx TraitDef<'gcx> {
|
2015-06-25 23:42:17 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"trait_defs", did, &self.trait_defs,
|
2016-04-29 06:00:23 +03:00
|
|
|
|| self.alloc_trait_def(self.sess.cstore.trait_def(self.global_tcx(), did))
|
2015-06-25 23:42:17 +03:00
|
|
|
)
|
|
|
|
}
|
2013-03-27 06:16:28 -04:00
|
|
|
|
2015-08-07 14:41:33 +03:00
|
|
|
/// Given the did of an ADT, return a master reference to its
|
|
|
|
/// definition. Unless you are planning on fulfilling the ADT's fields,
|
|
|
|
/// use lookup_adt_def instead.
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn lookup_adt_def_master(self, did: DefId) -> AdtDefMaster<'gcx> {
|
2015-07-20 22:13:36 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"adt_defs", did, &self.adt_defs,
|
2016-04-29 06:00:23 +03:00
|
|
|
|| self.sess.cstore.adt_def(self.global_tcx(), did)
|
2015-07-20 22:13:36 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:33 +03:00
|
|
|
/// Given the did of an ADT, return a reference to its definition.
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn lookup_adt_def(self, did: DefId) -> AdtDef<'gcx> {
|
2015-08-07 14:41:33 +03:00
|
|
|
// when reverse-variance goes away, a transmute::<AdtDefMaster,AdtDef>
|
2016-02-09 11:52:39 -05:00
|
|
|
// would be needed here.
|
2015-08-07 14:41:33 +03:00
|
|
|
self.lookup_adt_def_master(did)
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:39:49 +03:00
|
|
|
/// Given the did of an item, returns its generics.
|
2016-11-10 16:49:53 +02:00
|
|
|
pub fn item_generics(self, did: DefId) -> &'gcx Generics<'gcx> {
|
2016-08-08 23:39:49 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"generics", did, &self.generics,
|
2016-09-16 17:25:54 +03:00
|
|
|
|| self.alloc_generics(self.sess.cstore.item_generics(self.global_tcx(), did)))
|
2016-08-08 23:39:49 +03:00
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Given the did of an item, returns its full set of predicates.
|
2016-11-10 16:49:53 +02:00
|
|
|
pub fn item_predicates(self, did: DefId) -> GenericPredicates<'gcx> {
|
2015-06-25 23:42:17 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"predicates", did, &self.predicates,
|
2016-04-29 06:00:23 +03:00
|
|
|
|| self.sess.cstore.item_predicates(self.global_tcx(), did))
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2015-02-11 06:54:05 -05:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Given the did of a trait, returns its superpredicates.
|
2016-11-10 16:49:53 +02:00
|
|
|
pub fn item_super_predicates(self, did: DefId) -> GenericPredicates<'gcx> {
|
2015-06-25 23:42:17 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"super_predicates", did, &self.super_predicates,
|
2016-04-29 06:00:23 +03:00
|
|
|
|| self.sess.cstore.item_super_predicates(self.global_tcx(), did))
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2014-12-07 11:10:48 -05:00
|
|
|
|
2016-10-28 13:55:49 +03:00
|
|
|
/// Given the did of an item, returns its MIR, borrowed immutably.
|
|
|
|
pub fn item_mir(self, did: DefId) -> Ref<'gcx, Mir<'gcx>> {
|
|
|
|
lookup_locally_or_in_crate_store("mir_map", did, &self.mir_map, || {
|
|
|
|
let mir = self.sess.cstore.get_item_mir(self.global_tcx(), did);
|
|
|
|
let mir = self.alloc_mir(mir);
|
|
|
|
|
|
|
|
// Perma-borrow MIR from extern crates to prevent mutation.
|
|
|
|
mem::forget(mir.borrow());
|
|
|
|
|
|
|
|
mir
|
|
|
|
}).borrow()
|
|
|
|
}
|
|
|
|
|
2015-12-31 16:56:40 -05:00
|
|
|
/// If `type_needs_drop` returns true, then `ty` is definitely
|
|
|
|
/// non-copy and *might* have a destructor attached; if it returns
|
|
|
|
/// false, then `ty` definitely has no destructor (i.e. no drop glue).
|
|
|
|
///
|
|
|
|
/// (Note that this implies that if `ty` has a destructor attached,
|
|
|
|
/// then `type_needs_drop` will definitely return `true` for `ty`.)
|
2016-03-25 05:22:52 +02:00
|
|
|
pub fn type_needs_drop_given_env(self,
|
2016-04-29 06:00:23 +03:00
|
|
|
ty: Ty<'gcx>,
|
|
|
|
param_env: &ty::ParameterEnvironment<'gcx>) -> bool {
|
2015-12-31 16:56:40 -05:00
|
|
|
// Issue #22536: We first query type_moves_by_default. It sees a
|
|
|
|
// normalized version of the type, and therefore will definitely
|
|
|
|
// know whether the type implements Copy (and thus needs no
|
|
|
|
// cleanup/drop/zeroing) ...
|
2016-04-29 06:00:23 +03:00
|
|
|
let tcx = self.global_tcx();
|
|
|
|
let implements_copy = !ty.moves_by_default(tcx, param_env, DUMMY_SP);
|
2015-12-31 16:56:40 -05:00
|
|
|
|
|
|
|
if implements_copy { return false; }
|
|
|
|
|
|
|
|
// ... (issue #22536 continued) but as an optimization, still use
|
|
|
|
// prior logic of asking if the `needs_drop` bit is set; we need
|
|
|
|
// not zero non-Copy types if they have no destructor.
|
|
|
|
|
|
|
|
// FIXME(#22815): Note that calling `ty::type_contents` is a
|
|
|
|
// conservative heuristic; it may report that `needs_drop` is set
|
|
|
|
// when actual type does not actually have a destructor associated
|
|
|
|
// with it. But since `ty` absolutely did not have the `Copy`
|
|
|
|
// bound attached (see above), it is sound to treat it as having a
|
|
|
|
// destructor (e.g. zero its memory on move).
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
let contents = ty.type_contents(tcx);
|
2015-12-31 16:56:40 -05:00
|
|
|
debug!("type_needs_drop ty={:?} contents={:?}", ty, contents);
|
2016-04-29 06:00:23 +03:00
|
|
|
contents.needs_drop(tcx)
|
2015-12-31 16:56:40 -05:00
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Get the attributes of a definition.
|
2016-04-29 06:00:23 +03:00
|
|
|
pub fn get_attrs(self, did: DefId) -> Cow<'gcx, [ast::Attribute]> {
|
2015-09-04 13:52:28 -04:00
|
|
|
if let Some(id) = self.map.as_local_node_id(did) {
|
|
|
|
Cow::Borrowed(self.map.attrs(id))
|
2015-06-25 23:42:17 +03:00
|
|
|
} else {
|
2015-11-20 14:51:18 +02:00
|
|
|
Cow::Owned(self.sess.cstore.item_attrs(did))
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2013-04-10 22:47:22 +10:00
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Determine whether an item is annotated with an attribute
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn has_attr(self, did: DefId, attr: &str) -> bool {
|
2015-06-25 23:42:17 +03:00
|
|
|
self.get_attrs(did).iter().any(|item| item.check_name(attr))
|
|
|
|
}
|
2013-05-24 18:08:45 -04:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Determine whether an item is annotated with `#[repr(packed)]`
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn lookup_packed(self, did: DefId) -> bool {
|
2015-06-25 23:42:17 +03:00
|
|
|
self.lookup_repr_hints(did).contains(&attr::ReprPacked)
|
|
|
|
}
|
2013-05-05 13:48:32 -04:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Determine whether an item is annotated with `#[simd]`
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn lookup_simd(self, did: DefId) -> bool {
|
2015-06-25 23:42:17 +03:00
|
|
|
self.has_attr(did, "simd")
|
2015-07-13 11:35:00 -07:00
|
|
|
|| self.lookup_repr_hints(did).contains(&attr::ReprSimd)
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2013-05-02 20:47:11 +09:00
|
|
|
|
2016-08-27 01:13:48 +03:00
|
|
|
pub fn item_variances(self, item_id: DefId) -> Rc<Vec<ty::Variance>> {
|
2015-06-25 23:42:17 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"item_variance_map", item_id, &self.item_variance_map,
|
2015-11-20 14:51:18 +02:00
|
|
|
|| Rc::new(self.sess.cstore.item_variances(item_id)))
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2015-01-24 14:17:24 +01:00
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn trait_has_default_impl(self, trait_def_id: DefId) -> bool {
|
2015-06-25 23:42:17 +03:00
|
|
|
self.populate_implementations_for_trait_if_necessary(trait_def_id);
|
2013-08-23 14:34:00 -07:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
let def = self.lookup_trait_def(trait_def_id);
|
|
|
|
def.flags.get().intersects(TraitFlags::HAS_DEFAULT_IMPL)
|
2015-03-10 23:12:55 -05:00
|
|
|
}
|
2015-04-21 19:00:12 +03:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Records a trait-to-implementation mapping.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn record_trait_has_default_impl(self, trait_def_id: DefId) {
|
2015-06-25 23:42:17 +03:00
|
|
|
let def = self.lookup_trait_def(trait_def_id);
|
|
|
|
def.flags.set(def.flags.get() | TraitFlags::HAS_DEFAULT_IMPL)
|
2015-03-10 23:12:55 -05:00
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Populates the type context with all the inherent implementations for
|
|
|
|
/// the given type if necessary.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn populate_inherent_implementations_for_type_if_necessary(self,
|
2015-08-16 06:32:28 -04:00
|
|
|
type_id: DefId) {
|
2015-08-16 09:06:23 -04:00
|
|
|
if type_id.is_local() {
|
2015-06-25 23:42:17 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-22 16:35:02 -05:00
|
|
|
// The type is not local, hence we are reading this out of
|
|
|
|
// metadata and don't need to track edges.
|
|
|
|
let _ignore = self.dep_graph.in_ignore();
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
if self.populated_external_types.borrow().contains(&type_id) {
|
|
|
|
return
|
|
|
|
}
|
2015-01-01 12:29:48 +13:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
debug!("populate_inherent_implementations_for_type_if_necessary: searching for {:?}",
|
|
|
|
type_id);
|
2013-08-23 14:34:00 -07:00
|
|
|
|
2015-11-20 14:51:18 +02:00
|
|
|
let inherent_impls = self.sess.cstore.inherent_implementations_for_type(type_id);
|
2013-08-23 14:34:00 -07:00
|
|
|
|
2016-08-24 06:49:44 -04:00
|
|
|
self.inherent_impls.borrow_mut().insert(type_id, inherent_impls);
|
2015-06-25 23:42:17 +03:00
|
|
|
self.populated_external_types.borrow_mut().insert(type_id);
|
2013-08-23 14:34:00 -07:00
|
|
|
}
|
2015-03-04 15:19:27 -05:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Populates the type context with all the implementations for the given
|
|
|
|
/// trait if necessary.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn populate_implementations_for_trait_if_necessary(self, trait_id: DefId) {
|
2015-08-16 09:06:23 -04:00
|
|
|
if trait_id.is_local() {
|
2015-06-25 23:42:17 +03:00
|
|
|
return
|
|
|
|
}
|
2013-08-23 14:34:00 -07:00
|
|
|
|
2015-12-22 16:35:02 -05:00
|
|
|
// The type is not local, hence we are reading this out of
|
|
|
|
// metadata and don't need to track edges.
|
|
|
|
let _ignore = self.dep_graph.in_ignore();
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
let def = self.lookup_trait_def(trait_id);
|
|
|
|
if def.flags.get().intersects(TraitFlags::IMPLS_VALID) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-12 07:56:37 +03:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
debug!("populate_implementations_for_trait_if_necessary: searching for {:?}", def);
|
2013-08-23 14:34:00 -07:00
|
|
|
|
2015-11-20 14:51:18 +02:00
|
|
|
if self.sess.cstore.is_defaulted_trait(trait_id) {
|
2015-06-25 23:42:17 +03:00
|
|
|
self.record_trait_has_default_impl(trait_id);
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:49:01 +03:00
|
|
|
for impl_def_id in self.sess.cstore.implementations_of_trait(Some(trait_id)) {
|
2015-06-25 23:42:17 +03:00
|
|
|
let trait_ref = self.impl_trait_ref(impl_def_id).unwrap();
|
2015-12-22 10:20:47 -08:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
// Record the trait->implementation mapping.
|
2016-09-08 19:05:50 +03:00
|
|
|
let parent = self.sess.cstore.impl_parent(impl_def_id).unwrap_or(trait_id);
|
|
|
|
def.record_remote_impl(self, impl_def_id, trait_ref, parent);
|
2015-11-20 14:51:18 +02:00
|
|
|
}
|
2013-08-23 14:34:00 -07:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
def.flags.set(def.flags.get() | TraitFlags::IMPLS_VALID);
|
|
|
|
}
|
2014-02-05 17:31:33 +13:00
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn closure_kind(self, def_id: DefId) -> ty::ClosureKind {
|
2016-05-11 04:14:41 +03:00
|
|
|
// If this is a local def-id, it should be inserted into the
|
|
|
|
// tables by typeck; else, it will be retreived from
|
|
|
|
// the external crate metadata.
|
|
|
|
if let Some(&kind) = self.tables.borrow().closure_kinds.get(&def_id) {
|
|
|
|
return kind;
|
|
|
|
}
|
|
|
|
|
|
|
|
let kind = self.sess.cstore.closure_kind(def_id);
|
|
|
|
self.tables.borrow_mut().closure_kinds.insert(def_id, kind);
|
|
|
|
kind
|
2015-09-17 14:29:59 -04:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn closure_type(self,
|
2015-09-17 14:29:59 -04:00
|
|
|
def_id: DefId,
|
2016-05-11 04:14:41 +03:00
|
|
|
substs: ClosureSubsts<'tcx>)
|
|
|
|
-> ty::ClosureTy<'tcx>
|
2015-09-17 14:29:59 -04:00
|
|
|
{
|
2016-05-11 04:14:41 +03:00
|
|
|
// If this is a local def-id, it should be inserted into the
|
|
|
|
// tables by typeck; else, it will be retreived from
|
|
|
|
// the external crate metadata.
|
|
|
|
if let Some(ty) = self.tables.borrow().closure_tys.get(&def_id) {
|
2016-11-03 22:19:33 +02:00
|
|
|
return ty.subst(self, substs.substs);
|
2016-05-11 04:14:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let ty = self.sess.cstore.closure_ty(self.global_tcx(), def_id);
|
|
|
|
self.tables.borrow_mut().closure_tys.insert(def_id, ty.clone());
|
2016-11-03 22:19:33 +02:00
|
|
|
ty.subst(self, substs.substs)
|
2015-09-17 14:29:59 -04:00
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Given the def_id of an impl, return the def_id of the trait it implements.
|
|
|
|
/// If it implements no trait, return `None`.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn trait_id_of_impl(self, def_id: DefId) -> Option<DefId> {
|
2015-06-25 23:42:17 +03:00
|
|
|
self.impl_trait_ref(def_id).map(|tr| tr.def_id)
|
2014-02-05 17:31:33 +13:00
|
|
|
}
|
2015-06-25 23:42:17 +03:00
|
|
|
|
|
|
|
/// If the given def ID describes a method belonging to an impl, return the
|
|
|
|
/// ID of the impl that the method belongs to. Otherwise, return `None`.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn impl_of_method(self, def_id: DefId) -> Option<DefId> {
|
2015-06-25 23:42:17 +03:00
|
|
|
if def_id.krate != LOCAL_CRATE {
|
2016-11-10 02:06:34 +02:00
|
|
|
return self.sess.cstore.associated_item(self.global_tcx(), def_id)
|
2016-04-29 06:00:23 +03:00
|
|
|
.and_then(|item| {
|
2016-11-10 02:06:34 +02:00
|
|
|
match item.container {
|
2016-02-23 22:04:27 +02:00
|
|
|
TraitContainer(_) => None,
|
|
|
|
ImplContainer(def_id) => Some(def_id),
|
|
|
|
}
|
|
|
|
});
|
2014-02-05 17:31:33 +13:00
|
|
|
}
|
2016-11-10 02:06:34 +02:00
|
|
|
match self.associated_items.borrow().get(&def_id).cloned() {
|
2015-06-25 23:42:17 +03:00
|
|
|
Some(trait_item) => {
|
2016-11-10 02:06:34 +02:00
|
|
|
match trait_item.container {
|
2015-06-25 23:42:17 +03:00
|
|
|
TraitContainer(_) => None,
|
|
|
|
ImplContainer(def_id) => Some(def_id),
|
|
|
|
}
|
2013-08-23 14:34:00 -07:00
|
|
|
}
|
2015-06-25 23:42:17 +03:00
|
|
|
None => None
|
2013-08-23 14:34:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:39:49 +03:00
|
|
|
/// If the given def ID describes an item belonging to a trait,
|
|
|
|
/// return the ID of the trait that the trait item belongs to.
|
|
|
|
/// Otherwise, return `None`.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn trait_of_item(self, def_id: DefId) -> Option<DefId> {
|
2015-06-25 23:42:17 +03:00
|
|
|
if def_id.krate != LOCAL_CRATE {
|
2016-08-08 23:39:49 +03:00
|
|
|
return self.sess.cstore.trait_of_item(def_id);
|
2013-12-15 21:52:48 -05:00
|
|
|
}
|
2016-11-10 02:06:34 +02:00
|
|
|
match self.associated_items.borrow().get(&def_id) {
|
|
|
|
Some(associated_item) => {
|
|
|
|
match associated_item.container {
|
2015-06-25 23:42:17 +03:00
|
|
|
TraitContainer(def_id) => Some(def_id),
|
2016-08-08 23:39:49 +03:00
|
|
|
ImplContainer(_) => None
|
2014-12-13 14:40:38 -08:00
|
|
|
}
|
2013-10-30 16:32:33 -07:00
|
|
|
}
|
2015-06-25 23:42:17 +03:00
|
|
|
None => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct a parameter environment suitable for static contexts or other contexts where there
|
|
|
|
/// are no free type/lifetime parameters in scope.
|
2016-03-25 05:22:52 +02:00
|
|
|
pub fn empty_parameter_environment(self) -> ParameterEnvironment<'tcx> {
|
2015-12-08 23:38:36 +01:00
|
|
|
|
|
|
|
// for an empty parameter environment, there ARE no free
|
|
|
|
// regions, so it shouldn't matter what we use for the free id
|
|
|
|
let free_id_outlive = self.region_maps.node_extent(ast::DUMMY_NODE_ID);
|
2016-04-29 06:00:23 +03:00
|
|
|
ty::ParameterEnvironment {
|
2016-10-24 18:23:29 -06:00
|
|
|
free_substs: self.intern_substs(&[]),
|
2016-04-29 06:00:23 +03:00
|
|
|
caller_bounds: Vec::new(),
|
2016-08-25 23:58:52 +03:00
|
|
|
implicit_region_bound: self.mk_region(ty::ReEmpty),
|
2016-10-13 23:39:50 +03:00
|
|
|
free_id_outlive: free_id_outlive,
|
2016-11-08 14:02:55 +11:00
|
|
|
is_copy_cache: RefCell::new(FxHashMap()),
|
|
|
|
is_sized_cache: RefCell::new(FxHashMap()),
|
2016-04-29 06:00:23 +03:00
|
|
|
}
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs and returns a substitution that can be applied to move from
|
|
|
|
/// the "outer" view of a type or method to the "inner" view.
|
|
|
|
/// In general, this means converting from bound parameters to
|
|
|
|
/// free parameters. Since we currently represent bound/free type
|
|
|
|
/// parameters in the same way, this only has an effect on regions.
|
2016-08-08 23:39:49 +03:00
|
|
|
pub fn construct_free_substs(self, def_id: DefId,
|
|
|
|
free_id_outlive: CodeExtent)
|
|
|
|
-> &'gcx Substs<'gcx> {
|
|
|
|
|
|
|
|
let substs = Substs::for_item(self.global_tcx(), def_id, |def, _| {
|
|
|
|
// map bound 'a => free 'a
|
2016-08-25 23:58:52 +03:00
|
|
|
self.global_tcx().mk_region(ReFree(FreeRegion {
|
|
|
|
scope: free_id_outlive,
|
|
|
|
bound_region: def.to_bound_region()
|
|
|
|
}))
|
2016-08-08 23:39:49 +03:00
|
|
|
}, |def, _| {
|
|
|
|
// map T => T
|
2016-06-15 01:40:09 +03:00
|
|
|
self.global_tcx().mk_param_from_def(def)
|
|
|
|
});
|
2013-10-29 06:03:32 -04:00
|
|
|
|
2016-08-08 23:39:49 +03:00
|
|
|
debug!("construct_parameter_environment: {:?}", substs);
|
|
|
|
substs
|
2015-01-26 14:20:38 -05:00
|
|
|
}
|
2013-10-29 06:03:32 -04:00
|
|
|
|
2015-12-08 23:38:36 +01:00
|
|
|
/// See `ParameterEnvironment` struct def'n for details.
|
|
|
|
/// If you were using `free_id: NodeId`, you might try `self.region_maps.item_extent(free_id)`
|
2016-09-18 07:35:58 -07:00
|
|
|
/// for the `free_id_outlive` parameter. (But note that this is not always quite right.)
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn construct_parameter_environment(self,
|
|
|
|
span: Span,
|
2016-08-08 23:39:49 +03:00
|
|
|
def_id: DefId,
|
2016-05-03 04:56:42 +03:00
|
|
|
free_id_outlive: CodeExtent)
|
2016-04-29 06:00:23 +03:00
|
|
|
-> ParameterEnvironment<'gcx>
|
2015-06-25 23:42:17 +03:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// Construct the free substs.
|
|
|
|
//
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2016-08-08 23:39:49 +03:00
|
|
|
let free_substs = self.construct_free_substs(def_id, free_id_outlive);
|
2015-01-26 14:20:38 -05:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
//
|
|
|
|
// Compute the bounds on Self and the type parameters.
|
|
|
|
//
|
2014-05-31 18:53:13 -04:00
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
let tcx = self.global_tcx();
|
2016-11-10 16:49:53 +02:00
|
|
|
let generic_predicates = tcx.item_predicates(def_id);
|
2016-08-08 23:39:49 +03:00
|
|
|
let bounds = generic_predicates.instantiate(tcx, free_substs);
|
2016-04-29 06:00:23 +03:00
|
|
|
let bounds = tcx.liberate_late_bound_regions(free_id_outlive, &ty::Binder(bounds));
|
2016-06-13 20:10:32 +03:00
|
|
|
let predicates = bounds.predicates;
|
2013-10-29 06:03:32 -04:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
// Finally, we have to normalize the bounds in the environment, in
|
|
|
|
// case they contain any associated type projections. This process
|
|
|
|
// can yield errors if the put in illegal associated types, like
|
|
|
|
// `<i32 as Foo>::Bar` where `i32` does not implement `Foo`. We
|
|
|
|
// report these errors right here; this doesn't actually feel
|
|
|
|
// right to me, because constructing the environment feels like a
|
|
|
|
// kind of a "idempotent" action, but I'm not sure where would be
|
|
|
|
// a better place. In practice, we construct environments for
|
|
|
|
// every fn once during type checking, and we'll abort if there
|
|
|
|
// are any errors at that point, so after type checking you can be
|
|
|
|
// sure that this will succeed without errors anyway.
|
|
|
|
//
|
2014-09-12 11:42:58 -04:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
let unnormalized_env = ty::ParameterEnvironment {
|
2016-08-08 23:39:49 +03:00
|
|
|
free_substs: free_substs,
|
2016-08-25 23:58:52 +03:00
|
|
|
implicit_region_bound: tcx.mk_region(ty::ReScope(free_id_outlive)),
|
2015-06-25 23:42:17 +03:00
|
|
|
caller_bounds: predicates,
|
2015-12-08 23:38:36 +01:00
|
|
|
free_id_outlive: free_id_outlive,
|
2016-11-08 14:02:55 +11:00
|
|
|
is_copy_cache: RefCell::new(FxHashMap()),
|
|
|
|
is_sized_cache: RefCell::new(FxHashMap()),
|
2015-06-25 23:42:17 +03:00
|
|
|
};
|
2014-09-12 11:42:58 -04:00
|
|
|
|
2015-12-08 23:38:36 +01:00
|
|
|
let cause = traits::ObligationCause::misc(span, free_id_outlive.node_id(&self.region_maps));
|
2016-04-29 06:00:23 +03:00
|
|
|
traits::normalize_param_env_or_error(tcx, unnormalized_env, cause)
|
2014-02-07 14:37:19 -05:00
|
|
|
}
|
2014-04-21 19:21:53 -04:00
|
|
|
|
2016-08-25 23:58:52 +03:00
|
|
|
pub fn node_scope_region(self, id: NodeId) -> &'tcx Region {
|
|
|
|
self.mk_region(ty::ReScope(self.region_maps.node_extent(id)))
|
|
|
|
}
|
|
|
|
|
2016-11-02 18:22:59 -04:00
|
|
|
pub fn visit_all_item_likes_in_krate<V,F>(self,
|
|
|
|
dep_node_fn: F,
|
|
|
|
visitor: &mut V)
|
|
|
|
where F: FnMut(DefId) -> DepNode<DefId>, V: ItemLikeVisitor<'gcx>
|
2015-12-22 16:35:02 -05:00
|
|
|
{
|
2016-11-02 18:22:59 -04:00
|
|
|
dep_graph::visit_all_item_likes_in_krate(self.global_tcx(), dep_node_fn, visitor);
|
2015-12-22 16:35:02 -05:00
|
|
|
}
|
2016-02-12 12:43:13 -05:00
|
|
|
|
2015-12-29 10:55:34 -08:00
|
|
|
/// Looks up the span of `impl_did` if the impl is local; otherwise returns `Err`
|
|
|
|
/// with the name of the crate containing the impl.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn span_of_impl(self, impl_did: DefId) -> Result<Span, InternedString> {
|
2015-12-29 10:55:34 -08:00
|
|
|
if impl_did.is_local() {
|
|
|
|
let node_id = self.map.as_local_node_id(impl_did).unwrap();
|
|
|
|
Ok(self.map.span(node_id))
|
|
|
|
} else {
|
2016-02-14 13:01:44 -05:00
|
|
|
Err(self.sess.cstore.crate_name(impl_did.krate))
|
2015-12-29 10:55:34 -08:00
|
|
|
}
|
|
|
|
}
|
2015-01-02 04:09:35 -05:00
|
|
|
}
|
2014-04-21 19:21:53 -04:00
|
|
|
|
2016-05-02 18:07:47 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn with_freevars<T, F>(self, fid: NodeId, f: F) -> T where
|
2016-03-29 13:14:01 +03:00
|
|
|
F: FnOnce(&[hir::Freevar]) -> T,
|
2015-06-25 23:42:17 +03:00
|
|
|
{
|
|
|
|
match self.freevars.borrow().get(&fid) {
|
|
|
|
None => f(&[]),
|
|
|
|
Some(d) => f(&d[..])
|
|
|
|
}
|
|
|
|
}
|
2014-12-13 14:40:38 -08:00
|
|
|
}
|