Skip to content

Commit 6162b14

Browse files
committed
Generate the NodeId for existential type in the AST
1 parent 24a41c1 commit 6162b14

File tree

9 files changed

+45
-31
lines changed

9 files changed

+45
-31
lines changed

src/librustc/hir/lowering.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,16 +1129,10 @@ impl<'a> LoweringContext<'a> {
11291129
}
11301130
hir::TyTraitObject(bounds, lifetime_bound)
11311131
}
1132-
TyKind::ImplTrait(ref bounds) => {
1132+
TyKind::ImplTrait(exist_ty_node_id, ref bounds) => {
11331133
let span = t.span;
11341134
match itctx {
11351135
ImplTraitContext::Existential(fn_def_id) => {
1136-
1137-
// We need to manually repeat the code of `next_id` because the lowering
1138-
// needs to happen while the owner_id is pointing to the item itself,
1139-
// because items are their own owners
1140-
let exist_ty_node_id = self.sess.next_node_id();
1141-
11421136
// Make sure we know that some funky desugaring has been going on here.
11431137
// This is a first: there is code in other places like for loop
11441138
// desugaring that explicitly states that we don't want to track that.
@@ -1321,18 +1315,18 @@ impl<'a> LoweringContext<'a> {
13211315

13221316
fn visit_ty(&mut self, t: &'v hir::Ty) {
13231317
match t.node {
1324-
// Don't collect elided lifetimes used inside of `fn()` syntax
1318+
// Don't collect elided lifetimes used inside of `fn()` syntax
13251319
hir::Ty_::TyBareFn(_) => {
1326-
let old_collect_elided_lifetimes = self.collect_elided_lifetimes;
1327-
self.collect_elided_lifetimes = false;
1320+
let old_collect_elided_lifetimes = self.collect_elided_lifetimes;
1321+
self.collect_elided_lifetimes = false;
13281322

1329-
// Record the "stack height" of `for<'a>` lifetime bindings
1330-
// to be able to later fully undo their introduction.
1331-
let old_len = self.currently_bound_lifetimes.len();
1332-
hir::intravisit::walk_ty(self, t);
1333-
self.currently_bound_lifetimes.truncate(old_len);
1323+
// Record the "stack height" of `for<'a>` lifetime bindings
1324+
// to be able to later fully undo their introduction.
1325+
let old_len = self.currently_bound_lifetimes.len();
1326+
hir::intravisit::walk_ty(self, t);
1327+
self.currently_bound_lifetimes.truncate(old_len);
13341328

1335-
self.collect_elided_lifetimes = old_collect_elided_lifetimes;
1329+
self.collect_elided_lifetimes = old_collect_elided_lifetimes;
13361330
},
13371331
_ => hir::intravisit::walk_ty(self, t),
13381332
}
@@ -2811,12 +2805,28 @@ impl<'a> LoweringContext<'a> {
28112805
ItemKind::Use(ref use_tree) => {
28122806
let mut vec = SmallVector::one(hir::ItemId { id: i.id });
28132807
self.lower_item_id_use_tree(use_tree, i.id, &mut vec);
2814-
return vec;
2808+
vec
28152809
}
2816-
ItemKind::MacroDef(..) => return SmallVector::new(),
2817-
_ => {}
2810+
ItemKind::MacroDef(..) => SmallVector::new(),
2811+
ItemKind::Fn(ref decl, ..) => {
2812+
struct IdVisitor { ids: SmallVector<hir::ItemId> }
2813+
impl<'a> Visitor<'a> for IdVisitor {
2814+
fn visit_ty(&mut self, ty: &'a Ty) {
2815+
if let TyKind::ImplTrait(id, _) = ty.node {
2816+
self.ids.push(hir::ItemId { id });
2817+
}
2818+
visit::walk_ty(self, ty);
2819+
}
2820+
}
2821+
let mut visitor = IdVisitor { ids: SmallVector::one(hir::ItemId { id: i.id }) };
2822+
match decl.output {
2823+
FunctionRetTy::Default(_) => {},
2824+
FunctionRetTy::Ty(ref ty) => visitor.visit_ty(ty),
2825+
}
2826+
visitor.ids
2827+
},
2828+
_ => SmallVector::one(hir::ItemId { id: i.id }),
28182829
}
2819-
SmallVector::one(hir::ItemId { id: i.id })
28202830
}
28212831

28222832
fn lower_item_id_use_tree(&mut self,

src/librustc_driver/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ impl<'a> ReplaceBodyWithLoop<'a> {
669669
if let ast::FunctionRetTy::Ty(ref ty) = ret_ty.output {
670670
fn involves_impl_trait(ty: &ast::Ty) -> bool {
671671
match ty.node {
672-
ast::TyKind::ImplTrait(_) => true,
672+
ast::TyKind::ImplTrait(..) => true,
673673
ast::TyKind::Slice(ref subty) |
674674
ast::TyKind::Array(ref subty, _) |
675675
ast::TyKind::Ptr(ast::MutTy { ty: ref subty, .. }) |

src/librustc_passes/ast_validation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
208208
}
209209
self.no_questions_in_bounds(bounds, "trait object types", false);
210210
}
211-
TyKind::ImplTrait(ref bounds) => {
211+
TyKind::ImplTrait(_, ref bounds) => {
212212
if !bounds.iter()
213213
.any(|b| if let TraitTyParamBound(..) = *b { true } else { false }) {
214214
self.err_handler().span_err(ty.span, "at least one trait must be specified");
@@ -507,7 +507,7 @@ impl<'a> NestedImplTraitVisitor<'a> {
507507

508508
impl<'a> Visitor<'a> for NestedImplTraitVisitor<'a> {
509509
fn visit_ty(&mut self, t: &'a Ty) {
510-
if let TyKind::ImplTrait(_) = t.node {
510+
if let TyKind::ImplTrait(..) = t.node {
511511
if let Some(outer_impl_trait) = self.outer_impl_trait {
512512
struct_span_err!(self.session, t.span, E0666,
513513
"nested `impl Trait` is not allowed")
@@ -571,7 +571,7 @@ impl<'a> ImplTraitProjectionVisitor<'a> {
571571
impl<'a> Visitor<'a> for ImplTraitProjectionVisitor<'a> {
572572
fn visit_ty(&mut self, t: &'a Ty) {
573573
match t.node {
574-
TyKind::ImplTrait(_) => {
574+
TyKind::ImplTrait(..) => {
575575
if self.is_banned {
576576
struct_span_err!(self.session, t.span, E0667,
577577
"`impl Trait` is not allowed in path parameters")

src/librustc_save_analysis/sig.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl Sig for ast::Ty {
306306
let nested = pprust::bounds_to_string(bounds);
307307
Ok(text_sig(nested))
308308
}
309-
ast::TyKind::ImplTrait(ref bounds) => {
309+
ast::TyKind::ImplTrait(_, ref bounds) => {
310310
// FIXME recurse into bounds
311311
let nested = pprust::bounds_to_string(bounds);
312312
Ok(text_sig(format!("impl {}", nested)))

src/libsyntax/ast.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,11 @@ pub enum TyKind {
15811581
TraitObject(TyParamBounds, TraitObjectSyntax),
15821582
/// An `impl Bound1 + Bound2 + Bound3` type
15831583
/// where `Bound` is a trait or a lifetime.
1584-
ImplTrait(TyParamBounds),
1584+
///
1585+
/// The `NodeId` exists to prevent lowering from having to
1586+
/// generate `NodeId`s on the fly, which would complicate
1587+
/// the generation of `existential type` items significantly
1588+
ImplTrait(NodeId, TyParamBounds),
15851589
/// No-op; kept solely so that we can pretty-print faithfully
15861590
Paren(P<Ty>),
15871591
/// Unused for now

src/libsyntax/fold.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
387387
TyKind::TraitObject(bounds, syntax) => {
388388
TyKind::TraitObject(bounds.move_map(|b| fld.fold_ty_param_bound(b)), syntax)
389389
}
390-
TyKind::ImplTrait(bounds) => {
391-
TyKind::ImplTrait(bounds.move_map(|b| fld.fold_ty_param_bound(b)))
390+
TyKind::ImplTrait(id, bounds) => {
391+
TyKind::ImplTrait(fld.new_id(id), bounds.move_map(|b| fld.fold_ty_param_bound(b)))
392392
}
393393
TyKind::Mac(mac) => {
394394
TyKind::Mac(fld.fold_mac(mac))

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@ impl<'a> Parser<'a> {
15131513
// Always parse bounds greedily for better error recovery.
15141514
let bounds = self.parse_ty_param_bounds()?;
15151515
impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
1516-
TyKind::ImplTrait(bounds)
1516+
TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds)
15171517
} else if self.check_keyword(keywords::Dyn) &&
15181518
self.look_ahead(1, |t| t.can_begin_bound() &&
15191519
!can_continue_type_after_non_fn_ident(t)) {

src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ impl<'a> State<'a> {
10731073
let prefix = if syntax == ast::TraitObjectSyntax::Dyn { "dyn" } else { "" };
10741074
self.print_bounds(prefix, &bounds[..])?;
10751075
}
1076-
ast::TyKind::ImplTrait(ref bounds) => {
1076+
ast::TyKind::ImplTrait(_, ref bounds) => {
10771077
self.print_bounds("impl", &bounds[..])?;
10781078
}
10791079
ast::TyKind::Array(ref ty, ref length) => {

src/libsyntax/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
332332
visitor.visit_anon_const(length)
333333
}
334334
TyKind::TraitObject(ref bounds, ..) |
335-
TyKind::ImplTrait(ref bounds) => {
335+
TyKind::ImplTrait(_, ref bounds) => {
336336
walk_list!(visitor, visit_ty_param_bound, bounds);
337337
}
338338
TyKind::Typeof(ref expression) => {

0 commit comments

Comments
 (0)