diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index b56ab6dcb4ab2..84db29fe4d7a2 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -2434,10 +2434,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .struct_span_err(pat.span, "pattern requires `..` due to inaccessible fields"); if let Some(field) = fields.last() { + let tail_span = field.span.shrink_to_hi().to(pat.span.shrink_to_hi()); + let comma_hi_offset = + self.tcx.sess.source_map().span_to_snippet(tail_span).ok().and_then(|snippet| { + let trimmed = snippet.trim_start(); + trimmed.starts_with(',').then(|| (snippet.len() - trimmed.len() + 1) as u32) + }); err.span_suggestion_verbose( - field.span.shrink_to_hi(), + if let Some(comma_hi_offset) = comma_hi_offset { + tail_span.with_hi(tail_span.lo() + BytePos(comma_hi_offset)).shrink_to_hi() + } else { + field.span.shrink_to_hi() + }, "ignore the inaccessible and unused fields", - ", ..", + if comma_hi_offset.is_some() { " .." } else { ", .." }, Applicability::MachineApplicable, ); } else { diff --git a/tests/ui/privacy/inaccessible-private-fields-trailing-comma-149787.fixed b/tests/ui/privacy/inaccessible-private-fields-trailing-comma-149787.fixed new file mode 100644 index 0000000000000..6a73475f202f4 --- /dev/null +++ b/tests/ui/privacy/inaccessible-private-fields-trailing-comma-149787.fixed @@ -0,0 +1,23 @@ +#![allow(dead_code)] +#![allow(unused)] +//@ run-rustfix + +mod m { + pub(crate) struct S { + pub(crate) visible: u64, + hidden: u64, + } + + impl S { + pub(crate) fn new() -> Self { + loop {} + } + } +} + +fn main() { + let m::S { + //~^ ERROR pattern requires `..` due to inaccessible fields + visible, .. + } = m::S::new(); +} diff --git a/tests/ui/privacy/inaccessible-private-fields-trailing-comma-149787.rs b/tests/ui/privacy/inaccessible-private-fields-trailing-comma-149787.rs new file mode 100644 index 0000000000000..1a4fed68381a1 --- /dev/null +++ b/tests/ui/privacy/inaccessible-private-fields-trailing-comma-149787.rs @@ -0,0 +1,23 @@ +#![allow(dead_code)] +#![allow(unused)] +//@ run-rustfix + +mod m { + pub(crate) struct S { + pub(crate) visible: u64, + hidden: u64, + } + + impl S { + pub(crate) fn new() -> Self { + loop {} + } + } +} + +fn main() { + let m::S { + //~^ ERROR pattern requires `..` due to inaccessible fields + visible, + } = m::S::new(); +} diff --git a/tests/ui/privacy/inaccessible-private-fields-trailing-comma-149787.stderr b/tests/ui/privacy/inaccessible-private-fields-trailing-comma-149787.stderr new file mode 100644 index 0000000000000..4eacdecc6810c --- /dev/null +++ b/tests/ui/privacy/inaccessible-private-fields-trailing-comma-149787.stderr @@ -0,0 +1,17 @@ +error: pattern requires `..` due to inaccessible fields + --> $DIR/inaccessible-private-fields-trailing-comma-149787.rs:19:9 + | +LL | let m::S { + | _________^ +LL | | +LL | | visible, +LL | | } = m::S::new(); + | |_____^ + | +help: ignore the inaccessible and unused fields + | +LL | visible, .. + | ++ + +error: aborting due to 1 previous error +