Skip to content

Commit

Permalink
resolve: Use field spans for reporting the private constructor error
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov authored and da-x committed Oct 9, 2019
1 parent 5d8af38 commit 48f8bed
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 107 deletions.
30 changes: 12 additions & 18 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use rustc::hir::def::{self, DefKind, PartialRes, CtorKind, CtorOf, NonMacroAttrK
use rustc::hir::def::Namespace::*;
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
use rustc::hir::{TraitMap, GlobMap};
use rustc::ty;
use rustc::ty::{self, DefIdTree};
use rustc::util::nodemap::{NodeMap, NodeSet, FxHashMap, FxHashSet, DefIdMap};
use rustc::span_bug;

Expand Down Expand Up @@ -1006,7 +1006,7 @@ impl<'a> AsMut<Resolver<'a>> for Resolver<'a> {
fn as_mut(&mut self) -> &mut Resolver<'a> { self }
}

impl<'a, 'b> ty::DefIdTree for &'a Resolver<'b> {
impl<'a, 'b> DefIdTree for &'a Resolver<'b> {
fn parent(self, id: DefId) -> Option<DefId> {
match id.krate {
LOCAL_CRATE => self.definitions.def_key(id.index).parent,
Expand Down Expand Up @@ -2391,23 +2391,17 @@ impl<'a> Resolver<'a> {
binding.res().descr(),
ident.name,
);
// FIXME: use the ctor's `def_id` to check wether any of the fields is not visible
match binding.kind {
NameBindingKind::Res(Res::Def(DefKind::Ctor(
CtorOf::Struct,
CtorKind::Fn,
), _def_id), _) => {
err.note("a tuple struct constructor is private if any of its fields \
is private");
}
NameBindingKind::Res(Res::Def(DefKind::Ctor(
CtorOf::Variant,
CtorKind::Fn,
), _def_id), _) => {
err.note("a tuple variant constructor is private if any of its fields \
is private");
if let NameBindingKind::Res(
Res::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fn), ctor_def_id), _
) = binding.kind {
let def_id = (&*self).parent(ctor_def_id).expect("no parent for a constructor");
if let Some(fields) = self.field_names.get(&def_id) {
let first_field = fields.first().expect("empty field list in the map");
err.span_label(
fields.iter().fold(first_field.span, |acc, field| acc.to(field.span)),
"a tuple struct constructor is private if any of its fields is private",
);
}
_ => {}
}
err.emit();
}
Expand Down
Loading

0 comments on commit 48f8bed

Please sign in to comment.