rust-lang / rust-lang/rust

Diagnostics suggest incorrect paths when inline modules are used

Open
#119,521 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-suggestion-diagnostics C-bug D-invalid-suggestion T-compiler WG-diagnostics
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

struct A;

mod inner {
    struct A;
    
    impl From<super::A> for A {}
    impl From<A> for super::A {}
}

I expected to see this happen: an error suggesting the following code:

struct A;

mod inner {
    struct A;
    
    impl From<super::A> for A { fn from(_: super::A) -> Self { todo!() } }
    impl From<A> for super::A { fn from(_: A) -> Self { todo!() } }
}

Instead, this happened: an error suggested the following (incorrect) code:

struct A;

mod inner {
    struct A;
    
    impl From<super::A> for A { fn from(_: A) -> Self { todo!() } }
    impl From<A> for super::A { fn from(_: inner::A) -> Self { todo!() } }
}
error[E0046]: not all trait items implemented, missing: `from`
 --> ./t.rs:6:5
  |
6 |     impl From<super::A> for A {}
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^ missing `from` in implementation
  |
  = help: implement the missing item: `fn from(_: A) -> Self { todo!() }`

error[E0046]: not all trait items implemented, missing: `from`
 --> ./t.rs:7:5
  |
7 |     impl From<A> for super::A {}
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^ missing `from` in implementation
  |
  = help: implement the missing item: `fn from(_: inner::A) -> Self { todo!() }`
Meta

rustc --version --verbose:

rustc 1.74.1 (a28077b28 2023-12-04)
binary: rustc
commit-hash: a28077b28a02b92985b3a3faecf92813155f1ea1
commit-date: 2023-12-04
host: x86_64-unknown-linux-gnu
release: 1.74.1
LLVM version: 17.0.4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic with the inline-module Rust example and compare the suggested paths with the expected paths for both missing From::from implementations. Trace the compiler's diagnostic suggestion output for these impls; done means the suggestions preserve the correct super::A and inner A identities and a regression test covers this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.