Skip to content

Commit 9bab0f0

Browse files
committed
Hide suggestion to use struct ctor when it is not visible
1 parent bb345a0 commit 9bab0f0

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/librustc_resolve/lib.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,7 +2568,8 @@ impl<'a> Resolver<'a> {
25682568
let code = source.error_code(def.is_some());
25692569
let (base_msg, fallback_label, base_span) = if let Some(def) = def {
25702570
(format!("expected {}, found {} `{}`", expected, def.kind_name(), path_str),
2571-
format!("not a {}", expected), span)
2571+
format!("not a {}", expected),
2572+
span)
25722573
} else {
25732574
let item_str = path[path.len() - 1].node;
25742575
let item_span = path[path.len() - 1].span;
@@ -2585,7 +2586,8 @@ impl<'a> Resolver<'a> {
25852586
(mod_prefix, format!("`{}`", names_to_string(mod_path)))
25862587
};
25872588
(format!("cannot find {} `{}` in {}{}", expected, item_str, mod_prefix, mod_str),
2588-
format!("not found in {}", mod_str), item_span)
2589+
format!("not found in {}", mod_str),
2590+
item_span)
25892591
};
25902592
let code = DiagnosticId::Error(code.into());
25912593
let mut err = this.session.struct_span_err_with_code(base_span, &base_msg, code);
@@ -2701,17 +2703,21 @@ impl<'a> Resolver<'a> {
27012703
return (err, candidates);
27022704
},
27032705
_ if ns == ValueNS && is_struct_like(def) => {
2706+
let mut accessible_ctor = true;
27042707
if let Def::Struct(def_id) = def {
27052708
if let Some((ctor_def, ctor_vis))
27062709
= this.struct_constructors.get(&def_id).cloned() {
2707-
if is_expected(ctor_def) && !this.is_accessible(ctor_vis) {
2710+
accessible_ctor = this.is_accessible(ctor_vis);
2711+
if is_expected(ctor_def) && !accessible_ctor {
27082712
err.span_label(span, format!("constructor is not visible \
27092713
here due to private fields"));
27102714
}
27112715
}
27122716
}
2713-
err.span_label(span, format!("did you mean `{} {{ /* fields */ }}`?",
2714-
path_str));
2717+
if accessible_ctor {
2718+
err.span_label(span, format!("did you mean `{} {{ /* fields */ }}`?",
2719+
path_str));
2720+
}
27152721
return (err, candidates);
27162722
}
27172723
_ => {}

src/test/ui/resolve/privacy-struct-ctor.stderr

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ error[E0423]: expected value, found struct `Z`
66
| |
77
| did you mean `S`?
88
| constructor is not visible here due to private fields
9-
| did you mean `Z { /* fields */ }`?
109
help: possible better candidate is found in another module, you can import it into scope
1110
|
1211
22 | use m::n::Z;
@@ -16,10 +15,7 @@ error[E0423]: expected value, found struct `S`
1615
--> $DIR/privacy-struct-ctor.rs:35:5
1716
|
1817
35 | S;
19-
| ^
20-
| |
21-
| constructor is not visible here due to private fields
22-
| did you mean `S { /* fields */ }`?
18+
| ^ constructor is not visible here due to private fields
2319
help: possible better candidate is found in another module, you can import it into scope
2420
|
2521
31 | use m::S;
@@ -29,10 +25,7 @@ error[E0423]: expected value, found struct `xcrate::S`
2925
--> $DIR/privacy-struct-ctor.rs:40:5
3026
|
3127
40 | xcrate::S;
32-
| ^^^^^^^^^
33-
| |
34-
| constructor is not visible here due to private fields
35-
| did you mean `xcrate::S { /* fields */ }`?
28+
| ^^^^^^^^^ constructor is not visible here due to private fields
3629
help: possible better candidate is found in another module, you can import it into scope
3730
|
3831
31 | use m::S;

0 commit comments

Comments
 (0)