Skip to content

Commit 540278c

Browse files
authored
Rollup merge of rust-lang#65250 - da-x:ctor-in-error-msgs, r=petrochenkov
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
2 parents 963e4bc + 9d11bda commit 540278c

File tree

7 files changed

+196
-184
lines changed

7 files changed

+196
-184
lines changed

src/librustc_resolve/lib.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,26 +2378,38 @@ impl<'a> Resolver<'a> {
23782378
let mut reported_spans = FxHashSet::default();
23792379
for &PrivacyError(dedup_span, ident, binding) in &self.privacy_errors {
23802380
if reported_spans.insert(dedup_span) {
2381-
let mut err = struct_span_err!(
2382-
self.session,
2383-
ident.span,
2384-
E0603,
2385-
"{} `{}` is private",
2386-
binding.res().descr(),
2387-
ident.name,
2388-
);
2389-
if let NameBindingKind::Res(
2381+
let session = &self.session;
2382+
let mk_struct_span_error = |is_constructor| {
2383+
struct_span_err!(
2384+
session,
2385+
ident.span,
2386+
E0603,
2387+
"{}{} `{}` is private",
2388+
binding.res().descr(),
2389+
if is_constructor { " constructor"} else { "" },
2390+
ident.name,
2391+
)
2392+
};
2393+
2394+
let mut err = if let NameBindingKind::Res(
23902395
Res::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fn), ctor_def_id), _
23912396
) = binding.kind {
23922397
let def_id = (&*self).parent(ctor_def_id).expect("no parent for a constructor");
23932398
if let Some(fields) = self.field_names.get(&def_id) {
2399+
let mut err = mk_struct_span_error(true);
23942400
let first_field = fields.first().expect("empty field list in the map");
23952401
err.span_label(
23962402
fields.iter().fold(first_field.span, |acc, field| acc.to(field.span)),
2397-
"a tuple struct constructor is private if any of its fields is private",
2403+
"a constructor is private if any of the fields is private",
23982404
);
2405+
err
2406+
} else {
2407+
mk_struct_span_error(false)
23992408
}
2400-
}
2409+
} else {
2410+
mk_struct_span_error(false)
2411+
};
2412+
24012413
err.emit();
24022414
}
24032415
}

src/test/ui/privacy/privacy5.rs

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -48,80 +48,80 @@ mod a {
4848
}
4949

5050
fn this_crate() {
51-
let a = a::A(()); //~ ERROR tuple struct `A` is private
52-
let b = a::B(2); //~ ERROR tuple struct `B` is private
53-
let c = a::C(2, 3); //~ ERROR tuple struct `C` is private
51+
let a = a::A(()); //~ ERROR tuple struct constructor `A` is private
52+
let b = a::B(2); //~ ERROR tuple struct constructor `B` is private
53+
let c = a::C(2, 3); //~ ERROR tuple struct constructor `C` is private
5454
let d = a::D(4);
5555

56-
let a::A(()) = a; //~ ERROR tuple struct `A` is private
57-
let a::A(_) = a; //~ ERROR tuple struct `A` is private
58-
match a { a::A(()) => {} } //~ ERROR tuple struct `A` is private
59-
match a { a::A(_) => {} } //~ ERROR tuple struct `A` is private
60-
61-
let a::B(_) = b; //~ ERROR tuple struct `B` is private
62-
let a::B(_b) = b; //~ ERROR tuple struct `B` is private
63-
match b { a::B(_) => {} } //~ ERROR tuple struct `B` is private
64-
match b { a::B(_b) => {} } //~ ERROR tuple struct `B` is private
65-
match b { a::B(1) => {} a::B(_) => {} } //~ ERROR tuple struct `B` is private
66-
//~^ ERROR tuple struct `B` is private
67-
68-
let a::C(_, _) = c; //~ ERROR tuple struct `C` is private
69-
let a::C(_a, _) = c; //~ ERROR tuple struct `C` is private
70-
let a::C(_, _b) = c; //~ ERROR tuple struct `C` is private
71-
let a::C(_a, _b) = c; //~ ERROR tuple struct `C` is private
72-
match c { a::C(_, _) => {} } //~ ERROR tuple struct `C` is private
73-
match c { a::C(_a, _) => {} } //~ ERROR tuple struct `C` is private
74-
match c { a::C(_, _b) => {} } //~ ERROR tuple struct `C` is private
75-
match c { a::C(_a, _b) => {} } //~ ERROR tuple struct `C` is private
56+
let a::A(()) = a; //~ ERROR tuple struct constructor `A` is private
57+
let a::A(_) = a; //~ ERROR tuple struct constructor `A` is private
58+
match a { a::A(()) => {} } //~ ERROR tuple struct constructor `A` is private
59+
match a { a::A(_) => {} } //~ ERROR tuple struct constructor `A` is private
60+
61+
let a::B(_) = b; //~ ERROR tuple struct constructor `B` is private
62+
let a::B(_b) = b; //~ ERROR tuple struct constructor `B` is private
63+
match b { a::B(_) => {} } //~ ERROR tuple struct constructor `B` is private
64+
match b { a::B(_b) => {} } //~ ERROR tuple struct constructor `B` is private
65+
match b { a::B(1) => {} a::B(_) => {} } //~ ERROR tuple struct constructor `B` is private
66+
//~^ ERROR tuple struct constructor `B` is private
67+
68+
let a::C(_, _) = c; //~ ERROR tuple struct constructor `C` is private
69+
let a::C(_a, _) = c; //~ ERROR tuple struct constructor `C` is private
70+
let a::C(_, _b) = c; //~ ERROR tuple struct constructor `C` is private
71+
let a::C(_a, _b) = c; //~ ERROR tuple struct constructor `C` is private
72+
match c { a::C(_, _) => {} } //~ ERROR tuple struct constructor `C` is private
73+
match c { a::C(_a, _) => {} } //~ ERROR tuple struct constructor `C` is private
74+
match c { a::C(_, _b) => {} } //~ ERROR tuple struct constructor `C` is private
75+
match c { a::C(_a, _b) => {} } //~ ERROR tuple struct constructor `C` is private
7676

7777
let a::D(_) = d;
7878
let a::D(_d) = d;
7979
match d { a::D(_) => {} }
8080
match d { a::D(_d) => {} }
8181
match d { a::D(1) => {} a::D(_) => {} }
8282

83-
let a2 = a::A; //~ ERROR tuple struct `A` is private
84-
let b2 = a::B; //~ ERROR tuple struct `B` is private
85-
let c2 = a::C; //~ ERROR tuple struct `C` is private
83+
let a2 = a::A; //~ ERROR tuple struct constructor `A` is private
84+
let b2 = a::B; //~ ERROR tuple struct constructor `B` is private
85+
let c2 = a::C; //~ ERROR tuple struct constructor `C` is private
8686
let d2 = a::D;
8787
}
8888

8989
fn xcrate() {
90-
let a = other::A(()); //~ ERROR tuple struct `A` is private
91-
let b = other::B(2); //~ ERROR tuple struct `B` is private
92-
let c = other::C(2, 3); //~ ERROR tuple struct `C` is private
90+
let a = other::A(()); //~ ERROR tuple struct constructor `A` is private
91+
let b = other::B(2); //~ ERROR tuple struct constructor `B` is private
92+
let c = other::C(2, 3); //~ ERROR tuple struct constructor `C` is private
9393
let d = other::D(4);
9494

95-
let other::A(()) = a; //~ ERROR tuple struct `A` is private
96-
let other::A(_) = a; //~ ERROR tuple struct `A` is private
97-
match a { other::A(()) => {} } //~ ERROR tuple struct `A` is private
98-
match a { other::A(_) => {} } //~ ERROR tuple struct `A` is private
99-
100-
let other::B(_) = b; //~ ERROR tuple struct `B` is private
101-
let other::B(_b) = b; //~ ERROR tuple struct `B` is private
102-
match b { other::B(_) => {} } //~ ERROR tuple struct `B` is private
103-
match b { other::B(_b) => {} } //~ ERROR tuple struct `B` is private
104-
match b { other::B(1) => {} other::B(_) => {} } //~ ERROR tuple struct `B` is private
105-
//~^ ERROR tuple struct `B` is private
106-
107-
let other::C(_, _) = c; //~ ERROR tuple struct `C` is private
108-
let other::C(_a, _) = c; //~ ERROR tuple struct `C` is private
109-
let other::C(_, _b) = c; //~ ERROR tuple struct `C` is private
110-
let other::C(_a, _b) = c; //~ ERROR tuple struct `C` is private
111-
match c { other::C(_, _) => {} } //~ ERROR tuple struct `C` is private
112-
match c { other::C(_a, _) => {} } //~ ERROR tuple struct `C` is private
113-
match c { other::C(_, _b) => {} } //~ ERROR tuple struct `C` is private
114-
match c { other::C(_a, _b) => {} } //~ ERROR tuple struct `C` is private
95+
let other::A(()) = a; //~ ERROR tuple struct constructor `A` is private
96+
let other::A(_) = a; //~ ERROR tuple struct constructor `A` is private
97+
match a { other::A(()) => {} } //~ ERROR tuple struct constructor `A` is private
98+
match a { other::A(_) => {} } //~ ERROR tuple struct constructor `A` is private
99+
100+
let other::B(_) = b; //~ ERROR tuple struct constructor `B` is private
101+
let other::B(_b) = b; //~ ERROR tuple struct constructor `B` is private
102+
match b { other::B(_) => {} } //~ ERROR tuple struct constructor `B` is private
103+
match b { other::B(_b) => {} } //~ ERROR tuple struct constructor `B` is private
104+
match b { other::B(1) => {}//~ ERROR tuple struct constructor `B` is private
105+
other::B(_) => {} } //~ ERROR tuple struct constructor `B` is private
106+
107+
let other::C(_, _) = c; //~ ERROR tuple struct constructor `C` is private
108+
let other::C(_a, _) = c; //~ ERROR tuple struct constructor `C` is private
109+
let other::C(_, _b) = c; //~ ERROR tuple struct constructor `C` is private
110+
let other::C(_a, _b) = c; //~ ERROR tuple struct constructor `C` is private
111+
match c { other::C(_, _) => {} } //~ ERROR tuple struct constructor `C` is private
112+
match c { other::C(_a, _) => {} } //~ ERROR tuple struct constructor `C` is private
113+
match c { other::C(_, _b) => {} } //~ ERROR tuple struct constructor `C` is private
114+
match c { other::C(_a, _b) => {} } //~ ERROR tuple struct constructor `C` is private
115115

116116
let other::D(_) = d;
117117
let other::D(_d) = d;
118118
match d { other::D(_) => {} }
119119
match d { other::D(_d) => {} }
120120
match d { other::D(1) => {} other::D(_) => {} }
121121

122-
let a2 = other::A; //~ ERROR tuple struct `A` is private
123-
let b2 = other::B; //~ ERROR tuple struct `B` is private
124-
let c2 = other::C; //~ ERROR tuple struct `C` is private
122+
let a2 = other::A; //~ ERROR tuple struct constructor `A` is private
123+
let b2 = other::B; //~ ERROR tuple struct constructor `B` is private
124+
let c2 = other::C; //~ ERROR tuple struct constructor `C` is private
125125
let d2 = other::D;
126126
}
127127

0 commit comments

Comments
 (0)