Skip to content

Commit

Permalink
Rollup merge of rust-lang#65250 - da-x:ctor-in-error-msgs, r=petroche…
Browse files Browse the repository at this point in the history
…nkov

resolve: fix error title regarding private constructors

One reason is that constructors can be private while their types can be
public.

Idea credit to @petrochenkov, discussed at rust-lang#65153
  • Loading branch information
Centril committed Oct 13, 2019
2 parents 963e4bc + 9d11bda commit 540278c
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 184 deletions.
34 changes: 23 additions & 11 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2378,26 +2378,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

0 comments on commit 540278c

Please sign in to comment.