Skip to content
Closed
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
19 changes: 17 additions & 2 deletions compiler/rustc_hir_typeck/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2436,10 +2436,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.struct_span_err(pat.span, "pattern requires `..` due to inaccessible fields");

if let Some(field) = fields.last() {
// `..` does not allow a trailing comma, so if the last field already
// ends with one, insert `" .."` right after that comma rather than
// appending `", .."` after the field (which would produce `field, ..,`).
let sm = self.tcx.sess.source_map();
let after_field = field.span.shrink_to_hi();
let tail_span = after_field.with_hi(pat.span.hi());
// `span_through_char` scans past whitespace and comments, so this
// correctly handles cases like `field /*comment*/,`.
let comma_span = sm.span_through_char(tail_span, ',');
let (span, insertion) = if comma_span != tail_span {
// A comma was found; insert ` ..` right after it.
(comma_span.shrink_to_hi(), " ..")
} else {
(after_field, ", ..")
Comment on lines +2444 to +2452
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has_trailing_comma only skips whitespace (trim_start()), so cases like Foo { field /*comment*/, } won’t be detected as having a trailing comma. In that situation this code will take the else branch and insert ", .." immediately after the field, leaving the original comma later and still producing an invalid suggestion (field, .. /*comment*/,). Consider skipping over line/block comments (and whitespace) when checking what comes after the field, or scanning the tail snippet for the first non-trivia token and checking whether it’s a comma before deciding the insertion point.

Copilot uses AI. Check for mistakes.
};
err.span_suggestion_verbose(
field.span.shrink_to_hi(),
span,
"ignore the inaccessible and unused fields",
", ..",
insertion,
Applicability::MachineApplicable,
);
Comment on lines 2454 to 2459
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes a MachineApplicable rustfix suggestion in a way that depends on source formatting (presence of a trailing comma). There are existing UI/rustfix tests for this diagnostic (e.g. tests/ui/privacy/inaccessible-private-fields-76077.rs), but none appear to cover the Foo { visible, } case that triggered #149787. Please add/extend a UI test that includes a struct pattern with a trailing comma and asserts the suggested fix is visible, .. (without producing ..,) and that // @run-rustfix still applies cleanly.

Copilot uses AI. Check for mistakes.
} else {
Expand Down
Loading