Skip to content

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

@rbakbashev

Description

@rbakbashev

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.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions