Skip to content

rustc diagnostic for incorrect return-pos bounds of Fn traits as function arguments suggests deleting until file start #140543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rbakbashev opened this issue May 1, 2025 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@rbakbashev
Copy link

rbakbashev commented May 1, 2025

Code

fn main() {}

fn foo() {}

fn bar() {}

fn myfunc<E>(_func: impl Fn(E) -> std::fmt::Display) {
    ()
}

fn baz() {}

Current output

error[E0782]: expected a type, found a trait
 --> scc.rs:7:35
  |
7 | fn myfunc<E>(_func: impl Fn(E) -> std::fmt::Display) {
  |                                   ^^^^^^^^^^^^^^^^^
  |
help: you can add the `dyn` keyword if you want a trait object
  |
7 | fn myfunc<E>(_func: impl Fn(E) -> dyn std::fmt::Display) {
  |                                   +++
help: you might have meant to write a bound here
  |
1 - fn main() {}
2 -
3 - fn foo() {}
4 -
5 - fn bar() {}
6 -
7 - fn myfunc<E>(_func: impl Fn(E) -> std::fmt::Display) {
1 + : std::fmt::Display) {
  |

For more information about this error, try `rustc --explain E0782`.

Not sure what the desired output should be. The first suggestion makes sense, but in the second one it wants you to delete all the lines from the start of the file.

Rust Version

rustc 1.88.0-nightly (b45dd71d1 2025-04-30)
binary: rustc
commit-hash: b45dd71d1824f176fba88f6c40467030a16afa2c
commit-date: 2025-04-30
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Anything else?

Sorry for a mouthful in the title. This bug manifests in 2024 and 2021 editions, but not in the 2018 edition, where it generates a warning instead.

output of rustc --edition 2018 file.rs (just a warning)
warning: trait objects without an explicit `dyn` are deprecated
 --> scc.rs:7:35
  |
7 | fn myfunc<E>(_func: impl Fn(E) -> std::fmt::Display) {
  |                                   ^^^^^^^^^^^^^^^^^
  |
  = warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
  = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
  = note: `#[warn(bare_trait_objects)]` on by default
help: if this is a dyn-compatible trait, use `dyn`
  |
7 | fn myfunc<E>(_func: impl Fn(E) -> dyn std::fmt::Display) {
  |                                   +++

warning: function `foo` is never used
 --> scc.rs:3:4
  |
3 | fn foo() {}
  |    ^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: function `bar` is never used
 --> scc.rs:5:4
  |
5 | fn bar() {}
  |    ^^^

warning: function `myfunc` is never used
 --> scc.rs:7:4
  |
7 | fn myfunc<E>(_func: impl Fn(E) -> std::fmt::Display) {
  |    ^^^^^^

warning: function `baz` is never used
  --> scc.rs:11:4
   |
11 | fn baz() {}
   |    ^^^

warning: 5 warnings emitted

This is a bug in the latest nightly ("nightly-2025-05-01") and current stable (1.86.0).

Rust version of current stable
rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

This could be due to the fact that it used to be an error in nightly-2018-01-01, but is not in nightly-2022-05-01 (both are using 2018 edition).

error in 1.24.0-nightly
cargo +bisector-nightly-2018-01-01-x86_64-unknown-linux-gnu build
   Compiling bisect_rustc v0.0.0 (file:///home/user/misc/bisect_rustc)
error: `impl Trait` in argument position is experimental (see issue #34511)
 --> src/main.rs:7:21
  |
7 | fn myfunc<E>(_func: impl Fn(E) -> std::fmt::Display) {
  |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: add #![feature(universal_impl_trait)] to the crate attributes to enable

error[E0277]: the trait bound `std::fmt::Display + 'static: std::marker::Sized` is not satisfied
 --> src/main.rs:7:1
  |
7 | / fn myfunc<E>(_func: impl Fn(E) -> std::fmt::Display) {
8 | |     ()
9 | | }
  | |_^ `std::fmt::Display + 'static` does not have a constant size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `std::fmt::Display + 'static`
  = note: required by `std::ops::FnOnce`

error: aborting due to 2 previous errors

error: Could not compile `bisect_rustc`.

To learn more, run the command again with --verbose.

Bisecting rustc from nightly-2018-01-01 to nightly-2022-05-01 to find where it stopped being an error reveals nightly-2020-10-07:

cargo bisect-rustc --regress=success --start=2018-01-01 --end=2022-05-01 --preserve
********************************************************************************
Regression in nightly-2020-10-07
********************************************************************************

fetching https://static.rust-lang.org/dist/2020-10-06/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2020-10-06: 40 B / 40 B [[..]] 100.00 % 901.91 KB/s converted 2020-10-06 to a1dfd2490a6cb456b92e469fa550dc217e20ad6d
fetching https://static.rust-lang.org/dist/2020-10-07/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2020-10-07: 40 B / 40 B [[..]] 100.00 % 1.02 MB/s converted 2020-10-07 to 98edd1fbf8a68977a2a7c1312eb1ebff80515a92
looking for regression commit between 2020-10-06 and 2020-10-07
fetching (via remote github) commits from max(a1dfd2490a6cb456b92e469fa550dc217e20ad6d, 2020-10-04) to 98edd1fbf8a68977a2a7c1312eb1ebff80515a92
ending github query because we found starting sha: a1dfd2490a6cb456b92e469fa550dc217e20ad6d
get_commits_between returning commits, len: 6
  commit[0] 2020-10-05: Auto merge of #77080 - richkadel:llvm-coverage-counters-2, r=tmandry
  commit[1] 2020-10-06: Auto merge of #77606 - JohnTitor:rollup-7rgahdt, r=JohnTitor
  commit[2] 2020-10-06: Auto merge of #77594 - timvermeulen:chain_advance_by, r=scottmcm
  commit[3] 2020-10-06: Auto merge of #73905 - matthewjasper:projection-bounds-2, r=nikomatsakis
  commit[4] 2020-10-06: Auto merge of #76356 - caass:hooks, r=jyn514
  commit[5] 2020-10-06: Auto merge of #77386 - joshtriplett:static-glibc, r=petrochenkov
ERROR: no CI builds available between a1dfd2490a6cb456b92e469fa550dc217e20ad6d and 98edd1fbf8a68977a2a7c1312eb1ebff80515a92 within last 167 days

The PR that touches that code is #73905, but I am not sure how relevant this is. After all, it is an error in 2021 and 2024 editions on the current compiler.

I can try to implement the fix, but I am not sure what the compiler is even trying to suggest.

@rbakbashev rbakbashev added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 1, 2025
@rbakbashev
Copy link
Author

On another thought, I bisected the wrong thing. I should've looked at the commit where the error diagnostic started suggesting removing these lines, instead of looking where it became a hard error.

In nightly-2022-05-01 (2021 edition), this diagnostic only had one suggestion:

diagnostic in 2022-05-01
error[E0782]: trait objects must include the `dyn` keyword
 --> src/main.rs:7:35
  |
7 | fn myfunc<E>(_func: impl Fn(E) -> std::fmt::Display) {
  |                                   ^^^^^^^^^^^^^^^^^
  |
help: add `dyn` keyword before this trait
  |
7 - fn myfunc<E>(_func: impl Fn(E) -> std::fmt::Display) {
7 + fn myfunc<E>(_func: impl Fn(E) -> dyn std::fmt::Display) {
  |

For more information about this error, try `rustc --explain E0782`.

In nightly-2024-03-28, in #122120, it changed to include an additional suggestion, but it already did not look useful, as the line where it makes a suggestion is malformed.

output in 2024-03-28, including bisector run
$ cargo +bisector-nightly-2024-03-28-x86_64-unknown-linux-gnu check
    Checking bisect_rustc v0.0.0 (/home/user/misc/bisect_rustc)
error[E0782]: trait objects must include the `dyn` keyword
 --> src/main.rs:7:35
  |
7 | fn myfunc<E>(_func: impl Fn(E) -> std::fmt::Display) {
  |                                   ^^^^^^^^^^^^^^^^^
  |
help: add `dyn` keyword before this trait
  |
7 | fn myfunc<E>(_func: impl Fn(E) -> dyn std::fmt::Display) {
  |                                   +++
help: you might have meant to write a bound here
  |
1 | : std::fmt::Display) {
  | ~

For more information about this error, try `rustc --explain E0782`.
$ cat script.sh
#!/bin/sh
! cargo check 2>&1 | grep -q 'you might have meant'

$ cargo bisect-rustc --start=2022-05-01 --end=2025-05-01 --script script.sh --preserve
[..]
********************************************************************************
Regression in nightly-2024-03-28
********************************************************************************

fetching https://static.rust-lang.org/dist/2024-03-27/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2024-03-27: 40 B / 40 B [[..]] 100.00 % 1.02 MB/s converted 2024-03-27 to 47ecded3525392b77843534bed69b4302f9af8d2
fetching https://static.rust-lang.org/dist/2024-03-28/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2024-03-28: 40 B / 40 B [[..]] 100.00 % 1.08 MB/s converted 2024-03-28 to c9f8f3438a8134a413aa5d4903e0196e44e37bbc
looking for regression commit between 2024-03-27 and 2024-03-28
fetching (via remote github) commits from max(47ecded3525392b77843534bed69b4302f9af8d2, 2024-03-25) to c9f8f3438a8134a413aa5d4903e0196e44e37bbc
ending github query because we found starting sha: 47ecded3525392b77843534bed69b4302f9af8d2
get_commits_between returning commits, len: 9
  commit[0] 2024-03-26: Auto merge of #118644 - madsmtm:macos-weak-linking-test, r=compiler-errors
  commit[1] 2024-03-26: Auto merge of #123108 - matthiaskrgr:rollup-zossklv, r=matthiaskrgr
  commit[2] 2024-03-27: Auto merge of #122958 - jieyouxu:port-backtrace-dylib-dep, r=workingjubilee
  commit[3] 2024-03-27: Auto merge of #123121 - matthiaskrgr:rollup-e4glcv3, r=matthiaskrgr
  commit[4] 2024-03-27: Auto merge of #116016 - jhpratt:kill-rustc-serialize, r=ehuss
  commit[5] 2024-03-27: Auto merge of #123128 - GuillaumeGomez:rollup-3l3zu6s, r=GuillaumeGomez
  commit[6] 2024-03-27: Auto merge of #123006 - compiler-errors:defer-suggestion-work, r=fee1-dead
  commit[7] 2024-03-27: Auto merge of #122460 - jieyouxu:rmake-example-refactor, r=Nilstrieb
  commit[8] 2024-03-27: Auto merge of #122396 - kornelski:vec-err-debloat, r=joboet
ERROR: no CI builds available between 47ecded3525392b77843534bed69b4302f9af8d2 and c9f8f3438a8134a413aa5d4903e0196e44e37bbc within last 167 days

Later, in nightly-2025-02-12 it started suggesting removing all lines until the start of the file.

output in 2025-02-12 (same as OP), and bisector run
$ cargo +bisector-nightly-2025-02-12-x86_64-unknown-linux-gnu check
    Checking bisect_rustc v0.0.0 (/home/user/misc/bisect_rustc)
error[E0782]: expected a type, found a trait
 --> src/main.rs:7:35
  |
7 | fn myfunc<E>(_func: impl Fn(E) -> std::fmt::Display) {
  |                                   ^^^^^^^^^^^^^^^^^
  |
help: you can add the `dyn` keyword if you want a trait object
  |
7 | fn myfunc<E>(_func: impl Fn(E) -> dyn std::fmt::Display) {
  |                                   +++
help: you might have meant to write a bound here
  |
1 - fn main() {}
2 -
3 - fn foo() {}
4 -
5 - fn bar() {}
6 -
7 - fn myfunc<E>(_func: impl Fn(E) -> std::fmt::Display) {
1 + : std::fmt::Display) {
  |

For more information about this error, try `rustc --explain E0782`.
$ cat script.sh
#!/bin/sh
! cargo check 2>&1 | grep -q 'fn main'

$ cargo bisect-rustc --start=2022-05-01 --end=2025-05-01 --script script.sh --preserve
********************************************************************************
Regression in nightly-2025-02-12
********************************************************************************

fetching https://static.rust-lang.org/dist/2025-02-11/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2025-02-11: 40 B / 40 B [[..]] 100.00 % 1.16 MB/s converted 2025-02-11 to 6171d944aea415a3023d4262e0895aa3b18c771f
fetching https://static.rust-lang.org/dist/2025-02-12/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2025-02-12: 40 B / 40 B [[..]] 100.00 % 1.32 MB/s converted 2025-02-12 to 92bedea1c51e3a969d60972be854506ffd8c5cb6
looking for regression commit between 2025-02-11 and 2025-02-12
fetching (via remote github) commits from max(6171d944aea415a3023d4262e0895aa3b18c771f, 2025-02-09) to 92bedea1c51e3a969d60972be854506ffd8c5cb6
ending github query because we found starting sha: 6171d944aea415a3023d4262e0895aa3b18c771f
get_commits_between returning commits, len: 7
  commit[0] 2025-02-10: Auto merge of #133092 - madsmtm:bootstrap-deployment-target, r=Mark-Simulacrum,jieyouxu
  commit[1] 2025-02-11: Auto merge of #127541 - estebank:diff-suggestions, r=petrochenkov
  commit[2] 2025-02-11: Auto merge of #136845 - matthiaskrgr:rollup-ol4np4z, r=matthiaskrgr
  commit[3] 2025-02-11: Auto merge of #136851 - jhpratt:rollup-ftijn95, r=jhpratt
  commit[4] 2025-02-11: Auto merge of #136571 - marcoieni:ubuntu-24-large-runners, r=Mark-Simulacrum
  commit[5] 2025-02-11: Auto merge of #136586 - Kobzol:lto-rustdoc-fix-stage-1, r=onur-ozkan
  commit[6] 2025-02-11: Auto merge of #136605 - lsunsi:update-rustc-hash, r=lqd
validated commits found, specifying toolchains

[..]
searched toolchains 6171d944aea415a3023d4262e0895aa3b18c771f through 92bedea1c51e3a969d60972be854506ffd8c5cb6

********************************************************************************
Regression in ffa9afef183a3496a277c8da2e61403582a99508
********************************************************************************

searched nightlies: from nightly-2022-05-01 to nightly-2025-05-01
regressed nightly: nightly-2025-02-12
searched commit range: 6171d94...92bedea
regressed commit: ffa9afe

bisected with cargo-bisect-rustc v0.6.9

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc --start=2022-05-01 --end=2025-05-01 --script script.sh --preserve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant