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::ImplOrTraitItemId::*;
|
|
|
|
pub use self::Variance::*;
|
|
|
|
pub use self::DtorKind::*;
|
|
|
|
pub use self::ImplOrTraitItemContainer::*;
|
|
|
|
pub use self::BorrowKind::*;
|
|
|
|
pub use self::ImplOrTraitItem::*;
|
|
|
|
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-04-12 22:58:55 +09:00
|
|
|
use middle::cstore::{self, LOCAL_CRATE};
|
2016-03-29 12:54:26 +03:00
|
|
|
use hir::def::{self, Def, ExportMap};
|
|
|
|
use hir::def_id::DefId;
|
2015-03-15 04:01:57 +02:00
|
|
|
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
|
2015-12-08 23:38:36 +01:00
|
|
|
use middle::region::{CodeExtent};
|
2016-03-22 17:30:57 +02:00
|
|
|
use traits;
|
|
|
|
use ty;
|
|
|
|
use ty::subst::{Subst, Substs, VecPerParamSpace};
|
|
|
|
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;
|
2015-03-28 02:23:20 -07:00
|
|
|
use util::nodemap::FnvHashMap;
|
2014-12-07 11:10:48 -05:00
|
|
|
|
2015-12-01 16:07:15 +01:00
|
|
|
use serialize::{Encodable, Encoder, Decodable, Decoder};
|
2016-04-12 22:58:55 +09:00
|
|
|
use std::borrow::Cow;
|
2015-12-22 16:39:33 -05:00
|
|
|
use std::cell::Cell;
|
2015-09-14 14:55:56 +03:00
|
|
|
use std::hash::{Hash, Hasher};
|
2015-07-22 21:53:52 +03:00
|
|
|
use std::iter;
|
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;
|
2015-07-31 00:04:06 -07:00
|
|
|
use syntax::ast::{self, CrateNum, Name, NodeId};
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::attr::{self, AttrMetaMethods};
|
2015-12-31 16:56:40 -05:00
|
|
|
use syntax::codemap::{DUMMY_SP, Span};
|
2016-01-14 14:26:50 +03:00
|
|
|
use syntax::parse::token::InternedString;
|
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;
|
|
|
|
use hir::{ItemImpl, ItemTrait, PatKind};
|
|
|
|
use hir::intravisit::Visitor;
|
2012-06-06 14:19:52 -07:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
pub use self::sty::{Binder, DebruijnIndex};
|
|
|
|
pub use self::sty::{BuiltinBound, BuiltinBounds, ExistentialBounds};
|
|
|
|
pub use self::sty::{BareFnTy, FnSig, PolyFnSig, FnOutput, PolyFnOutput};
|
|
|
|
pub use self::sty::{ClosureTy, InferTy, ParamTy, ProjectionTy, TraitTy};
|
|
|
|
pub use self::sty::{ClosureSubsts, TypeAndMut};
|
|
|
|
pub use self::sty::{TraitRef, TypeVariants, PolyTraitRef};
|
|
|
|
pub use self::sty::{BoundRegion, EarlyBoundRegion, FreeRegion, Region};
|
|
|
|
pub use self::sty::{TyVid, IntVid, FloatVid, RegionVid, SkolemizedRegionVid};
|
|
|
|
pub use self::sty::BoundRegion::*;
|
|
|
|
pub use self::sty::FnOutput::*;
|
|
|
|
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.
|
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,
|
2015-08-25 21:52:15 +03:00
|
|
|
TraitDtor(bool)
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DtorKind {
|
|
|
|
pub fn is_present(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
TraitDtor(..) => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_drop_flag(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
&NoDtor => false,
|
2015-08-25 21:52:15 +03:00
|
|
|
&TraitDtor(flag) => flag
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 08:52:36 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
2014-08-04 13:56:56 -07:00
|
|
|
pub enum ImplOrTraitItemContainer {
|
2015-08-16 06:32:28 -04:00
|
|
|
TraitContainer(DefId),
|
|
|
|
ImplContainer(DefId),
|
2013-08-23 14:34:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-04 13:56:56 -07:00
|
|
|
impl ImplOrTraitItemContainer {
|
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>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ImplHeader<'tcx> {
|
2016-02-26 13:21:44 -08:00
|
|
|
pub fn with_fresh_ty_vars<'a>(selcx: &mut traits::SelectionContext<'a, 'tcx>,
|
|
|
|
impl_def_id: DefId)
|
|
|
|
-> ImplHeader<'tcx>
|
2016-02-26 10:51:10 -08:00
|
|
|
{
|
|
|
|
let tcx = selcx.tcx();
|
|
|
|
let impl_generics = tcx.lookup_item_type(impl_def_id).generics;
|
|
|
|
let impl_substs = selcx.infcx().fresh_substs_for_generics(DUMMY_SP, &impl_generics);
|
|
|
|
|
|
|
|
let header = ImplHeader {
|
|
|
|
impl_def_id: impl_def_id,
|
2016-02-26 13:21:44 -08:00
|
|
|
self_ty: tcx.lookup_item_type(impl_def_id).ty,
|
2016-02-26 10:51:10 -08:00
|
|
|
trait_ref: tcx.impl_trait_ref(impl_def_id),
|
2016-02-26 13:21:44 -08:00
|
|
|
predicates: tcx.lookup_predicates(impl_def_id).predicates.into_vec(),
|
|
|
|
}.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 08:51:23 +03:00
|
|
|
#[derive(Clone)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub enum ImplOrTraitItem<'tcx> {
|
2015-03-14 12:05:00 -06:00
|
|
|
ConstTraitItem(Rc<AssociatedConst<'tcx>>),
|
2014-09-29 22:11:30 +03:00
|
|
|
MethodTraitItem(Rc<Method<'tcx>>),
|
2015-05-26 17:12:39 +03:00
|
|
|
TypeTraitItem(Rc<AssociatedType<'tcx>>),
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
impl<'tcx> ImplOrTraitItem<'tcx> {
|
2014-08-04 13:56:56 -07:00
|
|
|
fn id(&self) -> ImplOrTraitItemId {
|
|
|
|
match *self {
|
2015-03-14 12:05:00 -06:00
|
|
|
ConstTraitItem(ref associated_const) => {
|
|
|
|
ConstTraitItemId(associated_const.def_id)
|
|
|
|
}
|
2014-08-04 13:56:56 -07:00
|
|
|
MethodTraitItem(ref method) => MethodTraitItemId(method.def_id),
|
2014-08-05 19:44:21 -07:00
|
|
|
TypeTraitItem(ref associated_type) => {
|
|
|
|
TypeTraitItemId(associated_type.def_id)
|
|
|
|
}
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 01:55:54 +00:00
|
|
|
pub fn def(&self) -> Def {
|
|
|
|
match *self {
|
|
|
|
ConstTraitItem(ref associated_const) => Def::AssociatedConst(associated_const.def_id),
|
|
|
|
MethodTraitItem(ref method) => Def::Method(method.def_id),
|
|
|
|
TypeTraitItem(ref ty) => Def::AssociatedTy(ty.container.id(), ty.def_id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn def_id(&self) -> DefId {
|
2014-08-04 13:56:56 -07:00
|
|
|
match *self {
|
2015-03-14 12:05:00 -06:00
|
|
|
ConstTraitItem(ref associated_const) => associated_const.def_id,
|
2014-08-04 13:56:56 -07:00
|
|
|
MethodTraitItem(ref method) => method.def_id,
|
2014-08-05 19:44:21 -07:00
|
|
|
TypeTraitItem(ref associated_type) => associated_type.def_id,
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn name(&self) -> Name {
|
2014-08-04 13:56:56 -07:00
|
|
|
match *self {
|
2015-03-14 12:05:00 -06:00
|
|
|
ConstTraitItem(ref associated_const) => associated_const.name,
|
2014-09-30 19:11:34 -05:00
|
|
|
MethodTraitItem(ref method) => method.name,
|
|
|
|
TypeTraitItem(ref associated_type) => associated_type.name,
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 06:08:11 +00:00
|
|
|
pub fn vis(&self) -> Visibility {
|
2015-03-14 12:05:00 -06:00
|
|
|
match *self {
|
|
|
|
ConstTraitItem(ref associated_const) => associated_const.vis,
|
|
|
|
MethodTraitItem(ref method) => method.vis,
|
|
|
|
TypeTraitItem(ref associated_type) => associated_type.vis,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 13:56:56 -07:00
|
|
|
pub fn container(&self) -> ImplOrTraitItemContainer {
|
|
|
|
match *self {
|
2015-03-14 12:05:00 -06:00
|
|
|
ConstTraitItem(ref associated_const) => associated_const.container,
|
2014-08-04 13:56:56 -07:00
|
|
|
MethodTraitItem(ref method) => method.container,
|
2014-08-05 19:44:21 -07:00
|
|
|
TypeTraitItem(ref associated_type) => associated_type.container,
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
|
|
|
}
|
2014-10-17 09:07:45 -04:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn as_opt_method(&self) -> Option<Rc<Method<'tcx>>> {
|
2014-10-17 09:07:45 -04:00
|
|
|
match *self {
|
|
|
|
MethodTraitItem(ref m) => Some((*m).clone()),
|
2015-03-14 12:05:00 -06:00
|
|
|
_ => None,
|
2014-10-17 09:07:45 -04:00
|
|
|
}
|
|
|
|
}
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2014-08-04 13:56:56 -07:00
|
|
|
pub enum ImplOrTraitItemId {
|
2015-08-16 06:32:28 -04:00
|
|
|
ConstTraitItemId(DefId),
|
|
|
|
MethodTraitItemId(DefId),
|
|
|
|
TypeTraitItemId(DefId),
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ImplOrTraitItemId {
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn def_id(&self) -> DefId {
|
2014-08-04 13:56:56 -07:00
|
|
|
match *self {
|
2015-03-14 12:05:00 -06:00
|
|
|
ConstTraitItemId(def_id) => def_id,
|
2014-08-04 13:56:56 -07:00
|
|
|
MethodTraitItemId(def_id) => def_id,
|
2014-08-05 19:44:21 -07:00
|
|
|
TypeTraitItemId(def_id) => def_id,
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 06:08:11 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Copy)]
|
|
|
|
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 {
|
|
|
|
pub fn from_hir(visibility: &hir::Visibility, id: NodeId, tcx: &TyCtxt) -> Self {
|
|
|
|
match *visibility {
|
|
|
|
hir::Public => Visibility::Public,
|
2016-04-02 20:24:02 +00:00
|
|
|
hir::Visibility::Crate => Visibility::Restricted(ast::CRATE_NODE_ID),
|
|
|
|
hir::Visibility::Restricted { id, .. } => match tcx.def_map.borrow().get(&id) {
|
|
|
|
Some(resolution) => Visibility::Restricted({
|
|
|
|
tcx.map.as_local_node_id(resolution.base_def.def_id()).unwrap()
|
|
|
|
}),
|
|
|
|
// 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.
|
|
|
|
None => Visibility::Public,
|
|
|
|
},
|
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-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub struct Method<'tcx> {
|
2015-07-31 00:04:06 -07:00
|
|
|
pub name: Name,
|
2015-02-11 06:54:05 -05:00
|
|
|
pub generics: Generics<'tcx>,
|
|
|
|
pub predicates: GenericPredicates<'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fty: BareFnTy<'tcx>,
|
2014-05-06 16:37:32 -07:00
|
|
|
pub explicit_self: ExplicitSelfCategory,
|
2016-03-25 06:08:11 +00:00
|
|
|
pub vis: Visibility,
|
2015-12-28 16:14:28 -08:00
|
|
|
pub defaultness: hir::Defaultness,
|
2015-08-16 06:32:28 -04:00
|
|
|
pub def_id: DefId,
|
2014-08-04 13:56:56 -07:00
|
|
|
pub container: ImplOrTraitItemContainer,
|
2013-02-19 02:40:42 -05:00
|
|
|
}
|
2013-01-30 12:17:17 -08:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
impl<'tcx> Method<'tcx> {
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn new(name: Name,
|
2014-09-29 22:11:30 +03:00
|
|
|
generics: ty::Generics<'tcx>,
|
2015-02-11 06:54:05 -05:00
|
|
|
predicates: GenericPredicates<'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
fty: BareFnTy<'tcx>,
|
2014-05-06 16:37:32 -07:00
|
|
|
explicit_self: ExplicitSelfCategory,
|
2016-03-25 06:08:11 +00:00
|
|
|
vis: Visibility,
|
2015-12-28 16:14:28 -08:00
|
|
|
defaultness: hir::Defaultness,
|
2015-08-16 06:32:28 -04:00
|
|
|
def_id: DefId,
|
2015-09-30 22:28:27 +03:00
|
|
|
container: ImplOrTraitItemContainer)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> Method<'tcx> {
|
2015-12-28 16:14:28 -08:00
|
|
|
Method {
|
2014-09-30 19:11:34 -05:00
|
|
|
name: name,
|
2013-05-02 08:11:15 -07:00
|
|
|
generics: generics,
|
2015-02-11 06:54:05 -05:00
|
|
|
predicates: predicates,
|
2013-05-02 08:11:15 -07:00
|
|
|
fty: fty,
|
|
|
|
explicit_self: explicit_self,
|
|
|
|
vis: vis,
|
2015-12-28 16:14:28 -08:00
|
|
|
defaultness: defaultness,
|
2013-07-15 17:26:56 -07:00
|
|
|
def_id: def_id,
|
2013-08-23 14:34:00 -07:00
|
|
|
container: container,
|
2013-05-02 08:11:15 -07:00
|
|
|
}
|
|
|
|
}
|
2013-08-23 14:34:00 -07:00
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn container_id(&self) -> DefId {
|
2013-08-23 14:34:00 -07:00
|
|
|
match self.container {
|
|
|
|
TraitContainer(id) => id,
|
|
|
|
ImplContainer(id) => id,
|
|
|
|
}
|
|
|
|
}
|
2013-05-02 08:11:15 -07:00
|
|
|
}
|
|
|
|
|
2015-09-25 02:40:57 +03:00
|
|
|
impl<'tcx> PartialEq for Method<'tcx> {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &Self) -> bool { self.def_id == other.def_id }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Eq for Method<'tcx> {}
|
|
|
|
|
|
|
|
impl<'tcx> Hash for Method<'tcx> {
|
|
|
|
#[inline]
|
|
|
|
fn hash<H: Hasher>(&self, s: &mut H) {
|
|
|
|
self.def_id.hash(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 12:05:00 -06:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct AssociatedConst<'tcx> {
|
2015-07-31 00:04:06 -07:00
|
|
|
pub name: Name,
|
2015-03-14 12:05:00 -06:00
|
|
|
pub ty: Ty<'tcx>,
|
2016-03-25 06:08:11 +00:00
|
|
|
pub vis: Visibility,
|
2015-12-28 16:14:28 -08:00
|
|
|
pub defaultness: hir::Defaultness,
|
2015-08-16 06:32:28 -04:00
|
|
|
pub def_id: DefId,
|
2015-03-14 12:05:00 -06:00
|
|
|
pub container: ImplOrTraitItemContainer,
|
2015-09-30 22:28:27 +03:00
|
|
|
pub has_value: bool
|
2015-03-14 12:05:00 -06:00
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2015-05-26 17:12:39 +03:00
|
|
|
pub struct AssociatedType<'tcx> {
|
2015-07-31 00:04:06 -07:00
|
|
|
pub name: Name,
|
2015-05-26 17:12:39 +03:00
|
|
|
pub ty: Option<Ty<'tcx>>,
|
2016-03-25 06:08:11 +00:00
|
|
|
pub vis: Visibility,
|
2015-12-28 16:14:28 -08:00
|
|
|
pub defaultness: hir::Defaultness,
|
2015-08-16 06:32:28 -04:00
|
|
|
pub def_id: DefId,
|
2014-08-05 19:44:21 -07:00
|
|
|
pub container: ImplOrTraitItemContainer,
|
|
|
|
}
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, PartialEq, RustcDecodable, RustcEncodable)]
|
2013-10-29 06:08:34 -04:00
|
|
|
pub struct ItemVariances {
|
2014-05-31 18:53:13 -04:00
|
|
|
pub types: VecPerParamSpace<Variance>,
|
|
|
|
pub regions: VecPerParamSpace<Variance>,
|
2013-10-29 06:08:34 -04:00
|
|
|
}
|
2012-08-09 09:59:50 -07: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
|
|
|
|
2015-07-04 07:07:10 +03:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
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>,
|
2015-07-02 08:52:36 +03:00
|
|
|
pub substs: &'tcx subst::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
|
2015-06-24 13:40:54 -07:00
|
|
|
pub type MethodMap<'tcx> = FnvHashMap<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,
|
|
|
|
const HAS_RE_EARLY_BOUND = 1 << 4,
|
|
|
|
const HAS_FREE_REGIONS = 1 << 5,
|
|
|
|
const HAS_TY_ERR = 1 << 6,
|
|
|
|
const HAS_PROJECTION = 1 << 7,
|
|
|
|
const HAS_TY_CLOSURE = 1 << 8,
|
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
|
2015-06-16 12:08:37 -04:00
|
|
|
const HAS_LOCAL_NAMES = 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
|
|
|
|
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 |
|
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 |
|
|
|
|
TypeFlags::HAS_LOCAL_NAMES.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
|
|
|
|
2015-12-01 16:07:15 +01:00
|
|
|
impl<'tcx> Encodable for Ty<'tcx> {
|
|
|
|
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
|
|
|
cstore::tls::with_encoding_context(s, |ecx, rbml_w| {
|
|
|
|
ecx.encode_ty(rbml_w, *self);
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Decodable for Ty<'tcx> {
|
|
|
|
fn decode<D: Decoder>(d: &mut D) -> Result<Ty<'tcx>, D::Error> {
|
|
|
|
cstore::tls::with_decoding_context(d, |dcx, rbml_r| {
|
|
|
|
Ok(dcx.decode_ty(rbml_r))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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`.
|
2015-08-20 01:46:28 +03:00
|
|
|
#[derive(PartialEq, Clone, Debug, Copy)]
|
2015-01-24 15:54:52 -05:00
|
|
|
pub enum UpvarCapture {
|
|
|
|
/// 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.
|
|
|
|
ByRef(UpvarBorrow),
|
|
|
|
}
|
|
|
|
|
2015-08-20 01:46:28 +03:00
|
|
|
#[derive(PartialEq, Clone, Copy)]
|
2014-02-07 14:37:19 -05:00
|
|
|
pub struct UpvarBorrow {
|
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.
|
2014-03-28 10:05:27 -07:00
|
|
|
pub region: ty::Region,
|
2014-02-07 14:37:19 -05:00
|
|
|
}
|
|
|
|
|
2015-01-24 15:54:52 -05:00
|
|
|
pub type UpvarCaptureMap = FnvHashMap<UpvarId, UpvarCapture>;
|
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.
|
2015-06-18 08:51:23 +03:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-11 16:37:36 -05:00
|
|
|
pub enum ObjectLifetimeDefault {
|
|
|
|
/// 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.
|
|
|
|
Specific(Region),
|
|
|
|
}
|
|
|
|
|
2015-06-18 08:51:23 +03:00
|
|
|
#[derive(Clone)]
|
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-05-31 18:53:13 -04:00
|
|
|
pub space: subst::ParamSpace,
|
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>>,
|
2015-06-17 10:02:32 -04:00
|
|
|
pub object_lifetime_default: ObjectLifetimeDefault,
|
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
|
|
|
}
|
|
|
|
|
2015-08-30 19:07:44 +03:00
|
|
|
#[derive(Clone)]
|
2013-10-29 06:03:32 -04:00
|
|
|
pub struct RegionParameterDef {
|
2015-07-31 00:04:06 -07:00
|
|
|
pub name: Name,
|
2015-08-16 06:32:28 -04:00
|
|
|
pub def_id: DefId,
|
2014-05-31 18:53:13 -04:00
|
|
|
pub space: subst::ParamSpace,
|
2014-12-04 12:06:42 -08:00
|
|
|
pub index: u32,
|
2014-08-27 21:46:52 -04:00
|
|
|
pub bounds: Vec<ty::Region>,
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
|
|
|
|
2014-12-07 11:10:48 -05:00
|
|
|
impl RegionParameterDef {
|
|
|
|
pub fn to_early_bound_region(&self) -> ty::Region {
|
2015-04-15 09:13:20 -04:00
|
|
|
ty::ReEarlyBound(ty::EarlyBoundRegion {
|
|
|
|
space: self.space,
|
|
|
|
index: self.index,
|
|
|
|
name: self.name,
|
|
|
|
})
|
2014-12-07 11:10:48 -05: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 {
|
|
|
|
ty::BoundRegion::BrNamed(self.def_id, self.name)
|
|
|
|
}
|
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.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub struct Generics<'tcx> {
|
|
|
|
pub types: VecPerParamSpace<TypeParameterDef<'tcx>>,
|
2014-05-31 18:53:13 -04:00
|
|
|
pub regions: VecPerParamSpace<RegionParameterDef>,
|
2014-12-05 11:04:49 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
impl<'tcx> Generics<'tcx> {
|
|
|
|
pub fn empty() -> Generics<'tcx> {
|
2014-12-05 00:03:03 -05:00
|
|
|
Generics {
|
|
|
|
types: VecPerParamSpace::empty(),
|
|
|
|
regions: VecPerParamSpace::empty(),
|
|
|
|
}
|
2014-02-01 15:57:59 +11:00
|
|
|
}
|
2014-05-31 18:53:13 -04:00
|
|
|
|
2015-02-11 06:54:05 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.types.is_empty() && self.regions.is_empty()
|
|
|
|
}
|
|
|
|
|
2014-05-31 18:53:13 -04:00
|
|
|
pub fn has_type_params(&self, space: subst::ParamSpace) -> bool {
|
2014-07-04 16:39:28 +02:00
|
|
|
!self.types.is_empty_in(space)
|
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-07-10 14:05:28 -04:00
|
|
|
|
|
|
|
pub fn has_region_params(&self, space: subst::ParamSpace) -> bool {
|
|
|
|
!self.regions.is_empty_in(space)
|
|
|
|
}
|
2015-02-11 06:54:05 -05:00
|
|
|
}
|
2014-11-15 17:25:05 -05: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> {
|
|
|
|
pub predicates: VecPerParamSpace<Predicate<'tcx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> GenericPredicates<'tcx> {
|
|
|
|
pub fn empty() -> GenericPredicates<'tcx> {
|
|
|
|
GenericPredicates {
|
|
|
|
predicates: VecPerParamSpace::empty(),
|
|
|
|
}
|
2015-01-04 07:23:21 -05:00
|
|
|
}
|
|
|
|
|
2016-02-29 23:36:51 +00:00
|
|
|
pub fn instantiate(&self, tcx: &TyCtxt<'tcx>, substs: &Substs<'tcx>)
|
2015-02-11 06:54:05 -05:00
|
|
|
-> InstantiatedPredicates<'tcx> {
|
|
|
|
InstantiatedPredicates {
|
2014-12-07 11:10:48 -05:00
|
|
|
predicates: self.predicates.subst(tcx, substs),
|
|
|
|
}
|
|
|
|
}
|
2015-02-24 09:24:42 -05:00
|
|
|
|
|
|
|
pub fn instantiate_supertrait(&self,
|
2016-02-29 23:36:51 +00:00
|
|
|
tcx: &TyCtxt<'tcx>,
|
2015-02-24 09:24:42 -05:00
|
|
|
poly_trait_ref: &ty::PolyTraitRef<'tcx>)
|
|
|
|
-> InstantiatedPredicates<'tcx>
|
|
|
|
{
|
|
|
|
InstantiatedPredicates {
|
|
|
|
predicates: self.predicates.map(|pred| pred.subst_supertrait(tcx, poly_trait_ref))
|
|
|
|
}
|
|
|
|
}
|
2014-12-07 11:10:48 -05:00
|
|
|
}
|
|
|
|
|
2015-06-18 08:51:23 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
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`
|
|
|
|
/// would be the parameters in the `TypeSpace`.
|
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
|
2014-12-13 05:34:34 -05:00
|
|
|
RegionOutlives(PolyRegionOutlivesPredicate),
|
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
|
|
|
|
|
|
|
/// No direct syntax. May be thought of as `where T : FnFoo<...>` for some 'TypeSpace'
|
|
|
|
/// substitutions `...` and T being a closure type. Satisfied (or refuted) once we know the
|
|
|
|
/// closure's kind.
|
|
|
|
ClosureKind(DefId, ClosureKind),
|
2014-12-17 14:16:28 -05:00
|
|
|
}
|
|
|
|
|
2015-02-24 09:24:42 -05:00
|
|
|
impl<'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.
|
|
|
|
pub fn subst_supertrait(&self,
|
2016-02-29 23:36:51 +00:00
|
|
|
tcx: &TyCtxt<'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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 08:51:23 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
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-01-20 16:56:29 -05:00
|
|
|
DepNode::TraitSelect(self.def_id())
|
|
|
|
}
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
pub fn input_types(&self) -> &[Ty<'tcx>] {
|
|
|
|
self.trait_ref.substs.types.as_slice()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
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>>;
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
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>>;
|
|
|
|
pub type PolyRegionOutlivesPredicate = PolyOutlivesPredicate<ty::Region, ty::Region>;
|
|
|
|
pub type PolyTypeOutlivesPredicate<'tcx> = PolyOutlivesPredicate<Ty<'tcx>, ty::Region>;
|
|
|
|
|
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.
|
2015-06-18 08:51:23 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
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
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn sort_key(&self) -> (DefId, Name) {
|
2014-12-26 04:36:04 -05:00
|
|
|
self.0.projection_ty.sort_key()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:50:50 -07:00
|
|
|
impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate {
|
|
|
|
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) => {
|
|
|
|
data.0.trait_ref.substs.types.as_slice().to_vec()
|
|
|
|
}
|
|
|
|
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) => {
|
|
|
|
let trait_inputs = data.0.projection_ty.trait_ref.substs.types.as_slice();
|
|
|
|
trait_inputs.iter()
|
|
|
|
.cloned()
|
2015-06-10 17:22:20 +01:00
|
|
|
.chain(Some(data.0.ty))
|
2015-02-17 10:57:15 -05:00
|
|
|
.collect()
|
|
|
|
}
|
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> {
|
2014-12-07 11:10:48 -05:00
|
|
|
pub predicates: VecPerParamSpace<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> {
|
|
|
|
InstantiatedPredicates { predicates: VecPerParamSpace::empty() }
|
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> {
|
2014-09-12 10:53:35 -04:00
|
|
|
self.substs.self_ty().unwrap()
|
|
|
|
}
|
2014-10-21 11:49:06 -04:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn input_types(&self) -> &[Ty<'tcx>] {
|
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.
|
|
|
|
self.substs.types.as_slice()
|
|
|
|
}
|
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)]
|
2015-01-02 04:09:35 -05:00
|
|
|
pub struct ParameterEnvironment<'a, 'tcx:'a> {
|
2016-02-29 23:36:51 +00:00
|
|
|
pub tcx: &'a TyCtxt<'tcx>,
|
2015-01-02 04:09:35 -05:00
|
|
|
|
2015-01-26 14:20:38 -05:00
|
|
|
/// See `construct_free_substs` for details.
|
2014-09-29 22:11:30 +03:00
|
|
|
pub free_substs: 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.
|
|
|
|
pub implicit_region_bound: 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
|
|
|
|
|
|
|
/// Caches the results of trait selection. This cache is used
|
|
|
|
/// for things that have to do with the parameters in scope.
|
2014-09-29 22:11:30 +03:00
|
|
|
pub selection_cache: traits::SelectionCache<'tcx>,
|
2015-08-07 10:37:17 -04:00
|
|
|
|
2015-10-18 19:15:57 +03:00
|
|
|
/// Caches the results of trait evaluation.
|
|
|
|
pub evaluation_cache: traits::EvaluationCache<'tcx>,
|
|
|
|
|
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,
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
|
|
|
|
2015-01-02 04:09:35 -05:00
|
|
|
impl<'a, 'tcx> ParameterEnvironment<'a, 'tcx> {
|
2015-01-26 14:20:38 -05:00
|
|
|
pub fn with_caller_bounds(&self,
|
|
|
|
caller_bounds: Vec<ty::Predicate<'tcx>>)
|
|
|
|
-> ParameterEnvironment<'a,'tcx>
|
|
|
|
{
|
|
|
|
ParameterEnvironment {
|
|
|
|
tcx: self.tcx,
|
|
|
|
free_substs: self.free_substs.clone(),
|
|
|
|
implicit_region_bound: self.implicit_region_bound,
|
|
|
|
caller_bounds: caller_bounds,
|
|
|
|
selection_cache: traits::SelectionCache::new(),
|
2015-10-18 19:15:57 +03:00
|
|
|
evaluation_cache: traits::EvaluationCache::new(),
|
2015-12-08 23:38:36 +01:00
|
|
|
free_id_outlive: self.free_id_outlive,
|
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-02-29 23:36:51 +00:00
|
|
|
pub fn for_item(cx: &'a TyCtxt<'tcx>, id: NodeId) -> ParameterEnvironment<'a, 'tcx> {
|
2014-08-11 17:12:01 -07:00
|
|
|
match cx.map.find(id) {
|
|
|
|
Some(ast_map::NodeImplItem(ref impl_item)) => {
|
2015-03-10 12:28:44 +02:00
|
|
|
match impl_item.node {
|
2015-11-12 15:57:51 +01:00
|
|
|
hir::ImplItemKind::Type(_) => {
|
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
|
|
|
|
let impl_id = cx.map.get_parent(id);
|
2015-09-02 16:11:32 -04:00
|
|
|
let impl_def_id = cx.map.local_def_id(impl_id);
|
2015-08-07 10:37:17 -04:00
|
|
|
let scheme = cx.lookup_item_type(impl_def_id);
|
|
|
|
let predicates = cx.lookup_predicates(impl_def_id);
|
|
|
|
cx.construct_parameter_environment(impl_item.span,
|
|
|
|
&scheme.generics,
|
|
|
|
&predicates,
|
2015-12-08 23:38:36 +01:00
|
|
|
cx.region_maps.item_extent(id))
|
2015-08-07 10:37:17 -04:00
|
|
|
}
|
2015-11-12 15:57:51 +01:00
|
|
|
hir::ImplItemKind::Const(_, _) => {
|
2015-09-02 16:11:32 -04:00
|
|
|
let def_id = cx.map.local_def_id(id);
|
2015-06-25 23:42:17 +03:00
|
|
|
let scheme = cx.lookup_item_type(def_id);
|
|
|
|
let predicates = cx.lookup_predicates(def_id);
|
|
|
|
cx.construct_parameter_environment(impl_item.span,
|
|
|
|
&scheme.generics,
|
|
|
|
&predicates,
|
2015-12-08 23:38:36 +01:00
|
|
|
cx.region_maps.item_extent(id))
|
2015-03-14 12:05:00 -06:00
|
|
|
}
|
2015-11-12 15:57:51 +01:00
|
|
|
hir::ImplItemKind::Method(_, ref body) => {
|
2015-09-02 16:11:32 -04:00
|
|
|
let method_def_id = cx.map.local_def_id(id);
|
2015-06-25 23:42:17 +03:00
|
|
|
match cx.impl_or_trait_item(method_def_id) {
|
2014-08-11 17:12:01 -07:00
|
|
|
MethodTraitItem(ref method_ty) => {
|
|
|
|
let method_generics = &method_ty.generics;
|
2015-02-11 10:28:52 -05:00
|
|
|
let method_bounds = &method_ty.predicates;
|
2015-06-25 23:42:17 +03:00
|
|
|
cx.construct_parameter_environment(
|
2015-03-10 12:28:44 +02:00
|
|
|
impl_item.span,
|
2014-08-11 17:12:01 -07:00
|
|
|
method_generics,
|
2015-02-11 10:28:52 -05:00
|
|
|
method_bounds,
|
2015-12-08 23:38:36 +01:00
|
|
|
cx.region_maps.call_site_extent(id, body.id))
|
2014-08-11 17:12:01 -07:00
|
|
|
}
|
2015-03-14 12:05:00 -06:00
|
|
|
_ => {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("ParameterEnvironment::for_item(): \
|
|
|
|
got non-method item from impl method?!")
|
2014-08-05 19:44:21 -07:00
|
|
|
}
|
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 {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::TypeTraitItem(..) => {
|
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
|
|
|
|
let trait_id = cx.map.get_parent(id);
|
2015-09-02 16:11:32 -04:00
|
|
|
let trait_def_id = cx.map.local_def_id(trait_id);
|
2015-08-07 10:37:17 -04:00
|
|
|
let trait_def = cx.lookup_trait_def(trait_def_id);
|
|
|
|
let predicates = cx.lookup_predicates(trait_def_id);
|
|
|
|
cx.construct_parameter_environment(trait_item.span,
|
|
|
|
&trait_def.generics,
|
|
|
|
&predicates,
|
2015-12-08 23:38:36 +01:00
|
|
|
cx.region_maps.item_extent(id))
|
2015-03-14 12:05:00 -06:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ConstTraitItem(..) => {
|
2015-09-02 16:11:32 -04:00
|
|
|
let def_id = cx.map.local_def_id(id);
|
2015-08-07 10:37:17 -04:00
|
|
|
let scheme = cx.lookup_item_type(def_id);
|
|
|
|
let predicates = cx.lookup_predicates(def_id);
|
|
|
|
cx.construct_parameter_environment(trait_item.span,
|
|
|
|
&scheme.generics,
|
|
|
|
&predicates,
|
2015-12-08 23:38:36 +01:00
|
|
|
cx.region_maps.item_extent(id))
|
2014-08-11 17:12:01 -07: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).
|
2015-09-02 16:11:32 -04:00
|
|
|
let method_def_id = cx.map.local_def_id(id);
|
2015-06-25 23:42:17 +03:00
|
|
|
match cx.impl_or_trait_item(method_def_id) {
|
2014-08-11 17:12:01 -07:00
|
|
|
MethodTraitItem(ref method_ty) => {
|
|
|
|
let method_generics = &method_ty.generics;
|
2015-02-11 10:28:52 -05:00
|
|
|
let method_bounds = &method_ty.predicates;
|
2015-12-08 23:38:36 +01:00
|
|
|
let extent = if let Some(ref body) = *body {
|
|
|
|
// default impl: use call_site extent as free_id_outlive bound.
|
|
|
|
cx.region_maps.call_site_extent(id, body.id)
|
|
|
|
} else {
|
|
|
|
// no default impl: use item extent as free_id_outlive bound.
|
|
|
|
cx.region_maps.item_extent(id)
|
|
|
|
};
|
2015-06-25 23:42:17 +03:00
|
|
|
cx.construct_parameter_environment(
|
2015-03-10 12:28:44 +02:00
|
|
|
trait_item.span,
|
2014-08-11 17:12:01 -07:00
|
|
|
method_generics,
|
2015-02-11 10:28:52 -05:00
|
|
|
method_bounds,
|
2015-12-08 23:38:36 +01:00
|
|
|
extent)
|
2014-08-11 17:12:01 -07:00
|
|
|
}
|
2015-03-14 12:05:00 -06:00
|
|
|
_ => {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("ParameterEnvironment::for_item(): \
|
|
|
|
got non-method item from provided \
|
|
|
|
method?!")
|
2014-08-05 19:44:21 -07:00
|
|
|
}
|
2014-08-11 17:12:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ast_map::NodeItem(item)) => {
|
|
|
|
match item.node {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemFn(_, _, _, _, _, ref body) => {
|
2014-08-11 17:12:01 -07:00
|
|
|
// We assume this is a function.
|
2015-09-02 16:11:32 -04:00
|
|
|
let fn_def_id = cx.map.local_def_id(id);
|
2015-06-25 23:42:17 +03:00
|
|
|
let fn_scheme = cx.lookup_item_type(fn_def_id);
|
|
|
|
let fn_predicates = cx.lookup_predicates(fn_def_id);
|
|
|
|
|
|
|
|
cx.construct_parameter_environment(item.span,
|
|
|
|
&fn_scheme.generics,
|
|
|
|
&fn_predicates,
|
2015-12-08 23:38:36 +01:00
|
|
|
cx.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(..) |
|
|
|
|
hir::ItemImpl(..) |
|
|
|
|
hir::ItemConst(..) |
|
|
|
|
hir::ItemStatic(..) => {
|
2015-09-02 16:11:32 -04:00
|
|
|
let def_id = cx.map.local_def_id(id);
|
2015-06-25 23:42:17 +03:00
|
|
|
let scheme = cx.lookup_item_type(def_id);
|
|
|
|
let predicates = cx.lookup_predicates(def_id);
|
|
|
|
cx.construct_parameter_environment(item.span,
|
|
|
|
&scheme.generics,
|
|
|
|
&predicates,
|
2015-12-08 23:38:36 +01:00
|
|
|
cx.region_maps.item_extent(id))
|
2014-08-11 17:12:01 -07:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemTrait(..) => {
|
2015-09-02 16:11:32 -04:00
|
|
|
let def_id = cx.map.local_def_id(id);
|
2015-08-07 10:37:17 -04:00
|
|
|
let trait_def = cx.lookup_trait_def(def_id);
|
|
|
|
let predicates = cx.lookup_predicates(def_id);
|
|
|
|
cx.construct_parameter_environment(item.span,
|
|
|
|
&trait_def.generics,
|
|
|
|
&predicates,
|
2015-12-08 23:38:36 +01:00
|
|
|
cx.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
Some(ast_map::NodeExpr(..)) => {
|
|
|
|
// This is a convenience to allow closures to work.
|
|
|
|
ParameterEnvironment::for_item(cx, cx.map.get_parent(id))
|
|
|
|
}
|
2014-08-11 17:12:01 -07:00
|
|
|
_ => {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("ParameterEnvironment::from_item(): \
|
|
|
|
`{}` is not an item",
|
|
|
|
cx.map.node_to_string(id))
|
2014-08-11 17:12:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 05:52:47 -05:00
|
|
|
/// A "type scheme", in ML terminology, is a type combined with some
|
|
|
|
/// set of generic types that the type is, well, generic over. In Rust
|
|
|
|
/// terms, it is the "type" of a fn item or struct -- this type will
|
|
|
|
/// include various generic parameters that must be substituted when
|
|
|
|
/// the item/struct is referenced. That is called converting the type
|
|
|
|
/// scheme to a monotype.
|
2012-08-08 12:58:22 -07:00
|
|
|
///
|
2014-05-06 15:59:45 -04:00
|
|
|
/// - `generics`: the set of type parameters and their bounds
|
|
|
|
/// - `ty`: the base types, which may reference the parameters defined
|
|
|
|
/// in `generics`
|
2014-12-30 08:59:33 -05:00
|
|
|
///
|
|
|
|
/// Note that TypeSchemes are also sometimes called "polytypes" (and
|
|
|
|
/// in fact this struct used to carry that name, so you may find some
|
|
|
|
/// stray references in a comment or something). We try to reserve the
|
|
|
|
/// "poly" prefix to refer to higher-ranked things, as in
|
|
|
|
/// `PolyTraitRef`.
|
2015-02-11 06:54:05 -05:00
|
|
|
///
|
|
|
|
/// Note that each item also comes with predicates, see
|
|
|
|
/// `lookup_predicates`.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2014-12-23 05:52:47 -05:00
|
|
|
pub struct TypeScheme<'tcx> {
|
2014-09-29 22:11:30 +03:00
|
|
|
pub generics: Generics<'tcx>,
|
2015-02-11 06:54:05 -05:00
|
|
|
pub ty: Ty<'tcx>,
|
2013-02-19 02:40:42 -05:00
|
|
|
}
|
2011-06-15 11:19:50 -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,
|
2015-08-25 21:52:15 +03:00
|
|
|
const IS_NO_DROP_FLAG = 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-01-14 14:26:50 +03:00
|
|
|
pub kind: VariantKind,
|
2015-07-22 20:10:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:33 +03:00
|
|
|
pub struct FieldDefData<'tcx, 'container: 'tcx> {
|
2015-08-07 13:48:29 +03:00
|
|
|
/// The field's DefId. NOTE: the fields of tuple-like enum variants
|
|
|
|
/// are not real items, and don't have entries in tcache etc.
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 16:07:15 +01:00
|
|
|
impl<'tcx> Encodable for AdtDef<'tcx> {
|
|
|
|
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
|
|
|
self.did.encode(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Decodable for AdtDef<'tcx> {
|
|
|
|
fn decode<D: Decoder>(d: &mut D) -> Result<AdtDef<'tcx>, D::Error> {
|
2016-03-22 22:01:37 -05:00
|
|
|
let def_id: DefId = Decodable::decode(d)?;
|
2015-12-01 16:07:15 +01:00
|
|
|
|
|
|
|
cstore::tls::with_decoding_context(d, |dcx, _| {
|
|
|
|
let def_id = dcx.translate_def_id(def_id);
|
|
|
|
Ok(dcx.tcx().lookup_adt_def(def_id))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 21:53:52 +03:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
2015-08-07 14:41:33 +03:00
|
|
|
pub enum AdtKind { Struct, Enum }
|
2015-07-22 20:10:18 +03:00
|
|
|
|
2016-01-17 22:57:54 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
|
2015-10-08 03:20:57 +03:00
|
|
|
pub enum VariantKind { Struct, Tuple, Unit }
|
2015-07-22 21:53:52 +03:00
|
|
|
|
2016-01-17 22:57:54 +03:00
|
|
|
impl VariantKind {
|
|
|
|
pub fn from_variant_data(vdata: &hir::VariantData) -> Self {
|
|
|
|
match *vdata {
|
|
|
|
hir::VariantData::Struct(..) => VariantKind::Struct,
|
|
|
|
hir::VariantData::Tuple(..) => VariantKind::Tuple,
|
|
|
|
hir::VariantData::Unit(..) => VariantKind::Unit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:33 +03:00
|
|
|
impl<'tcx, 'container> AdtDefData<'tcx, 'container> {
|
2016-02-29 23:36:51 +00:00
|
|
|
fn new(tcx: &TyCtxt<'tcx>,
|
2015-07-22 21:53:52 +03:00
|
|
|
did: DefId,
|
2015-08-07 14:41:33 +03:00
|
|
|
kind: AdtKind,
|
|
|
|
variants: Vec<VariantDefData<'tcx, 'container>>) -> Self {
|
|
|
|
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-08-25 21:52:15 +03:00
|
|
|
if attr::contains_name(&attrs, "unsafe_no_drop_flag") {
|
|
|
|
flags = flags | AdtFlags::IS_NO_DROP_FLAG;
|
|
|
|
}
|
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
|
|
|
}
|
2015-08-07 14:41:33 +03:00
|
|
|
if let AdtKind::Enum = kind {
|
|
|
|
flags = flags | AdtFlags::IS_ENUM;
|
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-02-29 23:36:51 +00:00
|
|
|
fn calculate_dtorck(&'tcx self, tcx: &TyCtxt<'tcx>) {
|
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
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if self.flags.get().intersects(AdtFlags::IS_ENUM) {
|
|
|
|
AdtKind::Enum
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-29 23:36:51 +00:00
|
|
|
pub fn is_dtorck(&'tcx self, tcx: &TyCtxt<'tcx>) -> 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 {
|
|
|
|
match self.dtor_kind() {
|
|
|
|
NoDtor => false,
|
|
|
|
TraitDtor(..) => true
|
|
|
|
}
|
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.
|
2015-08-07 14:41:33 +03:00
|
|
|
pub fn struct_variant(&self) -> &VariantDefData<'tcx, 'container> {
|
2015-12-27 02:17:16 +01:00
|
|
|
assert_eq!(self.adt_kind(), AdtKind::Struct);
|
2015-07-22 21:53:52 +03:00
|
|
|
&self.variants[0]
|
2015-07-20 22:13:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-02-29 23:36:51 +00:00
|
|
|
pub fn type_scheme(&self, tcx: &TyCtxt<'tcx>) -> TypeScheme<'tcx> {
|
2015-07-20 22:13:36 +03:00
|
|
|
tcx.lookup_item_type(self.did)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-02-29 23:36:51 +00:00
|
|
|
pub fn predicates(&self, tcx: &TyCtxt<'tcx>) -> GenericPredicates<'tcx> {
|
2015-07-20 22:13:36 +03:00
|
|
|
tcx.lookup_predicates(self.did)
|
|
|
|
}
|
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<
|
2015-08-07 14:41:33 +03:00
|
|
|
slice::Iter<VariantDefData<'tcx, 'container>>,
|
|
|
|
slice::Iter<FieldDefData<'tcx, 'container>>,
|
|
|
|
for<'s> fn(&'s VariantDefData<'tcx, 'container>)
|
|
|
|
-> slice::Iter<'s, FieldDefData<'tcx, '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())
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:33 +03:00
|
|
|
pub fn variant_with_id(&self, vid: DefId) -> &VariantDefData<'tcx, '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-01-20 22:31:10 +03:00
|
|
|
pub fn variant_of_def(&self, def: Def) -> &VariantDefData<'tcx, 'container> {
|
2015-07-22 21:53:52 +03:00
|
|
|
match def {
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Variant(_, vid) => self.variant_with_id(vid),
|
|
|
|
Def::Struct(..) | Def::TyAlias(..) => 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() {
|
|
|
|
Some(_) => {
|
|
|
|
TraitDtor(!self.flags.get().intersects(AdtFlags::IS_NO_DROP_FLAG))
|
|
|
|
}
|
|
|
|
None => NoDtor,
|
|
|
|
}
|
|
|
|
}
|
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-14 15:49:39 +03:00
|
|
|
pub fn sized_constraint(&self, tcx: &ty::TyCtxt<'tcx>) -> Ty<'tcx> {
|
|
|
|
let dep_node = DepNode::SizedConstraint(self.did);
|
|
|
|
match self.sized_constraint.get(dep_node) {
|
|
|
|
None => {
|
|
|
|
let this = tcx.lookup_adt_def_master(self.did);
|
|
|
|
this.calculate_sized_constraint_inner(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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> AdtDefData<'tcx, 'tcx> {
|
|
|
|
fn sized_constraint_for_tys<TYS>(
|
|
|
|
&'tcx self,
|
|
|
|
tcx: &ty::TyCtxt<'tcx>,
|
|
|
|
stack: &mut Vec<AdtDefMaster<'tcx>>,
|
|
|
|
tys: TYS
|
|
|
|
) -> Ty<'tcx>
|
|
|
|
where TYS: IntoIterator<Item=Ty<'tcx>>
|
|
|
|
{
|
|
|
|
let tys : Vec<_> = tys.into_iter()
|
|
|
|
.map(|ty| self.sized_constraint_for_ty(tcx, stack, ty))
|
2016-04-18 00:04:21 +03:00
|
|
|
.flat_map(|ty| match ty.sty {
|
2016-04-22 01:46:23 +03:00
|
|
|
ty::TyTuple(ref tys) => tys.last().cloned(),
|
|
|
|
_ => Some(ty)
|
2016-04-18 00:04:21 +03:00
|
|
|
})
|
2016-04-14 15:49:39 +03:00
|
|
|
.filter(|ty| *ty != tcx.types.bool)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
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],
|
|
|
|
_ => tcx.mk_tup(tys)
|
|
|
|
}
|
|
|
|
}
|
2016-04-18 00:04:21 +03:00
|
|
|
|
2016-04-14 15:49:39 +03:00
|
|
|
fn sized_constraint_for_ty(
|
|
|
|
&'tcx self,
|
|
|
|
tcx: &ty::TyCtxt<'tcx>,
|
|
|
|
stack: &mut Vec<AdtDefMaster<'tcx>>,
|
|
|
|
ty: Ty<'tcx>
|
|
|
|
) -> Ty<'tcx> {
|
|
|
|
let result = match ty.sty {
|
|
|
|
TyBool | TyChar | TyInt(..) | TyUint(..) | TyFloat(..) |
|
|
|
|
TyBox(..) | TyRawPtr(..) | TyRef(..) | TyFnDef(..) | TyFnPtr(_) |
|
|
|
|
TyArray(..) | TyClosure(..) => {
|
|
|
|
// these are always sized - return a primitive
|
|
|
|
tcx.types.bool
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
|
|
|
|
TyTuple(ref tys) => {
|
|
|
|
self.sized_constraint_for_tys(tcx, stack, tys.iter().cloned())
|
|
|
|
}
|
|
|
|
|
|
|
|
TyEnum(adt, substs) | TyStruct(adt, substs) => {
|
|
|
|
// 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);
|
|
|
|
self.sized_constraint_for_ty(tcx, stack, adt_ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
TyProjection(..) => {
|
|
|
|
// must calculate explicitly.
|
|
|
|
// FIXME: consider special-casing always-Sized projections
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
_ => return ty
|
|
|
|
};
|
|
|
|
let sized_predicate = Binder(TraitRef {
|
|
|
|
def_id: sized_trait,
|
|
|
|
substs: tcx.mk_substs(Substs::new_trait(
|
|
|
|
vec![], vec![], ty
|
|
|
|
))
|
|
|
|
}).to_predicate();
|
|
|
|
let predicates = tcx.lookup_predicates(self.did).predicates;
|
|
|
|
if predicates.into_iter().any(|p| p == sized_predicate) {
|
|
|
|
tcx.types.bool
|
|
|
|
} else {
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-04-16 15:16:36 +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-04-14 15:49:39 +03:00
|
|
|
fn calculate_sized_constraint_inner(&'tcx self, tcx: &ty::TyCtxt<'tcx>,
|
|
|
|
stack: &mut Vec<AdtDefMaster<'tcx>>)
|
|
|
|
{
|
|
|
|
|
|
|
|
let dep_node = DepNode::SizedConstraint(self.did);
|
|
|
|
|
|
|
|
if self.sized_constraint.get(dep_node).is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if stack.contains(&self) {
|
|
|
|
debug!("calculate_sized_constraint: {:?} is recursive", self);
|
|
|
|
self.sized_constraint.fulfill(dep_node, tcx.types.err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stack.push(self);
|
|
|
|
let ty = self.sized_constraint_for_tys(
|
|
|
|
tcx, stack,
|
|
|
|
self.variants.iter().flat_map(|v| {
|
|
|
|
v.fields.last()
|
|
|
|
}).map(|f| f.unsubst_ty())
|
|
|
|
);
|
|
|
|
|
|
|
|
let self_ = stack.pop().unwrap();
|
|
|
|
assert_eq!(self_, self);
|
|
|
|
|
|
|
|
match self.sized_constraint.get(dep_node) {
|
|
|
|
Some(old_ty) => {
|
|
|
|
debug!("calculate_sized_constraint: {:?} recurred", self);
|
|
|
|
assert_eq!(old_ty, tcx.types.err)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
debug!("calculate_sized_constraint: {:?} => {:?}", self, ty);
|
|
|
|
self.sized_constraint.fulfill(dep_node, ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn kind(&self) -> VariantKind {
|
2016-01-14 14:26:50 +03:00
|
|
|
self.kind
|
2015-07-22 21:53:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_tuple_struct(&self) -> bool {
|
|
|
|
self.kind() == VariantKind::Tuple
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:33 +03:00
|
|
|
impl<'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-02-29 23:36:51 +00:00
|
|
|
pub fn ty(&self, tcx: &TyCtxt<'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.
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub struct ItemSubsts<'tcx> {
|
|
|
|
pub substs: 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
|
|
|
}
|
|
|
|
|
2015-01-24 22:00:03 +02:00
|
|
|
impl ClosureKind {
|
2016-02-29 23:36:51 +00:00
|
|
|
pub fn trait_did(&self, cx: &TyCtxt) -> DefId {
|
2014-08-20 09:12:16 -07:00
|
|
|
let result = match *self {
|
2016-02-12 16:44:27 +01:00
|
|
|
ClosureKind::Fn => cx.lang_items.require(FnTraitLangItem),
|
|
|
|
ClosureKind::FnMut => {
|
2014-08-20 09:12:16 -07:00
|
|
|
cx.lang_items.require(FnMutTraitLangItem)
|
|
|
|
}
|
2016-02-12 16:44:27 +01:00
|
|
|
ClosureKind::FnOnce => {
|
2014-08-20 09:12:16 -07:00
|
|
|
cx.lang_items.require(FnOnceTraitLangItem)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match result {
|
|
|
|
Ok(trait_did) => trait_did,
|
2015-02-18 14:48:57 -05:00
|
|
|
Err(err) => cx.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 empty() -> ItemSubsts<'tcx> {
|
|
|
|
ItemSubsts { substs: Substs::empty() }
|
2014-12-01 16:34:57 -05:00
|
|
|
}
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
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
|
|
|
|
/// typeck::collect (e.g., `cx.impl_or_trait_items`, `cx.tcache`, etc). All of
|
|
|
|
/// 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-02-29 23:36:51 +00:00
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn node_id_to_type(&self, id: NodeId) -> Ty<'tcx> {
|
2015-06-25 23:42:17 +03:00
|
|
|
match self.node_id_to_type_opt(id) {
|
|
|
|
Some(ty) => ty,
|
2016-03-25 01:14:29 +01:00
|
|
|
None => bug!("node_id_to_type: no type for node `{}`",
|
|
|
|
self.map.node_to_string(id))
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
}
|
2012-02-07 21:19:53 -08:00
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn node_id_to_type_opt(&self, id: NodeId) -> Option<Ty<'tcx>> {
|
2015-06-24 13:40:54 -07:00
|
|
|
self.tables.borrow().node_types.get(&id).cloned()
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2012-02-07 21:19:53 -08:00
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn node_id_item_substs(&self, id: NodeId) -> ItemSubsts<'tcx> {
|
2015-06-24 13:40:54 -07:00
|
|
|
match self.tables.borrow().item_substs.get(&id) {
|
2015-06-25 23:42:17 +03:00
|
|
|
None => ItemSubsts::empty(),
|
|
|
|
Some(ts) => ts.clone(),
|
|
|
|
}
|
|
|
|
}
|
2012-02-14 15:21:53 -08:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
// Returns the type of a pattern as a monotype. Like @expr_ty, this function
|
|
|
|
// doesn't provide type parameter substitutions.
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn pat_ty(&self, pat: &hir::Pat) -> Ty<'tcx> {
|
2015-06-25 23:42:17 +03:00
|
|
|
self.node_id_to_type(pat.id)
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn pat_ty_opt(&self, pat: &hir::Pat) -> Option<Ty<'tcx>> {
|
2015-06-25 23:42:17 +03:00
|
|
|
self.node_id_to_type_opt(pat.id)
|
|
|
|
}
|
2012-11-28 15:42:16 -08:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
// Returns the type of an expression as a monotype.
|
|
|
|
//
|
|
|
|
// NB (1): This is the PRE-ADJUSTMENT TYPE for the expression. That is, in
|
|
|
|
// some cases, we insert `AutoAdjustment` annotations such as auto-deref or
|
|
|
|
// auto-ref. The type returned by this function does not consider such
|
|
|
|
// adjustments. See `expr_ty_adjusted()` instead.
|
|
|
|
//
|
|
|
|
// NB (2): This type doesn't provide type parameter substitutions; e.g. if you
|
|
|
|
// ask for the type of "id" in "id(3)", it will return "fn(&isize) -> isize"
|
|
|
|
// instead of "fn(ty) -> T with T = isize".
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn expr_ty(&self, expr: &hir::Expr) -> Ty<'tcx> {
|
2015-06-25 23:42:17 +03:00
|
|
|
self.node_id_to_type(expr.id)
|
2012-11-28 15:42:16 -08:00
|
|
|
}
|
2013-05-31 15:17:22 -07:00
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn expr_ty_opt(&self, expr: &hir::Expr) -> Option<Ty<'tcx>> {
|
2015-06-25 23:42:17 +03:00
|
|
|
self.node_id_to_type_opt(expr.id)
|
2013-06-23 14:41:45 +12:00
|
|
|
}
|
2012-11-28 15:42:16 -08:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Returns the type of `expr`, considering any `AutoAdjustment`
|
|
|
|
/// entry recorded for that expression.
|
|
|
|
///
|
|
|
|
/// It would almost certainly be better to store the adjusted ty in with
|
|
|
|
/// the `AutoAdjustment`, but I opted not to do this because it would
|
|
|
|
/// require serializing and deserializing the type and, although that's not
|
|
|
|
/// hard to do, I just hate that code so much I didn't want to touch it
|
|
|
|
/// unless it was to fix it properly, which seemed a distraction from the
|
|
|
|
/// thread at hand! -nmatsakis
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn expr_ty_adjusted(&self, expr: &hir::Expr) -> Ty<'tcx> {
|
2015-06-25 23:42:17 +03:00
|
|
|
self.expr_ty(expr)
|
|
|
|
.adjust(self, expr.span, expr.id,
|
2015-06-24 13:40:54 -07:00
|
|
|
self.tables.borrow().adjustments.get(&expr.id),
|
2015-06-25 23:42:17 +03:00
|
|
|
|method_call| {
|
2015-06-24 13:40:54 -07:00
|
|
|
self.tables.borrow().method_map.get(&method_call).map(|method| method.ty)
|
2015-06-25 23:42:17 +03:00
|
|
|
})
|
|
|
|
}
|
2013-06-23 14:41:45 +12:00
|
|
|
|
2016-01-28 13:10:04 +13:00
|
|
|
pub fn expr_ty_adjusted_opt(&self, expr: &hir::Expr) -> Option<Ty<'tcx>> {
|
|
|
|
self.expr_ty_opt(expr).map(|t| t.adjust(self,
|
|
|
|
expr.span,
|
|
|
|
expr.id,
|
|
|
|
self.tables.borrow().adjustments.get(&expr.id),
|
|
|
|
|method_call| {
|
|
|
|
self.tables.borrow().method_map.get(&method_call).map(|method| method.ty)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
pub fn expr_span(&self, id: NodeId) -> Span {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
pub fn local_var_name_str(&self, id: NodeId) -> InternedString {
|
|
|
|
match self.map.find(id) {
|
|
|
|
Some(ast_map::NodeLocal(pat)) => {
|
|
|
|
match pat.node {
|
2016-02-14 15:25:12 +03:00
|
|
|
PatKind::Ident(_, ref path1, _) => path1.node.name.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-01-20 22:31:10 +03:00
|
|
|
pub fn resolve_expr(&self, expr: &hir::Expr) -> Def {
|
2015-06-25 23:42:17 +03:00
|
|
|
match self.def_map.borrow().get(&expr.id) {
|
|
|
|
Some(def) => def.full_def(),
|
|
|
|
None => {
|
2016-03-25 18:31:27 +01:00
|
|
|
span_bug!(expr.span, "no def-map entry for expr {}", expr.id);
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07: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(..) => {
|
2015-06-25 23:42:17 +03:00
|
|
|
// We can't use resolve_expr here, as this needs to run on broken
|
|
|
|
// programs. We don't need to through - associated items are all
|
|
|
|
// rvalues.
|
|
|
|
match self.def_map.borrow().get(&expr.id) {
|
|
|
|
Some(&def::PathResolution {
|
2016-01-20 22:31:10 +03:00
|
|
|
base_def: Def::Static(..), ..
|
2015-06-25 23:42:17 +03:00
|
|
|
}) | Some(&def::PathResolution {
|
2016-01-20 22:31:10 +03:00
|
|
|
base_def: Def::Upvar(..), ..
|
2015-06-25 23:42:17 +03:00
|
|
|
}) | Some(&def::PathResolution {
|
2016-01-20 22:31:10 +03:00
|
|
|
base_def: Def::Local(..), ..
|
2015-06-25 23:42:17 +03:00
|
|
|
}) => {
|
|
|
|
true
|
|
|
|
}
|
2016-01-20 22:31:10 +03:00
|
|
|
Some(&def::PathResolution { base_def: Def::Err, .. })=> true,
|
2015-06-25 23:42:17 +03:00
|
|
|
Some(..) => false,
|
2016-03-25 18:31:27 +01:00
|
|
|
None => span_bug!(expr.span, "no def for path {}", expr.id)
|
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(..) |
|
|
|
|
hir::ExprVec(..) |
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn provided_trait_methods(&self, id: DefId) -> Vec<Rc<Method<'tcx>>> {
|
2015-09-04 13:52:28 -04:00
|
|
|
if let Some(id) = self.map.as_local_node_id(id) {
|
|
|
|
if let ItemTrait(_, _, _, ref ms) = self.map.expect_item(id).node {
|
2015-06-25 23:42:17 +03:00
|
|
|
ms.iter().filter_map(|ti| {
|
2015-07-31 00:04:06 -07:00
|
|
|
if let hir::MethodTraitItem(_, Some(_)) = ti.node {
|
2015-09-02 16:11:32 -04:00
|
|
|
match self.impl_or_trait_item(self.map.local_def_id(ti.id)) {
|
2015-06-25 23:42:17 +03:00
|
|
|
MethodTraitItem(m) => Some(m),
|
|
|
|
_ => {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("provided_trait_methods(): \
|
|
|
|
non-method item found from \
|
|
|
|
looking up provided method?!")
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
} else {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("provided_trait_methods: `{:?}` is not a trait", id)
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
} else {
|
2015-11-20 14:51:18 +02:00
|
|
|
self.sess.cstore.provided_trait_methods(self, id)
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn associated_consts(&self, id: DefId) -> Vec<Rc<AssociatedConst<'tcx>>> {
|
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 {
|
2015-06-25 23:42:17 +03:00
|
|
|
ItemTrait(_, _, _, ref tis) => {
|
|
|
|
tis.iter().filter_map(|ti| {
|
2015-07-31 00:04:06 -07:00
|
|
|
if let hir::ConstTraitItem(_, _) = ti.node {
|
2015-09-02 16:11:32 -04:00
|
|
|
match self.impl_or_trait_item(self.map.local_def_id(ti.id)) {
|
2015-06-25 23:42:17 +03:00
|
|
|
ConstTraitItem(ac) => Some(ac),
|
|
|
|
_ => {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("associated_consts(): \
|
|
|
|
non-const item found from \
|
|
|
|
looking up a constant?!")
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
ItemImpl(_, _, _, _, _, ref iis) => {
|
|
|
|
iis.iter().filter_map(|ii| {
|
2015-11-12 15:57:51 +01:00
|
|
|
if let hir::ImplItemKind::Const(_, _) = ii.node {
|
2015-09-02 16:11:32 -04:00
|
|
|
match self.impl_or_trait_item(self.map.local_def_id(ii.id)) {
|
2015-06-25 23:42:17 +03:00
|
|
|
ConstTraitItem(ac) => Some(ac),
|
|
|
|
_ => {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("associated_consts(): \
|
|
|
|
non-const item found from \
|
|
|
|
looking up a constant?!")
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
_ => {
|
2016-03-25 01:14:29 +01:00
|
|
|
bug!("associated_consts: `{:?}` is not a trait or impl", id)
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-20 14:51:18 +02:00
|
|
|
self.sess.cstore.associated_consts(self, id)
|
2015-03-27 01:43:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn trait_impl_polarity(&self, id: DefId) -> Option<hir::ImplPolarity> {
|
2015-09-04 13:52:28 -04:00
|
|
|
if let Some(id) = self.map.as_local_node_id(id) {
|
|
|
|
match self.map.find(id) {
|
2015-06-25 23:42:17 +03:00
|
|
|
Some(ast_map::NodeItem(item)) => {
|
|
|
|
match item.node {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemImpl(_, polarity, _, _, _, _) => Some(polarity),
|
2015-06-25 23:42:17 +03:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
} 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
|
|
|
|
2015-09-14 14:55:56 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn impl_or_trait_item(&self, id: DefId) -> ImplOrTraitItem<'tcx> {
|
2015-06-25 23:42:17 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"impl_or_trait_items", id, &self.impl_or_trait_items,
|
2016-02-23 22:04:27 +02:00
|
|
|
|| self.sess.cstore.impl_or_trait_item(self, id)
|
|
|
|
.expect("missing ImplOrTraitItem in metadata"))
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn trait_item_def_ids(&self, id: DefId) -> Rc<Vec<ImplOrTraitItemId>> {
|
2015-06-25 23:42:17 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"trait_item_def_ids", id, &self.trait_item_def_ids,
|
2015-11-20 14:51:18 +02:00
|
|
|
|| Rc::new(self.sess.cstore.trait_item_def_ids(id)))
|
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.
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn impl_trait_ref(&self, id: DefId) -> Option<TraitRef<'tcx>> {
|
2015-06-25 23:42:17 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"impl_trait_refs", id, &self.impl_trait_refs,
|
2015-11-20 14:51:18 +02:00
|
|
|
|| self.sess.cstore.impl_trait_ref(self, id))
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether this DefId refers to an impl
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn is_impl(&self, id: DefId) -> bool {
|
2015-09-04 13:52:28 -04:00
|
|
|
if let Some(id) = self.map.as_local_node_id(id) {
|
2015-06-25 23:42:17 +03:00
|
|
|
if let Some(ast_map::NodeItem(
|
2015-09-04 13:52:28 -04:00
|
|
|
&hir::Item { node: hir::ItemImpl(..), .. })) = self.map.find(id) {
|
2015-06-25 23:42:17 +03:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-20 14:51:18 +02:00
|
|
|
self.sess.cstore.is_impl(id)
|
2015-03-27 01:43:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn trait_ref_to_def_id(&self, tr: &hir::TraitRef) -> DefId {
|
2015-06-25 23:42:17 +03:00
|
|
|
self.def_map.borrow().get(&tr.ref_id).expect("no def-map entry for trait").def_id()
|
|
|
|
}
|
2015-03-27 01:43:14 +01:00
|
|
|
|
2016-03-16 05:57:03 -04:00
|
|
|
pub fn def_key(&self, id: DefId) -> ast_map::DefKey {
|
|
|
|
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-03-16 05:50:38 -04:00
|
|
|
/// Returns the `DefPath` of an item. Note that if `id` is not
|
|
|
|
/// local to this crate -- or is inlined into this crate -- the
|
|
|
|
/// result will be a non-local `DefPath`.
|
2015-09-17 14:29:59 -04:00
|
|
|
pub fn def_path(&self, id: DefId) -> ast_map::DefPath {
|
|
|
|
if id.is_local() {
|
|
|
|
self.map.def_path(id)
|
|
|
|
} else {
|
2016-03-16 05:50:38 -04:00
|
|
|
self.sess.cstore.relative_def_path(id)
|
2015-09-17 14:29:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 06:32:28 -04: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)
|
2015-08-12 20:22:25 +02:00
|
|
|
} else {
|
2015-11-20 14:51:18 +02:00
|
|
|
self.sess.cstore.item_name(id)
|
2015-08-12 20:22:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:07:26 +03:00
|
|
|
// Register a given item type
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn register_item_type(&self, did: DefId, ty: TypeScheme<'tcx>) {
|
2015-07-01 23:07:26 +03:00
|
|
|
self.tcache.borrow_mut().insert(did, ty);
|
|
|
|
}
|
2011-06-15 11:19:50 -07: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.
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn lookup_item_type(&self, did: DefId) -> TypeScheme<'tcx> {
|
2015-06-25 23:42:17 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"tcache", did, &self.tcache,
|
2015-11-20 14:51:18 +02:00
|
|
|
|| self.sess.cstore.item_type(self, did))
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2012-03-03 17:49:23 -08:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Given the did of a trait, returns its canonical trait ref.
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn lookup_trait_def(&self, did: DefId) -> &'tcx TraitDef<'tcx> {
|
2015-06-25 23:42:17 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"trait_defs", did, &self.trait_defs,
|
2015-11-20 14:51:18 +02:00
|
|
|
|| self.alloc_trait_def(self.sess.cstore.trait_def(self, 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.
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn lookup_adt_def_master(&self, did: DefId) -> AdtDefMaster<'tcx> {
|
2015-07-20 22:13:36 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"adt_defs", did, &self.adt_defs,
|
2015-11-20 14:51:18 +02:00
|
|
|
|| self.sess.cstore.adt_def(self, 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.
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn lookup_adt_def(&self, did: DefId) -> AdtDef<'tcx> {
|
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)
|
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Given the did of an item, returns its full set of predicates.
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn lookup_predicates(&self, did: DefId) -> GenericPredicates<'tcx> {
|
2015-06-25 23:42:17 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"predicates", did, &self.predicates,
|
2015-11-20 14:51:18 +02:00
|
|
|
|| self.sess.cstore.item_predicates(self, 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.
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn lookup_super_predicates(&self, did: DefId) -> GenericPredicates<'tcx> {
|
2015-06-25 23:42:17 +03:00
|
|
|
lookup_locally_or_in_crate_store(
|
|
|
|
"super_predicates", did, &self.super_predicates,
|
2015-11-20 14:51:18 +02:00
|
|
|
|| self.sess.cstore.item_super_predicates(self, did))
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
2014-12-07 11:10:48 -05:00
|
|
|
|
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`.)
|
|
|
|
pub fn type_needs_drop_given_env<'a>(&self,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
param_env: &ty::ParameterEnvironment<'a,'tcx>) -> bool {
|
|
|
|
// 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) ...
|
|
|
|
let implements_copy = !ty.moves_by_default(param_env, DUMMY_SP);
|
|
|
|
|
|
|
|
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).
|
|
|
|
|
|
|
|
let contents = ty.type_contents(self);
|
|
|
|
debug!("type_needs_drop ty={:?} contents={:?}", ty, contents);
|
|
|
|
contents.needs_drop(self)
|
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Get the attributes of a definition.
|
2015-09-14 21:58:20 +12:00
|
|
|
pub fn get_attrs(&self, did: DefId) -> Cow<'tcx, [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
|
|
|
|
pub fn has_attr(&self, did: DefId, attr: &str) -> bool {
|
|
|
|
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)]`
|
|
|
|
pub fn lookup_packed(&self, did: DefId) -> bool {
|
|
|
|
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]`
|
|
|
|
pub fn lookup_simd(&self, did: DefId) -> bool {
|
|
|
|
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
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn item_variances(&self, item_id: DefId) -> Rc<ItemVariances> {
|
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
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
pub fn trait_has_default_impl(&self, trait_def_id: DefId) -> bool {
|
|
|
|
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.
|
|
|
|
pub fn record_trait_has_default_impl(&self, trait_def_id: DefId) {
|
|
|
|
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
|
|
|
/// Load primitive inherent implementations if necessary
|
|
|
|
pub fn populate_implementations_for_primitive_if_necessary(&self,
|
2015-08-16 06:32:28 -04:00
|
|
|
primitive_def_id: DefId) {
|
2015-08-16 09:06:23 -04:00
|
|
|
if primitive_def_id.is_local() {
|
2015-06-25 23:42:17 +03:00
|
|
|
return
|
|
|
|
}
|
2015-03-10 23:12:55 -05:00
|
|
|
|
2015-12-22 16:35:02 -05:00
|
|
|
// The primitive is not local, hence we are reading this out
|
|
|
|
// of metadata.
|
|
|
|
let _ignore = self.dep_graph.in_ignore();
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
if self.populated_external_primitive_impls.borrow().contains(&primitive_def_id) {
|
|
|
|
return
|
|
|
|
}
|
2015-03-10 23:12:55 -05:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
debug!("populate_implementations_for_primitive_if_necessary: searching for {:?}",
|
|
|
|
primitive_def_id);
|
2015-03-10 23:12:55 -05:00
|
|
|
|
2015-11-20 14:51:18 +02:00
|
|
|
let impl_items = self.sess.cstore.impl_items(primitive_def_id);
|
2015-04-21 19:00:12 +03:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
// Store the implementation info.
|
|
|
|
self.impl_items.borrow_mut().insert(primitive_def_id, impl_items);
|
|
|
|
self.populated_external_primitive_impls.borrow_mut().insert(primitive_def_id);
|
2013-08-23 14:34:00 -07:00
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// Populates the type context with all the inherent implementations for
|
|
|
|
/// the given type if necessary.
|
|
|
|
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);
|
|
|
|
for &impl_def_id in &inherent_impls {
|
2015-06-25 23:42:17 +03:00
|
|
|
// Store the implementation info.
|
2015-11-20 14:51:18 +02:00
|
|
|
let impl_items = self.sess.cstore.impl_items(impl_def_id);
|
2015-06-25 23:42:17 +03:00
|
|
|
self.impl_items.borrow_mut().insert(impl_def_id, impl_items);
|
2015-11-20 14:51:18 +02:00
|
|
|
}
|
2013-08-23 14:34:00 -07:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
self.inherent_impls.borrow_mut().insert(type_id, Rc::new(inherent_impls));
|
|
|
|
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.
|
2015-08-16 06:32:28 -04: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);
|
|
|
|
}
|
|
|
|
|
2015-11-20 14:51:18 +02:00
|
|
|
for impl_def_id in self.sess.cstore.implementations_of_trait(trait_id) {
|
|
|
|
let impl_items = self.sess.cstore.impl_items(impl_def_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.
|
2015-12-22 10:20:47 -08:00
|
|
|
if let Some(parent) = self.sess.cstore.impl_parent(impl_def_id) {
|
|
|
|
def.record_remote_impl(self, impl_def_id, trait_ref, parent);
|
|
|
|
} else {
|
|
|
|
def.record_remote_impl(self, impl_def_id, trait_ref, trait_id);
|
|
|
|
}
|
2013-08-23 14:34:00 -07:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
// For any methods that use a default implementation, add them to
|
|
|
|
// the map. This is a bit unfortunate.
|
|
|
|
for impl_item_def_id in &impl_items {
|
|
|
|
let method_def_id = impl_item_def_id.def_id();
|
2015-09-30 22:28:27 +03:00
|
|
|
// load impl items eagerly for convenience
|
|
|
|
// FIXME: we may want to load these lazily
|
|
|
|
self.impl_or_trait_item(method_def_id);
|
2013-08-23 14:34:00 -07:00
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
// Store the implementation info.
|
|
|
|
self.impl_items.borrow_mut().insert(impl_def_id, impl_items);
|
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
|
|
|
|
2015-09-17 14:29:59 -04:00
|
|
|
pub fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind {
|
|
|
|
Tables::closure_kind(&self.tables, self, def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn closure_type(&self,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: &ClosureSubsts<'tcx>)
|
|
|
|
-> ty::ClosureTy<'tcx>
|
|
|
|
{
|
|
|
|
Tables::closure_type(&self.tables, self, def_id, substs)
|
|
|
|
}
|
|
|
|
|
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`.
|
2015-08-16 06:32:28 -04: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`.
|
2015-08-16 06:32:28 -04: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-02-23 22:04:27 +02:00
|
|
|
return self.sess.cstore.impl_or_trait_item(self, def_id).and_then(|item| {
|
|
|
|
match item.container() {
|
|
|
|
TraitContainer(_) => None,
|
|
|
|
ImplContainer(def_id) => Some(def_id),
|
|
|
|
}
|
|
|
|
});
|
2014-02-05 17:31:33 +13:00
|
|
|
}
|
2015-06-25 23:42:17 +03:00
|
|
|
match self.impl_or_trait_items.borrow().get(&def_id).cloned() {
|
|
|
|
Some(trait_item) => {
|
|
|
|
match trait_item.container() {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
/// If the given def ID describes an item belonging to a trait (either a
|
|
|
|
/// default method or an implementation of a trait method), return the ID of
|
|
|
|
/// the trait that the method belongs to. Otherwise, return `None`.
|
2015-08-16 06:32:28 -04: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 {
|
2015-11-20 14:51:18 +02:00
|
|
|
return self.sess.cstore.trait_of_item(self, def_id);
|
2013-12-15 21:52:48 -05:00
|
|
|
}
|
2015-06-25 23:42:17 +03:00
|
|
|
match self.impl_or_trait_items.borrow().get(&def_id).cloned() {
|
|
|
|
Some(impl_or_trait_item) => {
|
|
|
|
match impl_or_trait_item.container() {
|
|
|
|
TraitContainer(def_id) => Some(def_id),
|
|
|
|
ImplContainer(def_id) => self.trait_id_of_impl(def_id),
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If the given def ID describes an item belonging to a trait, (either a
|
|
|
|
/// default method or an implementation of a trait method), return the ID of
|
|
|
|
/// the method inside trait definition (this means that if the given def ID
|
|
|
|
/// is already that of the original trait method, then the return value is
|
|
|
|
/// the same).
|
|
|
|
/// Otherwise, return `None`.
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn trait_item_of_item(&self, def_id: DefId) -> Option<ImplOrTraitItemId> {
|
2015-06-25 23:42:17 +03:00
|
|
|
let impl_item = match self.impl_or_trait_items.borrow().get(&def_id) {
|
|
|
|
Some(m) => m.clone(),
|
|
|
|
None => return None,
|
2014-12-13 14:40:38 -08:00
|
|
|
};
|
2015-06-25 23:42:17 +03:00
|
|
|
let name = impl_item.name();
|
|
|
|
match self.trait_of_item(def_id) {
|
|
|
|
Some(trait_did) => {
|
2015-07-02 08:52:36 +03:00
|
|
|
self.trait_items(trait_did).iter()
|
|
|
|
.find(|item| item.name() == name)
|
|
|
|
.map(|item| item.id())
|
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.
|
2015-08-07 10:37:17 -04:00
|
|
|
pub fn empty_parameter_environment<'a>(&'a self)
|
|
|
|
-> ParameterEnvironment<'a,'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);
|
2015-06-25 23:42:17 +03:00
|
|
|
ty::ParameterEnvironment { tcx: self,
|
|
|
|
free_substs: Substs::empty(),
|
|
|
|
caller_bounds: Vec::new(),
|
|
|
|
implicit_region_bound: ty::ReEmpty,
|
2015-08-07 10:37:17 -04:00
|
|
|
selection_cache: traits::SelectionCache::new(),
|
2015-10-18 19:15:57 +03:00
|
|
|
evaluation_cache: traits::EvaluationCache::new(),
|
2015-12-08 23:38:36 +01:00
|
|
|
free_id_outlive: free_id_outlive }
|
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.
|
|
|
|
pub fn construct_free_substs(&self, generics: &Generics<'tcx>,
|
2015-12-08 23:38:36 +01:00
|
|
|
free_id_outlive: CodeExtent) -> Substs<'tcx> {
|
2015-06-25 23:42:17 +03:00
|
|
|
// map T => T
|
|
|
|
let mut types = VecPerParamSpace::empty();
|
|
|
|
for def in generics.types.as_slice() {
|
|
|
|
debug!("construct_parameter_environment(): push_types_from_defs: def={:?}",
|
|
|
|
def);
|
|
|
|
types.push(def.space, self.mk_param_from_def(def));
|
|
|
|
}
|
2013-10-29 06:03:32 -04:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
// map bound 'a => free 'a
|
|
|
|
let mut regions = VecPerParamSpace::empty();
|
|
|
|
for def in generics.regions.as_slice() {
|
2015-06-25 04:09:46 +03:00
|
|
|
let region =
|
2015-06-25 23:42:17 +03:00
|
|
|
ReFree(FreeRegion { scope: free_id_outlive,
|
2015-06-25 04:09:46 +03:00
|
|
|
bound_region: BrNamed(def.def_id, def.name) });
|
|
|
|
debug!("push_region_params {:?}", region);
|
|
|
|
regions.push(def.space, region);
|
2015-01-26 14:20:38 -05:00
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
Substs {
|
|
|
|
types: types,
|
2016-03-09 18:22:05 -05:00
|
|
|
regions: regions,
|
2015-06-25 23:42:17 +03:00
|
|
|
}
|
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)`
|
|
|
|
/// for the `free_id_outlive` parameter. (But note that that is not always quite right.)
|
2015-06-25 23:42:17 +03:00
|
|
|
pub fn construct_parameter_environment<'a>(&'a self,
|
|
|
|
span: Span,
|
|
|
|
generics: &ty::Generics<'tcx>,
|
|
|
|
generic_predicates: &ty::GenericPredicates<'tcx>,
|
2015-12-08 23:38:36 +01:00
|
|
|
free_id_outlive: CodeExtent)
|
2015-06-25 23:42:17 +03:00
|
|
|
-> ParameterEnvironment<'a, 'tcx>
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Construct the free substs.
|
|
|
|
//
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2015-12-08 23:38:36 +01:00
|
|
|
let free_substs = self.construct_free_substs(generics, 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
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
let bounds = generic_predicates.instantiate(self, &free_substs);
|
|
|
|
let bounds = self.liberate_late_bound_regions(free_id_outlive, &ty::Binder(bounds));
|
|
|
|
let predicates = bounds.predicates.into_vec();
|
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 {
|
|
|
|
tcx: self,
|
|
|
|
free_substs: free_substs,
|
2015-08-22 17:39:21 +03:00
|
|
|
implicit_region_bound: ty::ReScope(free_id_outlive),
|
2015-06-25 23:42:17 +03:00
|
|
|
caller_bounds: predicates,
|
|
|
|
selection_cache: traits::SelectionCache::new(),
|
2015-10-18 19:15:57 +03:00
|
|
|
evaluation_cache: traits::EvaluationCache::new(),
|
2015-12-08 23:38:36 +01:00
|
|
|
free_id_outlive: free_id_outlive,
|
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));
|
2015-06-25 23:42:17 +03:00
|
|
|
traits::normalize_param_env_or_error(unnormalized_env, cause)
|
2014-02-07 14:37:19 -05:00
|
|
|
}
|
2014-04-21 19:21:53 -04:00
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn is_method_call(&self, expr_id: NodeId) -> bool {
|
2015-06-24 13:40:54 -07:00
|
|
|
self.tables.borrow().method_map.contains_key(&MethodCall::expr(expr_id))
|
2014-04-21 19:21:53 -04:00
|
|
|
}
|
2015-01-24 15:54:52 -05:00
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn is_overloaded_autoderef(&self, expr_id: NodeId, autoderefs: u32) -> bool {
|
2015-07-07 18:45:21 +03:00
|
|
|
self.tables.borrow().method_map.contains_key(&MethodCall::autoderef(expr_id,
|
|
|
|
autoderefs))
|
|
|
|
}
|
|
|
|
|
2015-01-24 15:54:52 -05:00
|
|
|
pub fn upvar_capture(&self, upvar_id: ty::UpvarId) -> Option<ty::UpvarCapture> {
|
2015-06-24 13:40:54 -07:00
|
|
|
Some(self.tables.borrow().upvar_capture_map.get(&upvar_id).unwrap().clone())
|
2015-01-24 15:54:52 -05:00
|
|
|
}
|
2015-12-22 16:35:02 -05:00
|
|
|
|
|
|
|
pub fn visit_all_items_in_krate<V,F>(&self,
|
|
|
|
dep_node_fn: F,
|
|
|
|
visitor: &mut V)
|
2016-03-28 17:37:34 -04:00
|
|
|
where F: FnMut(DefId) -> DepNode<DefId>, V: Visitor<'tcx>
|
2015-12-22 16:35:02 -05:00
|
|
|
{
|
|
|
|
dep_graph::visit_all_items_in_krate(self, dep_node_fn, visitor);
|
|
|
|
}
|
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-02-12 12:43:13 -05: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
|
|
|
|
2014-05-06 16:37:32 -07:00
|
|
|
/// The category of explicit self.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
|
2014-05-06 16:37:32 -07:00
|
|
|
pub enum ExplicitSelfCategory {
|
2015-12-28 12:52:43 +01:00
|
|
|
Static,
|
|
|
|
ByValue,
|
|
|
|
ByReference(Region, hir::Mutability),
|
|
|
|
ByBox,
|
2014-05-06 16:37:32 -07:00
|
|
|
}
|
|
|
|
|
2016-02-29 23:36:51 +00:00
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
2015-07-31 00:04:06 -07: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
|
|
|
}
|