-
Notifications
You must be signed in to change notification settings - Fork 13.4k
field does not exist error: note fields if Levenshtein suggestion fails #43442
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
bors
merged 2 commits into
rust-lang:master
from
zackmdavis:note_available_field_names_if_levenshtein_fails
Aug 4, 2017
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2956,6 +2956,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |
format!("did you mean `{}`?", suggested_field_name)); | ||
} else { | ||
err.span_label(field.span, "unknown field"); | ||
let struct_variant_def = def.struct_variant(); | ||
let available_field_names = self.available_field_names( | ||
struct_variant_def); | ||
err.note(&format!("available fields are: {}", | ||
available_field_names.join(", "))); | ||
}; | ||
} | ||
ty::TyRawPtr(..) => { | ||
|
@@ -2979,7 +2984,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |
// Return an hint about the closest match in field names | ||
fn suggest_field_name(variant: &'tcx ty::VariantDef, | ||
field: &Spanned<ast::Name>, | ||
skip : Vec<InternedString>) | ||
skip: Vec<InternedString>) | ||
-> Option<Symbol> { | ||
let name = field.node.as_str(); | ||
let names = variant.fields.iter().filter_map(|field| { | ||
|
@@ -2992,8 +2997,18 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |
} | ||
}); | ||
|
||
// only find fits with at least one matching letter | ||
find_best_match_for_name(names, &name, Some(name.len())) | ||
find_best_match_for_name(names, &name, None) | ||
} | ||
|
||
fn available_field_names(&self, variant: &'tcx ty::VariantDef) -> Vec<String> { | ||
let mut available = Vec::new(); | ||
for field in variant.fields.iter() { | ||
let (_, def_scope) = self.tcx.adjust(field.name, variant.did, self.body_id); | ||
if field.vis.is_accessible_from(def_scope, self.tcx) { | ||
available.push(field.name.to_string()); | ||
} | ||
} | ||
available | ||
} | ||
|
||
// Check tuple index expressions | ||
|
@@ -3107,14 +3122,22 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |
format!("field does not exist - did you mean `{}`?", field_name)); | ||
} else { | ||
match ty.sty { | ||
ty::TyAdt(adt, ..) if adt.is_enum() => { | ||
err.span_label(field.name.span, format!("`{}::{}` does not have this field", | ||
ty, variant.name)); | ||
} | ||
_ => { | ||
err.span_label(field.name.span, format!("`{}` does not have this field", ty)); | ||
ty::TyAdt(adt, ..) => { | ||
if adt.is_enum() { | ||
err.span_label(field.name.span, | ||
format!("`{}::{}` does not have this field", | ||
ty, variant.name)); | ||
} else { | ||
err.span_label(field.name.span, | ||
format!("`{}` does not have this field", ty)); | ||
} | ||
let available_field_names = self.available_field_names(variant); | ||
err.note(&format!("available fields are: {}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, of course |
||
available_field_names.join(", "))); | ||
} | ||
_ => bug!("non-ADT passed to report_unknown_field") | ||
} | ||
|
||
}; | ||
err.emit(); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/test/ui/did_you_mean/issue-42599_available_fields_note.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
mod submodule { | ||
|
||
#[derive(Default)] | ||
pub struct Demo { | ||
pub favorite_integer: isize, | ||
secret_integer: isize, | ||
pub innocently_misspellable: () | ||
} | ||
|
||
impl Demo { | ||
fn new_with_secret_two() -> Self { | ||
Self { secret_integer: 2, inocently_mispellable: () } | ||
} | ||
|
||
fn new_with_secret_three() -> Self { | ||
Self { secret_integer: 3, egregiously_nonexistent_field: () } | ||
} | ||
} | ||
|
||
} | ||
|
||
fn main() { | ||
use submodule::Demo; | ||
|
||
let demo = Demo::default(); | ||
let innocent_field_misaccess = demo.inocently_mispellable; | ||
// note shouldn't suggest private `secret_integer` field | ||
let egregious_field_misaccess = demo.egregiously_nonexistent_field; | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/ui/did_you_mean/issue-42599_available_fields_note.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
error[E0560]: struct `submodule::Demo` has no field named `inocently_mispellable` | ||
--> $DIR/issue-42599_available_fields_note.rs:22:39 | ||
| | ||
22 | Self { secret_integer: 2, inocently_mispellable: () } | ||
| ^^^^^^^^^^^^^^^^^^^^^^ field does not exist - did you mean `innocently_misspellable`? | ||
|
||
error[E0560]: struct `submodule::Demo` has no field named `egregiously_nonexistent_field` | ||
--> $DIR/issue-42599_available_fields_note.rs:26:39 | ||
| | ||
26 | Self { secret_integer: 3, egregiously_nonexistent_field: () } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `submodule::Demo` does not have this field | ||
| | ||
= note: available fields are: favorite_integer, secret_integer, innocently_misspellable | ||
|
||
error[E0609]: no field `inocently_mispellable` on type `submodule::Demo` | ||
--> $DIR/issue-42599_available_fields_note.rs:36:41 | ||
| | ||
36 | let innocent_field_misaccess = demo.inocently_mispellable; | ||
| ^^^^^^^^^^^^^^^^^^^^^ did you mean `innocently_misspellable`? | ||
|
||
error[E0609]: no field `egregiously_nonexistent_field` on type `submodule::Demo` | ||
--> $DIR/issue-42599_available_fields_note.rs:38:42 | ||
| | ||
38 | let egregious_field_misaccess = demo.egregiously_nonexistent_field; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unknown field | ||
| | ||
= note: available fields are: favorite_integer, innocently_misspellable | ||
|
||
error: aborting due to 4 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm; ideally we would put backticks around these field names. But, more importantly, we generally have a rule that we only list out things exhaustively up to a certain limit (typically 5), and then after that we will say "...and others" or something. See e.g. this code in method suggestions.