librustc: Don't ICE when operator traits are not implemented properly. #12638
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.
From my comment on #11450:
The reason for the ICE is because for operators
rustc
does a little bit of magic. Notice that while you implement theMul
trait for some type&T
(i.e a reference to some T), you can simply doVec2 {..} * 2.0f32
. That is,2.0f32
isf32
and not&f32
. This works becauserustc
will automatically take a reference. So what's happening is that withfoo * T
, the compiler is expecting themul
method to take some&U
and then it can compare to make sureT == U
(or more specifically thatT
coerces toU
). But in this case, the argument of themul
method is not a reference and hence the "no ref" error.I don't think we should ICE in this case since we do catch the mismatched trait/impl method and hence provide a better error message that way.
Fixes #11450