Skip to content

Commit 782a6de

Browse files
committed
Revert changes to creation of fictive constructors for struct variants
1 parent 2cbc25e commit 782a6de

File tree

3 files changed

+29
-62
lines changed

3 files changed

+29
-62
lines changed

src/librustc_metadata/decoder.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -822,14 +822,16 @@ impl<'a, 'tcx> CrateMetadata {
822822
callback(def::Export { def: ctor_def, vis, ident, span });
823823
}
824824
}
825-
Def::Variant(..) => {
826-
if let Some(ctor_def_id) = self.get_ctor_def_id(child_index) {
827-
let ctor_kind = self.get_ctor_kind(child_index);
828-
let ctor_def = Def::Ctor(
829-
hir::CtorOf::Variant, ctor_def_id, ctor_kind);
830-
let vis = self.get_visibility(ctor_def_id.index);
831-
callback(def::Export { def: ctor_def, ident, vis, span });
832-
}
825+
Def::Variant(def_id) => {
826+
// Braced variants, unlike structs, generate unusable names in
827+
// value namespace, they are reserved for possible future use.
828+
// It's ok to use the variant's id as a ctor id since an
829+
// error will be reported on any use of such resolution anyway.
830+
let ctor_def_id = self.get_ctor_def_id(child_index).unwrap_or(def_id);
831+
let ctor_kind = self.get_ctor_kind(child_index);
832+
let ctor_def = Def::Ctor(hir::CtorOf::Variant, ctor_def_id, ctor_kind);
833+
let vis = self.get_visibility(ctor_def_id.index);
834+
callback(def::Export { def: ctor_def, ident, vis, span });
833835
}
834836
_ => {}
835837
}

src/librustc_resolve/build_reduced_graph.rs

+13-45
Original file line numberDiff line numberDiff line change
@@ -582,31 +582,22 @@ impl<'a> Resolver<'a> {
582582
vis: ty::Visibility,
583583
expansion: Mark) {
584584
let ident = variant.node.ident;
585-
let def_id = self.definitions.local_def_id(variant.node.id);
586585

587586
// Define a name in the type namespace.
587+
let def_id = self.definitions.local_def_id(variant.node.id);
588588
let def = Def::Variant(def_id);
589589
self.define(parent, ident, TypeNS, (def, vis, variant.span, expansion));
590590

591591
// Define a constructor name in the value namespace.
592592
// Braced variants, unlike structs, generate unusable names in
593593
// value namespace, they are reserved for possible future use.
594-
if let Some(ctor_node_id) = variant.node.data.ctor_id() {
595-
let ctor_def_id = self.definitions.local_def_id(ctor_node_id);
596-
let ctor_kind = CtorKind::from_ast(&variant.node.data);
597-
let ctor_def = Def::Ctor(hir::CtorOf::Variant, ctor_def_id, ctor_kind);
598-
599-
self.define(parent, ident, ValueNS, (ctor_def, vis, variant.span, expansion));
600-
} else {
601-
// We normally don't have a `Def::Ctor(hir::CtorOf::Variant, ..)` for
602-
// `Struct`-variants, but we must define one for name resolution to succeed. This also
603-
// takes place in `build_reduced_graph_for_external_crate_def`.
604-
let def_id = self.definitions.local_def_id(variant.node.id);
605-
let ctor_kind = CtorKind::from_ast(&variant.node.data);
606-
let ctor_def = Def::Ctor(hir::CtorOf::Variant, def_id, ctor_kind);
607-
608-
self.define(parent, ident, ValueNS, (ctor_def, vis, variant.span, expansion));
609-
}
594+
// It's ok to use the variant's id as a ctor id since an
595+
// error will be reported on any use of such resolution anyway.
596+
let ctor_node_id = variant.node.data.ctor_id().unwrap_or(variant.node.id);
597+
let ctor_def_id = self.definitions.local_def_id(ctor_node_id);
598+
let ctor_kind = CtorKind::from_ast(&variant.node.data);
599+
let ctor_def = Def::Ctor(hir::CtorOf::Variant, ctor_def_id, ctor_kind);
600+
self.define(parent, ident, ValueNS, (ctor_def, vis, variant.span, expansion));
610601
}
611602

612603
/// Constructs the reduced graph for one foreign item.
@@ -658,27 +649,13 @@ impl<'a> Resolver<'a> {
658649
span);
659650
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
660651
}
661-
Def::Variant(def_id) => {
652+
Def::Variant(..) | Def::TyAlias(..) | Def::ForeignTy(..) | Def::Existential(..) |
653+
Def::TraitAlias(..) | Def::PrimTy(..) | Def::ToolMod => {
662654
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));
663-
664-
if hir::def::CtorKind::Fictive == self.cstore.ctor_kind_untracked(def_id) {
665-
// We do not normally generate `Def::Ctor(hir::CtorOf::Variant, ..)` for
666-
// `Struct`-variants. Therefore, `build_reduced_graph_for_external_crate_def`
667-
// will not be called to define one. However, name resolution currently expects
668-
// there to be one, so we generate one here. This is easy to solve for local
669-
// code, see `build_reduced_graph_for_variant` for this case.
670-
let ctor_def = Def::Ctor(hir::CtorOf::Variant, def_id,
671-
hir::def::CtorKind::Fictive);
672-
673-
let _ = self.try_define(
674-
parent, ident, ValueNS,
675-
(ctor_def, vis, DUMMY_SP, expansion).to_name_binding(self.arenas),
676-
);
677-
}
678655
}
679-
Def::TyAlias(..) | Def::ForeignTy(..) | Def::Existential(..) | Def::TraitAlias(..) |
680-
Def::PrimTy(..) | Def::ToolMod => {
681-
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));
656+
Def::Fn(..) | Def::Static(..) | Def::Const(..) |
657+
Def::Ctor(hir::CtorOf::Variant, ..) => {
658+
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));
682659
}
683660
Def::Ctor(hir::CtorOf::Struct, def_id, ..) => {
684661
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));
@@ -689,15 +666,6 @@ impl<'a> Resolver<'a> {
689666
self.struct_constructors.insert(struct_def_id, (def, vis));
690667
}
691668
}
692-
Def::Ctor(hir::CtorOf::Variant, ..) => {
693-
let _ = self.try_define(
694-
parent, ident, ValueNS,
695-
(def, vis, DUMMY_SP, expansion).to_name_binding(self.arenas),
696-
);
697-
}
698-
Def::Fn(..) | Def::Static(..) | Def::Const(..) => {
699-
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));
700-
}
701669
Def::Trait(def_id) => {
702670
let module_kind = ModuleKind::Def(def, ident.name);
703671
let module = self.new_module(parent,

src/librustc_typeck/check/method/mod.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -417,15 +417,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
417417
if let Some(variant_def) = variant_def {
418418
check_type_alias_enum_variants_enabled(tcx, span);
419419

420-
let def = if let Some(ctor_def_id) = variant_def.ctor_def_id {
421-
Def::Ctor(hir::CtorOf::Variant, ctor_def_id, variant_def.ctor_kind)
422-
} else {
423-
// Normally, there do not exist any `Def::Ctor` for `Struct`-variants but
424-
// in this case, we can get better error messages as diagnostics will
425-
// specialize the message around a `CtorKind::Fictive`.
426-
Def::Ctor(hir::CtorOf::Variant, variant_def.def_id,
427-
hir::def::CtorKind::Fictive)
428-
};
420+
// Braced variants generate unusable names in value namespace (reserved for
421+
// possible future use), so variants resolved as associated items may refer to
422+
// them as well. It's ok to use the variant's id as a ctor id since an
423+
// error will be reported on any use of such resolution anyway.
424+
let ctor_def_id = variant_def.ctor_def_id.unwrap_or(variant_def.def_id);
425+
let def = Def::Ctor(hir::CtorOf::Variant, ctor_def_id, variant_def.ctor_kind);
429426

430427
tcx.check_stability(def.def_id(), Some(expr_id), span);
431428
return Ok(def);

0 commit comments

Comments
 (0)