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

typeck: silence lint for "collision safe" items #99898

Closed

Commits on Nov 21, 2022

  1. typeck: silence lint for collision safe items

    When an unstable method exists on a type and the user is invoking a
    method that would no longer have priority when the unstable method is
    stabilized, an "unstable name collision" lint is emitted. In the vast
    majority of circumstances, this is desirable.
    
    However, when adding a new inherent method to the standard library,
    which deliberately shadows the name of a method in the `Deref` target,
    then this lint can be triggered, affecting users on stable who don't
    even know about the new unstable inherent method. As the new method is
    being added to the standard library, by the library team, it can be
    known that the lint isn't necessary, as the library team can ensure that
    the new inherent method has the same behaviour and won't cause any
    problems for users when it is stabilized.
    
    ```rust
    pub struct Foo;
    pub struct Bar;
    
    impl std::ops::Deref for Foo {
        type Target = Bar;
        fn deref(&self) -> &Self::Target { &Bar }
    }
    
    impl Foo {
        #[unstable(feature = "new_feature", issue = "none", collision_safe)]
        pub fn example(&self) -> u32 { 4 }
    }
    
    impl Bar {
        #[stable(feature = "old_feature", since = "1.0.0")]
        pub fn example(&self) -> u32 { 3 }
    }
    
    // ..in another crate..
    fn main() {
        let foo = Foo;
        assert_eq!(foo.example(), 3);
        // still invokes `Bar`'s `example`, as the `new_feature` isn't enabled, but doesn't
        // trigger a name collision lint (in practice, both `example` functions should
        // have identical behaviour)
    }
    ```
    
    Without this addition, the new inherent method would need to be
    insta-stable in order to avoid breaking stable users.
    
    Signed-off-by: David Wood <david.wood@huawei.com>
    davidtwco committed Nov 21, 2022
    Configuration menu
    Copy the full SHA
    8644b90 View commit details
    Browse the repository at this point in the history
  2. typeck: always lint for user's shadows

    `collision_safe` works when the new-unstable item is only shadowing a
    from-std stable item, but it could also shadow a user-defined item from
    a extension trait, in which case the lint should still fire. Use the
    presence of a stability attribute on a chosen item as a heuristic for a
    std item and continue to lint if the chosen item isn't from std even if
    the unstable items are collision-safe.
    
    Signed-off-by: David Wood <david.wood@huawei.com>
    davidtwco committed Nov 21, 2022
    Configuration menu
    Copy the full SHA
    f552299 View commit details
    Browse the repository at this point in the history