-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
4 changed files
with
120 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/ui/stability-attribute/stability-attribute-collision-safe-user.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// aux-build:stability-attribute-collision-safe.rs | ||
#![deny(unstable_name_collisions)] | ||
|
||
extern crate stability_attribute_collision_safe; | ||
use stability_attribute_collision_safe::{Trait, OtherTrait}; | ||
|
||
pub trait LocalTrait { | ||
fn not_safe(&self) -> u32 { | ||
1 | ||
} | ||
|
||
fn safe(&self) -> u32 { | ||
1 | ||
} | ||
} | ||
|
||
impl LocalTrait for char {} | ||
|
||
|
||
fn main() { | ||
// Despite having `collision_safe` on defn, the fn chosen doesn't have a stability attribute, | ||
// thus could be user code (and is in this test), so the lint is still appropriate.. | ||
assert_eq!('x'.safe(), 1); | ||
//~^ ERROR an associated function with this name may be added to the standard library in the future | ||
//~^^ WARN once this associated item is added to the standard library, the ambiguity may cause an error or change in behavior! | ||
|
||
// ..but with `collision_safe` on defn, if the chosen item has a stability attribute, then | ||
// assumed to be from std or somewhere that's been checked to be okay, so no lint.. | ||
assert_eq!('x'.safe_and_shadowing_a_stable_item(), 4); // okay! | ||
|
||
// ..and not safe functions should, of course, still lint.. | ||
assert_eq!('x'.not_safe(), 1); | ||
//~^ ERROR an associated function with this name may be added to the standard library in the future | ||
//~^^ WARN once this associated item is added to the standard library, the ambiguity may cause an error or change in behavior! | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/ui/stability-attribute/stability-attribute-collision-safe-user.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
error: an associated function with this name may be added to the standard library in the future | ||
--> $DIR/stability-attribute-collision-safe-user.rs:23:20 | ||
| | ||
LL | assert_eq!('x'.safe(), 1); | ||
| ^^^^ | ||
| | ||
= warning: once this associated item is added to the standard library, the ambiguity may cause an error or change in behavior! | ||
= note: for more information, see issue #48919 <https://github.com/rust-lang/rust/issues/48919> | ||
= help: call with fully qualified syntax `LocalTrait::safe(...)` to keep using the current method | ||
= help: add `#![feature(new_trait_feature)]` to the crate attributes to enable `safe` | ||
note: the lint level is defined here | ||
--> $DIR/stability-attribute-collision-safe-user.rs:2:9 | ||
| | ||
LL | #![deny(unstable_name_collisions)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: an associated function with this name may be added to the standard library in the future | ||
--> $DIR/stability-attribute-collision-safe-user.rs:32:20 | ||
| | ||
LL | assert_eq!('x'.not_safe(), 1); | ||
| ^^^^^^^^ | ||
| | ||
= warning: once this associated item is added to the standard library, the ambiguity may cause an error or change in behavior! | ||
= note: for more information, see issue #48919 <https://github.com/rust-lang/rust/issues/48919> | ||
= help: call with fully qualified syntax `LocalTrait::not_safe(...)` to keep using the current method | ||
= help: add `#![feature(new_trait_feature)]` to the crate attributes to enable `not_safe` | ||
|
||
error: aborting due to 2 previous errors | ||
|