]> git.lizzy.rs Git - rust.git/commitdiff
Revert changes to creation of fictive constructors for struct variants
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sun, 24 Mar 2019 15:41:09 +0000 (18:41 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sun, 24 Mar 2019 15:41:09 +0000 (18:41 +0300)
src/librustc_metadata/decoder.rs
src/librustc_resolve/build_reduced_graph.rs
src/librustc_typeck/check/method/mod.rs

index 0e750cd15eebd55494056f272bc2bdb0e5e4ecb1..32aa75166aec03d9b9c173623c7ea5d380a5fb06 100644 (file)
@@ -822,14 +822,16 @@ pub fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F, sess: &Sessio
                                 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 });
                         }
                         _ => {}
                     }
index 72197d4a17aa503757c9b60b7d7f147762b9eeab..0cb6872ce76544f8d586079f6ec65be87a1bfec7 100644 (file)
@@ -582,31 +582,22 @@ fn build_reduced_graph_for_variant(&mut self,
                                        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 @@ fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'a>, chi
                                              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 @@ fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'a>, chi
                     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,
index 0060b1031a761a837569fa6a528b4c3d99537344..26024ece05427c036b709271f11ccbdd68c4beea 100644 (file)
@@ -417,15 +417,12 @@ pub fn resolve_ufcs(
                 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);