Skip to content

Commit

Permalink
Rollup merge of rust-lang#132038 - kailan:deprecated-use, r=pnkfelix
Browse files Browse the repository at this point in the history
Add lint rule for `#[deprecated]` on re-exports

As reported in rust-lang#30827 and rust-lang#84584, marking a re-export (`pub use`) with `#[deprecated]` does not produce a warning for consumers. In fact, there are instances of this in the `core` and `std` crates (see past issue rust-lang#82080 where this caused some confusion).

This change modifies the stability annotation visitor to mark `#[deprecated]` annotations on `use` statements with `AnnotationKind::DeprecationProhibited` so that library developers are aware that the annotation is not warning their users as expected.

```rust
#[deprecated]
pub use a_module::ActiveType;
```
```
error: this `#[deprecated]` annotation has no effect
  --> $DIR/deprecated_use.rs:6:1
   |
LL | #[deprecated]
   | ^^^^^^^^^^^^^ help: remove the unnecessary deprecation attribute
   |
   = note: `#[deny(useless_deprecated)]` on by default

error: aborting due to 1 previous error
```
  • Loading branch information
matthiaskrgr authored Dec 12, 2024
2 parents 606a2c9 + ca664bb commit 0c0b1de
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 9 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
kind = AnnotationKind::DeprecationProhibited;
const_stab_inherit = InheritConstStability::Yes;
}
hir::ItemKind::Use(_, _) => {
kind = AnnotationKind::DeprecationProhibited;
}
hir::ItemKind::Struct(ref sd, _) => {
if let Some(ctor_def_id) = sd.ctor_def_id() {
self.annotate(
Expand Down
5 changes: 0 additions & 5 deletions library/core/src/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ pub use self::global::GlobalAlloc;
#[stable(feature = "alloc_layout", since = "1.28.0")]
pub use self::layout::Layout;
#[stable(feature = "alloc_layout", since = "1.28.0")]
#[deprecated(
since = "1.52.0",
note = "Name does not follow std convention, use LayoutError",
suggestion = "LayoutError"
)]
#[allow(deprecated, deprecated_in_future)]
pub use self::layout::LayoutErr;
#[stable(feature = "alloc_layout_error", since = "1.50.0")]
Expand Down
3 changes: 1 addition & 2 deletions library/std/src/collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,7 @@ pub use self::hash_map::HashMap;
#[doc(inline)]
pub use self::hash_set::HashSet;
#[stable(feature = "rust1", since = "1.0.0")]
// FIXME(#82080) The deprecation here is only theoretical, and does not actually produce a warning.
#[deprecated(note = "moved to `std::ops::Bound`", since = "1.26.0")]
// FIXME(#82080) This has moved but #[deprecated] on `use` is unsupported.
#[doc(hidden)]
pub use crate::ops::Bound;

Expand Down
7 changes: 7 additions & 0 deletions tests/ui/deprecation/deprecation-sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ impl Default for X {
}
}

mod inner {
pub struct Y;
}

#[deprecated] //~ ERROR this `#[deprecated]` annotation has no effect
pub use inner::Y;

fn main() { }
8 changes: 7 additions & 1 deletion tests/ui/deprecation/deprecation-sanity.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ LL | #[deprecated = "hello"]
|
= note: `#[deny(useless_deprecated)]` on by default

error: aborting due to 10 previous errors
error: this `#[deprecated]` annotation has no effect
--> $DIR/deprecation-sanity.rs:46:1
|
LL | #[deprecated]
| ^^^^^^^^^^^^^ help: remove the unnecessary deprecation attribute

error: aborting due to 11 previous errors

Some errors have detailed explanations: E0538, E0539, E0541, E0565.
For more information about an error, try `rustc --explain E0538`.
1 change: 1 addition & 0 deletions tests/ui/imports/unused-import-issue-87973.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//@ run-rustfix
#![deny(unused_imports)]
#![allow(useless_deprecated)]

// Check that attributes get removed too. See #87973.
//~^ ERROR unused import
Expand Down
1 change: 1 addition & 0 deletions tests/ui/imports/unused-import-issue-87973.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//@ run-rustfix
#![deny(unused_imports)]
#![allow(useless_deprecated)]

// Check that attributes get removed too. See #87973.
#[deprecated]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/unused-import-issue-87973.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: unused import: `std::fs`
--> $DIR/unused-import-issue-87973.rs:8:5
--> $DIR/unused-import-issue-87973.rs:9:5
|
LL | use std::fs;
| ^^^^^^^
Expand Down

0 comments on commit 0c0b1de

Please sign in to comment.