Skip to content

#[derive] fails to detect associated type when qualified path is used #50730

Open
@durka

Description

@durka

The deriving code scans the field types in order to generate bounds on associated types, if they happen to be used as a field type (this isn't foolproof but it catches a lot of cases).

/// This method helps to extract all the type parameters referenced from a
/// type. For a type parameter `<T>`, it looks for either a `TyPath` that
/// is not global and starts with `T`, or a `TyQPath`.

The comment in this code implies that field: T::Assoc and field: <T as Trait>::Assoc would both work. However, the second one is missed and the required bound is not generated, resulting in a type error:

#[derive(Debug)]
struct Foo<T: FromStr> {
    field: <T as FromStr>::Err
}
error[E0277]: `<T as std::str::FromStr>::Err` doesn't implement `std::fmt::Debug`
  --> expr.rs:18:77
   |
18 |     field: <T as FromStr>::Err
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ `<T as std::str::FromStr>::Err` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
   |
   = help: the trait `std::fmt::Debug` is not implemented for `<T as std::str::FromStr>::Err`
   = help: consider adding a `where <T as std::str::FromStr>::Err: std::fmt::Debug` bound
   = note: required because of the requirements on the impl of `std::fmt::Debug` for `&<T as std::str::FromStr>::Err`
   = note: required for the cast to the object type `std::fmt::Debug`

cc #26925.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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