Skip to content

Commit 5c3d1e5

Browse files
davidtwcopetrochenkov
authored andcommitted
Separate variant id and variant constructor id.
This commit makes two changes - separating the `NodeId` that identifies an enum variant from the `NodeId` that identifies the variant's constructor; and no longer creating a `NodeId` for `Struct`-style enum variants and structs. Separation of the variant id and variant constructor id will allow the rest of RFC 2008 to be implemented by lowering the visibility of the variant's constructor without lowering the visbility of the variant itself. No longer creating a `NodeId` for `Struct`-style enum variants and structs mostly simplifies logic as previously this `NodeId` wasn't used. There were various cases where the `NodeId` wouldn't be used unless there was an unit or tuple struct or enum variant but not all uses of this `NodeId` had that condition, by removing this `NodeId`, this must be explicitly dealt with. This change mostly applied cleanly, but there were one or two cases in name resolution and one case in type check where the existing logic required a id for `Struct`-style enum variants and structs.
1 parent fb5ed48 commit 5c3d1e5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+818
-520
lines changed

src/librustc/hir/def.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ pub enum NonMacroAttrKind {
3737
pub enum Def {
3838
// Type namespace
3939
Mod(DefId),
40-
Struct(DefId), // `DefId` refers to `NodeId` of the struct itself
40+
/// `DefId` refers to `NodeId` of the struct. `Def::VariantCtor` represents the constructor of
41+
/// a struct.
42+
Struct(DefId),
4143
Union(DefId),
4244
Enum(DefId),
45+
/// `DefId` refers to the `NodeId` of the variant. `Def::VariantCtor` represents the
46+
/// constructor of an enum variant.
4347
Variant(DefId),
4448
Trait(DefId),
4549
/// `existential type Foo: Bar;`
@@ -61,8 +65,8 @@ pub enum Def {
6165
Const(DefId),
6266
ConstParam(DefId),
6367
Static(DefId, bool /* is_mutbl */),
64-
StructCtor(DefId, CtorKind), // `DefId` refers to `NodeId` of the struct's constructor
65-
VariantCtor(DefId, CtorKind), // `DefId` refers to the enum variant
68+
/// `DefId` refers to `NodeId` of the struct or enum variant's constructor.
69+
Ctor(hir::CtorOf, DefId, CtorKind),
6670
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
6771
Method(DefId),
6872
AssociatedConst(DefId),
@@ -265,10 +269,9 @@ impl Def {
265269
pub fn opt_def_id(&self) -> Option<DefId> {
266270
match *self {
267271
Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
268-
Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) |
272+
Def::Variant(id) | Def::Ctor(_, id, ..) | Def::Enum(id) |
269273
Def::TyAlias(id) | Def::TraitAlias(id) |
270274
Def::AssociatedTy(id) | Def::TyParam(id) | Def::ConstParam(id) | Def::Struct(id) |
271-
Def::StructCtor(id, ..) |
272275
Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
273276
Def::AssociatedConst(id) | Def::Macro(id, ..) |
274277
Def::Existential(id) | Def::AssociatedExistential(id) | Def::ForeignTy(id) => {
@@ -303,20 +306,21 @@ impl Def {
303306
Def::Fn(..) => "function",
304307
Def::Mod(..) => "module",
305308
Def::Static(..) => "static",
306-
Def::Variant(..) => "variant",
307-
Def::VariantCtor(.., CtorKind::Fn) => "tuple variant",
308-
Def::VariantCtor(.., CtorKind::Const) => "unit variant",
309-
Def::VariantCtor(.., CtorKind::Fictive) => "struct variant",
310309
Def::Enum(..) => "enum",
310+
Def::Variant(..) => "variant",
311+
Def::Ctor(hir::CtorOf::Variant, _, CtorKind::Fn) => "tuple variant",
312+
Def::Ctor(hir::CtorOf::Variant, _, CtorKind::Const) => "unit variant",
313+
Def::Ctor(hir::CtorOf::Variant, _, CtorKind::Fictive) => "struct variant",
314+
Def::Struct(..) => "struct",
315+
Def::Ctor(hir::CtorOf::Struct, _, CtorKind::Fn) => "tuple struct",
316+
Def::Ctor(hir::CtorOf::Struct, _, CtorKind::Const) => "unit struct",
317+
Def::Ctor(hir::CtorOf::Struct, _, CtorKind::Fictive) =>
318+
bug!("impossible struct constructor"),
311319
Def::Existential(..) => "existential type",
312320
Def::TyAlias(..) => "type alias",
313321
Def::TraitAlias(..) => "trait alias",
314322
Def::AssociatedTy(..) => "associated type",
315323
Def::AssociatedExistential(..) => "associated existential type",
316-
Def::Struct(..) => "struct",
317-
Def::StructCtor(.., CtorKind::Fn) => "tuple struct",
318-
Def::StructCtor(.., CtorKind::Const) => "unit struct",
319-
Def::StructCtor(.., CtorKind::Fictive) => bug!("impossible struct constructor"),
320324
Def::SelfCtor(..) => "self constructor",
321325
Def::Union(..) => "union",
322326
Def::Trait(..) => "trait",

src/librustc/hir/intravisit.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
559559
generics: &'v Generics,
560560
parent_item_id: HirId) {
561561
visitor.visit_ident(variant.node.ident);
562+
visitor.visit_id(variant.node.id);
562563
visitor.visit_variant_data(&variant.node.data,
563564
variant.node.ident.name,
564565
generics,
@@ -923,7 +924,9 @@ pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'
923924

924925

925926
pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &'v VariantData) {
926-
visitor.visit_id(struct_definition.hir_id());
927+
if let Some(ctor_hir_id) = struct_definition.ctor_hir_id() {
928+
visitor.visit_id(ctor_hir_id);
929+
}
927930
walk_list!(visitor, visit_struct_field, struct_definition.fields());
928931
}
929932

src/librustc/hir/lowering.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -1615,9 +1615,11 @@ impl<'a> LoweringContext<'a> {
16151615
}
16161616

16171617
fn lower_variant(&mut self, v: &Variant) -> hir::Variant {
1618+
let LoweredNodeId { node_id: _, hir_id } = self.lower_node_id(v.node.id);
16181619
Spanned {
16191620
node: hir::VariantKind {
16201621
ident: v.node.ident,
1622+
id: hir_id,
16211623
attrs: self.lower_attrs(&v.node.attrs),
16221624
data: self.lower_variant_data(&v.node.data),
16231625
disr_expr: v.node.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
@@ -2669,19 +2671,10 @@ impl<'a> LoweringContext<'a> {
26692671

26702672
fn lower_variant_data(&mut self, vdata: &VariantData) -> hir::VariantData {
26712673
match *vdata {
2672-
VariantData::Struct(ref fields, id, recovered) => {
2673-
let LoweredNodeId { node_id: _, hir_id } = self.lower_node_id(id);
2674-
2675-
hir::VariantData::Struct(
2676-
fields
2677-
.iter()
2678-
.enumerate()
2679-
.map(|f| self.lower_struct_field(f))
2680-
.collect(),
2681-
hir_id,
2682-
recovered,
2683-
)
2684-
},
2674+
VariantData::Struct(ref fields, recovered) => hir::VariantData::Struct(
2675+
fields.iter().enumerate().map(|f| self.lower_struct_field(f)).collect(),
2676+
recovered,
2677+
),
26852678
VariantData::Tuple(ref fields, id) => {
26862679
let LoweredNodeId { node_id: _, hir_id } = self.lower_node_id(id);
26872680

@@ -2696,7 +2689,6 @@ impl<'a> LoweringContext<'a> {
26962689
},
26972690
VariantData::Unit(id) => {
26982691
let LoweredNodeId { node_id: _, hir_id } = self.lower_node_id(id);
2699-
27002692
hir::VariantData::Unit(hir_id)
27012693
},
27022694
}

src/librustc/hir/map/collector.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,11 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
360360
this.insert(i.span, i.hir_id, Node::Item(i));
361361
this.with_parent(i.hir_id, |this| {
362362
if let ItemKind::Struct(ref struct_def, _) = i.node {
363-
// If this is a tuple-like struct, register the constructor.
364-
if !struct_def.is_struct() {
365-
this.insert(i.span, struct_def.hir_id(), Node::StructCtor(struct_def));
363+
// If this is a tuple or unit-like struct, register the constructor.
364+
if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
365+
this.insert(i.span,
366+
ctor_hir_id,
367+
Node::Ctor(hir::CtorOf::Struct, struct_def));
366368
}
367369
}
368370
intravisit::walk_item(this, i);
@@ -515,8 +517,12 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
515517
}
516518

517519
fn visit_variant(&mut self, v: &'hir Variant, g: &'hir Generics, item_id: HirId) {
518-
self.insert(v.span, v.node.data.hir_id(), Node::Variant(v));
519-
self.with_parent(v.node.data.hir_id(), |this| {
520+
self.insert(v.span, v.node.id, Node::Variant(v));
521+
self.with_parent(v.node.id, |this| {
522+
// Register the constructor of this variant.
523+
if let Some(ctor_hir_id) = v.node.data.ctor_hir_id() {
524+
this.insert(v.span, ctor_hir_id, Node::Ctor(hir::CtorOf::Variant, &v.node.data));
525+
}
520526
intravisit::walk_variant(this, v, g, item_id);
521527
});
522528
}

src/librustc/hir/map/def_collector.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
158158
self.with_parent(def, |this| {
159159
match i.node {
160160
ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
161-
// If this is a tuple-like struct, register the constructor.
162-
if !struct_def.is_struct() {
163-
this.create_def(struct_def.id(),
161+
// If this is a unit or tuple-like struct, register the constructor.
162+
if let Some(ctor_hir_id) = struct_def.ctor_id() {
163+
this.create_def(ctor_hir_id,
164164
DefPathData::StructCtor,
165165
REGULAR_SPACE,
166166
i.span);
@@ -193,11 +193,19 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
193193
}
194194

195195
fn visit_variant(&mut self, v: &'a Variant, g: &'a Generics, item_id: NodeId) {
196-
let def = self.create_def(v.node.data.id(),
196+
let def = self.create_def(v.node.id,
197197
DefPathData::EnumVariant(v.node.ident.as_interned_str()),
198198
REGULAR_SPACE,
199199
v.span);
200-
self.with_parent(def, |this| visit::walk_variant(this, v, g, item_id));
200+
self.with_parent(def, |this| {
201+
if let Some(ctor_hir_id) = v.node.data.ctor_id() {
202+
this.create_def(ctor_hir_id,
203+
DefPathData::VariantCtor,
204+
REGULAR_SPACE,
205+
v.span);
206+
}
207+
visit::walk_variant(this, v, g, item_id)
208+
});
201209
}
202210

203211
fn visit_variant_data(&mut self, data: &'a VariantData, _: Ident,

src/librustc/hir/map/definitions.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,10 @@ pub enum DefPathData {
366366
EnumVariant(InternedString),
367367
/// A struct field
368368
Field(InternedString),
369-
/// Implicit ctor for a tuple-like struct
369+
/// Implicit ctor for a unit or tuple-like struct
370370
StructCtor,
371+
/// Implicit ctor for a unit or tuple-like enum variant
372+
VariantCtor,
371373
/// A constant expression (see {ast,hir}::AnonConst).
372374
AnonConst,
373375
/// An `impl Trait` type node
@@ -653,6 +655,7 @@ impl DefPathData {
653655
Misc |
654656
ClosureExpr |
655657
StructCtor |
658+
VariantCtor |
656659
AnonConst |
657660
ImplTrait => None
658661
}
@@ -683,7 +686,8 @@ impl DefPathData {
683686
Impl => "{{impl}}",
684687
Misc => "{{misc}}",
685688
ClosureExpr => "{{closure}}",
686-
StructCtor => "{{constructor}}",
689+
StructCtor => "{{struct constructor}}",
690+
VariantCtor => "{{variant constructor}}",
687691
AnonConst => "{{constant}}",
688692
ImplTrait => "{{opaque}}",
689693
};

src/librustc/hir/map/mod.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,15 @@ impl<'hir> Map<'hir> {
366366
}
367367
}
368368
Node::Variant(variant) => {
369-
let def_id = self.local_def_id_from_hir_id(variant.node.data.hir_id());
369+
let def_id = self.local_def_id_from_hir_id(variant.node.id);
370370
Some(Def::Variant(def_id))
371371
}
372-
Node::StructCtor(variant) => {
373-
let def_id = self.local_def_id_from_hir_id(variant.hir_id());
374-
Some(Def::StructCtor(def_id, def::CtorKind::from_hir(variant)))
372+
Node::Ctor(ctor_of, variant_data) => {
373+
variant_data.ctor_hir_id()
374+
.map(|hir_id| self.local_def_id_from_hir_id(hir_id))
375+
.map(|def_id| Def::Ctor(
376+
ctor_of, def_id, def::CtorKind::from_hir(variant_data),
377+
))
375378
}
376379
Node::AnonConst(_) |
377380
Node::Field(_) |
@@ -516,8 +519,7 @@ impl<'hir> Map<'hir> {
516519
Node::AnonConst(_) => {
517520
BodyOwnerKind::Const
518521
}
519-
Node::Variant(&Spanned { node: VariantKind { data: VariantData::Tuple(..), .. }, .. }) |
520-
Node::StructCtor(..) |
522+
Node::Ctor(..) |
521523
Node::Item(&Item { node: ItemKind::Fn(..), .. }) |
522524
Node::TraitItem(&TraitItem { node: TraitItemKind::Method(..), .. }) |
523525
Node::ImplItem(&ImplItem { node: ImplItemKind::Method(..), .. }) => {
@@ -948,8 +950,8 @@ impl<'hir> Map<'hir> {
948950
_ => bug!("struct ID bound to non-struct {}", self.hir_to_string(id))
949951
}
950952
}
951-
Some(Node::StructCtor(data)) => data,
952953
Some(Node::Variant(variant)) => &variant.node.data,
954+
Some(Node::Ctor(_, data)) => data,
953955
_ => bug!("expected struct or variant, found {}", self.hir_to_string(id))
954956
}
955957
}
@@ -993,7 +995,7 @@ impl<'hir> Map<'hir> {
993995
Node::Lifetime(lt) => lt.name.ident().name,
994996
Node::GenericParam(param) => param.name.ident().name,
995997
Node::Binding(&Pat { node: PatKind::Binding(_, _, l, _), .. }) => l.name,
996-
Node::StructCtor(_) => self.name(self.get_parent(id)),
998+
Node::Ctor(..) => self.name(self.get_parent(id)),
997999
_ => bug!("no name for {}", self.node_to_string(id))
9981000
}
9991001
}
@@ -1019,9 +1021,9 @@ impl<'hir> Map<'hir> {
10191021
Some(Node::Expr(ref e)) => Some(&*e.attrs),
10201022
Some(Node::Stmt(ref s)) => Some(s.node.attrs()),
10211023
Some(Node::GenericParam(param)) => Some(&param.attrs[..]),
1022-
// unit/tuple structs take the attributes straight from
1023-
// the struct definition.
1024-
Some(Node::StructCtor(_)) => return self.attrs(self.get_parent(id)),
1024+
// Unit/tuple structs/variants take the attributes straight from
1025+
// the struct/variant definition.
1026+
Some(Node::Ctor(..)) => return self.attrs(self.get_parent(id)),
10251027
_ => None
10261028
};
10271029
attrs.unwrap_or(&[])
@@ -1068,7 +1070,10 @@ impl<'hir> Map<'hir> {
10681070
Some(Node::Binding(pat)) => pat.span,
10691071
Some(Node::Pat(pat)) => pat.span,
10701072
Some(Node::Block(block)) => block.span,
1071-
Some(Node::StructCtor(_)) => self.expect_item(self.get_parent(id)).span,
1073+
Some(Node::Ctor(CtorOf::Struct, _)) =>
1074+
self.expect_item(self.get_parent(id)).span,
1075+
Some(Node::Ctor(CtorOf::Variant, _)) =>
1076+
self.expect_variant(self.node_to_hir_id(self.get_parent_node(id))).span,
10721077
Some(Node::Lifetime(lifetime)) => lifetime.span,
10731078
Some(Node::GenericParam(param)) => param.span,
10741079
Some(Node::Visibility(&Spanned {
@@ -1324,7 +1329,7 @@ impl<'a> print::State<'a> {
13241329
// these cases do not carry enough information in the
13251330
// hir_map to reconstruct their full structure for pretty
13261331
// printing.
1327-
Node::StructCtor(_) => bug!("cannot print isolated StructCtor"),
1332+
Node::Ctor(..) => bug!("cannot print isolated Ctor"),
13281333
Node::Local(a) => self.print_local_decl(&a),
13291334
Node::MacroDef(_) => bug!("cannot print MacroDef"),
13301335
Node::Crate => bug!("cannot print Crate"),
@@ -1443,8 +1448,8 @@ fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
14431448
Some(Node::Local(_)) => {
14441449
format!("local {}{}", map.node_to_pretty_string(id), id_str)
14451450
}
1446-
Some(Node::StructCtor(_)) => {
1447-
format!("struct_ctor {}{}", path_str(), id_str)
1451+
Some(Node::Ctor(..)) => {
1452+
format!("ctor {}{}", path_str(), id_str)
14481453
}
14491454
Some(Node::Lifetime(_)) => {
14501455
format!("lifetime {}{}", map.node_to_pretty_string(id), id_str)

0 commit comments

Comments
 (0)