Skip to content

Commit a13c70c

Browse files
committed
rustc: use partially resolved definitions to replace the T::A hack.
1 parent 09253e2 commit a13c70c

File tree

16 files changed

+372
-371
lines changed

16 files changed

+372
-371
lines changed

src/librustc/middle/astconv_util.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,47 @@
1717
use middle::def;
1818
use middle::ty::{self, Ty};
1919
use syntax::ast;
20+
use syntax::codemap::Span;
2021
use util::ppaux::Repr;
2122

2223
pub const NO_REGIONS: uint = 1;
2324
pub const NO_TPS: uint = 2;
2425

2526
pub fn check_path_args(tcx: &ty::ctxt,
26-
path: &ast::Path,
27+
span: Span,
28+
segments: &[ast::PathSegment],
2729
flags: uint) {
2830
if (flags & NO_TPS) != 0 {
29-
if path.segments.iter().any(|s| s.parameters.has_types()) {
30-
span_err!(tcx.sess, path.span, E0109,
31+
if segments.iter().any(|s| s.parameters.has_types()) {
32+
span_err!(tcx.sess, span, E0109,
3133
"type parameters are not allowed on this type");
3234
}
3335
}
3436

3537
if (flags & NO_REGIONS) != 0 {
36-
if path.segments.iter().any(|s| s.parameters.has_lifetimes()) {
37-
span_err!(tcx.sess, path.span, E0110,
38+
if segments.iter().any(|s| s.parameters.has_lifetimes()) {
39+
span_err!(tcx.sess, span, E0110,
3840
"lifetime parameters are not allowed on this type");
3941
}
4042
}
4143
}
4244

45+
pub fn prim_ty_to_ty<'tcx>(tcx: &ty::ctxt<'tcx>,
46+
span: Span,
47+
segments: &[ast::PathSegment],
48+
nty: ast::PrimTy)
49+
-> Ty<'tcx> {
50+
check_path_args(tcx, span, segments, NO_TPS | NO_REGIONS);
51+
match nty {
52+
ast::TyBool => tcx.types.bool,
53+
ast::TyChar => tcx.types.char,
54+
ast::TyInt(it) => ty::mk_mach_int(tcx, it),
55+
ast::TyUint(uit) => ty::mk_mach_uint(tcx, uit),
56+
ast::TyFloat(ft) => ty::mk_mach_float(tcx, ft),
57+
ast::TyStr => ty::mk_str(tcx)
58+
}
59+
}
60+
4361
pub fn ast_ty_to_prim_ty<'tcx>(tcx: &ty::ctxt<'tcx>, ast_ty: &ast::Ty)
4462
-> Option<Ty<'tcx>> {
4563
if let ast::TyPath(ref path) = ast_ty.node {
@@ -51,15 +69,7 @@ pub fn ast_ty_to_prim_ty<'tcx>(tcx: &ty::ctxt<'tcx>, ast_ty: &ast::Ty)
5169
Some(&d) => d
5270
};
5371
if let def::DefPrimTy(nty) = def {
54-
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
55-
Some(match nty {
56-
ast::TyBool => tcx.types.bool,
57-
ast::TyChar => tcx.types.char,
58-
ast::TyInt(it) => ty::mk_mach_int(tcx, it),
59-
ast::TyUint(uit) => ty::mk_mach_uint(tcx, uit),
60-
ast::TyFloat(ft) => ty::mk_mach_float(tcx, ft),
61-
ast::TyStr => ty::mk_str(tcx)
62-
})
72+
Some(prim_ty_to_ty(tcx, path.span, &path.segments[], nty))
6373
} else {
6474
None
6575
}

src/librustc/middle/astencode.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,6 @@ impl tr for def::Def {
442442
def::DefTy(did, is_enum) => def::DefTy(did.tr(dcx), is_enum),
443443
def::DefAssociatedTy(trait_did, did) =>
444444
def::DefAssociatedTy(trait_did.tr(dcx), did.tr(dcx)),
445-
def::DefAssociatedPath(def::TyParamProvenance::FromSelf(did), ident) =>
446-
def::DefAssociatedPath(def::TyParamProvenance::FromSelf(did.tr(dcx)), ident),
447-
def::DefAssociatedPath(def::TyParamProvenance::FromParam(did), ident) =>
448-
def::DefAssociatedPath(def::TyParamProvenance::FromParam(did.tr(dcx)), ident),
449445
def::DefPrimTy(p) => def::DefPrimTy(p),
450446
def::DefTyParam(s, index, def_id, n) => def::DefTyParam(s, index, def_id.tr(dcx), n),
451447
def::DefUse(did) => def::DefUse(did.tr(dcx)),

src/librustc/middle/def.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ pub enum Def {
3333
DefVariant(ast::DefId /* enum */, ast::DefId /* variant */, bool /* is_structure */),
3434
DefTy(ast::DefId, bool /* is_enum */),
3535
DefAssociatedTy(ast::DefId /* trait */, ast::DefId),
36-
// A partially resolved path to an associated type `T::U` where `T` is a concrete
37-
// type (indicated by the DefId) which implements a trait which has an associated
38-
// type `U` (indicated by the Ident).
39-
// FIXME(#20301) -- should use Name
40-
DefAssociatedPath(TyParamProvenance, ast::Ident),
4136
DefTrait(ast::DefId),
4237
DefPrimTy(ast::PrimTy),
4338
DefTyParam(ParamSpace, u32, ast::DefId, ast::Name),
@@ -59,8 +54,24 @@ pub enum Def {
5954
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */, MethodProvenance),
6055
}
6156

57+
/// The result of resolving the prefix of a path to a type:
58+
///
59+
/// module::Type::AssocA::AssocB::AssocC::MethodOrAssocType
60+
/// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~
61+
/// base_type extra_associated_types
62+
///
63+
/// <T as Trait>::AssocA::AssocB::AssocC::MethodOrAssocType
64+
/// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~
65+
/// base_type extra_associated_types
66+
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
67+
pub struct PartialDef {
68+
pub base_type: Def,
69+
pub extra_associated_types: u32,
70+
}
71+
6272
// Definition mapping
6373
pub type DefMap = RefCell<NodeMap<Def>>;
74+
pub type PartialDefMap = RefCell<NodeMap<PartialDef>>;
6475
// This is the replacement export map. It maps a module to all of the exports
6576
// within.
6677
pub type ExportMap = NodeMap<Vec<Export>>;
@@ -77,12 +88,6 @@ pub enum MethodProvenance {
7788
FromImpl(ast::DefId),
7889
}
7990

80-
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
81-
pub enum TyParamProvenance {
82-
FromSelf(ast::DefId),
83-
FromParam(ast::DefId),
84-
}
85-
8691
impl MethodProvenance {
8792
pub fn map<F>(self, f: F) -> MethodProvenance where
8893
F: FnOnce(ast::DefId) -> ast::DefId,
@@ -94,15 +99,6 @@ impl MethodProvenance {
9499
}
95100
}
96101

97-
impl TyParamProvenance {
98-
pub fn def_id(&self) -> ast::DefId {
99-
match *self {
100-
TyParamProvenance::FromSelf(ref did) => did.clone(),
101-
TyParamProvenance::FromParam(ref did) => did.clone(),
102-
}
103-
}
104-
}
105-
106102
#[derive(Clone, Copy, Eq, PartialEq)]
107103
pub enum TraitItemKind {
108104
NonstaticMethodTraitItemKind,
@@ -135,9 +131,7 @@ impl Def {
135131
DefForeignMod(id) | DefStatic(id, _) |
136132
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
137133
DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
138-
DefMethod(id, _, _) | DefConst(id) |
139-
DefAssociatedPath(TyParamProvenance::FromSelf(id), _) |
140-
DefAssociatedPath(TyParamProvenance::FromParam(id), _) => {
134+
DefMethod(id, _, _) | DefConst(id) => {
141135
id
142136
}
143137
DefLocal(id) |

src/librustc/middle/mem_categorization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
582582
def::DefTrait(_) | def::DefTy(..) | def::DefPrimTy(_) |
583583
def::DefTyParam(..) | def::DefRegion(_) |
584584
def::DefLabel(_) | def::DefSelfTy(..) |
585-
def::DefAssociatedTy(..) | def::DefAssociatedPath(..)=> {
585+
def::DefAssociatedTy(..) => {
586586
Ok(Rc::new(cmt_ {
587587
id:id,
588588
span:span,

src/librustc/middle/ty.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use lint;
4545
use metadata::csearch;
4646
use middle;
4747
use middle::const_eval;
48-
use middle::def::{self, DefMap, ExportMap};
48+
use middle::def::{self, DefMap, ExportMap, PartialDefMap};
4949
use middle::dependency_format;
5050
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem};
5151
use middle::lang_items::{FnOnceTraitLangItem, TyDescStructLangItem};
@@ -678,6 +678,7 @@ pub struct ctxt<'tcx> {
678678

679679
pub sess: Session,
680680
pub def_map: DefMap,
681+
pub partial_def_map: PartialDefMap,
681682

682683
pub named_region_map: resolve_lifetime::NamedRegionMap,
683684

@@ -2332,7 +2333,8 @@ impl<'tcx> CommonTypes<'tcx> {
23322333

23332334
pub fn mk_ctxt<'tcx>(s: Session,
23342335
arenas: &'tcx CtxtArenas<'tcx>,
2335-
dm: DefMap,
2336+
def_map: DefMap,
2337+
partial_def_map: PartialDefMap,
23362338
named_region_map: resolve_lifetime::NamedRegionMap,
23372339
map: ast_map::Map<'tcx>,
23382340
freevars: RefCell<FreevarMap>,
@@ -2354,7 +2356,8 @@ pub fn mk_ctxt<'tcx>(s: Session,
23542356
item_variance_map: RefCell::new(DefIdMap()),
23552357
variance_computed: Cell::new(false),
23562358
sess: s,
2357-
def_map: dm,
2359+
def_map: def_map,
2360+
partial_def_map: partial_def_map,
23582361
region_maps: region_maps,
23592362
node_types: RefCell::new(FnvHashMap()),
23602363
item_substs: RefCell::new(NodeMap()),

src/librustc_driver/driver.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ pub fn phase_3_run_analysis_passes<'tcx>(sess: Session,
561561

562562
let resolve::CrateMap {
563563
def_map,
564+
partial_def_map,
564565
freevars,
565566
export_map,
566567
trait_map,
@@ -601,6 +602,7 @@ pub fn phase_3_run_analysis_passes<'tcx>(sess: Session,
601602
let ty_cx = ty::mk_ctxt(sess,
602603
arenas,
603604
def_map,
605+
partial_def_map,
604606
named_region_map,
605607
ast_map,
606608
freevars,

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
939939
is_public,
940940
DUMMY_SP)
941941
}
942-
DefTy(..) | DefAssociatedTy(..) | DefAssociatedPath(..) => {
942+
DefTy(..) | DefAssociatedTy(..) => {
943943
debug!("(building reduced graph for external \
944944
crate) building type {}", final_ident);
945945

0 commit comments

Comments
 (0)