Skip to content

typeck: minor pattern typing improvements #70386

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

Merged
merged 4 commits into from
Mar 26, 2020
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
39 changes: 27 additions & 12 deletions src/librustc_typeck/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
on_error();
return tcx.types.err;
}
Res::Def(DefKind::AssocConst, _) | Res::Def(DefKind::AssocFn, _) => {
Res::Def(DefKind::AssocConst | DefKind::AssocFn, _) => {
report_unexpected_res(res);
return tcx.types.err;
}
Expand Down Expand Up @@ -1020,7 +1020,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty::Adt(adt, substs) => (substs, adt),
_ => span_bug!(pat.span, "struct pattern is not an ADT"),
};
let kind_name = adt.variant_descr();

// Index the struct fields' types.
let field_map = variant
Expand Down Expand Up @@ -1074,7 +1073,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

if !inexistent_fields.is_empty() && !variant.recovered {
self.error_inexistent_fields(
kind_name,
adt.variant_descr(),
&inexistent_fields,
&mut unmentioned_fields,
variant,
Expand All @@ -1083,18 +1082,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

// Require `..` if struct has non_exhaustive attribute.
if variant.is_field_list_non_exhaustive() && !adt.did.is_local() && !etc {
struct_span_err!(
tcx.sess,
pat.span,
E0638,
"`..` required with {} marked as non-exhaustive",
kind_name
)
.emit();
self.error_foreign_non_exhaustive_spat(pat, adt.variant_descr(), fields.is_empty());
}

// Report an error if incorrect number of the fields were specified.
if kind_name == "union" {
if adt.is_union() {
if fields.len() != 1 {
tcx.sess
.struct_span_err(pat.span, "union patterns should have exactly one field")
Expand All @@ -1109,6 +1101,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
no_field_errors
}

fn error_foreign_non_exhaustive_spat(&self, pat: &Pat<'_>, descr: &str, no_fields: bool) {
let sess = self.tcx.sess;
let sm = sess.source_map();
let sp_brace = sm.end_point(pat.span);
let sp_comma = sm.end_point(pat.span.with_hi(sp_brace.hi()));
let sugg = if no_fields || sp_brace != sp_comma { ".. }" } else { ", .. }" };

let mut err = struct_span_err!(
sess,
pat.span,
E0638,
"`..` required with {} marked as non-exhaustive",
descr
);
err.span_suggestion_verbose(
sp_comma,
"add `..` at the end of the field list to ignore all other fields",
sugg.to_string(),
Applicability::MachineApplicable,
);
err.emit();
}

fn error_field_already_bound(&self, span: Span, ident: ast::Ident, other_field: Span) {
struct_span_err!(
self.tcx.sess,
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/rfc-2008-non-exhaustive/struct.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,33 @@ error[E0638]: `..` required with struct marked as non-exhaustive
|
LL | let NormalStruct { first_field, second_field } = ns;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: add `..` at the end of the field list to ignore all other fields
|
LL | let NormalStruct { first_field, second_field , .. } = ns;
| ^^^^^^
Comment on lines +68 to +69
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like it if we could get around pointing at the closing brace and the preceding space, but that would complicate the span gather code quite dramatically...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Trust me, I tried several clever iterations to fix that but I got nowhere. :)


error[E0638]: `..` required with struct marked as non-exhaustive
--> $DIR/struct.rs:26:9
|
LL | let TupleStruct { 0: first_field, 1: second_field } = ts;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: add `..` at the end of the field list to ignore all other fields
|
LL | let TupleStruct { 0: first_field, 1: second_field , .. } = ts;
| ^^^^^^

error[E0638]: `..` required with struct marked as non-exhaustive
--> $DIR/struct.rs:35:9
|
LL | let UnitStruct { } = us;
| ^^^^^^^^^^^^^^
|
help: add `..` at the end of the field list to ignore all other fields
|
LL | let UnitStruct { .. } = us;
| ^^^^

error: aborting due to 9 previous errors

Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/rfc-2008-non-exhaustive/variant.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,22 @@ error[E0638]: `..` required with variant marked as non-exhaustive
|
LL | NonExhaustiveVariants::Struct { field } => ""
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: add `..` at the end of the field list to ignore all other fields
|
LL | NonExhaustiveVariants::Struct { field , .. } => ""
| ^^^^^^

error[E0638]: `..` required with variant marked as non-exhaustive
--> $DIR/variant.rs:30:12
|
LL | if let NonExhaustiveVariants::Struct { field } = variant_struct {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: add `..` at the end of the field list to ignore all other fields
|
LL | if let NonExhaustiveVariants::Struct { field , .. } = variant_struct {
| ^^^^^^

error: aborting due to 8 previous errors

Expand Down