-
Notifications
You must be signed in to change notification settings - Fork 13k
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
tuple struct expressions with non-visible (private) fields should bias towards suggesting methods #103215
Comments
Here's another instance of this issue I came across in #rust-beginners on the community discord (https://discord.com/channels/273534239310479360/273541522815713281/1036653255175979058) use std::collections::HashMap;
struct PassportBuilder {
fields: HashMap<String, String>,
}
let mut builder = PassportBuilder{ fields: HashMap<String, String>{} }; raising this error - error[[E0423]](https://doc.rust-lang.org/stable/error-index.html#E0423): expected value, found struct `HashMap`
--> src/main.rs:6:44
|
6 | let mut builder = PassportBuilder{ fields: HashMap<String, String>{} };
| ^^^^^^^ help: use struct literal syntax instead: `HashMap { base: val }` this one minimises to |
The original MCVE was fixed by #116858. Plecra's example was not fixed, but a very slightly modified version was fixed: use std::collections::HashMap;
struct PassportBuilder {
fields: HashMap<String, String>,
}
let mut builder = PassportBuilder { fields: HashMap::<String, String>{} }; (note the turbofish after Considering that Plecra's version without the turbofish is deeply broken syntax-wise, I think it's fine to not have it emit a helpful diagnostic. Diagnostics when not using turbofish
Since |
Given the following code (play):
The current output is:
Ideally the output should look like:
In other words: If a type from an external crate has non-public fields, we should not be suggesting the user attempt to use them for constructing an instance of the type. Especially in the case of tuple struct expressions, which can be a sign of an omitted constructor method name. (I'm thinking specifically of people who are coming from Java, and thus may be used to writing something like
new BufReader(f)
where we would writeBufReader::new(f)
.)A simple heuristic here could be to look for methods on the type that 1. do not take
self
parameter, and 2. (optional) have a matching number of argumentsThis issue is similar to #66067 and #52144, except worse, because (if I understand them correctly) those two are both talking about improving the UX for incorrect tuple struct expressions that at least are also referring to tuple struct definitions.
In the scenario described here, we have a record struct definition being used as a tuple struct, which, in the presence of private fields, we should interpret as a strong hint that someone probably meant to use a method.
The text was updated successfully, but these errors were encountered: