Skip to content

Commit 521d3ea

Browse files
committed
rustc_resolve: bring back "struct called like a function" cross-crate.
1 parent 564f2ee commit 521d3ea

File tree

6 files changed

+43
-2
lines changed

6 files changed

+43
-2
lines changed

src/librustc/middle/cstore.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ pub trait CrateStore<'tcx> {
200200
-> Option<DefIndex>;
201201
fn def_key(&self, def: DefId) -> hir_map::DefKey;
202202
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath>;
203+
fn variant_kind(&self, def_id: DefId) -> Option<ty::VariantKind>;
203204
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>;
204205
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
205206
fn item_children(&self, did: DefId) -> Vec<def::Export>;
@@ -283,7 +284,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
283284
fn stability(&self, def: DefId) -> Option<attr::Stability> { bug!("stability") }
284285
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> { bug!("deprecation") }
285286
fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
286-
fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind { bug!("closure_kind") }
287+
fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind { bug!("closure_kind") }
287288
fn closure_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
288289
-> ty::ClosureTy<'tcx> { bug!("closure_ty") }
289290
fn item_variances(&self, def: DefId) -> Vec<ty::Variance> { bug!("item_variances") }
@@ -376,6 +377,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
376377
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath> {
377378
bug!("relative_def_path")
378379
}
380+
fn variant_kind(&self, def_id: DefId) -> Option<ty::VariantKind> { bug!("variant_kind") }
379381
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
380382
{ bug!("struct_ctor_def_id") }
381383
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }

src/librustc_metadata/csearch.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
342342
self.get_crate_data(def.krate).def_path(def.index)
343343
}
344344

345+
fn variant_kind(&self, def_id: DefId) -> Option<ty::VariantKind>
346+
{
347+
self.dep_graph.read(DepNode::MetaData(def_id));
348+
self.get_crate_data(def_id.krate).get_variant_kind(def_id.index)
349+
}
350+
345351
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
346352
{
347353
self.dep_graph.read(DepNode::MetaData(struct_def_id));

src/librustc_metadata/decoder.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,15 @@ impl<'a, 'tcx> CrateMetadata {
787787
self.entry(id).variances.decode(self).collect()
788788
}
789789

790+
pub fn get_variant_kind(&self, node_id: DefIndex) -> Option<ty::VariantKind> {
791+
match self.entry(node_id).kind {
792+
EntryKind::Struct(data) |
793+
EntryKind::Union(data) |
794+
EntryKind::Variant(data) => Some(data.decode(self).kind),
795+
_ => None
796+
}
797+
}
798+
790799
pub fn get_struct_ctor_def_id(&self, node_id: DefIndex) -> Option<DefId> {
791800
match self.entry(node_id).kind {
792801
EntryKind::Struct(data) => {

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,16 @@ impl<'b> Resolver<'b> {
411411
let module = self.new_module(parent_link, Some(def), None);
412412
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
413413
}
414-
Def::Variant(..) => {
414+
Def::Variant(variant_id) => {
415415
debug!("(building reduced graph for external crate) building variant {}", name);
416416
// Variants are always treated as importable to allow them to be glob used.
417417
// All variants are defined in both type and value namespaces as future-proofing.
418418
let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
419419
let _ = self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
420+
if self.session.cstore.variant_kind(variant_id) == Some(ty::VariantKind::Struct) {
421+
// Not adding fields for variants as they are not accessed with a self receiver
422+
self.structs.insert(variant_id, Vec::new());
423+
}
420424
}
421425
Def::Fn(..) |
422426
Def::Static(..) |
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub enum Homura {
12+
Madoka { age: u32 }
13+
}

src/test/compile-fail/issue-19452.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// aux-build:issue_19452_aux.rs
12+
extern crate issue_19452_aux;
13+
1114
enum Homura {
1215
Madoka { age: u32 }
1316
}
@@ -16,4 +19,8 @@ fn main() {
1619
let homura = Homura::Madoka;
1720
//~^ ERROR uses it like a function
1821
//~| struct called like a function
22+
23+
let homura = issue_19452_aux::Homura::Madoka;
24+
//~^ ERROR uses it like a function
25+
//~| struct called like a function
1926
}

0 commit comments

Comments
 (0)