Point at return type when it is the source of the type expectation#153974
Point at return type when it is the source of the type expectation#153974rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Conversation
|
r? @nnethercote rustbot has assigned @nnethercote. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
I'm not married to the wording of the new span label, just couldn't come up with something that sounded better. |
This comment was marked as resolved.
This comment was marked as resolved.
There was a problem hiding this comment.
I'm willing to r+ this because it's a small improvement over the status quo. Having said that...
- According to #43608 a better message would be one that said "you forgot the semicolon".
- But I don't understand that, because the example in #43608 doesn't make much sense even with the semicolon. Why would you call
ptr::readand ignore the result? Without the original, more complicated code it's hard to know.
| --> $DIR/expectation-from-return-type.rs:5:15 | ||
| | | ||
| LL | fn main() { | ||
| | - the call expression's return type is influenced by this return type |
There was a problem hiding this comment.
Avoid the passive voice? I.e. "this return type influences the call expression's return type".
And more accurate would be "this return type influences the call expression's return type, which influences the call expression's first argument type". That fully explains the confusing chain of influences to the ^^ point below. It might also be worth naming the return type as ()?
There was a problem hiding this comment.
Figuring out the chain from the argument to the return type to the enclosing item's return type might be possible, but suspect will look convoluted and be a bit brittle, which is why I went for a simpler approach (that is not as comprehensive). I can try to do that in a bit.
|
Yeah. Suggesting a semicolon makes this code compile but it is not the semantically correct suggestion we'd give. We don't really have enough context to figure out what the user wanted. Most likely, they'd want to assign the resulting value to a binding, or change the return type. Using the function for its side-effects is only a far third likely intention. Because of that I think that not providing a suggestion and instead provide as much context as possible for the user to figure things out is a better idea. |
| "this implicit `()` return type influences the call expression's return type" | ||
| ), | ||
| FnRetTy::Return(_) => { | ||
| "this return type influences the call expression's return type" |
There was a problem hiding this comment.
Is this arm reachable? It doesn't occur in the tests, AFAICT.
There was a problem hiding this comment.
When calling an fn that returns a return type as a returned expression, point at the return type to explain that it affects the expected type.
```
error[E0308]: mismatched types
--> f56.rs:5:15
|
3 | fn main() {
| - the call expression's return type is influenced by this return type
4 | let a = 0;
5 | ptr::read(&a)
| --------- ^^ expected `*const ()`, found `&{integer}`
| |
| arguments to this function are incorrect
|
= note: expected raw pointer `*const ()`
found reference `&{integer}`
note: function defined here
--> library/core/src/ptr/mod.rs:1681:21
|
1681 | pub const unsafe fn read<T>(src: *const T) -> T {
| ^^^^
```
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@bors r+ rollup |
Point at return type when it is the source of the type expectation
When calling an fn that returns a return type as a returned expression, point at the return type to explain that it affects the expected type.
```
error[E0308]: mismatched types
--> f56.rs:5:15
|
3 | fn main() {
| - the call expression's return type is influenced by this return type
4 | let a = 0;
5 | ptr::read(&a)
| --------- ^^ expected `*const ()`, found `&{integer}`
| |
| arguments to this function are incorrect
|
= note: expected raw pointer `*const ()`
found reference `&{integer}`
note: function defined here
--> library/core/src/ptr/mod.rs:1681:21
|
1681 | pub const unsafe fn read<T>(src: *const T) -> T {
| ^^^^
```
Fix rust-lang#43608.
Point at return type when it is the source of the type expectation
When calling an fn that returns a return type as a returned expression, point at the return type to explain that it affects the expected type.
```
error[E0308]: mismatched types
--> f56.rs:5:15
|
3 | fn main() {
| - the call expression's return type is influenced by this return type
4 | let a = 0;
5 | ptr::read(&a)
| --------- ^^ expected `*const ()`, found `&{integer}`
| |
| arguments to this function are incorrect
|
= note: expected raw pointer `*const ()`
found reference `&{integer}`
note: function defined here
--> library/core/src/ptr/mod.rs:1681:21
|
1681 | pub const unsafe fn read<T>(src: *const T) -> T {
| ^^^^
```
Fix rust-lang#43608.
Point at return type when it is the source of the type expectation
When calling an fn that returns a return type as a returned expression, point at the return type to explain that it affects the expected type.
```
error[E0308]: mismatched types
--> f56.rs:5:15
|
3 | fn main() {
| - the call expression's return type is influenced by this return type
4 | let a = 0;
5 | ptr::read(&a)
| --------- ^^ expected `*const ()`, found `&{integer}`
| |
| arguments to this function are incorrect
|
= note: expected raw pointer `*const ()`
found reference `&{integer}`
note: function defined here
--> library/core/src/ptr/mod.rs:1681:21
|
1681 | pub const unsafe fn read<T>(src: *const T) -> T {
| ^^^^
```
Fix rust-lang#43608.
…uwer Rollup of 12 pull requests Successful merges: - #152870 (Do not enable split debuginfo for windows-gnu) - #153333 (Fix bootstrap rust build failure for vxworks) - #153681 (ci: add runners for vanilla LLVM 22) - #153824 (Add `Wake` diagnostic item for `alloc::task::Wake`) - #154077 (Optimize 128-bit integer formatting) - #154078 (`define_callbacks` tweaks) - #151905 (Split the `dec2flt::RawFloat` trait for easier reuse) - #153170 (Add is_disconnected functions to mpsc and mpmc channels) - #153804 (Derive Macro Eq: link to more detailed documentation) - #153974 (Point at return type when it is the source of the type expectation) - #154018 (simplify and remove `tests/ui/kindck` and related tests) - #154041 (bootstrap: Allow `--bless`ing changes to editor settings files)
Point at return type when it is the source of the type expectation
When calling an fn that returns a return type as a returned expression, point at the return type to explain that it affects the expected type.
```
error[E0308]: mismatched types
--> f56.rs:5:15
|
3 | fn main() {
| - the call expression's return type is influenced by this return type
4 | let a = 0;
5 | ptr::read(&a)
| --------- ^^ expected `*const ()`, found `&{integer}`
| |
| arguments to this function are incorrect
|
= note: expected raw pointer `*const ()`
found reference `&{integer}`
note: function defined here
--> library/core/src/ptr/mod.rs:1681:21
|
1681 | pub const unsafe fn read<T>(src: *const T) -> T {
| ^^^^
```
Fix rust-lang#43608.
…uwer Rollup of 19 pull requests Successful merges: - #152870 (Do not enable split debuginfo for windows-gnu) - #153333 (Fix bootstrap rust build failure for vxworks) - #153681 (ci: add runners for vanilla LLVM 22) - #153727 (When single impl can satisfy inference error, suggest type) - #153824 (Add `Wake` diagnostic item for `alloc::task::Wake`) - #154077 (Optimize 128-bit integer formatting) - #154078 (`define_callbacks` tweaks) - #151905 (Split the `dec2flt::RawFloat` trait for easier reuse) - #153170 (Add is_disconnected functions to mpsc and mpmc channels) - #153308 (Add hygiene annotations for tokens in `macro_rules!` bodies) - #153557 (fix inference variables leaking into HIR const literal lowering logic) - #153804 (Derive Macro Eq: link to more detailed documentation) - #153913 (Fix some suggestions of the `for-loops-over-fallibles` lint) - #153974 (Point at return type when it is the source of the type expectation) - #153987 (mGCA: Lower const generic args to infer when needed) - #154018 (simplify and remove `tests/ui/kindck` and related tests) - #154036 (borrowck/type_check: remove helper left-over from unsized locals) - #154038 (merge `regions-outlives-nominal-type-*` tests into one file) - #154041 (bootstrap: Allow `--bless`ing changes to editor settings files)
…uwer Rollup of 19 pull requests Successful merges: - #152870 (Do not enable split debuginfo for windows-gnu) - #153333 (Fix bootstrap rust build failure for vxworks) - #153681 (ci: add runners for vanilla LLVM 22) - #153727 (When single impl can satisfy inference error, suggest type) - #153824 (Add `Wake` diagnostic item for `alloc::task::Wake`) - #154077 (Optimize 128-bit integer formatting) - #154078 (`define_callbacks` tweaks) - #151905 (Split the `dec2flt::RawFloat` trait for easier reuse) - #153170 (Add is_disconnected functions to mpsc and mpmc channels) - #153308 (Add hygiene annotations for tokens in `macro_rules!` bodies) - #153557 (fix inference variables leaking into HIR const literal lowering logic) - #153804 (Derive Macro Eq: link to more detailed documentation) - #153913 (Fix some suggestions of the `for-loops-over-fallibles` lint) - #153974 (Point at return type when it is the source of the type expectation) - #153987 (mGCA: Lower const generic args to infer when needed) - #154018 (simplify and remove `tests/ui/kindck` and related tests) - #154036 (borrowck/type_check: remove helper left-over from unsized locals) - #154038 (merge `regions-outlives-nominal-type-*` tests into one file) - #154041 (bootstrap: Allow `--bless`ing changes to editor settings files)
When calling an fn that returns a return type as a returned expression, point at the return type to explain that it affects the expected type.
Fix #43608.