Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve: fix error title regarding private constructors #65250

Merged
merged 2 commits into from
Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2379,26 +2379,38 @@ impl<'a> Resolver<'a> {
let mut reported_spans = FxHashSet::default();
for &PrivacyError(dedup_span, ident, binding) in &self.privacy_errors {
if reported_spans.insert(dedup_span) {
let mut err = struct_span_err!(
self.session,
ident.span,
E0603,
"{} `{}` is private",
binding.res().descr(),
ident.name,
);
if let NameBindingKind::Res(
let session = &self.session;
let mk_struct_span_error = |is_constructor| {
struct_span_err!(
session,
ident.span,
E0603,
"{}{} `{}` is private",
binding.res().descr(),
if is_constructor { " constructor"} else { "" },
ident.name,
)
};

let mut err = 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 mut err = mk_struct_span_error(true);
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",
"a constructor is private if any of the fields is private",
);
err
} else {
mk_struct_span_error(false)
}
}
} else {
mk_struct_span_error(false)
};

err.emit();
}
}
Expand Down
104 changes: 52 additions & 52 deletions src/test/ui/privacy/privacy5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,80 +48,80 @@ mod a {
}

fn this_crate() {
let a = a::A(()); //~ ERROR tuple struct `A` is private
let b = a::B(2); //~ ERROR tuple struct `B` is private
let c = a::C(2, 3); //~ ERROR tuple struct `C` is private
let a = a::A(()); //~ ERROR tuple struct constructor `A` is private
let b = a::B(2); //~ ERROR tuple struct constructor `B` is private
let c = a::C(2, 3); //~ ERROR tuple struct constructor `C` is private
let d = a::D(4);

let a::A(()) = a; //~ ERROR tuple struct `A` is private
let a::A(_) = a; //~ ERROR tuple struct `A` is private
match a { a::A(()) => {} } //~ ERROR tuple struct `A` is private
match a { a::A(_) => {} } //~ ERROR tuple struct `A` is private

let a::B(_) = b; //~ ERROR tuple struct `B` is private
let a::B(_b) = b; //~ ERROR tuple struct `B` is private
match b { a::B(_) => {} } //~ ERROR tuple struct `B` is private
match b { a::B(_b) => {} } //~ ERROR tuple struct `B` is private
match b { a::B(1) => {} a::B(_) => {} } //~ ERROR tuple struct `B` is private
//~^ ERROR tuple struct `B` is private

let a::C(_, _) = c; //~ ERROR tuple struct `C` is private
let a::C(_a, _) = c; //~ ERROR tuple struct `C` is private
let a::C(_, _b) = c; //~ ERROR tuple struct `C` is private
let a::C(_a, _b) = c; //~ ERROR tuple struct `C` is private
match c { a::C(_, _) => {} } //~ ERROR tuple struct `C` is private
match c { a::C(_a, _) => {} } //~ ERROR tuple struct `C` is private
match c { a::C(_, _b) => {} } //~ ERROR tuple struct `C` is private
match c { a::C(_a, _b) => {} } //~ ERROR tuple struct `C` is private
let a::A(()) = a; //~ ERROR tuple struct constructor `A` is private
let a::A(_) = a; //~ ERROR tuple struct constructor `A` is private
match a { a::A(()) => {} } //~ ERROR tuple struct constructor `A` is private
match a { a::A(_) => {} } //~ ERROR tuple struct constructor `A` is private

let a::B(_) = b; //~ ERROR tuple struct constructor `B` is private
let a::B(_b) = b; //~ ERROR tuple struct constructor `B` is private
match b { a::B(_) => {} } //~ ERROR tuple struct constructor `B` is private
match b { a::B(_b) => {} } //~ ERROR tuple struct constructor `B` is private
match b { a::B(1) => {} a::B(_) => {} } //~ ERROR tuple struct constructor `B` is private
//~^ ERROR tuple struct constructor `B` is private

let a::C(_, _) = c; //~ ERROR tuple struct constructor `C` is private
let a::C(_a, _) = c; //~ ERROR tuple struct constructor `C` is private
let a::C(_, _b) = c; //~ ERROR tuple struct constructor `C` is private
let a::C(_a, _b) = c; //~ ERROR tuple struct constructor `C` is private
match c { a::C(_, _) => {} } //~ ERROR tuple struct constructor `C` is private
match c { a::C(_a, _) => {} } //~ ERROR tuple struct constructor `C` is private
match c { a::C(_, _b) => {} } //~ ERROR tuple struct constructor `C` is private
match c { a::C(_a, _b) => {} } //~ ERROR tuple struct constructor `C` is private

let a::D(_) = d;
let a::D(_d) = d;
match d { a::D(_) => {} }
match d { a::D(_d) => {} }
match d { a::D(1) => {} a::D(_) => {} }

let a2 = a::A; //~ ERROR tuple struct `A` is private
let b2 = a::B; //~ ERROR tuple struct `B` is private
let c2 = a::C; //~ ERROR tuple struct `C` is private
let a2 = a::A; //~ ERROR tuple struct constructor `A` is private
let b2 = a::B; //~ ERROR tuple struct constructor `B` is private
let c2 = a::C; //~ ERROR tuple struct constructor `C` is private
let d2 = a::D;
}

fn xcrate() {
let a = other::A(()); //~ ERROR tuple struct `A` is private
let b = other::B(2); //~ ERROR tuple struct `B` is private
let c = other::C(2, 3); //~ ERROR tuple struct `C` is private
let a = other::A(()); //~ ERROR tuple struct constructor `A` is private
let b = other::B(2); //~ ERROR tuple struct constructor `B` is private
let c = other::C(2, 3); //~ ERROR tuple struct constructor `C` is private
let d = other::D(4);

let other::A(()) = a; //~ ERROR tuple struct `A` is private
let other::A(_) = a; //~ ERROR tuple struct `A` is private
match a { other::A(()) => {} } //~ ERROR tuple struct `A` is private
match a { other::A(_) => {} } //~ ERROR tuple struct `A` is private

let other::B(_) = b; //~ ERROR tuple struct `B` is private
let other::B(_b) = b; //~ ERROR tuple struct `B` is private
match b { other::B(_) => {} } //~ ERROR tuple struct `B` is private
match b { other::B(_b) => {} } //~ ERROR tuple struct `B` is private
match b { other::B(1) => {} other::B(_) => {} } //~ ERROR tuple struct `B` is private
//~^ ERROR tuple struct `B` is private

let other::C(_, _) = c; //~ ERROR tuple struct `C` is private
let other::C(_a, _) = c; //~ ERROR tuple struct `C` is private
let other::C(_, _b) = c; //~ ERROR tuple struct `C` is private
let other::C(_a, _b) = c; //~ ERROR tuple struct `C` is private
match c { other::C(_, _) => {} } //~ ERROR tuple struct `C` is private
match c { other::C(_a, _) => {} } //~ ERROR tuple struct `C` is private
match c { other::C(_, _b) => {} } //~ ERROR tuple struct `C` is private
match c { other::C(_a, _b) => {} } //~ ERROR tuple struct `C` is private
let other::A(()) = a; //~ ERROR tuple struct constructor `A` is private
let other::A(_) = a; //~ ERROR tuple struct constructor `A` is private
match a { other::A(()) => {} } //~ ERROR tuple struct constructor `A` is private
match a { other::A(_) => {} } //~ ERROR tuple struct constructor `A` is private

let other::B(_) = b; //~ ERROR tuple struct constructor `B` is private
let other::B(_b) = b; //~ ERROR tuple struct constructor `B` is private
match b { other::B(_) => {} } //~ ERROR tuple struct constructor `B` is private
match b { other::B(_b) => {} } //~ ERROR tuple struct constructor `B` is private
match b { other::B(1) => {}//~ ERROR tuple struct constructor `B` is private
other::B(_) => {} } //~ ERROR tuple struct constructor `B` is private

let other::C(_, _) = c; //~ ERROR tuple struct constructor `C` is private
let other::C(_a, _) = c; //~ ERROR tuple struct constructor `C` is private
let other::C(_, _b) = c; //~ ERROR tuple struct constructor `C` is private
let other::C(_a, _b) = c; //~ ERROR tuple struct constructor `C` is private
match c { other::C(_, _) => {} } //~ ERROR tuple struct constructor `C` is private
match c { other::C(_a, _) => {} } //~ ERROR tuple struct constructor `C` is private
match c { other::C(_, _b) => {} } //~ ERROR tuple struct constructor `C` is private
match c { other::C(_a, _b) => {} } //~ ERROR tuple struct constructor `C` is private

let other::D(_) = d;
let other::D(_d) = d;
match d { other::D(_) => {} }
match d { other::D(_d) => {} }
match d { other::D(1) => {} other::D(_) => {} }

let a2 = other::A; //~ ERROR tuple struct `A` is private
let b2 = other::B; //~ ERROR tuple struct `B` is private
let c2 = other::C; //~ ERROR tuple struct `C` is private
let a2 = other::A; //~ ERROR tuple struct constructor `A` is private
let b2 = other::B; //~ ERROR tuple struct constructor `B` is private
let c2 = other::C; //~ ERROR tuple struct constructor `C` is private
let d2 = other::D;
}

Expand Down
Loading