E0107: "expected 1 type argument" can be read in two opposite ways: as "this needs to *have* 1 type argument" and "this needs to *be* 1 type argument" #66228
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
A-suggestion-diagnostics
Area: Suggestions generated by the compiler applied by `cargo fix`
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
D-newcomer-roadblock
Diagnostics: Confusing error or lint; hard to understand for new users.
D-papercut
Diagnostics: An error or lint that needs small tweaks.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Summary
When you have nested type arguments, underlining an inner type argument and saying 'expected 1 type argument' does not obviously mean "expected [this to have] 1 type arg (but it hasn't got any)": it can also be read as "expected [this to be] 1 type arg (but it isn't one)".
Minimal complete example
Code that gives a confusing error message:
This results in the following error message:
Expected and observed interpretation by the user
Expected interpretation:
rustc
wanted me to interpret underlining + "expected 1 type argument" as "This should have 1 type argument". In other words, this was the desired train of thought:Observed interpretation: I interpreted underlining + "expected 1 type argument" + as "There should be 1 type argument here".** That led to the following train of thought:
Some notes on the user persona: the user (me, in this case) was somebody with about 10 years experience programming R and Python; they had read The Rust Programming Language; they were working on lesson 7 of Michael Snoyman's Rust Crash Course when they encountered this error message; they lurked sometimes on the /r/rust subreddit; and they had no experience of Rust beyond that.
Not as problematic when user has passed at least 1 type arg
This particular form of the E0107 error message is printed whenever
provided type args < required type args
. But it's less ambiguous when the user has passed at least one type arg, because then the entire expression (the type and its too-few args) gets underlined:Possible fixes
Potential fix 1: change error phrasing to "expected this to have 1 type argument"
This fix is (a) an improvement on the current message, and (b) easy to implement -- a one-line change in the code. The message takes a (little!) bit more work to parse, when you break it down: knowing what 'this' refers to relies on reading the underlining and mentally discarding the type args.
Potential fix 2: mention the type's name in the error message
I like this fix a lot better, because it makes the error message self-contained and unambiguous while keeping it short. But getting the type name into the string requires more intimate knowledge of the error-reporting code, so you need a bit more codebase knowledge to fix it.
Who could do the work
The relevant code is in astconv.rs#L389-L397
If we choose fix 1, I could create a pull request for that -- it's a one-line fix.
For fix 2, I'd be willing to help out, but at the moment I'm not sure how to obtain the span of code that refers to only to the type that didn't get enough parameters. For something this unimportantant, the maintainer might get a better return on investment if they did it themselves, or opted for fix 1.
Meta
rustc --version --verbose
:This issue has been assigned to @Patryk27 via this comment.
The text was updated successfully, but these errors were encountered: