diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2017-12-31 16:35:52 -0800 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2018-01-15 02:03:02 -0800 |
| commit | 9bab0f09d30c90126993fa824171925a8fa579a7 (patch) | |
| tree | 43e8dec251bf64e2ac33dc633d7912a157df3297 | |
| parent | bb345a0be3ba3fa1970fe02789791c5c72788d8f (diff) | |
| download | rust-9bab0f09d30c90126993fa824171925a8fa579a7.tar.gz rust-9bab0f09d30c90126993fa824171925a8fa579a7.zip | |
Hide suggestion to use struct ctor when it is not visible
| -rw-r--r-- | src/librustc_resolve/lib.rs | 16 | ||||
| -rw-r--r-- | src/test/ui/resolve/privacy-struct-ctor.stderr | 11 |
2 files changed, 13 insertions, 14 deletions
diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 0a29441cef7..d67578e8c06 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -2568,7 +2568,8 @@ impl<'a> Resolver<'a> { let code = source.error_code(def.is_some()); let (base_msg, fallback_label, base_span) = if let Some(def) = def { (format!("expected {}, found {} `{}`", expected, def.kind_name(), path_str), - format!("not a {}", expected), span) + format!("not a {}", expected), + span) } else { let item_str = path[path.len() - 1].node; let item_span = path[path.len() - 1].span; @@ -2585,7 +2586,8 @@ impl<'a> Resolver<'a> { (mod_prefix, format!("`{}`", names_to_string(mod_path))) }; (format!("cannot find {} `{}` in {}{}", expected, item_str, mod_prefix, mod_str), - format!("not found in {}", mod_str), item_span) + format!("not found in {}", mod_str), + item_span) }; let code = DiagnosticId::Error(code.into()); let mut err = this.session.struct_span_err_with_code(base_span, &base_msg, code); @@ -2701,17 +2703,21 @@ impl<'a> Resolver<'a> { return (err, candidates); }, _ if ns == ValueNS && is_struct_like(def) => { + let mut accessible_ctor = true; if let Def::Struct(def_id) = def { if let Some((ctor_def, ctor_vis)) = this.struct_constructors.get(&def_id).cloned() { - if is_expected(ctor_def) && !this.is_accessible(ctor_vis) { + accessible_ctor = this.is_accessible(ctor_vis); + if is_expected(ctor_def) && !accessible_ctor { err.span_label(span, format!("constructor is not visible \ here due to private fields")); } } } - err.span_label(span, format!("did you mean `{} {{ /* fields */ }}`?", - path_str)); + if accessible_ctor { + err.span_label(span, format!("did you mean `{} {{ /* fields */ }}`?", + path_str)); + } return (err, candidates); } _ => {} diff --git a/src/test/ui/resolve/privacy-struct-ctor.stderr b/src/test/ui/resolve/privacy-struct-ctor.stderr index 39bedf59641..b688d2fc95b 100644 --- a/src/test/ui/resolve/privacy-struct-ctor.stderr +++ b/src/test/ui/resolve/privacy-struct-ctor.stderr @@ -6,7 +6,6 @@ error[E0423]: expected value, found struct `Z` | | | did you mean `S`? | constructor is not visible here due to private fields - | did you mean `Z { /* fields */ }`? help: possible better candidate is found in another module, you can import it into scope | 22 | use m::n::Z; @@ -16,10 +15,7 @@ error[E0423]: expected value, found struct `S` --> $DIR/privacy-struct-ctor.rs:35:5 | 35 | S; - | ^ - | | - | constructor is not visible here due to private fields - | did you mean `S { /* fields */ }`? + | ^ constructor is not visible here due to private fields help: possible better candidate is found in another module, you can import it into scope | 31 | use m::S; @@ -29,10 +25,7 @@ error[E0423]: expected value, found struct `xcrate::S` --> $DIR/privacy-struct-ctor.rs:40:5 | 40 | xcrate::S; - | ^^^^^^^^^ - | | - | constructor is not visible here due to private fields - | did you mean `xcrate::S { /* fields */ }`? + | ^^^^^^^^^ constructor is not visible here due to private fields help: possible better candidate is found in another module, you can import it into scope | 31 | use m::S; |
