Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions compiler/rustc_hir_typeck/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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

Loading