Skip to content
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

Bad diagnostic output for Fn traits with E0277 #95147

Closed
ThePuzzlemaker opened this issue Mar 20, 2022 · 4 comments · Fixed by #95260
Closed

Bad diagnostic output for Fn traits with E0277 #95147

ThePuzzlemaker opened this issue Mar 20, 2022 · 4 comments · Fixed by #95260
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ThePuzzlemaker
Copy link
Contributor

ThePuzzlemaker commented Mar 20, 2022

Given the following code:

fn takes_fnonce(f: impl FnOnce(&i32)) { f(&0) }

fn takes_fnmut(f: impl FnMut(i32)) { takes_fnonce(f) }

(playground)

The current output is:

error[E0277]: expected a `FnOnce<(&i32,)>` closure, found `impl FnMut(i32)`
 --> src/lib.rs:3:51
  |
3 | fn takes_fnmut(f: impl FnMut(i32)) { takes_fnonce(f) }
  |                                      ------------ ^ expected an `FnOnce<(&i32,)>` closure, found `impl FnMut(i32)`
  |                                      |
  |                                      required by a bound introduced by this call
  |
note: required by a bound in `takes_fnonce`
 --> src/lib.rs:1:25
  |
1 | fn takes_fnonce(f: impl FnOnce(&i32)) { f(&0) }
  |                         ^^^^^^^^^^^^ required by this bound in `takes_fnonce`
help: consider further restricting this bound
  |
3 | fn takes_fnmut(f: impl FnMut(i32) + for<'r> std::ops::FnOnce<(&'r i32,)>) { takes_fnonce(f) }
  |                                   ++++++++++++++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `fn-trait-bad-rendering` due to previous error

Ideally the output should look like:

error[E0277]: expected a `FnOnce<(&i32,)>` closure, found `impl FnMut(i32)`
 --> src/lib.rs:3:51
  |
3 | fn takes_fnmut(f: impl FnMut(i32)) { takes_fnonce(f) }
  |                                      ------------ ^ expected an `FnOnce(&i32)` closure, found `impl FnMut(i32)`
  |                                      |
  |                                      required by a bound introduced by this call
  |
note: required by a bound in `takes_fnonce`
 --> src/lib.rs:1:25
  |
1 | fn takes_fnonce(f: impl FnOnce(&i32)) { f(&0) }
  |                         ^^^^^^^^^^^^ required by this bound in `takes_fnonce`
help: consider further restricting this bound
  |
3 | fn takes_fnmut(f: impl FnMut(i32) + for<'r> FnOnce(&'r i32)) { takes_fnonce(f) }
  |                                   +++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `fn-trait-bad-rendering` due to previous error

Frankly it would be nice if we could improve the "further restricting this bound" to be something different (as restricting the bound is impossible in this case--something can't be both FnMut(i32) and FnOnce(&i32)), but we should at least render the FnOnce trait syntax right.

rustc --version --verbose:

rustc 1.61.0-nightly (8d60bf427 2022-03-19)
binary: rustc
commit-hash: 8d60bf427a4b055f464122062e76b3ec34d4f8ba
commit-date: 2022-03-19
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0

I cannot seem to find any place to start a bisection, so as far as I can tell this behaviour has always been present.

This code is an MCVE of the following less contrived case (using a bit of truncated code, as the exact details of the implementation of this lexer-like structure are not too important), found by @slavfox:

struct Foo;

impl Foo {
    fn peek(&self) -> Option<char> { todo!() }
    fn advance(&mut self) { todo!() }
    fn eat_while(&mut self, predicate: impl FnMut(char) -> bool) -> String {
        let mut buf = String::new();
        while let Some(ch) = self.peek().filter(predicate) {
            buf.push(ch);
            self.advance();
        }
        buf
    }
}

I tested this and it does not seem like the ~constness of the trait bounds of Option::filter affect the overall diagnostic rendering.

I may try to fix this in a PR if I can find out why it is occurring.

@rustbot label: +D-newcomer-roadblock +D-papercut

@ThePuzzlemaker ThePuzzlemaker 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 Mar 20, 2022
@rustbot rustbot added D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Mar 20, 2022
@ThePuzzlemaker
Copy link
Contributor Author

It appears this stems from the #[rustc_on_unimplemented(...)] message. I'll have to see if there's a way I can hack this into working with paren sugar.

@ThePuzzlemaker
Copy link
Contributor Author

@rustbot claim

@ThePuzzlemaker ThePuzzlemaker changed the title Bad diagnostic output for providing a FnMut(T) where an FnOnce(&T) is expected Bad diagnostic output for Fn traits with E0277 Mar 20, 2022
@ThePuzzlemaker
Copy link
Contributor Author

@rustbot release-assignment
This looks a bit tricky and I'm sure I'm going to manage to find the wrong way to solve it :P

@compiler-errors
Copy link
Member

@rustbot claim

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 D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants