Revert changes to creation of fictive constructors for struct variants

This commit is contained in:
Vadim Petrochenkov 2019-03-24 18:41:09 +03:00
parent 2cbc25e6fc
commit 782a6debe4
3 changed files with 29 additions and 62 deletions

View file

@ -822,14 +822,16 @@ impl<'a, 'tcx> CrateMetadata {
callback(def::Export { def: ctor_def, vis, ident, span });
}
}
Def::Variant(..) => {
if let Some(ctor_def_id) = self.get_ctor_def_id(child_index) {
let ctor_kind = self.get_ctor_kind(child_index);
let ctor_def = Def::Ctor(
hir::CtorOf::Variant, ctor_def_id, ctor_kind);
let vis = self.get_visibility(ctor_def_id.index);
callback(def::Export { def: ctor_def, ident, vis, span });
}
Def::Variant(def_id) => {
// Braced variants, unlike structs, generate unusable names in
// value namespace, they are reserved for possible future use.
// It's ok to use the variant's id as a ctor id since an
// error will be reported on any use of such resolution anyway.
let ctor_def_id = self.get_ctor_def_id(child_index).unwrap_or(def_id);
let ctor_kind = self.get_ctor_kind(child_index);
let ctor_def = Def::Ctor(hir::CtorOf::Variant, ctor_def_id, ctor_kind);
let vis = self.get_visibility(ctor_def_id.index);
callback(def::Export { def: ctor_def, ident, vis, span });
}
_ => {}
}

View file

@ -582,31 +582,22 @@ impl<'a> Resolver<'a> {
vis: ty::Visibility,
expansion: Mark) {
let ident = variant.node.ident;
let def_id = self.definitions.local_def_id(variant.node.id);
// Define a name in the type namespace.
let def_id = self.definitions.local_def_id(variant.node.id);
let def = Def::Variant(def_id);
self.define(parent, ident, TypeNS, (def, vis, variant.span, expansion));
// Define a constructor name in the value namespace.
// Braced variants, unlike structs, generate unusable names in
// value namespace, they are reserved for possible future use.
if let Some(ctor_node_id) = variant.node.data.ctor_id() {
let ctor_def_id = self.definitions.local_def_id(ctor_node_id);
let ctor_kind = CtorKind::from_ast(&variant.node.data);
let ctor_def = Def::Ctor(hir::CtorOf::Variant, ctor_def_id, ctor_kind);
self.define(parent, ident, ValueNS, (ctor_def, vis, variant.span, expansion));
} else {
// We normally don't have a `Def::Ctor(hir::CtorOf::Variant, ..)` for
// `Struct`-variants, but we must define one for name resolution to succeed. This also
// takes place in `build_reduced_graph_for_external_crate_def`.
let def_id = self.definitions.local_def_id(variant.node.id);
let ctor_kind = CtorKind::from_ast(&variant.node.data);
let ctor_def = Def::Ctor(hir::CtorOf::Variant, def_id, ctor_kind);
self.define(parent, ident, ValueNS, (ctor_def, vis, variant.span, expansion));
}
// It's ok to use the variant's id as a ctor id since an
// error will be reported on any use of such resolution anyway.
let ctor_node_id = variant.node.data.ctor_id().unwrap_or(variant.node.id);
let ctor_def_id = self.definitions.local_def_id(ctor_node_id);
let ctor_kind = CtorKind::from_ast(&variant.node.data);
let ctor_def = Def::Ctor(hir::CtorOf::Variant, ctor_def_id, ctor_kind);
self.define(parent, ident, ValueNS, (ctor_def, vis, variant.span, expansion));
}
/// Constructs the reduced graph for one foreign item.
@ -658,27 +649,13 @@ impl<'a> Resolver<'a> {
span);
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
}
Def::Variant(def_id) => {
Def::Variant(..) | Def::TyAlias(..) | Def::ForeignTy(..) | Def::Existential(..) |
Def::TraitAlias(..) | Def::PrimTy(..) | Def::ToolMod => {
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));
if hir::def::CtorKind::Fictive == self.cstore.ctor_kind_untracked(def_id) {
// We do not normally generate `Def::Ctor(hir::CtorOf::Variant, ..)` for
// `Struct`-variants. Therefore, `build_reduced_graph_for_external_crate_def`
// will not be called to define one. However, name resolution currently expects
// there to be one, so we generate one here. This is easy to solve for local
// code, see `build_reduced_graph_for_variant` for this case.
let ctor_def = Def::Ctor(hir::CtorOf::Variant, def_id,
hir::def::CtorKind::Fictive);
let _ = self.try_define(
parent, ident, ValueNS,
(ctor_def, vis, DUMMY_SP, expansion).to_name_binding(self.arenas),
);
}
}
Def::TyAlias(..) | Def::ForeignTy(..) | Def::Existential(..) | Def::TraitAlias(..) |
Def::PrimTy(..) | Def::ToolMod => {
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));
Def::Fn(..) | Def::Static(..) | Def::Const(..) |
Def::Ctor(hir::CtorOf::Variant, ..) => {
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));
}
Def::Ctor(hir::CtorOf::Struct, def_id, ..) => {
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));
@ -689,15 +666,6 @@ impl<'a> Resolver<'a> {
self.struct_constructors.insert(struct_def_id, (def, vis));
}
}
Def::Ctor(hir::CtorOf::Variant, ..) => {
let _ = self.try_define(
parent, ident, ValueNS,
(def, vis, DUMMY_SP, expansion).to_name_binding(self.arenas),
);
}
Def::Fn(..) | Def::Static(..) | Def::Const(..) => {
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));
}
Def::Trait(def_id) => {
let module_kind = ModuleKind::Def(def, ident.name);
let module = self.new_module(parent,

View file

@ -417,15 +417,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if let Some(variant_def) = variant_def {
check_type_alias_enum_variants_enabled(tcx, span);
let def = if let Some(ctor_def_id) = variant_def.ctor_def_id {
Def::Ctor(hir::CtorOf::Variant, ctor_def_id, variant_def.ctor_kind)
} else {
// Normally, there do not exist any `Def::Ctor` for `Struct`-variants but
// in this case, we can get better error messages as diagnostics will
// specialize the message around a `CtorKind::Fictive`.
Def::Ctor(hir::CtorOf::Variant, variant_def.def_id,
hir::def::CtorKind::Fictive)
};
// Braced variants generate unusable names in value namespace (reserved for
// possible future use), so variants resolved as associated items may refer to
// them as well. It's ok to use the variant's id as a ctor id since an
// error will be reported on any use of such resolution anyway.
let ctor_def_id = variant_def.ctor_def_id.unwrap_or(variant_def.def_id);
let def = Def::Ctor(hir::CtorOf::Variant, ctor_def_id, variant_def.ctor_kind);
tcx.check_stability(def.def_id(), Some(expr_id), span);
return Ok(def);