rust/compiler
Stuart Cook c3b51b3182
Rollup merge of #146874 - Enselic:multiple-adt-versions, r=jieyouxu
compiler: Hint at multiple crate versions if trait impl is for wrong ADT

If a user does e.g.

    impl From<Bar> for foo::Foo

and get a compilation error about that `From<Bar>` is not implemented for `Foo`, check if multiple versions of the crate with `Foo` is present in the dependency graph. If so, give a hint about it.

Note that a test is added as a separate commit so it is easy to see what effect the fix has on the emitted error message.

This can be seen as a continuation of rust-lang/rust#124944.

I think this closes RUST-71693 but I haven't checked since it lacks a minimal reproducer. If this gets merged I'll ask that reporter if this fix works for them.

## Real world example

I encountered this case in the wild and didn't realize I had multiple versions of a crate in my dependency graph. So I was a bit confused at first. For reference, here is what that looked like.

<details>
<summary>Click to expand</summary>

### Before fix

```
error[E0277]: the trait bound `lambda_http::lambda_runtime::Diagnostic: From<Error>` is not satisfied
   --> src/main.rs:73:5
    |
73  |     lambda_http::run(service_fn(handle_event)).await
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<Error>` is not implemented for `lambda_http::lambda_runtime::Diagnostic`
    |
    = help: the following other types implement trait `From<T>`:
              `lambda_http::lambda_runtime::Diagnostic` implements `From<&str>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<Box<dyn StdError + Send + Sync>>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<Box<dyn StdError>>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<Infallible>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<lambda_runtime::deserializer::DeserializeError>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<std::io::Error>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<std::string::String>`
    = note: required for `Error` to implement `Into<lambda_http::lambda_runtime::Diagnostic>`
note: required by a bound in `lambda_http::run`
   --> /home/martin/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/lambda_http-0.17.0/src/lib.rs:199:26
    |
194 | pub async fn run<'a, R, S, E>(handler: S) -> Result<(), Error>
    |              --- required by a bound in this function
...
199 |     E: std::fmt::Debug + Into<Diagnostic>,
    |                          ^^^^^^^^^^^^^^^^ required by this bound in `run`

error[E0277]: the trait bound `lambda_http::lambda_runtime::Diagnostic: From<Error>` is not satisfied
   --> src/main.rs:73:48
    |
73  |     lambda_http::run(service_fn(handle_event)).await
    |                                                ^^^^^ the trait `From<Error>` is not implemented for `lambda_http::lambda_runtime::Diagnostic`
    |
    = help: the following other types implement trait `From<T>`:
              `lambda_http::lambda_runtime::Diagnostic` implements `From<&str>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<Box<dyn StdError + Send + Sync>>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<Box<dyn StdError>>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<Infallible>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<lambda_runtime::deserializer::DeserializeError>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<std::io::Error>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<std::string::String>`
    = note: required for `Error` to implement `Into<lambda_http::lambda_runtime::Diagnostic>`
note: required by a bound in `lambda_http::run`
   --> /home/martin/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/lambda_http-0.17.0/src/lib.rs:199:26
    |
194 | pub async fn run<'a, R, S, E>(handler: S) -> Result<(), Error>
    |              --- required by a bound in this function
...
199 |     E: std::fmt::Debug + Into<Diagnostic>,
    |                          ^^^^^^^^^^^^^^^^ required by this bound in `run`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `auto-merge-dependabot-pull-requests-webhook` (bin "auto-merge-dependabot-pull-requests-webhook") due to 2 previous errors
```

### After fix

```
   Compiling auto-merge-dependabot-pull-requests-webhook v0.1.0 (/home/martin/src/auto-merge-dependabot-prs/rust-webhook)
error[E0277]: the trait bound `lambda_http::lambda_runtime::Diagnostic: From<Error>` is not satisfied
   --> src/main.rs:73:5
    |
 73 |     lambda_http::run(service_fn(handle_event)).await
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<Error>` is not implemented for `lambda_http::lambda_runtime::Diagnostic`
    |
help: item with same name found
   --> /home/martin/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/lambda_runtime-0.13.0/src/diagnostic.rs:43:1
    |
 43 | pub struct Diagnostic {
    | ^^^^^^^^^^^^^^^^^^^^^
    = note: perhaps two different versions of crate `lambda_runtime` are being used?
    = help: the following other types implement trait `From<T>`:
              `lambda_http::lambda_runtime::Diagnostic` implements `From<&str>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<Box<dyn StdError + Send + Sync>>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<Box<dyn StdError>>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<Infallible>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<lambda_runtime::deserializer::DeserializeError>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<std::io::Error>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<std::string::String>`
    = note: required for `Error` to implement `Into<lambda_http::lambda_runtime::Diagnostic>`
note: required by a bound in `lambda_http::run`
   --> /home/martin/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/lambda_http-0.17.0/src/lib.rs:199:26
    |
194 | pub async fn run<'a, R, S, E>(handler: S) -> Result<(), Error>
    |              --- required by a bound in this function
...
199 |     E: std::fmt::Debug + Into<Diagnostic>,
    |                          ^^^^^^^^^^^^^^^^ required by this bound in `run`

error[E0277]: the trait bound `lambda_http::lambda_runtime::Diagnostic: From<Error>` is not satisfied
   --> src/main.rs:73:48
    |
 73 |     lambda_http::run(service_fn(handle_event)).await
    |                                                ^^^^^ the trait `From<Error>` is not implemented for `lambda_http::lambda_runtime::Diagnostic`
    |
help: item with same name found
   --> /home/martin/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/lambda_runtime-0.13.0/src/diagnostic.rs:43:1
    |
 43 | pub struct Diagnostic {
    | ^^^^^^^^^^^^^^^^^^^^^
    = note: perhaps two different versions of crate `lambda_runtime` are being used?
    = help: the following other types implement trait `From<T>`:
              `lambda_http::lambda_runtime::Diagnostic` implements `From<&str>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<Box<dyn StdError + Send + Sync>>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<Box<dyn StdError>>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<Infallible>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<lambda_runtime::deserializer::DeserializeError>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<std::io::Error>`
              `lambda_http::lambda_runtime::Diagnostic` implements `From<std::string::String>`
    = note: required for `Error` to implement `Into<lambda_http::lambda_runtime::Diagnostic>`
note: required by a bound in `lambda_http::run`
   --> /home/martin/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/lambda_http-0.17.0/src/lib.rs:199:26
    |
194 | pub async fn run<'a, R, S, E>(handler: S) -> Result<(), Error>
    |              --- required by a bound in this function
...
199 |     E: std::fmt::Debug + Into<Diagnostic>,
    |                          ^^^^^^^^^^^^^^^^ required by this bound in `run`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `auto-merge-dependabot-pull-requests-webhook` (bin "auto-merge-dependabot-pull-requests-webhook") due to 2 previous errors
```

</details>

try-job: dist-various-1
try-job: aarch64-msvc-1
2025-10-06 21:20:08 +11:00
..
rustc Make llvm_enzyme a regular cargo feature 2025-09-15 15:31:56 +00:00
rustc_abi Rollup merge of #147134 - workingjubilee:remove-explicit-abialign-deref, r=Zalathar 2025-09-29 15:44:55 +10:00
rustc_arena
rustc_ast Use Iterator::eq and (dogfood) eq_by in compiler and library 2025-09-29 08:08:05 +03:00
rustc_ast_ir turn pointer width into an integer in target.json 2025-08-27 23:44:49 +02:00
rustc_ast_lowering Auto merge of #145882 - m-ou-se:format-args-extend-1-arg, r=petrochenkov 2025-09-26 04:34:09 +00:00
rustc_ast_passes Extract common logic for iterating over features 2025-10-02 21:43:14 +00:00
rustc_ast_pretty Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_attr_parsing support link modifier as-needed for raw-dylib-elf 2025-10-06 08:56:40 +08:00
rustc_baked_icu_data Use default locale fallback data 2025-08-28 09:48:54 +00:00
rustc_borrowck Auto merge of #147138 - jackh726:split-canonical-bound, r=lcnr 2025-10-02 08:09:33 +00:00
rustc_builtin_macros change flt back to ftl 2025-10-04 18:18:58 +00:00
rustc_codegen_cranelift Auto merge of #147055 - beepster4096:subtype_is_not_a_projection, r=lcnr 2025-10-02 01:54:48 +00:00
rustc_codegen_gcc codegen: Generate dbg_value for the ref statement 2025-10-02 14:55:51 +08:00
rustc_codegen_llvm Remove inherent methods from llvm::CallConv::from_conv 2025-10-04 18:47:18 +10:00
rustc_codegen_ssa support link modifier as-needed for raw-dylib-elf 2025-10-06 08:56:40 +08:00
rustc_const_eval change flt back to ftl 2025-10-04 18:18:58 +00:00
rustc_data_structures avoid calling insert_presorted more than once 2025-09-10 08:40:12 +02:00
rustc_driver compiler: Add Windows resources to rustc-main and rustc_driver 2025-09-05 14:06:31 -04:00
rustc_driver_impl fixes for numerous clippy warnings 2025-09-19 20:56:07 +00:00
rustc_error_codes Rollup merge of #146585 - hkBst:indexing-1, r=jdonszelmann 2025-10-02 10:27:49 +02:00
rustc_error_messages Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_errors fix: Filter suggestion parts that match existing code 2025-09-04 17:42:13 -06:00
rustc_expand change flt back to ftl 2025-10-04 18:18:58 +00:00
rustc_feature Extract common logic for iterating over features 2025-10-02 21:43:14 +00:00
rustc_fluent_macro Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_fs_util Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_graphviz
rustc_hashes
rustc_hir support link modifier as-needed for raw-dylib-elf 2025-10-06 08:56:40 +08:00
rustc_hir_analysis Auto merge of #147138 - jackh726:split-canonical-bound, r=lcnr 2025-10-02 08:09:33 +00:00
rustc_hir_id rustc_hir_id: Add a comment explaining why the crate exists 2025-08-20 15:04:00 -07:00
rustc_hir_pretty Rollup merge of #146102 - fmease:rm-dead-eff-code-iii, r=fee1-dead 2025-09-02 17:08:58 +02:00
rustc_hir_typeck Rollup merge of #146585 - hkBst:indexing-1, r=jdonszelmann 2025-10-02 10:27:49 +02:00
rustc_incremental Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_index Apply cfg(bootstrap) replacement 2025-09-26 19:09:23 -04:00
rustc_index_macros Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_infer Auto merge of #147138 - jackh726:split-canonical-bound, r=lcnr 2025-10-02 08:09:33 +00:00
rustc_interface Rollup merge of #146596 - bjorn3:dummy_backend, r=SparrowLii 2025-09-30 21:53:33 +02:00
rustc_lexer Fix a crash/mislex when more than one frontmatter closing possibility is considered 2025-09-22 15:10:41 -04:00
rustc_lint Auto merge of #147375 - folkertdev:power-align-union, r=RalfJung 2025-10-05 22:14:55 +00:00
rustc_lint_defs Apply cfg(bootstrap) replacement 2025-09-26 19:09:23 -04:00
rustc_llvm codegen: Generate dbg_value for the ref statement 2025-10-02 14:55:51 +08:00
rustc_log Update tracing, again 2025-09-08 09:23:37 -07:00
rustc_macros resolve: Do not finalize shadowed bindings 2025-09-25 20:36:14 +03:00
rustc_metadata support link modifier as-needed for raw-dylib-elf 2025-10-06 08:56:40 +08:00
rustc_middle Auto merge of #147345 - Kivooeo:tidy-flt-fix, r=Kobzol 2025-10-05 15:55:18 +00:00
rustc_mir_build Auto merge of #147055 - beepster4096:subtype_is_not_a_projection, r=lcnr 2025-10-02 01:54:48 +00:00
rustc_mir_dataflow mir-opt: Eliminate dead statements even if they are used by debuginfos 2025-10-02 14:58:59 +08:00
rustc_mir_transform mir-opt: Eliminate dead statements even if they are used by debuginfos 2025-10-02 14:58:59 +08:00
rustc_monomorphize Auto merge of #145717 - BoxyUwU:erase_regions_rename, r=lcnr 2025-09-09 15:04:44 +00:00
rustc_next_trait_solver Auto merge of #147138 - jackh726:split-canonical-bound, r=lcnr 2025-10-02 08:09:33 +00:00
rustc_parse Auto merge of #147360 - chenyukang:yukang-fix-assoc-eq-missing-term, r=nnethercote 2025-10-06 01:24:16 +00:00
rustc_parse_format Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_passes Auto merge of #147345 - Kivooeo:tidy-flt-fix, r=Kobzol 2025-10-05 15:55:18 +00:00
rustc_pattern_analysis Rename various "concrete opaque type" terminology to say "hidden type" 2025-09-27 22:58:02 +01:00
rustc_privacy fix doc comments to be more standard 2025-09-26 09:25:56 +00:00
rustc_proc_macro Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_public Auto merge of #147138 - jackh726:split-canonical-bound, r=lcnr 2025-10-02 08:09:33 +00:00
rustc_public_bridge Remove useless lifetime parameter. 2025-07-23 23:54:37 +00:00
rustc_query_impl fixup limit handling code 2025-09-08 15:07:12 -07:00
rustc_query_system Remove some unnecessary locals. 2025-10-03 20:36:21 +10:00
rustc_resolve Auto merge of #147345 - Kivooeo:tidy-flt-fix, r=Kobzol 2025-10-05 15:55:18 +00:00
rustc_sanitizers Split Bound into Canonical and Bound 2025-09-30 12:58:28 -04:00
rustc_serialize Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_session support link modifier as-needed for raw-dylib-elf 2025-10-06 08:56:40 +08:00
rustc_span Auto merge of #147377 - karolzwolak:dont-create-empty-ident-issue-147365, r=nnethercote 2025-10-06 04:34:57 +00:00
rustc_symbol_mangling Auto merge of #147138 - jackh726:split-canonical-bound, r=lcnr 2025-10-02 08:09:33 +00:00
rustc_target Fill out AVR target metadata 2025-10-05 12:01:23 +02:00
rustc_thread_pool Fix a dangling reference in rustc_thread_pool 2025-09-22 14:04:04 -07:00
rustc_trait_selection Rollup merge of #146874 - Enselic:multiple-adt-versions, r=jieyouxu 2025-10-06 21:20:08 +11:00
rustc_traits erase_regions to erase_and_anonymize_regions 2025-09-09 14:49:16 +02:00
rustc_transmute remove explicit deref of AbiAlign for most methods 2025-09-28 15:02:14 -07:00
rustc_ty_utils change flt back to ftl 2025-10-04 18:18:58 +00:00
rustc_type_ir Rollup merge of #147251 - jackh726:global-cache-non-concurrent-change, r=lcnr 2025-10-04 17:11:12 +02:00
rustc_type_ir_macros Revert introduction of [workspace.dependencies]. 2025-09-02 19:12:54 +10:00
rustc_windows_rc [win] Use find-msvc-tools instead of cc to find the linker and rc on Windows 2025-09-19 12:00:30 -07:00