Skip to content

Commit 73e35ee

Browse files
committed
Fix incorrect trailing comma suggested in no_accessible_fields
1 parent 9f6cd6d commit 73e35ee

4 files changed

Lines changed: 75 additions & 2 deletions

File tree

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,10 +2434,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24342434
.struct_span_err(pat.span, "pattern requires `..` due to inaccessible fields");
24352435

24362436
if let Some(field) = fields.last() {
2437+
let tail_span = field.span.shrink_to_hi().to(pat.span.shrink_to_hi());
2438+
let comma_hi_offset =
2439+
self.tcx.sess.source_map().span_to_snippet(tail_span).ok().and_then(|snippet| {
2440+
let trimmed = snippet.trim_start();
2441+
trimmed.starts_with(',').then(|| (snippet.len() - trimmed.len() + 1) as u32)
2442+
});
24372443
err.span_suggestion_verbose(
2438-
field.span.shrink_to_hi(),
2444+
if let Some(comma_hi_offset) = comma_hi_offset {
2445+
tail_span.with_hi(tail_span.lo() + BytePos(comma_hi_offset)).shrink_to_hi()
2446+
} else {
2447+
field.span.shrink_to_hi()
2448+
},
24392449
"ignore the inaccessible and unused fields",
2440-
", ..",
2450+
if comma_hi_offset.is_some() { " .." } else { ", .." },
24412451
Applicability::MachineApplicable,
24422452
);
24432453
} else {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![allow(dead_code)]
2+
#![allow(unused)]
3+
//@ run-rustfix
4+
5+
mod m {
6+
pub(crate) struct S {
7+
pub(crate) visible: u64,
8+
hidden: u64,
9+
}
10+
11+
impl S {
12+
pub(crate) fn new() -> Self {
13+
loop {}
14+
}
15+
}
16+
}
17+
18+
fn main() {
19+
let m::S {
20+
//~^ ERROR pattern requires `..` due to inaccessible fields
21+
visible, ..
22+
} = m::S::new();
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![allow(dead_code)]
2+
#![allow(unused)]
3+
//@ run-rustfix
4+
5+
mod m {
6+
pub(crate) struct S {
7+
pub(crate) visible: u64,
8+
hidden: u64,
9+
}
10+
11+
impl S {
12+
pub(crate) fn new() -> Self {
13+
loop {}
14+
}
15+
}
16+
}
17+
18+
fn main() {
19+
let m::S {
20+
//~^ ERROR pattern requires `..` due to inaccessible fields
21+
visible,
22+
} = m::S::new();
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: pattern requires `..` due to inaccessible fields
2+
--> $DIR/inaccessible-private-fields-trailing-comma-149787.rs:19:9
3+
|
4+
LL | let m::S {
5+
| _________^
6+
LL | |
7+
LL | | visible,
8+
LL | | } = m::S::new();
9+
| |_____^
10+
|
11+
help: ignore the inaccessible and unused fields
12+
|
13+
LL | visible, ..
14+
| ++
15+
16+
error: aborting due to 1 previous error
17+

0 commit comments

Comments
 (0)