1
Fork 0

Rollup merge of #61782 - Electron-libre:suggest_tuple_struct_syntax, r=estebank

suggest tuple struct syntax

refs #57242
This commit is contained in:
Mazdak Farrokhzad 2019-06-20 08:36:01 +02:00 committed by GitHub
commit 2ead0072b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 35 deletions

View file

@ -1125,7 +1125,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.emit();
} else {
self.report_unknown_field(adt_ty, variant, field, ast_fields, kind_name);
self.report_unknown_field(adt_ty, variant, field, ast_fields, kind_name, span);
}
tcx.types.err
@ -1196,6 +1196,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
field: &hir::Field,
skip_fields: &[hir::Field],
kind_name: &str,
ty_span: Span
) {
if variant.recovered {
return;
@ -1215,37 +1216,57 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
},
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)));
}
}
_ => bug!("non-ADT passed to report_unknown_field")
match variant.ctor_kind {
CtorKind::Fn => {
err.span_label(variant.ident.span, format!("`{adt}` defined here", adt=ty));
err.span_label(field.ident.span, "field does not exist");
err.span_label(ty_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")
}
};
}
}
err.emit();
}

View file

@ -1,8 +1,13 @@
error[E0560]: struct `NonCopyable` has no field named `p`
--> $DIR/issue-4736.rs:4:26
|
LL | struct NonCopyable(());
| ----------- `NonCopyable` defined here
...
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

View file

@ -1,10 +1,13 @@
error[E0560]: struct `S` has no field named `0b1`
--> $DIR/numeric-fields.rs:4:15
|
LL | struct S(u8, u16);
| - `S` defined here
...
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