Skip to content

Commit 48f8bed

Browse files
petrochenkovda-x
authored andcommitted
resolve: Use field spans for reporting the private constructor error
1 parent 5d8af38 commit 48f8bed

File tree

5 files changed

+204
-107
lines changed

5 files changed

+204
-107
lines changed

src/librustc_resolve/lib.rs

+12-18
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc::hir::def::{self, DefKind, PartialRes, CtorKind, CtorOf, NonMacroAttrK
3030
use rustc::hir::def::Namespace::*;
3131
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
3232
use rustc::hir::{TraitMap, GlobMap};
33-
use rustc::ty;
33+
use rustc::ty::{self, DefIdTree};
3434
use rustc::util::nodemap::{NodeMap, NodeSet, FxHashMap, FxHashSet, DefIdMap};
3535
use rustc::span_bug;
3636

@@ -1006,7 +1006,7 @@ impl<'a> AsMut<Resolver<'a>> for Resolver<'a> {
10061006
fn as_mut(&mut self) -> &mut Resolver<'a> { self }
10071007
}
10081008

1009-
impl<'a, 'b> ty::DefIdTree for &'a Resolver<'b> {
1009+
impl<'a, 'b> DefIdTree for &'a Resolver<'b> {
10101010
fn parent(self, id: DefId) -> Option<DefId> {
10111011
match id.krate {
10121012
LOCAL_CRATE => self.definitions.def_key(id.index).parent,
@@ -2391,23 +2391,17 @@ impl<'a> Resolver<'a> {
23912391
binding.res().descr(),
23922392
ident.name,
23932393
);
2394-
// FIXME: use the ctor's `def_id` to check wether any of the fields is not visible
2395-
match binding.kind {
2396-
NameBindingKind::Res(Res::Def(DefKind::Ctor(
2397-
CtorOf::Struct,
2398-
CtorKind::Fn,
2399-
), _def_id), _) => {
2400-
err.note("a tuple struct constructor is private if any of its fields \
2401-
is private");
2402-
}
2403-
NameBindingKind::Res(Res::Def(DefKind::Ctor(
2404-
CtorOf::Variant,
2405-
CtorKind::Fn,
2406-
), _def_id), _) => {
2407-
err.note("a tuple variant constructor is private if any of its fields \
2408-
is private");
2394+
if let NameBindingKind::Res(
2395+
Res::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fn), ctor_def_id), _
2396+
) = binding.kind {
2397+
let def_id = (&*self).parent(ctor_def_id).expect("no parent for a constructor");
2398+
if let Some(fields) = self.field_names.get(&def_id) {
2399+
let first_field = fields.first().expect("empty field list in the map");
2400+
err.span_label(
2401+
fields.iter().fold(first_field.span, |acc, field| acc.to(field.span)),
2402+
"a tuple struct constructor is private if any of its fields is private",
2403+
);
24092404
}
2410-
_ => {}
24112405
}
24122406
err.emit();
24132407
}

0 commit comments

Comments
 (0)