Skip to content

Commit ab75479

Browse files
authored
Rollup merge of rust-lang#104747 - petrochenkov:ctorfields, r=cjgillot
resolve: Don't use constructor def ids in the map for field names Also do some minor cleanup to insertion of those field names. Addresses a FIXME left in rust-lang#103578.
2 parents b1aa7f5 + 5fc359f commit ab75479

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_middle::metadata::ModChild;
3030
use rustc_middle::ty::{self, DefIdTree};
3131
use rustc_session::cstore::CrateStore;
3232
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
33-
use rustc_span::source_map::{respan, Spanned};
33+
use rustc_span::source_map::respan;
3434
use rustc_span::symbol::{kw, sym, Ident, Symbol};
3535
use rustc_span::Span;
3636

@@ -329,10 +329,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
329329
.iter()
330330
.map(|field| respan(field.span, field.ident.map_or(kw::Empty, |ident| ident.name)))
331331
.collect();
332-
self.insert_field_names(def_id, field_names);
332+
self.r.field_names.insert(def_id, field_names);
333333
}
334334

335-
fn insert_field_names(&mut self, def_id: DefId, field_names: Vec<Spanned<Symbol>>) {
335+
fn insert_field_names_extern(&mut self, def_id: DefId) {
336+
let field_names =
337+
self.r.cstore().struct_field_names_untracked(def_id, self.r.session).collect();
336338
self.r.field_names.insert(def_id, field_names);
337339
}
338340

@@ -995,8 +997,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
995997
let cstore = self.r.cstore();
996998
match res {
997999
Res::Def(DefKind::Struct, def_id) => {
998-
let field_names =
999-
cstore.struct_field_names_untracked(def_id, self.r.session).collect();
10001000
if let Some((ctor_kind, ctor_def_id)) = cstore.ctor_untracked(def_id) {
10011001
let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
10021002
let ctor_vis = cstore.visibility_untracked(ctor_def_id);
@@ -1006,13 +1006,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
10061006
.struct_constructors
10071007
.insert(def_id, (ctor_res, ctor_vis, field_visibilities));
10081008
}
1009-
self.insert_field_names(def_id, field_names);
1010-
}
1011-
Res::Def(DefKind::Union, def_id) => {
1012-
let field_names =
1013-
cstore.struct_field_names_untracked(def_id, self.r.session).collect();
1014-
self.insert_field_names(def_id, field_names);
1009+
self.insert_field_names_extern(def_id)
10151010
}
1011+
Res::Def(DefKind::Union, def_id) => self.insert_field_names_extern(def_id),
10161012
Res::Def(DefKind::AssocFn, def_id) => {
10171013
if cstore.fn_has_self_parameter_untracked(def_id, self.r.session) {
10181014
self.r.has_self.insert(def_id);
@@ -1514,20 +1510,16 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
15141510
};
15151511

15161512
// Define a constructor name in the value namespace.
1517-
let fields_id = if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(&variant.data) {
1513+
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(&variant.data) {
15181514
let ctor_def_id = self.r.local_def_id(ctor_node_id);
15191515
let ctor_res =
15201516
Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id.to_def_id());
15211517
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id));
15221518
self.r.visibilities.insert(ctor_def_id, ctor_vis);
1523-
ctor_def_id
1524-
} else {
1525-
def_id
1526-
};
1519+
}
15271520

15281521
// Record field names for error reporting.
1529-
// FIXME: Always use non-ctor id as the key.
1530-
self.insert_field_names_local(fields_id.to_def_id(), &variant.data);
1522+
self.insert_field_names_local(def_id.to_def_id(), &variant.data);
15311523

15321524
visit::walk_variant(self, variant);
15331525
}

compiler/rustc_resolve/src/late/diagnostics.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_hir::def::Namespace::{self, *};
2121
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind};
2222
use rustc_hir::def_id::{DefId, CRATE_DEF_ID, LOCAL_CRATE};
2323
use rustc_hir::PrimTy;
24+
use rustc_middle::ty::DefIdTree;
2425
use rustc_session::lint;
2526
use rustc_session::parse::feature_err;
2627
use rustc_session::Session;
@@ -1462,7 +1463,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
14621463
_ => return false,
14631464
}
14641465
}
1465-
(Res::Def(DefKind::Ctor(_, CtorKind::Fn), def_id), _) if ns == ValueNS => {
1466+
(Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_def_id), _) if ns == ValueNS => {
1467+
let def_id = self.r.parent(ctor_def_id);
14661468
if let Some(span) = self.def_span(def_id) {
14671469
err.span_label(span, &format!("`{}` defined here", path_str));
14681470
}
@@ -1953,7 +1955,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
19531955
));
19541956
}
19551957
} else {
1956-
let needs_placeholder = |def_id: DefId, kind: CtorKind| {
1958+
let needs_placeholder = |ctor_def_id: DefId, kind: CtorKind| {
1959+
let def_id = self.r.parent(ctor_def_id);
19571960
let has_no_fields = self.r.field_names.get(&def_id).map_or(false, |f| f.is_empty());
19581961
match kind {
19591962
CtorKind::Const => false,

0 commit comments

Comments
 (0)