Skip to content

Commit 41553d6

Browse files
committed
rustc: lower trait type paths as TyTraitObject.
1 parent 9783947 commit 41553d6

File tree

5 files changed

+42
-60
lines changed

5 files changed

+42
-60
lines changed

src/librustc/hir/lowering.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ impl<'a> LoweringContext<'a> {
337337
return self.lower_ty(ty);
338338
}
339339
TyKind::Path(ref qself, ref path) => {
340-
hir::TyPath(self.lower_qpath(t.id, qself, path, ParamMode::Explicit))
340+
let qpath = self.lower_qpath(t.id, qself, path, ParamMode::Explicit);
341+
return self.ty_path(t.id, t.span, qpath);
341342
}
342343
TyKind::ImplicitSelf => {
343344
hir::TyPath(hir::QPath::Resolved(None, P(hir::Path {
@@ -470,7 +471,8 @@ impl<'a> LoweringContext<'a> {
470471
// Otherwise, the base path is an implicit `Self` type path,
471472
// e.g. `Vec` in `Vec::new` or `<I as Iterator>::Item` in
472473
// `<I as Iterator>::Item::default`.
473-
self.ty(p.span, hir::TyPath(hir::QPath::Resolved(qself, path)))
474+
let new_id = self.next_id();
475+
self.ty_path(new_id, p.span, hir::QPath::Resolved(qself, path))
474476
};
475477

476478
// Anything after the base path are associated "extensions",
@@ -493,7 +495,8 @@ impl<'a> LoweringContext<'a> {
493495
}
494496

495497
// Wrap the associated extension in another type node.
496-
ty = self.ty(p.span, hir::TyPath(qpath));
498+
let new_id = self.next_id();
499+
ty = self.ty_path(new_id, p.span, qpath);
497500
}
498501

499502
// Should've returned in the for loop above.
@@ -2352,12 +2355,33 @@ impl<'a> LoweringContext<'a> {
23522355
self.expr_block(block, attrs)
23532356
}
23542357

2355-
fn ty(&mut self, span: Span, node: hir::Ty_) -> P<hir::Ty> {
2356-
P(hir::Ty {
2357-
id: self.next_id(),
2358-
node: node,
2359-
span: span,
2360-
})
2358+
fn ty_path(&mut self, id: NodeId, span: Span, qpath: hir::QPath) -> P<hir::Ty> {
2359+
let mut id = id;
2360+
let node = match qpath {
2361+
hir::QPath::Resolved(None, path) => {
2362+
// Turn trait object paths into `TyTraitObject` instead.
2363+
if let Def::Trait(_) = path.def {
2364+
let principal = hir::TraitTyParamBound(hir::PolyTraitRef {
2365+
bound_lifetimes: hir_vec![],
2366+
trait_ref: hir::TraitRef {
2367+
path: path.and_then(|path| path),
2368+
ref_id: id,
2369+
},
2370+
span,
2371+
}, hir::TraitBoundModifier::None);
2372+
2373+
// The original ID is taken by the `PolyTraitRef`,
2374+
// so the `Ty` itself needs a different one.
2375+
id = self.next_id();
2376+
2377+
hir::TyTraitObject(hir_vec![principal])
2378+
} else {
2379+
hir::TyPath(hir::QPath::Resolved(None, path))
2380+
}
2381+
}
2382+
_ => hir::TyPath(qpath)
2383+
};
2384+
P(hir::Ty { id, node, span })
23612385
}
23622386

23632387
fn elided_lifetime(&mut self, span: Span) -> hir::Lifetime {

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![feature(conservative_impl_trait)]
3030
#![feature(const_fn)]
3131
#![feature(core_intrinsics)]
32+
#![feature(field_init_shorthand)]
3233
#![feature(libc)]
3334
#![feature(loop_break_value)]
3435
#![feature(nonzero)]

src/librustc/middle/resolve_lifetime.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -322,24 +322,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
322322
intravisit::walk_ty(this, ty);
323323
});
324324
}
325-
hir::TyPath(hir::QPath::Resolved(None, ref path)) => {
326-
// if this path references a trait, then this will resolve to
327-
// a trait ref, which introduces a binding scope.
328-
match path.def {
329-
Def::Trait(..) => {
330-
let scope = Scope::Binder {
331-
lifetimes: FxHashMap(),
332-
s: self.scope
333-
};
334-
self.with(scope, |_, this| {
335-
this.visit_path(path, ty.id);
336-
});
337-
}
338-
_ => {
339-
intravisit::walk_ty(self, ty);
340-
}
341-
}
342-
}
343325
_ => {
344326
intravisit::walk_ty(self, ty)
345327
}
@@ -889,7 +871,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
889871
Def::Struct(_) |
890872
Def::Union(_) |
891873
Def::Enum(_) |
892-
Def::Trait(_) |
893874
Def::PrimTy(_) => return def == path.def,
894875
_ => {}
895876
}
@@ -970,21 +951,13 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
970951
}
971952

972953
fn visit_ty(&mut self, ty: &hir::Ty) {
973-
let delta = match ty.node {
974-
hir::TyBareFn(_) => 1,
975-
hir::TyPath(hir::QPath::Resolved(None, ref path)) => {
976-
// if this path references a trait, then this will resolve to
977-
// a trait ref, which introduces a binding scope.
978-
match path.def {
979-
Def::Trait(..) => 1,
980-
_ => 0
981-
}
982-
}
983-
_ => 0
984-
};
985-
self.binder_depth += delta;
954+
if let hir::TyBareFn(_) = ty.node {
955+
self.binder_depth += 1;
956+
}
986957
intravisit::walk_ty(self, ty);
987-
self.binder_depth -= delta;
958+
if let hir::TyBareFn(_) = ty.node {
959+
self.binder_depth -= 1;
960+
}
988961
}
989962

990963
fn visit_poly_trait_ref(&mut self,

src/librustc_typeck/astconv.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,6 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
11511151
rscope: &RegionScope,
11521152
opt_self_ty: Option<Ty<'tcx>>,
11531153
path: &hir::Path,
1154-
path_id: ast::NodeId,
11551154
permit_variants: bool)
11561155
-> Ty<'tcx> {
11571156
let tcx = self.tcx();
@@ -1161,21 +1160,6 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
11611160

11621161
let span = path.span;
11631162
match path.def {
1164-
Def::Trait(trait_def_id) => {
1165-
// N.B. this case overlaps somewhat with
1166-
// TyTraitObject, see that fn for details
1167-
1168-
assert_eq!(opt_self_ty, None);
1169-
tcx.prohibit_type_params(path.segments.split_last().unwrap().1);
1170-
1171-
self.trait_path_to_object_type(rscope,
1172-
span,
1173-
trait_def_id,
1174-
path_id,
1175-
path.segments.last().unwrap(),
1176-
span,
1177-
partition_bounds(&[]))
1178-
}
11791163
Def::Enum(did) | Def::TyAlias(did) | Def::Struct(did) | Def::Union(did) => {
11801164
assert_eq!(opt_self_ty, None);
11811165
tcx.prohibit_type_params(path.segments.split_last().unwrap().1);
@@ -1421,7 +1405,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
14211405
let opt_self_ty = maybe_qself.as_ref().map(|qself| {
14221406
self.ast_ty_to_ty(rscope, qself)
14231407
});
1424-
self.def_to_ty(rscope, opt_self_ty, path, ast_ty.id, false)
1408+
self.def_to_ty(rscope, opt_self_ty, path, false)
14251409
}
14261410
hir::TyPath(hir::QPath::TypeRelative(ref qself, ref segment)) => {
14271411
debug!("ast_ty_to_ty: qself={:?} segment={:?}", qself, segment);

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3976,7 +3976,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
39763976
match *qpath {
39773977
hir::QPath::Resolved(ref maybe_qself, ref path) => {
39783978
let opt_self_ty = maybe_qself.as_ref().map(|qself| self.to_ty(qself));
3979-
let ty = AstConv::def_to_ty(self, self, opt_self_ty, path, node_id, true);
3979+
let ty = AstConv::def_to_ty(self, self, opt_self_ty, path, true);
39803980
(path.def, ty)
39813981
}
39823982
hir::QPath::TypeRelative(ref qself, ref segment) => {

0 commit comments

Comments
 (0)