Skip to content

Commit

Permalink
rustc: use partially resolved definitions to replace the T::A hack.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Feb 24, 2015
1 parent 5809f8a commit 0f49254
Show file tree
Hide file tree
Showing 16 changed files with 376 additions and 375 deletions.
38 changes: 24 additions & 14 deletions src/librustc/middle/astconv_util.rs
Expand Up @@ -17,29 +17,47 @@
use middle::def;
use middle::ty::{self, Ty};
use syntax::ast;
use syntax::codemap::Span;
use util::ppaux::Repr;

pub const NO_REGIONS: uint = 1;
pub const NO_TPS: uint = 2;

pub fn check_path_args(tcx: &ty::ctxt,
path: &ast::Path,
span: Span,
segments: &[ast::PathSegment],
flags: uint) {
if (flags & NO_TPS) != 0 {
if path.segments.iter().any(|s| s.parameters.has_types()) {
span_err!(tcx.sess, path.span, E0109,
if segments.iter().any(|s| s.parameters.has_types()) {
span_err!(tcx.sess, span, E0109,
"type parameters are not allowed on this type");
}
}

if (flags & NO_REGIONS) != 0 {
if path.segments.iter().any(|s| s.parameters.has_lifetimes()) {
span_err!(tcx.sess, path.span, E0110,
if segments.iter().any(|s| s.parameters.has_lifetimes()) {
span_err!(tcx.sess, span, E0110,
"lifetime parameters are not allowed on this type");
}
}
}

pub fn prim_ty_to_ty<'tcx>(tcx: &ty::ctxt<'tcx>,
span: Span,
segments: &[ast::PathSegment],
nty: ast::PrimTy)
-> Ty<'tcx> {
check_path_args(tcx, span, segments, NO_TPS | NO_REGIONS);
match nty {
ast::TyBool => tcx.types.bool,
ast::TyChar => tcx.types.char,
ast::TyInt(it) => ty::mk_mach_int(tcx, it),
ast::TyUint(uit) => ty::mk_mach_uint(tcx, uit),
ast::TyFloat(ft) => ty::mk_mach_float(tcx, ft),
ast::TyStr => ty::mk_str(tcx)
}
}

pub fn ast_ty_to_prim_ty<'tcx>(tcx: &ty::ctxt<'tcx>, ast_ty: &ast::Ty)
-> Option<Ty<'tcx>> {
if let ast::TyPath(ref path) = ast_ty.node {
Expand All @@ -51,15 +69,7 @@ pub fn ast_ty_to_prim_ty<'tcx>(tcx: &ty::ctxt<'tcx>, ast_ty: &ast::Ty)
Some(&d) => d
};
if let def::DefPrimTy(nty) = def {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Some(match nty {
ast::TyBool => tcx.types.bool,
ast::TyChar => tcx.types.char,
ast::TyInt(it) => ty::mk_mach_int(tcx, it),
ast::TyUint(uit) => ty::mk_mach_uint(tcx, uit),
ast::TyFloat(ft) => ty::mk_mach_float(tcx, ft),
ast::TyStr => ty::mk_str(tcx)
})
Some(prim_ty_to_ty(tcx, path.span, &path.segments[], nty))
} else {
None
}
Expand Down
4 changes: 0 additions & 4 deletions src/librustc/middle/astencode.rs
Expand Up @@ -444,10 +444,6 @@ impl tr for def::Def {
def::DefTy(did, is_enum) => def::DefTy(did.tr(dcx), is_enum),
def::DefAssociatedTy(trait_did, did) =>
def::DefAssociatedTy(trait_did.tr(dcx), did.tr(dcx)),
def::DefAssociatedPath(def::TyParamProvenance::FromSelf(did), ident) =>
def::DefAssociatedPath(def::TyParamProvenance::FromSelf(did.tr(dcx)), ident),
def::DefAssociatedPath(def::TyParamProvenance::FromParam(did), ident) =>
def::DefAssociatedPath(def::TyParamProvenance::FromParam(did.tr(dcx)), ident),
def::DefPrimTy(p) => def::DefPrimTy(p),
def::DefTyParam(s, index, def_id, n) => def::DefTyParam(s, index, def_id.tr(dcx), n),
def::DefUse(did) => def::DefUse(did.tr(dcx)),
Expand Down
40 changes: 17 additions & 23 deletions src/librustc/middle/def.rs
Expand Up @@ -33,11 +33,6 @@ pub enum Def {
DefVariant(ast::DefId /* enum */, ast::DefId /* variant */, bool /* is_structure */),
DefTy(ast::DefId, bool /* is_enum */),
DefAssociatedTy(ast::DefId /* trait */, ast::DefId),
// A partially resolved path to an associated type `T::U` where `T` is a concrete
// type (indicated by the DefId) which implements a trait which has an associated
// type `U` (indicated by the Ident).
// FIXME(#20301) -- should use Name
DefAssociatedPath(TyParamProvenance, ast::Ident),
DefTrait(ast::DefId),
DefPrimTy(ast::PrimTy),
DefTyParam(ParamSpace, u32, ast::DefId, ast::Name),
Expand All @@ -59,8 +54,24 @@ pub enum Def {
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */, MethodProvenance),
}

/// The result of resolving the prefix of a path to a type:
///
/// module::Type::AssocA::AssocB::AssocC::MethodOrAssocType
/// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~
/// base_type extra_associated_types
///
/// <T as Trait>::AssocA::AssocB::AssocC::MethodOrAssocType
/// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~
/// base_type extra_associated_types
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct PartialDef {
pub base_type: Def,
pub extra_associated_types: u32,
}

// Definition mapping
pub type DefMap = RefCell<NodeMap<Def>>;
pub type PartialDefMap = RefCell<NodeMap<PartialDef>>;
// This is the replacement export map. It maps a module to all of the exports
// within.
pub type ExportMap = NodeMap<Vec<Export>>;
Expand All @@ -77,12 +88,6 @@ pub enum MethodProvenance {
FromImpl(ast::DefId),
}

#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum TyParamProvenance {
FromSelf(ast::DefId),
FromParam(ast::DefId),
}

impl MethodProvenance {
pub fn map<F>(self, f: F) -> MethodProvenance where
F: FnOnce(ast::DefId) -> ast::DefId,
Expand All @@ -94,15 +99,6 @@ impl MethodProvenance {
}
}

impl TyParamProvenance {
pub fn def_id(&self) -> ast::DefId {
match *self {
TyParamProvenance::FromSelf(ref did) => did.clone(),
TyParamProvenance::FromParam(ref did) => did.clone(),
}
}
}

#[derive(Clone, Copy, Eq, PartialEq)]
pub enum TraitItemKind {
NonstaticMethodTraitItemKind,
Expand Down Expand Up @@ -135,9 +131,7 @@ impl Def {
DefForeignMod(id) | DefStatic(id, _) |
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
DefMethod(id, _, _) | DefConst(id) |
DefAssociatedPath(TyParamProvenance::FromSelf(id), _) |
DefAssociatedPath(TyParamProvenance::FromParam(id), _) => {
DefMethod(id, _, _) | DefConst(id) => {
id
}
DefLocal(id) |
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/mem_categorization.rs
Expand Up @@ -582,7 +582,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
def::DefTrait(_) | def::DefTy(..) | def::DefPrimTy(_) |
def::DefTyParam(..) | def::DefRegion(_) |
def::DefLabel(_) | def::DefSelfTy(..) |
def::DefAssociatedTy(..) | def::DefAssociatedPath(..)=> {
def::DefAssociatedTy(..) => {
Ok(Rc::new(cmt_ {
id:id,
span:span,
Expand Down
9 changes: 6 additions & 3 deletions src/librustc/middle/ty.rs
Expand Up @@ -46,7 +46,7 @@ use metadata::csearch;
use middle;
use middle::check_const;
use middle::const_eval;
use middle::def::{self, DefMap, ExportMap};
use middle::def::{self, DefMap, ExportMap, PartialDefMap};
use middle::dependency_format;
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem};
use middle::lang_items::{FnOnceTraitLangItem, TyDescStructLangItem};
Expand Down Expand Up @@ -682,6 +682,7 @@ pub struct ctxt<'tcx> {

pub sess: Session,
pub def_map: DefMap,
pub partial_def_map: PartialDefMap,

pub named_region_map: resolve_lifetime::NamedRegionMap,

Expand Down Expand Up @@ -2423,7 +2424,8 @@ impl<'tcx> CommonTypes<'tcx> {

pub fn mk_ctxt<'tcx>(s: Session,
arenas: &'tcx CtxtArenas<'tcx>,
dm: DefMap,
def_map: DefMap,
partial_def_map: PartialDefMap,
named_region_map: resolve_lifetime::NamedRegionMap,
map: ast_map::Map<'tcx>,
freevars: RefCell<FreevarMap>,
Expand All @@ -2445,7 +2447,8 @@ pub fn mk_ctxt<'tcx>(s: Session,
item_variance_map: RefCell::new(DefIdMap()),
variance_computed: Cell::new(false),
sess: s,
def_map: dm,
def_map: def_map,
partial_def_map: partial_def_map,
region_maps: region_maps,
node_types: RefCell::new(FnvHashMap()),
item_substs: RefCell::new(NodeMap()),
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_driver/driver.rs
Expand Up @@ -567,6 +567,7 @@ pub fn phase_3_run_analysis_passes<'tcx>(sess: Session,

let resolve::CrateMap {
def_map,
partial_def_map,
freevars,
export_map,
trait_map,
Expand Down Expand Up @@ -607,6 +608,7 @@ pub fn phase_3_run_analysis_passes<'tcx>(sess: Session,
let ty_cx = ty::mk_ctxt(sess,
arenas,
def_map,
partial_def_map,
named_region_map,
ast_map,
freevars,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -940,7 +940,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
is_public,
DUMMY_SP)
}
DefTy(..) | DefAssociatedTy(..) | DefAssociatedPath(..) => {
DefTy(..) | DefAssociatedTy(..) => {
debug!("(building reduced graph for external \
crate) building type {}", final_ident);

Expand Down

0 comments on commit 0f49254

Please sign in to comment.