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

manual_map sugg with dyn causes error #12659

Open
gepbird opened this issue Apr 9, 2024 · 2 comments · May be fixed by #12712
Open

manual_map sugg with dyn causes error #12659

gepbird opened this issue Apr 9, 2024 · 2 comments · May be fixed by #12712
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@gepbird
Copy link

gepbird commented Apr 9, 2024

Reproducer

This code compiles:

trait DummyTrait {}

fn foo<P, T>(function: P)
where
    P: FnOnce() -> Result<T, String>,
    T: DummyTrait,
{
    let _: Option<Result<Box<dyn DummyTrait>, String>> = match Some(()) {
        Some(_) => Some(match function() {
            Ok(value) => Ok(Box::new(value)),
            _ => todo!(),
        }),
        None => None,
    };
}

But when applying the manual map suggestion, it no longer compiles:

error[E0308]: mismatched types
  --> src/lib.rs:8:58
   |
3  |   fn foo<P, T>(function: P)
   |             - found this type parameter
...
8  |       let _: Option<Result<Box<dyn DummyTrait>, String>> = Some(()).map(|_| match function() {
   |  ____________-------------------------------------------___^
   | |            |
   | |            expected due to this
9  | |             Ok(value) => Ok(Box::new(value)),
10 | |             _ => todo!(),
11 | |         });
   | |__________^ expected `Option<Result<Box<...>, ...>>`, found `Option<Result<Box<T>, _>>`
   |
   = note: expected enum `std::option::Option<std::result::Result<std::boxed::Box<dyn DummyTrait>, std::string::Stri
ng>>`
              found enum `std::option::Option<std::result::Result<std::boxed::Box<T>, _>>`
   = help: type parameters must be constrained to match other types
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

This was the most minimal example I could make to reproduce the issue.

The project where I got this problem:

This code compiles

Full output of clippy:

    Checking timetable-optimizer-web v0.1.0 (/home/gep/timetable-optimizer/web)
    Checking timetable-optimizer-cli v0.1.0 (/home/gep/timetable-optimizer/cli)
    Checking timetable-optimizer-lib v0.1.0 (/home/gep/timetable-optimizer/lib)
warning: failed to automatically apply fixes suggested by rustc to crate `timetable_optimizer_cli`

after fixes were automatically applied the compiler reported errors within these files:

  * cli/src/filter/mod.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0308]: mismatched types
  --> cli/src/filter/mod.rs:25:3
   |
16 |   fn parse_with_key<F, T>(
   |                        - found this type parameter
...
20 |   ) -> Option<Result<Box<dyn Filter>, String>>
   |        --------------------------------------- expected `std::option::Option<std::result::Result<std::boxed::Box<(dyn filter::Filter + 'static)>, std::string::String>>` because of re
turn type
...
25 | /   spec.strip_prefix(&(key.to_string() + "=")).map(|value| match parse_fn(value) {
26 | |       Ok(filter) => Ok(Box::new(filter)),
27 | |       Err(e) => Err(e),
28 | |     })
   | |______^ expected `Option<Result<Box<dyn Filter>, String>>`, found `Option<Result<Box<T>, String>>`
   |
   = note: expected enum `std::option::Option<std::result::Result<std::boxed::Box<(dyn filter::Filter + 'static)>, _>>`
              found enum `std::option::Option<std::result::Result<std::boxed::Box<T>, _>>`
   = help: type parameters must be constrained to match other types
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: manual implementation of `Option::map`
  --> cli/src/filter/mod.rs:25:3
   |
25 | /   match spec.strip_prefix(&(key.to_string() + "=")) {
26 | |     Some(value) => Some(match parse_fn(value) {
27 | |       Ok(filter) => Ok(Box::new(filter)),
28 | |       Err(e) => Err(e),
29 | |     }),
30 | |     None => None,
31 | |   }
   | |___^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_map
   = note: `#[warn(clippy::manual_map)]` on by default
help: try
   |
25 ~   spec.strip_prefix(&(key.to_string() + "=")).map(|value| match parse_fn(value) {
26 +       Ok(filter) => Ok(Box::new(filter)),
27 +       Err(e) => Err(e),
28 +     })
   |

warning: `timetable-optimizer-cli` (bin "timetable-optimizer-cli") generated 1 warning (run `cargo clippy --fix --bin "timetable-optimizer-cli"` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `timetable_optimizer_cli`

after fixes were automatically applied the compiler reported errors within these files:

  * cli/src/filter/mod.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0308]: mismatched types
  --> cli/src/filter/mod.rs:25:3
   |
16 |   fn parse_with_key<F, T>(
   |                        - found this type parameter
...
20 |   ) -> Option<Result<Box<dyn Filter>, String>>
   |        --------------------------------------- expected `std::option::Option<std::result::Result<std::boxed::Box<(dyn filter::Filter + 'static)>, std::string::String>>` because of re
turn type
...
25 | /   spec.strip_prefix(&(key.to_string() + "=")).map(|value| match parse_fn(value) {
26 | |       Ok(filter) => Ok(Box::new(filter)),
27 | |       Err(e) => Err(e),
28 | |     })
   | |______^ expected `Option<Result<Box<dyn Filter>, String>>`, found `Option<Result<Box<T>, String>>`
   |
   = note: expected enum `std::option::Option<std::result::Result<std::boxed::Box<(dyn filter::Filter + 'static)>, _>>`
              found enum `std::option::Option<std::result::Result<std::boxed::Box<T>, _>>`
   = help: type parameters must be constrained to match other types
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: `timetable-optimizer-cli` (bin "timetable-optimizer-cli" test) generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 3.28s

Version

rustc 1.77.1 (7cf61ebde 2024-03-27)
binary: rustc
commit-hash: 7cf61ebde7b22796c69757901dd346d0fe70bd97
commit-date: 2024-03-27
host: x86_64-unknown-linux-gnu
release: 1.77.1
LLVM version: 17.0.6

Additional Labels

@rustbot label +I-suggestion-causes-error

@gepbird gepbird added the C-bug Category: Clippy is not doing the correct thing label Apr 9, 2024
@rustbot
Copy link
Collaborator

rustbot commented Apr 9, 2024

Error: Label I-suggestion-causes-error can only be set by Rust team members

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@y21 y21 added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 9, 2024
@J-ZhengLi
Copy link
Member

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants