Skip to content

Compiler error uses re-export name of not-imported crate for a std trait #81973

Description

@davepacheco

I tried this code:

use serde::Deserialize;
use std::error::Error;

pub struct S<ErrorType: Error> {
    p: std::marker::PhantomData<ErrorType>,
}

impl<ErrorType> S<ErrorType> {
    fn f(&self) {
    }
}

I expected to see an error like this:

error[E0277]: the trait bound `ErrorType: std::error::Error` is not satisfied
 --> src/lib.rs:8:17
  |
4 | pub struct S<ErrorType: Error> {
  |                         ----- required by this bound in `S`
...
8 | impl<ErrorType> S<ErrorType> {
  |                 ^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `ErrorType`
  |
help: consider restricting type parameter `ErrorType`
  |
8 | impl<ErrorType: std::error::Error> S<ErrorType> {
  |               ^^^^^^^^^^^^^^^^^^^

Instead, I saw an error like this:

error[E0277]: the trait bound `ErrorType: StdError` is not satisfied
 --> src/lib.rs:8:17
  |
4 | pub struct S<ErrorType: Error> {
  |                         ----- required by this bound in `S`
...
8 | impl<ErrorType> S<ErrorType> {
  |                 ^^^^^^^^^^^^ the trait `StdError` is not implemented for `ErrorType`
  |
help: consider restricting type parameter `ErrorType`
  |
8 | impl<ErrorType: StdError> S<ErrorType> {
  |               ^^^^^^^^^^

Note that the compiler is referring to StdError -- that's not a type in my code nor in scope where the error occurs. I found that if I comment out the use serde::Deserialize; at the top, then the compiler refers to the trait as std::error::Error (as I'd expect). serde does have this in it (under some conditions):

pub use std::error::Error as StdError;

but I haven't pulled that name into scope here.

You can play with this here:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=44c3501773900c0afe548a9a4b871f09

Meta

With the playground link above, the behavior looks the same on stable 1.49, beta 1.50.0-beta.9, and nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions