From f7ecf1c54806d0a2b614510b46d1b25c178450f0 Mon Sep 17 00:00:00 2001 From: Cedric Date: Mon, 17 Jun 2019 20:04:26 +0200 Subject: [PATCH] suggest tuple struct syntax --- src/librustc_typeck/check/expr.rs | 71 +++++++++++++---------- src/test/ui/issues/issue-4736.stderr | 5 +- src/test/ui/numeric/numeric-fields.stderr | 7 ++- 3 files changed, 49 insertions(+), 34 deletions(-) diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index fa9e0d8a857..06d07d99242 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -1215,38 +1215,49 @@ fn report_unknown_field( } }, ty); - // prevent all specified fields from being suggested - let skip_fields = skip_fields.iter().map(|ref x| x.ident.as_str()); - if let Some(field_name) = Self::suggest_field_name(variant, - &field.ident.as_str(), - skip_fields.collect()) { - err.span_suggestion( - field.ident.span, - "a field with a similar name exists", - field_name.to_string(), - Applicability::MaybeIncorrect, - ); - } else { - match ty.sty { - ty::Adt(adt, ..) => { - if adt.is_enum() { - err.span_label(field.ident.span, - format!("`{}::{}` does not have this field", - ty, variant.ident)); - } else { - err.span_label(field.ident.span, - format!("`{}` does not have this field", ty)); - } - let available_field_names = self.available_field_names(variant); - if !available_field_names.is_empty() { - err.note(&format!("available fields are: {}", - self.name_series_display(available_field_names))); + match variant.ctor_kind { + CtorKind::Fn => { + err.span_label(field.ident.span, "field does not exist"); + err.span_label( + field.ident.span, + format!("`{adt}` is a tuple {kind_name}, use the appropriate syntax: `{adt}(/* fields */)`", adt=ty, kind_name=kind_name) + ); + } + _ => { + // prevent all specified fields from being suggested + let skip_fields = skip_fields.iter().map(|ref x| x.ident.as_str()); + if let Some(field_name) = Self::suggest_field_name(variant, + &field.ident.as_str(), + skip_fields.collect()) { + err.span_suggestion( + field.ident.span, + "a field with a similar name exists", + field_name.to_string(), + Applicability::MaybeIncorrect, + ); + } else { + match ty.sty { + ty::Adt(adt, ..) => { + if adt.is_enum() { + err.span_label(field.ident.span, + format!("`{}::{}` does not have this field", + ty, variant.ident)); + } else { + err.span_label(field.ident.span, + format!("`{}` does not have this field", ty)); + } + let available_field_names = self.available_field_names(variant); + if !available_field_names.is_empty() { + err.note(&format!("available fields are: {}", + self.name_series_display(available_field_names))); + } + } + _ => bug!("non-ADT passed to report_unknown_field") } - } - _ => bug!("non-ADT passed to report_unknown_field") + }; } - }; - err.emit(); + } + err.emit(); } // Return an hint about the closest match in field names diff --git a/src/test/ui/issues/issue-4736.stderr b/src/test/ui/issues/issue-4736.stderr index b4ac12643bc..557ee5593f3 100644 --- a/src/test/ui/issues/issue-4736.stderr +++ b/src/test/ui/issues/issue-4736.stderr @@ -2,7 +2,10 @@ error[E0560]: struct `NonCopyable` has no field named `p` --> $DIR/issue-4736.rs:4:26 | LL | let z = NonCopyable{ p: () }; - | ^ help: a field with a similar name exists: `0` + | ^ + | | + | field does not exist + | `NonCopyable` is a tuple struct, use the appropriate syntax: `NonCopyable(/* fields */)` error: aborting due to previous error diff --git a/src/test/ui/numeric/numeric-fields.stderr b/src/test/ui/numeric/numeric-fields.stderr index 13c18d740fc..fef7486b853 100644 --- a/src/test/ui/numeric/numeric-fields.stderr +++ b/src/test/ui/numeric/numeric-fields.stderr @@ -2,9 +2,10 @@ error[E0560]: struct `S` has no field named `0b1` --> $DIR/numeric-fields.rs:4:15 | LL | let s = S{0b1: 10, 0: 11}; - | ^^^ `S` does not have this field - | - = note: available fields are: `0`, `1` + | ^^^ + | | + | field does not exist + | `S` is a tuple struct, use the appropriate syntax: `S(/* fields */)` error[E0026]: struct `S` does not have a field named `0x1` --> $DIR/numeric-fields.rs:7:17 -- 2.44.0