Skip to content

Incorrect path in E0004 match suggestion for enum from another module #137845

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

Closed
makai410 opened this issue Mar 1, 2025 · 4 comments
Closed

Incorrect path in E0004 match suggestion for enum from another module #137845

makai410 opened this issue Mar 1, 2025 · 4 comments
Labels
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.

Comments

@makai410
Copy link
Contributor

makai410 commented Mar 1, 2025

Code

pub mod cat {
    pub enum Meow {
        Foo,
    }
}
fn main() {
    let v = cat::Meow::Foo;
    match v {}
}

Current output

error[E0004]: non-exhaustive patterns: `Meow::Foo` not covered
 --> src/main.rs:8:11
  |
8 |     match v {}
  |           ^ pattern `Meow::Foo` not covered
  |
note: `Meow` defined here
 --> src/main.rs:2:14
  |
2 |     pub enum Meow {
  |              ^^^^
3 |         Foo,
  |         --- not covered
  = note: the matched value is of type `Meow`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
  |
8 ~     match v {
9 +         Meow::Foo => todo!(),
10~     }
  |

For more information about this error, try `rustc --explain E0004`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

error[E0004]: non-exhaustive patterns: `Meow::Foo` not covered
 --> src/main.rs:8:11
  |
8 |     match v {}
  |           ^ pattern `Meow::Foo` not covered
  |
note: `Meow` defined here
 --> src/main.rs:2:14
  |
2 |     pub enum Meow {
  |              ^^^^
3 |         Foo,
  |         --- not covered
  = note: the matched value is of type `Meow`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
  |
8 ~     match v {
9 +         cat::Meow::Foo => todo!(),
10~     }
  |

For more information about this error, try `rustc --explain E0004`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Rationale and extra context

I think there should be a discussion about whether the path should be in front of the name of the enum. Anyway, the code suggestion is wrong because it will cause an error if we apply it.

Other cases

Rust Version

playground(1.87.0-nightly (2025-02-28 287487624357c19b22d2))

Anything else?

No response

@makai410 makai410 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 1, 2025
@makai410
Copy link
Contributor Author

makai410 commented Mar 1, 2025

This issue is not as simple as "missing path". There is some bugs on trimming the def path.

Consider the examples below:

Example 1

pub mod cat {
    pub mod frog {
        pub enum Meow {
            Foo,
        }
    }
}
fn dog() {
    enum Meow {
        Foo,
    }
}
fn main() {
    let v = cat::frog::Meow::Foo;
    match v {
    }
}

with its output:

error[E0004]: non-exhaustive patterns: `frog::Meow::Foo` not covered
  --> src/main.rs:15:11
   |
15 |     match v {
   |           ^ pattern `frog::Meow::Foo` not covered
   |
note: `frog::Meow` defined here
  --> src/main.rs:3:18
   |
3  |         pub enum Meow {
   |                  ^^^^
4  |             Foo,
   |             --- not covered
   = note: the matched value is of type `frog::Meow`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
   |
16 |     match v {
17 ~         frog::Meow::Foo => todo!(),
18 ~     }
   |

For more information about this error, try `rustc --explain E0004`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Example 2

pub mod cat {
    pub mod cat {
        pub enum Meow {
            Foo,
        }
    }
}
fn dog() {
    enum Meow {
        Foo,
    }
}
fn main() {
    let v = cat::cat::Meow::Foo;
    match v {
    }
}

with its output:

error[E0004]: non-exhaustive patterns: `cat::cat::Meow::Foo` not covered
  --> src/main.rs:15:11
   |
15 |     match v {
   |           ^ pattern `cat::cat::Meow::Foo` not covered
   |
note: `cat::cat::Meow` defined here
  --> src/main.rs:3:18
   |
3  |         pub enum Meow {
   |                  ^^^^
4  |             Foo,
   |             --- not covered
   = note: the matched value is of type `cat::cat::Meow`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
   |
16 |     match v {
17 ~         cat::cat::Meow::Foo => todo!(),
18 ~     }
   |

For more information about this error, try `rustc --explain E0004`.
error: could not compile `playground` (bin "playground") due to 1 previous error

it seems to emit a right code suggestion, but if we remove the function dog:

Example 3

pub mod cat {
    pub mod cat {
        pub enum Meow {
            Foo,
        }
    }
}
fn main() {
    let v = cat::cat::Meow::Foo;
    match v {
    }
}

with its output:

error[E0004]: non-exhaustive patterns: `Meow::Foo` not covered
  --> src/main.rs:10:11
   |
10 |     match v {
   |           ^ pattern `Meow::Foo` not covered
   |
note: `Meow` defined here
  --> src/main.rs:3:18
   |
3  |         pub enum Meow {
   |                  ^^^^
4  |             Foo,
   |             --- not covered
   = note: the matched value is of type `Meow`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
   |
11 |     match v {
12 ~         Meow::Foo => todo!(),
13 ~     }
   |

For more information about this error, try `rustc --explain E0004`.
error: could not compile `playground` (bin "playground") due to 1 previous error

@makai410
Copy link
Contributor Author

makai410 commented Mar 1, 2025

This issue corresponds to #137682 , actually reporting the same problem.

@makai410 makai410 changed the title Missing path in E0004 match suggestion for enum from another module Incorrect path in E0004 match suggestion for enum from another module Mar 1, 2025
@moxian
Copy link
Contributor

moxian commented Mar 2, 2025

Seemingly bisects to #73996 ; the cat:: prefix used to be present in the diagnostics in rustc 1.47.0 and earlier.

#137682 is technically a different problem, since the incorrect function name suggestion has been present all the way since rust 1.10.0

@makai410
Copy link
Contributor Author

makai410 commented Mar 2, 2025

Seemingly bisects to #73996 ; the cat:: prefix used to be present in the diagnostics in rustc 1.47.0 and earlier.

#137682 is technically a different problem, since the incorrect function name suggestion has been present all the way since rust 1.10.0

Thanks for catching this! I will close this issue since it's by design.

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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants