Skip to content

Introduce matches(T: Trait) condition to rustc_on_unimplemented #44755

Closed
@arielb1

Description

@arielb1

We want to allow rustc_on_unimplemented to check arbitrary type conditions to make the error message depend on the input types. The sanest way to do that is to allow conditioning on traits. For example, for #42526 we want something like:

trait IsNoneError {}
impl IsNoneError for NoneError {}

#[rustc_on_unimplemented(
               on(all(from_method="from",
                      from_desugaring="?",
                      matches("IsNoneError", Self="T")),
                  message="can't use Try on Option in a function returning Result")
                  )]
trait From<T> {
    // ...
}

The syntax I prefer is matches("TRAIT_NAME", Self="SELF_NAME", "PARAM_1_NAME", ..) where TRAIT_NAME and SELF_NAME are mandatory, but probably any syntax can work (this is all feature gated under the rustc_on_unimplemented attribute, so we can experiment).

At least for now, we can limit the parameters to be type parameters from our generics - the on_unimplemented code already knows how to look these up. You can use these to implement anything you like with the proper trait bound.

Mentoring notes here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions