Skip to content

Commit 77dc3ef

Browse files
committed
Auto merge of #132038 - kailan:deprecated-use, r=<try>
Add lint rule for `#[deprecated]` on re-exports As reported in #30827 and #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 #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 ``` try-job: dist-x86_64-linux
2 parents e217f94 + ca664bb commit 77dc3ef

File tree

8 files changed

+21
-9
lines changed

8 files changed

+21
-9
lines changed

Diff for: compiler/rustc_passes/src/stability.rs

+3
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
411411
kind = AnnotationKind::DeprecationProhibited;
412412
const_stab_inherit = InheritConstStability::Yes;
413413
}
414+
hir::ItemKind::Use(_, _) => {
415+
kind = AnnotationKind::DeprecationProhibited;
416+
}
414417
hir::ItemKind::Struct(ref sd, _) => {
415418
if let Some(ctor_def_id) = sd.ctor_def_id() {
416419
self.annotate(

Diff for: library/core/src/alloc/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ pub use self::global::GlobalAlloc;
1010
#[stable(feature = "alloc_layout", since = "1.28.0")]
1111
pub use self::layout::Layout;
1212
#[stable(feature = "alloc_layout", since = "1.28.0")]
13-
#[deprecated(
14-
since = "1.52.0",
15-
note = "Name does not follow std convention, use LayoutError",
16-
suggestion = "LayoutError"
17-
)]
1813
#[allow(deprecated, deprecated_in_future)]
1914
pub use self::layout::LayoutErr;
2015
#[stable(feature = "alloc_layout_error", since = "1.50.0")]

Diff for: library/std/src/collections/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,7 @@ pub use self::hash_map::HashMap;
433433
#[doc(inline)]
434434
pub use self::hash_set::HashSet;
435435
#[stable(feature = "rust1", since = "1.0.0")]
436-
// FIXME(#82080) The deprecation here is only theoretical, and does not actually produce a warning.
437-
#[deprecated(note = "moved to `std::ops::Bound`", since = "1.26.0")]
436+
// FIXME(#82080) This has moved but #[deprecated] on `use` is unsupported.
438437
#[doc(hidden)]
439438
pub use crate::ops::Bound;
440439

Diff for: tests/ui/deprecation/deprecation-sanity.rs

+7
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ impl Default for X {
3939
}
4040
}
4141

42+
mod inner {
43+
pub struct Y;
44+
}
45+
46+
#[deprecated] //~ ERROR this `#[deprecated]` annotation has no effect
47+
pub use inner::Y;
48+
4249
fn main() { }

Diff for: tests/ui/deprecation/deprecation-sanity.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ LL | #[deprecated = "hello"]
6868
|
6969
= note: `#[deny(useless_deprecated)]` on by default
7070

71-
error: aborting due to 10 previous errors
71+
error: this `#[deprecated]` annotation has no effect
72+
--> $DIR/deprecation-sanity.rs:46:1
73+
|
74+
LL | #[deprecated]
75+
| ^^^^^^^^^^^^^ help: remove the unnecessary deprecation attribute
76+
77+
error: aborting due to 11 previous errors
7278

7379
Some errors have detailed explanations: E0538, E0539, E0541, E0565.
7480
For more information about an error, try `rustc --explain E0538`.

Diff for: tests/ui/imports/unused-import-issue-87973.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@ run-rustfix
22
#![deny(unused_imports)]
3+
#![allow(useless_deprecated)]
34

45
// Check that attributes get removed too. See #87973.
56
//~^ ERROR unused import

Diff for: tests/ui/imports/unused-import-issue-87973.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@ run-rustfix
22
#![deny(unused_imports)]
3+
#![allow(useless_deprecated)]
34

45
// Check that attributes get removed too. See #87973.
56
#[deprecated]

Diff for: tests/ui/imports/unused-import-issue-87973.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unused import: `std::fs`
2-
--> $DIR/unused-import-issue-87973.rs:8:5
2+
--> $DIR/unused-import-issue-87973.rs:9:5
33
|
44
LL | use std::fs;
55
| ^^^^^^^

0 commit comments

Comments
 (0)