Skip to content
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

Stabilize #[diagnostic::do_not_recommend] #132056

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Commits on Nov 18, 2024

  1. Improve testing coverage for #[diagnostic::do_not_recommend]

    This PR aims to improve the testing coverage for
    `#[diagnostic::do_not_recommend]`. It ensures that all tests are run for
    the old and new solver to verify that the behaviour is the same for both
    variants. It also adds two new tests:
    
    * A test with 4 traits having wild card impl for each other, with
    alternating `#[diagnostic::do_not_recommend]` attributse
    * A test with a lifetime dependend wild card impl, which is something
    that's not supported yet
    weiznich committed Nov 18, 2024
    Configuration menu
    Copy the full SHA
    fda91cd View commit details
    Browse the repository at this point in the history
  2. Check #[diagnostic::do_not_recommend] for arguments

    This commit adds a check that verifies that no arguments are passed to
    `#[diagnostic::do_not_recommend]`. If we detect arguments we emit a warning.
    weiznich committed Nov 18, 2024
    Configuration menu
    Copy the full SHA
    15c81ec View commit details
    Browse the repository at this point in the history
  3. Stabilize #[diagnostic::do_not_recommend]

    This commit seeks to stabilize the `#[diagnostic::do_not_recommend]`
    attribute.
    This attribute was first proposed as `#[do_not_recommend`] attribute in
    RFC 2397 (rust-lang/rfcs#2397). It gives the
    crate authors the ability to not suggest to the compiler to not show
    certain traits in it's error messages. With the presence of the
    `#[diagnostic]` tool attribute namespace it was decided to move the
    attribute there, as that lowers the amount of guarantees the compiler
    needs to give about the exact way this influences error messages. It
    turns the attribute into a hint which can be ignored. In addition to the
    original proposed functionality this attribute now also hides the marked
    trait in help messages ("This trait is implemented by: ").
    The attribute does not accept any argument and can only be placed on
    trait implementations. If it is placed somewhere else a lint warning is
    emitted and the attribute is otherwise ignored. If an argument is
    detected a lint warning is emitted and the argument is ignored. This
    follows the rules outlined by the diagnostic namespace.
    
    This attribute allows crates like diesel to improve their error messages
    drastically. The most common example here is the following error
    message:
    
    ```
    error[E0277]: the trait bound `&str: Expression` is not satisfied
      --> /home/weiznich/Documents/rust/rust/tests/ui/diagnostic_namespace/do_not_recommend.rs:53:15
       |
    LL |     SelectInt.check("bar");
       |               ^^^^^ the trait `Expression` is not implemented for `&str`, which is required by `&str: AsExpression<Integer>`
       |
       = help: the following other types implement trait `Expression`:
                 Bound<T>
                 SelectInt
    note: required for `&str` to implement `AsExpression<Integer>`
      --> /home/weiznich/Documents/rust/rust/tests/ui/diagnostic_namespace/do_not_recommend.rs:26:13
       |
    LL | impl<T, ST> AsExpression<ST> for T
       |             ^^^^^^^^^^^^^^^^     ^
    LL | where
    LL |     T: Expression<SqlType = ST>,
       |        ------------------------ unsatisfied trait bound introduced here
    ```
    
    By applying the new attribute to the wild card trait implementation of
    `AsExpression` for `T: Expression` the error message becomes:
    
    ```
    error[E0277]: the trait bound `&str: AsExpression<Integer>` is not satisfied
      --> $DIR/as_expression.rs:55:15
       |
    LL |     SelectInt.check("bar");
       |               ^^^^^ the trait `AsExpression<Integer>` is not implemented for `&str`
       |
       = help: the trait `AsExpression<Text>` is implemented for `&str`
       = help: for that trait implementation, expected `Text`, found `Integer`
    ```
    
    which makes it much easier for users to understand that they are facing
    a type mismatch.
    
    Other explored example usages included
    
    * This standard library error message: rust-lang#128008
    * That bevy derived example:
    https://github.com/rust-lang/rust/blob/e1f306899514ea80abc1d1c9f6a57762afb304a3/tests/ui/diagnostic_namespace/do_not_recommend/supress_suggestions_in_help.rs (No
    more tuple pyramids)
    
    Fixes rust-lang#51992
    weiznich committed Nov 18, 2024
    Configuration menu
    Copy the full SHA
    56f25f7 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    3b184b6 View commit details
    Browse the repository at this point in the history