diff options
| author | Michael Goulet <michael@errs.io> | 2023-04-06 16:52:17 +0000 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2023-04-06 16:52:17 +0000 |
| commit | 8ed2dc0bcee8c7ca43d2b6a66c9743cacf8dcddf (patch) | |
| tree | 1e6642b2803f0e19edc45a0c36311f5788a82a7e /compiler/rustc_resolve/src | |
| parent | 00d54c879bfb6adb5b7a611af6bd522499d07a1d (diff) | |
| download | rust-8ed2dc0bcee8c7ca43d2b6a66c9743cacf8dcddf.tar.gz rust-8ed2dc0bcee8c7ca43d2b6a66c9743cacf8dcddf.zip | |
Make span a bit better
Diffstat (limited to 'compiler/rustc_resolve/src')
| -rw-r--r-- | compiler/rustc_resolve/src/diagnostics.rs | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index bb473d62a7b..0c9d306081e 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1608,11 +1608,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { struct_span_err!(self.tcx.sess, ident.span, E0603, "{} `{}` is private", descr, ident); err.span_label(ident.span, &format!("private {}", descr)); + let mut non_exhaustive = None; + // If an ADT is foreign and marked as `non_exhaustive`, then that's + // probably why we have the privacy error. + // Otherwise, point out if the struct has any private fields. if let Some(def_id) = res.opt_def_id() && !def_id.is_local() && let Some(attr) = self.tcx.get_attr(def_id, sym::non_exhaustive) { - err.span_label(attr.span, format!("the {nonimport_descr} is `#[non_exhaustive]`")); + non_exhaustive = Some(attr.span); } else if let Some(span) = ctor_fields_span { err.span_label(span, "a constructor is private if any of the fields is private"); if let Res::Def(_, d) = res && let Some(fields) = self.field_visibility_spans.get(&d) { @@ -1662,6 +1666,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { if !first && binding.vis.is_public() { note_span.push_span_label(def_span, "consider importing it directly"); } + // Final step in the import chain, point out if the ADT is `non_exhaustive` + // which is probably why this privacy violation occurred. + if next_binding.is_none() && let Some(span) = non_exhaustive { + note_span.push_span_label( + span, + format!("cannot be constructed because it is `#[non_exhaustive]`"), + ); + } err.span_note(note_span, &msg); } |
