Skip to content

Point at return type when it is the source of the type expectation#153974

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
estebank:issue-43608
Mar 19, 2026
Merged

Point at return type when it is the source of the type expectation#153974
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
estebank:issue-43608

Conversation

@estebank
Copy link
Contributor

@estebank estebank commented Mar 16, 2026

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 #43608.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 16, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 16, 2026

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 14 candidates

@estebank
Copy link
Contributor Author

I'm not married to the wording of the new span label, just couldn't come up with something that sounded better.

@rust-log-analyzer

This comment was marked as resolved.

Copy link
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::read and ignore the result? Without the original, more complicated code it's hard to know.

View changes since this review

--> $DIR/expectation-from-return-type.rs:5:15
|
LL | fn main() {
| - the call expression's return type is influenced by this return type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@nnethercote nnethercote added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 17, 2026
@estebank
Copy link
Contributor Author

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.

Copy link
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, good enough. r=me after squashing the commits.

View changes since this review

"this implicit `()` return type influences the call expression's return type"
),
FnRetTy::Return(_) => {
"this return type influences the call expression's return type"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this arm reachable? It doesn't occur in the tests, AFAICT.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {
     |                     ^^^^
```
@rustbot
Copy link
Collaborator

rustbot commented Mar 18, 2026

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.

@nnethercote
Copy link
Contributor

@bors r+ rollup

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 19, 2026

📌 Commit ca2139d has been approved by nnethercote

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 19, 2026
Zalathar added a commit to Zalathar/rust that referenced this pull request Mar 19, 2026
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.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 19, 2026
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.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 19, 2026
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.
rust-bors bot pushed a commit that referenced this pull request Mar 19, 2026
…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)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 19, 2026
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.
rust-bors bot pushed a commit that referenced this pull request Mar 19, 2026
…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)
rust-bors bot pushed a commit that referenced this pull request Mar 19, 2026
…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)
@rust-bors rust-bors bot merged commit db7db62 into rust-lang:main Mar 19, 2026
11 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Confusing mismatched type error

4 participants