Closed
Description
Code
fn f<T>(t: &T) -> T {
t
}
Current output
error[E0308]: mismatched types
--> src/lib.rs:2:5
|
1 | fn f<T>(t: &T) -> T {
| - - expected `T` because of return type
| |
| expected this type parameter
2 | t
| ^ expected type parameter `T`, found `&T`
|
= note: expected type parameter `_`
found reference `&_`
= note: the caller chooses a type for `T` which can be different from `&T`
Desired output
error[E0308]: mismatched types
--> src/lib.rs:2:5
|
1 | fn f<T>(t: &T) -> T {
| - - expected `T` because of return type
| |
| expected this type parameter
2 | t
| ^ expected type parameter `T`, found `&T`
|
= note: expected type parameter `_`
found reference `&_`
Rationale and extra context
The statement "the caller chooses a type for T
which can be different from &T
" is confusing because T
and &T
are always different. I think it would be best to supress that note when the types only differ in reference-ness. The message was introduced in #122195.
Other cases
No response
Rust Version
rustc 1.81.0-nightly (3cf924b93 2024-06-15)
binary: rustc
commit-hash: 3cf924b934322fd7b514600a7dc84fc517515346
commit-date: 2024-06-15
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7
Anything else?
No response
Activity
jendrikw commentedon Jun 16, 2024
@rustbot label +D-confusing
fmease commentedon Jun 16, 2024
Thanks, that note definitely doesn't make sense.
CC @jieyouxu (#122195).
I suggested to move
note_caller_chooses_ty_for_ty_param
out oftry_suggest_return_impl_trait
here in order to catch more cases but that was a bit too aggressive as can be witnessed here — that's on me.We should replicate some parts of
try_suggest_return_impl_trait
, namely checking that the type param in question does not occur in the return expression type. That did cross my mind back then but I didn't really follow up on that thought for some reason.jieyouxu commentedon Jun 16, 2024
Thanks for the cc @fmease, I'll take a look ~today/tomorrow
hir_typeck: be more conservative in making "note caller chooses ty pa…
jieyouxu commentedon Jun 16, 2024
Opened #126558 to avoid making the note if found return type contains the expected return type (type parameter).
Rollup merge of rust-lang#126558 - jieyouxu:caller-chooses-ty, r=fmease
hir_typeck: be more conservative in making "note caller chooses ty pa…
Rollup merge of rust-lang#126558 - jieyouxu:caller-chooses-ty, r=fmease
Rollup merge of rust-lang#126558 - jieyouxu:caller-chooses-ty, r=fmease
Unrolled build for rust-lang#126558