Skip to content

Commit 3752b3d

Browse files
committedMar 24, 2019
Auto merge of #59382 - davidtwco:rfc-2008-refactoring, r=petrochenkov
Separate `DefId`s for variants and their constructors Part of #44109. Split off from #59376. See [Zulip topic](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/rfc-2008/near/132663140) for previous discussion. r? @petrochenkov
2 parents 0576ac1 + 23cae1d commit 3752b3d

Some content is hidden

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

73 files changed

+859
-780
lines changed
 

‎src/librustc/hir/def.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ use crate::ty;
99

1010
use self::Namespace::*;
1111

12+
/// Encodes if a `Def::Ctor` is the constructor of an enum variant or a struct.
13+
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
14+
pub enum CtorOf {
15+
/// This `Def::Ctor` is a synthesized constructor of a tuple or unit struct.
16+
Struct,
17+
/// This `Def::Ctor` is a synthesized constructor of a tuple or unit variant.
18+
Variant,
19+
}
20+
1221
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
1322
pub enum CtorKind {
1423
/// Constructor function automatically created by a tuple struct/variant.
@@ -37,9 +46,11 @@ pub enum NonMacroAttrKind {
3746
pub enum Def {
3847
// Type namespace
3948
Mod(DefId),
40-
Struct(DefId), // `DefId` refers to `NodeId` of the struct itself
49+
/// `DefId` refers to the struct itself, `Def::Ctor` refers to its constructor if it exists.
50+
Struct(DefId),
4151
Union(DefId),
4252
Enum(DefId),
53+
/// `DefId` refers to the variant itself, `Def::Ctor` refers to its constructor if it exists.
4354
Variant(DefId),
4455
Trait(DefId),
4556
/// `existential type Foo: Bar;`
@@ -61,8 +72,8 @@ pub enum Def {
6172
Const(DefId),
6273
ConstParam(DefId),
6374
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
75+
/// `DefId` refers to the struct or enum variant's constructor.
76+
Ctor(DefId, CtorOf, CtorKind),
6677
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
6778
Method(DefId),
6879
AssociatedConst(DefId),
@@ -265,10 +276,9 @@ impl Def {
265276
pub fn opt_def_id(&self) -> Option<DefId> {
266277
match *self {
267278
Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
268-
Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) |
279+
Def::Variant(id) | Def::Ctor(id, ..) | Def::Enum(id) |
269280
Def::TyAlias(id) | Def::TraitAlias(id) |
270281
Def::AssociatedTy(id) | Def::TyParam(id) | Def::ConstParam(id) | Def::Struct(id) |
271-
Def::StructCtor(id, ..) |
272282
Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
273283
Def::AssociatedConst(id) | Def::Macro(id, ..) |
274284
Def::Existential(id) | Def::AssociatedExistential(id) | Def::ForeignTy(id) => {
@@ -303,20 +313,21 @@ impl Def {
303313
Def::Fn(..) => "function",
304314
Def::Mod(..) => "module",
305315
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",
310316
Def::Enum(..) => "enum",
317+
Def::Variant(..) => "variant",
318+
Def::Ctor(_, CtorOf::Variant, CtorKind::Fn) => "tuple variant",
319+
Def::Ctor(_, CtorOf::Variant, CtorKind::Const) => "unit variant",
320+
Def::Ctor(_, CtorOf::Variant, CtorKind::Fictive) => "struct variant",
321+
Def::Struct(..) => "struct",
322+
Def::Ctor(_, CtorOf::Struct, CtorKind::Fn) => "tuple struct",
323+
Def::Ctor(_, CtorOf::Struct, CtorKind::Const) => "unit struct",
324+
Def::Ctor(_, CtorOf::Struct, CtorKind::Fictive) =>
325+
bug!("impossible struct constructor"),
311326
Def::Existential(..) => "existential type",
312327
Def::TyAlias(..) => "type alias",
313328
Def::TraitAlias(..) => "trait alias",
314329
Def::AssociatedTy(..) => "associated type",
315330
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"),
320331
Def::SelfCtor(..) => "self constructor",
321332
Def::Union(..) => "union",
322333
Def::Trait(..) => "trait",

‎src/librustc/hir/intravisit.rs

Lines changed: 4 additions & 1 deletion
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

0 commit comments

Comments
 (0)
Please sign in to comment.