Repository navigation
Confusing "type parameter" in error message. #47319
Copy link
Copy link
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
I have thought this is odd for a while, however after teaching a Rust introduction class I found numerous attendees stumbling on this weird error for a simple and common case.
error[E0308]: mismatched types
--> src/codelab.rs:100:5
|
100 | tail.val
| ^^^^^^^^ expected enum `std::option::Option`, found type parameter
|
= note: expected type `std::option::Option<T>`
= note: found type `T`The "found type parameter" part is confusing, as I (and most students) interpret this as some sort of weird syntax-like error. For example it feels similar to the "expected {, found ;" you get from syntax errors.
I think that simply adding `T` after the message would make this much more understandable.
error[E0308]: mismatched types
--> src/codelab.rs:100:5
|
100 | tail.val
| ^^^^^^^^ expected enum `std::option::Option`, found type parameter `T`
|
= note: expected type `std::option::Option<T>`
= note: found type `T`Possibly more controversial is dropping "parameter" or "type parameter" I don't think they add much value to the error message.
This issue has been assigned to @dkadashev via this comment.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
Mark-Simulacrum commentedon Jan 10, 2018
Well, I think rather we can say something like "generic
T" or similar -- we probably don't want just "T" since there could be a type with that name in scope as well.Likely a rather simple fix, but I wasn't able to quickly find where this diagnostic gets emitted.
estebank commentedon Jan 10, 2018
I would go further and say that the full output should be:
I believe you could modify
note_and_explain_type_errin order to check if the error is ofTypeError::Sorts(ty.sty: TypeVariants::Param(param), _) | TypeError::Sorts(_, ty.sty: TypeVariants::Param(param))(but notTypeError::Sorts(ty.sty: TypeVariants::Param(param), ty.sty: TypeVariants::Param(param)). The hard part is to either look at the enclosing scope (meaning passing someDefIdinto the mentioned method) or expandingParamTyto hold itsSpanin order to be able to show it in errors (I think there are other cases where it'd be needed to have it, so I'd be inclined to go down that route).As for the main span's label, these are the lines where the "expected {}, found {}" is being produced. You could either enclose it with a check for an expected/found type parameter, or (better yet) modify
sort_stringto include the type argument's name.kevincox commentedon Jan 11, 2018
This seems a little unnecessary to me as the names are often obvious enough. But I'm not opposed to this.
gaurikholkar-zz commentedon Jan 11, 2018
Anyone working on this?
estebank commentedon Jan 11, 2018
Usually (except in the case of nested
impl/fnblocks), but adding that makes it so that if you're usingrustcdirectly, you have all the necessary context available before you go back to your editor, and if you're using an editor that supports the RLS, the editor will highlight the proper span. Also consider that error messages have to be understandable to both people very acquainted with the language as well as to people who have just started programming. When in doubt, I lean towards the side of over explaining.Furthermore, consider the current snippet: we don't know wether this was in an
impl, astructor anfn. Having that information available would make it easier to give more targeted explanations on stackoverflow, for example.Feel free to take it on!
jdahlstrom commentedon Jan 17, 2018
Another pretty confusing instance of this in #47499, only with two type parameters.
kevincox commentedon Jan 17, 2018
Has anyone taken ownership? I'd be willing to start a patch this weekend if no one is working on it.
estebank commentedon Jan 17, 2018
I believe @gaurikholkar wanted to take it up, but I don't know if she's already started.
gaurikholkar-zz commentedon Jan 18, 2018
I've strated working on it @kevincox
gaurikholkar-zz commentedon Jan 21, 2018
Made the following changes to my locally and WIP.
gaurikholkar-zz commentedon Jan 28, 2018
I guess we go with the
DefIdthen @estebank ?estebank commentedon Jan 28, 2018
As things stand, I believe that will be the best bet. Could you try and see how hard it would be to add a
HashMap<DefId, Span>for param ids to, I'm guessingtcx?1 remaining item
dkadashev commentedon Oct 20, 2019
Hi @estebank, if no one is working on this (which seems to be the case) then I'd like to pick this up (as a first contribution to the compiler) if that's OK.
I think it makes sense to make two separate PRs, the first one for the "modify sort_string to include the type argument's name" part and the second for the span part, since these changes are fairly unrelated to each other, and the former one is trivial and seems to be helpful on its own. Or it can be just separate commits in a single PR if that is preferable. Please let me know what's the best way here.
Also does the recommendation from #47319 (comment) still stand or is there a better way now, given the amount of time passed?
estebank commentedon Oct 24, 2019
@dkadashev I believe that is indeed the case.
dkadashev commentedon Oct 31, 2019
Status update: after playing with this for a while and hitting pretty much the same problems described in #47574 I think I finally have a fix (nice and small). Preparing a PR now (will take some time though - clean builds, etc.)
dkadashev commentedon Oct 31, 2019
@rustbot claim
dkadashev commentedon Oct 31, 2019
Some examples.
Two parameters case that was covered by #47499:
One parameter case from the top of this ticket:
Same for
impl- including type params on different levels:Rollup merge of rust-lang#66014 - dkadashev:47319-type-param-def-loca…
Rollup merge of rust-lang#66014 - dkadashev:47319-type-param-def-loca…