Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak suggestion for missing field in patterns #81416

Merged
merged 1 commit into from
Jan 28, 2021
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
25 changes: 7 additions & 18 deletions compiler/rustc_typeck/src/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1525,24 +1525,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => return err,
},
[.., field] => {
// if last field has a trailing comma, use the comma
// as the span to avoid trailing comma in ultimate
// suggestion (Issue #78511)
let tail = field.span.shrink_to_hi().until(pat.span.shrink_to_hi());
let tail_through_comma = self.tcx.sess.source_map().span_through_char(tail, ',');
let sp = if tail_through_comma == tail {
field.span.shrink_to_hi()
} else {
tail_through_comma
};
(
match pat.kind {
PatKind::Struct(_, [_, ..], _) => ", ",
_ => "",
},
"",
sp,
)
// Account for last field having a trailing comma or parse recovery at the tail of
// the pattern to avoid invalid suggestion (#78511).
let tail = field.span.shrink_to_hi().with_hi(pat.span.hi());
match &pat.kind {
PatKind::Struct(..) => (", ", " }", tail),
_ => return err,
}
}
};
err.span_suggestion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ LL | Struct { a, _ } = Struct { a: 1, b: 2 };
|
help: include the missing field in the pattern
|
LL | Struct { a, b _ } = Struct { a: 1, b: 2 };
| ^^^
LL | Struct { a, b } = Struct { a: 1, b: 2 };
| ^^^^^
help: if you don't care about this missing field, you can explicitly ignore it
|
LL | Struct { a, .. _ } = Struct { a: 1, b: 2 };
| ^^^^
LL | Struct { a, .. } = Struct { a: 1, b: 2 };
| ^^^^^^

error: aborting due to 5 previous errors

Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/error-codes/E0027.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ LL | Dog { age: x } => {}
help: include the missing field in the pattern
|
LL | Dog { age: x, name } => {}
| ^^^^^^
| ^^^^^^^^
help: if you don't care about this missing field, you can explicitly ignore it
|
LL | Dog { age: x, .. } => {}
| ^^^^
| ^^^^^^

error[E0027]: pattern does not mention field `age`
--> $DIR/E0027.rs:15:9
Expand All @@ -22,11 +22,11 @@ LL | Dog { name: x, } => {}
help: include the missing field in the pattern
|
LL | Dog { name: x, age } => {}
| ^^^^^
| ^^^^^^^
help: if you don't care about this missing field, you can explicitly ignore it
|
LL | Dog { name: x, .. } => {}
| ^^^^
| ^^^^^^

error[E0027]: pattern does not mention field `age`
--> $DIR/E0027.rs:19:9
Expand All @@ -37,11 +37,11 @@ LL | Dog { name: x , } => {}
help: include the missing field in the pattern
|
LL | Dog { name: x, age } => {}
| ^^^^^
| ^^^^^^^
help: if you don't care about this missing field, you can explicitly ignore it
|
LL | Dog { name: x, .. } => {}
| ^^^^
| ^^^^^^

error[E0027]: pattern does not mention fields `name`, `age`
--> $DIR/E0027.rs:22:9
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/structs/struct-pat-derived-error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ LL | let A { x, y } = self.d;
help: include the missing fields in the pattern
|
LL | let A { x, y, b, c } = self.d;
| ^^^^^^
| ^^^^^^^^
help: if you don't care about these missing fields, you can explicitly ignore them
|
LL | let A { x, y, .. } = self.d;
| ^^^^
| ^^^^^^

error: aborting due to 3 previous errors

Expand Down