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

suggest tuple struct syntax #61782

Merged
merged 5 commits into from
Jun 20, 2019
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
81 changes: 51 additions & 30 deletions src/librustc_typeck/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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)));
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")
}
}
_ => bug!("non-ADT passed to report_unknown_field")
};
}
};
}
err.emit();
}

Expand Down
7 changes: 6 additions & 1 deletion src/test/ui/issues/issue-4736.stderr
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 6 additions & 3 deletions src/test/ui/numeric/numeric-fields.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand Down