Skip to content

Commit b2e2c32

Browse files
committed
Generate the NodeId for existential type in the AST
1 parent bdcace0 commit b2e2c32

File tree

9 files changed

+49
-31
lines changed

9 files changed

+49
-31
lines changed

src/librustc/hir/lowering.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,12 +1167,14 @@ impl<'a> LoweringContext<'a> {
11671167
}
11681168
hir::TyTraitObject(bounds, lifetime_bound)
11691169
}
1170-
TyKind::ImplTrait(ref bounds) => {
1170+
TyKind::ImplTrait(exist_ty_node_id, ref bounds) => {
11711171
let span = t.span;
11721172
match itctx {
11731173
ImplTraitContext::Existential(fn_def_id) => {
11741174
self.lower_existential_impl_trait(
1175-
span, fn_def_id, |this| this.lower_param_bounds(bounds, itctx))
1175+
span, fn_def_id, exist_ty_node_id,
1176+
|this| this.lower_param_bounds(bounds, itctx),
1177+
)
11761178
}
11771179
ImplTraitContext::Universal(def_id) => {
11781180
let def_node_id = self.next_id().node_id;
@@ -1240,13 +1242,9 @@ impl<'a> LoweringContext<'a> {
12401242
&mut self,
12411243
span: Span,
12421244
fn_def_id: DefId,
1245+
exist_ty_node_id: NodeId,
12431246
lower_bounds: impl FnOnce(&mut LoweringContext) -> hir::GenericBounds,
12441247
) -> hir::Ty_ {
1245-
// We need to manually repeat the code of `next_id` because the lowering
1246-
// needs to happen while the owner_id is pointing to the item itself,
1247-
// because items are their own owners
1248-
let exist_ty_node_id = self.sess.next_node_id();
1249-
12501248
// Make sure we know that some funky desugaring has been going on here.
12511249
// This is a first: there is code in other places like for loop
12521250
// desugaring that explicitly states that we don't want to track that.
@@ -1365,18 +1363,18 @@ impl<'a> LoweringContext<'a> {
13651363

13661364
fn visit_ty(&mut self, t: &'v hir::Ty) {
13671365
match t.node {
1368-
// Don't collect elided lifetimes used inside of `fn()` syntax
1366+
// Don't collect elided lifetimes used inside of `fn()` syntax
13691367
hir::Ty_::TyBareFn(_) => {
1370-
let old_collect_elided_lifetimes = self.collect_elided_lifetimes;
1371-
self.collect_elided_lifetimes = false;
1368+
let old_collect_elided_lifetimes = self.collect_elided_lifetimes;
1369+
self.collect_elided_lifetimes = false;
13721370

1373-
// Record the "stack height" of `for<'a>` lifetime bindings
1374-
// to be able to later fully undo their introduction.
1375-
let old_len = self.currently_bound_lifetimes.len();
1376-
hir::intravisit::walk_ty(self, t);
1377-
self.currently_bound_lifetimes.truncate(old_len);
1371+
// Record the "stack height" of `for<'a>` lifetime bindings
1372+
// to be able to later fully undo their introduction.
1373+
let old_len = self.currently_bound_lifetimes.len();
1374+
hir::intravisit::walk_ty(self, t);
1375+
self.currently_bound_lifetimes.truncate(old_len);
13781376

1379-
self.collect_elided_lifetimes = old_collect_elided_lifetimes;
1377+
self.collect_elided_lifetimes = old_collect_elided_lifetimes;
13801378
},
13811379
_ => hir::intravisit::walk_ty(self, t),
13821380
}
@@ -3093,12 +3091,28 @@ impl<'a> LoweringContext<'a> {
30933091
ItemKind::Use(ref use_tree) => {
30943092
let mut vec = SmallVector::one(hir::ItemId { id: i.id });
30953093
self.lower_item_id_use_tree(use_tree, i.id, &mut vec);
3096-
return vec;
3094+
vec
30973095
}
3098-
ItemKind::MacroDef(..) => return SmallVector::new(),
3099-
_ => {}
3096+
ItemKind::MacroDef(..) => SmallVector::new(),
3097+
ItemKind::Fn(ref decl, ..) => {
3098+
struct IdVisitor { ids: SmallVector<hir::ItemId> }
3099+
impl<'a> Visitor<'a> for IdVisitor {
3100+
fn visit_ty(&mut self, ty: &'a Ty) {
3101+
if let TyKind::ImplTrait(id, _) = ty.node {
3102+
self.ids.push(hir::ItemId { id });
3103+
}
3104+
visit::walk_ty(self, ty);
3105+
}
3106+
}
3107+
let mut visitor = IdVisitor { ids: SmallVector::one(hir::ItemId { id: i.id }) };
3108+
match decl.output {
3109+
FunctionRetTy::Default(_) => {},
3110+
FunctionRetTy::Ty(ref ty) => visitor.visit_ty(ty),
3111+
}
3112+
visitor.ids
3113+
},
3114+
_ => SmallVector::one(hir::ItemId { id: i.id }),
31003115
}
3101-
SmallVector::one(hir::ItemId { id: i.id })
31023116
}
31033117

31043118
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 GenericBound::Trait(..) = *b { true } else { false }) {
214214
self.err_handler().span_err(ty.span, "at least one trait must be specified");
@@ -505,7 +505,7 @@ impl<'a> NestedImplTraitVisitor<'a> {
505505

506506
impl<'a> Visitor<'a> for NestedImplTraitVisitor<'a> {
507507
fn visit_ty(&mut self, t: &'a Ty) {
508-
if let TyKind::ImplTrait(_) = t.node {
508+
if let TyKind::ImplTrait(..) = t.node {
509509
if let Some(outer_impl_trait) = self.outer_impl_trait {
510510
struct_span_err!(self.session, t.span, E0666,
511511
"nested `impl Trait` is not allowed")
@@ -570,7 +570,7 @@ impl<'a> ImplTraitProjectionVisitor<'a> {
570570
impl<'a> Visitor<'a> for ImplTraitProjectionVisitor<'a> {
571571
fn visit_ty(&mut self, t: &'a Ty) {
572572
match t.node {
573-
TyKind::ImplTrait(_) => {
573+
TyKind::ImplTrait(..) => {
574574
if self.is_banned {
575575
struct_span_err!(self.session, t.span, E0667,
576576
"`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
@@ -1547,7 +1547,11 @@ pub enum TyKind {
15471547
TraitObject(GenericBounds, TraitObjectSyntax),
15481548
/// An `impl Bound1 + Bound2 + Bound3` type
15491549
/// where `Bound` is a trait or a lifetime.
1550-
ImplTrait(GenericBounds),
1550+
///
1551+
/// The `NodeId` exists to prevent lowering from having to
1552+
/// generate `NodeId`s on the fly, which would complicate
1553+
/// the generation of `existential type` items significantly
1554+
ImplTrait(NodeId, GenericBounds),
15511555
/// No-op; kept solely so that we can pretty-print faithfully
15521556
Paren(P<Ty>),
15531557
/// Unused for now

src/libsyntax/fold.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
396396
TyKind::TraitObject(bounds, syntax) => {
397397
TyKind::TraitObject(bounds.move_map(|b| fld.fold_param_bound(b)), syntax)
398398
}
399-
TyKind::ImplTrait(bounds) => {
400-
TyKind::ImplTrait(bounds.move_map(|b| fld.fold_param_bound(b)))
399+
TyKind::ImplTrait(id, bounds) => {
400+
TyKind::ImplTrait(fld.new_id(id), bounds.move_map(|b| fld.fold_param_bound(b)))
401401
}
402402
TyKind::Mac(mac) => {
403403
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
@@ -1537,7 +1537,7 @@ impl<'a> Parser<'a> {
15371537
// Always parse bounds greedily for better error recovery.
15381538
let bounds = self.parse_generic_bounds()?;
15391539
impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
1540-
TyKind::ImplTrait(bounds)
1540+
TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds)
15411541
} else if self.check_keyword(keywords::Dyn) &&
15421542
self.look_ahead(1, |t| t.can_begin_bound() &&
15431543
!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
@@ -1078,7 +1078,7 @@ impl<'a> State<'a> {
10781078
let prefix = if syntax == ast::TraitObjectSyntax::Dyn { "dyn" } else { "" };
10791079
self.print_type_bounds(prefix, &bounds[..])?;
10801080
}
1081-
ast::TyKind::ImplTrait(ref bounds) => {
1081+
ast::TyKind::ImplTrait(_, ref bounds) => {
10821082
self.print_type_bounds("impl", &bounds[..])?;
10831083
}
10841084
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
@@ -338,7 +338,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
338338
visitor.visit_anon_const(length)
339339
}
340340
TyKind::TraitObject(ref bounds, ..) |
341-
TyKind::ImplTrait(ref bounds) => {
341+
TyKind::ImplTrait(_, ref bounds) => {
342342
walk_list!(visitor, visit_param_bound, bounds);
343343
}
344344
TyKind::Typeof(ref expression) => {

0 commit comments

Comments
 (0)