Skip to content

Commit 57fda67

Browse files
committed
Fix diagnostic for matching/creating x-crate re-exported tuple structs with private fields.
The more helpful diagnostic already existed but wasn't working if the struct in question was a re-export from a different crate.
1 parent 377d1a9 commit 57fda67

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
2727
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
2828
use rustc_middle::mir::{self, Body, Promoted};
2929
use rustc_middle::ty::codec::TyDecoder;
30-
use rustc_middle::ty::{self, Ty, TyCtxt};
30+
use rustc_middle::ty::{self, Ty, TyCtxt, Visibility};
3131
use rustc_serialize::{opaque, Decodable, Decoder};
3232
use rustc_session::Session;
3333
use rustc_span::hygiene::ExpnDataDecodeMode;
@@ -1305,6 +1305,17 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13051305
.collect()
13061306
}
13071307

1308+
fn get_struct_field_visibilities(&self, id: DefIndex) -> Vec<Visibility> {
1309+
self.root
1310+
.tables
1311+
.children
1312+
.get(self, id)
1313+
.unwrap_or_else(Lazy::empty)
1314+
.decode(self)
1315+
.map(|field_index| self.get_visibility(field_index))
1316+
.collect()
1317+
}
1318+
13081319
fn get_inherent_implementations_for_type(
13091320
&self,
13101321
tcx: TyCtxt<'tcx>,

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::expand::allocator::AllocatorKind;
88
use rustc_data_structures::stable_map::FxHashMap;
99
use rustc_data_structures::svh::Svh;
1010
use rustc_hir as hir;
11-
use rustc_hir::def::DefKind;
11+
use rustc_hir::def::{CtorKind, DefKind};
1212
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
1313
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1414
use rustc_middle::hir::exports::Export;
@@ -17,7 +17,7 @@ use rustc_middle::middle::cstore::{CrateSource, CrateStore, EncodedMetadata};
1717
use rustc_middle::middle::exported_symbols::ExportedSymbol;
1818
use rustc_middle::middle::stability::DeprecationEntry;
1919
use rustc_middle::ty::query::Providers;
20-
use rustc_middle::ty::{self, TyCtxt};
20+
use rustc_middle::ty::{self, TyCtxt, Visibility};
2121
use rustc_session::utils::NativeLibKind;
2222
use rustc_session::{CrateDisambiguator, Session};
2323
use rustc_span::source_map::{Span, Spanned};
@@ -392,6 +392,20 @@ impl CStore {
392392
self.get_crate_data(def.krate).get_struct_field_names(def.index, sess)
393393
}
394394

395+
pub fn struct_field_visibilities_untracked(&self, def: DefId) -> Vec<Visibility> {
396+
self.get_crate_data(def.krate).get_struct_field_visibilities(def.index)
397+
}
398+
399+
pub fn ctor_def_id_and_kind_untracked(&self, def: DefId) -> Option<(DefId, CtorKind)> {
400+
self.get_crate_data(def.krate).get_ctor_def_id(def.index).map(|ctor_def_id| {
401+
(ctor_def_id, self.get_crate_data(def.krate).get_ctor_kind(def.index))
402+
})
403+
}
404+
405+
pub fn visibility_untracked(&self, def: DefId) -> Visibility {
406+
self.get_crate_data(def.krate).get_visibility(def.index)
407+
}
408+
395409
pub fn item_children_untracked(
396410
&self,
397411
def_id: DefId,

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,20 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
995995
// Record some extra data for better diagnostics.
996996
let cstore = self.r.cstore();
997997
match res {
998-
Res::Def(DefKind::Struct | DefKind::Union, def_id) => {
998+
Res::Def(DefKind::Struct, def_id) => {
999+
let field_names = cstore.struct_field_names_untracked(def_id, self.r.session);
1000+
let ctor = cstore.ctor_def_id_and_kind_untracked(def_id);
1001+
if let Some((ctor_def_id, ctor_kind)) = ctor {
1002+
let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
1003+
let ctor_vis = cstore.visibility_untracked(ctor_def_id);
1004+
let field_visibilities = cstore.struct_field_visibilities_untracked(def_id);
1005+
self.r
1006+
.struct_constructors
1007+
.insert(def_id, (ctor_res, ctor_vis, field_visibilities));
1008+
}
1009+
self.insert_field_names(def_id, field_names);
1010+
}
1011+
Res::Def(DefKind::Union, def_id) => {
9991012
let field_names = cstore.struct_field_names_untracked(def_id, self.r.session);
10001013
self.insert_field_names(def_id, field_names);
10011014
}
@@ -1007,12 +1020,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
10071020
self.r.has_self.insert(def_id);
10081021
}
10091022
}
1010-
Res::Def(DefKind::Ctor(CtorOf::Struct, ..), def_id) => {
1011-
let parent = cstore.def_key(def_id).parent;
1012-
if let Some(struct_def_id) = parent.map(|index| DefId { index, ..def_id }) {
1013-
self.r.struct_constructors.insert(struct_def_id, (res, vis, vec![]));
1014-
}
1015-
}
10161023
_ => {}
10171024
}
10181025
}

0 commit comments

Comments
 (0)