Skip to content

Commit

Permalink
rustc_resolve: bring back "struct called like a function" cross-crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Sep 20, 2016
1 parent 564f2ee commit 521d3ea
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/librustc/middle/cstore.rs
Expand Up @@ -200,6 +200,7 @@ pub trait CrateStore<'tcx> {
-> Option<DefIndex>;
fn def_key(&self, def: DefId) -> hir_map::DefKey;
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath>;
fn variant_kind(&self, def_id: DefId) -> Option<ty::VariantKind>;
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>;
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
fn item_children(&self, did: DefId) -> Vec<def::Export>;
Expand Down Expand Up @@ -283,7 +284,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
fn stability(&self, def: DefId) -> Option<attr::Stability> { bug!("stability") }
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> { bug!("deprecation") }
fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind { bug!("closure_kind") }
fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind { bug!("closure_kind") }
fn closure_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
-> ty::ClosureTy<'tcx> { bug!("closure_ty") }
fn item_variances(&self, def: DefId) -> Vec<ty::Variance> { bug!("item_variances") }
Expand Down Expand Up @@ -376,6 +377,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath> {
bug!("relative_def_path")
}
fn variant_kind(&self, def_id: DefId) -> Option<ty::VariantKind> { bug!("variant_kind") }
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
{ bug!("struct_ctor_def_id") }
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }
Expand Down
6 changes: 6 additions & 0 deletions src/librustc_metadata/csearch.rs
Expand Up @@ -342,6 +342,12 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
self.get_crate_data(def.krate).def_path(def.index)
}

fn variant_kind(&self, def_id: DefId) -> Option<ty::VariantKind>
{
self.dep_graph.read(DepNode::MetaData(def_id));
self.get_crate_data(def_id.krate).get_variant_kind(def_id.index)
}

fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
{
self.dep_graph.read(DepNode::MetaData(struct_def_id));
Expand Down
9 changes: 9 additions & 0 deletions src/librustc_metadata/decoder.rs
Expand Up @@ -787,6 +787,15 @@ impl<'a, 'tcx> CrateMetadata {
self.entry(id).variances.decode(self).collect()
}

pub fn get_variant_kind(&self, node_id: DefIndex) -> Option<ty::VariantKind> {
match self.entry(node_id).kind {
EntryKind::Struct(data) |
EntryKind::Union(data) |
EntryKind::Variant(data) => Some(data.decode(self).kind),
_ => None
}
}

pub fn get_struct_ctor_def_id(&self, node_id: DefIndex) -> Option<DefId> {
match self.entry(node_id).kind {
EntryKind::Struct(data) => {
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -411,12 +411,16 @@ impl<'b> Resolver<'b> {
let module = self.new_module(parent_link, Some(def), None);
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
}
Def::Variant(..) => {
Def::Variant(variant_id) => {
debug!("(building reduced graph for external crate) building variant {}", name);
// Variants are always treated as importable to allow them to be glob used.
// All variants are defined in both type and value namespaces as future-proofing.
let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
let _ = self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
if self.session.cstore.variant_kind(variant_id) == Some(ty::VariantKind::Struct) {
// Not adding fields for variants as they are not accessed with a self receiver
self.structs.insert(variant_id, Vec::new());
}
}
Def::Fn(..) |
Def::Static(..) |
Expand Down
13 changes: 13 additions & 0 deletions src/test/compile-fail/auxiliary/issue_19452_aux.rs
@@ -0,0 +1,13 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub enum Homura {
Madoka { age: u32 }
}
7 changes: 7 additions & 0 deletions src/test/compile-fail/issue-19452.rs
Expand Up @@ -8,6 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:issue_19452_aux.rs
extern crate issue_19452_aux;

enum Homura {
Madoka { age: u32 }
}
Expand All @@ -16,4 +19,8 @@ fn main() {
let homura = Homura::Madoka;
//~^ ERROR uses it like a function
//~| struct called like a function

let homura = issue_19452_aux::Homura::Madoka;
//~^ ERROR uses it like a function
//~| struct called like a function
}

0 comments on commit 521d3ea

Please sign in to comment.