Skip to content

Commit 741a3d4

Browse files
committed
Auto merge of #58002 - oli-obk:deprecated_sugg, r=zackmdavis
Add suggestions to deprecation lints Clippy used to do this suggestion, but the clippy lints happen after the deprecation lints so we ended up never seeing the structured suggestions.
2 parents f29b4fb + 4056b57 commit 741a3d4

File tree

8 files changed

+111
-26
lines changed

8 files changed

+111
-26
lines changed

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
#![feature(abi_unadjusted)]
125125
#![feature(adx_target_feature)]
126126
#![feature(maybe_uninit)]
127+
#![feature(unrestricted_attribute_tokens)]
127128

128129
#[prelude_import]
129130
#[allow(unused)]

src/libcore/sync/atomic.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,15 @@ pub enum Ordering {
290290
/// [`AtomicBool`]: struct.AtomicBool.html
291291
#[cfg(target_has_atomic = "8")]
292292
#[stable(feature = "rust1", since = "1.0.0")]
293-
#[rustc_deprecated(since = "1.34.0", reason = "the `new` function is now preferred")]
293+
#[cfg_attr(not(stage0), rustc_deprecated(
294+
since = "1.34.0",
295+
reason = "the `new` function is now preferred",
296+
suggestion = "AtomicBool::new(false)",
297+
))]
298+
#[cfg_attr(stage0, rustc_deprecated(
299+
since = "1.34.0",
300+
reason = "the `new` function is now preferred",
301+
))]
294302
pub const ATOMIC_BOOL_INIT: AtomicBool = AtomicBool::new(false);
295303

296304
#[cfg(target_has_atomic = "8")]
@@ -1127,6 +1135,7 @@ macro_rules! atomic_int {
11271135
$extra_feature:expr,
11281136
$min_fn:ident, $max_fn:ident,
11291137
$align:expr,
1138+
$atomic_new:expr,
11301139
$int_type:ident $atomic_type:ident $atomic_init:ident) => {
11311140
/// An integer type which can be safely shared between threads.
11321141
///
@@ -1148,7 +1157,15 @@ macro_rules! atomic_int {
11481157

11491158
/// An atomic integer initialized to `0`.
11501159
#[$stable]
1151-
#[rustc_deprecated(since = "1.34.0", reason = "the `new` function is now preferred")]
1160+
#[cfg_attr(stage0, rustc_deprecated(
1161+
since = "1.34.0",
1162+
reason = "the `new` function is now preferred",
1163+
))]
1164+
#[cfg_attr(not(stage0), rustc_deprecated(
1165+
since = "1.34.0",
1166+
reason = "the `new` function is now preferred",
1167+
suggestion = $atomic_new,
1168+
))]
11521169
pub const $atomic_init: $atomic_type = $atomic_type::new(0);
11531170

11541171
#[$stable]
@@ -1878,6 +1895,7 @@ atomic_int! {
18781895
"#![feature(integer_atomics)]\n\n",
18791896
atomic_min, atomic_max,
18801897
1,
1898+
"AtomicI8::new(0)",
18811899
i8 AtomicI8 ATOMIC_I8_INIT
18821900
}
18831901
#[cfg(target_has_atomic = "8")]
@@ -1892,6 +1910,7 @@ atomic_int! {
18921910
"#![feature(integer_atomics)]\n\n",
18931911
atomic_umin, atomic_umax,
18941912
1,
1913+
"AtomicU8::new(0)",
18951914
u8 AtomicU8 ATOMIC_U8_INIT
18961915
}
18971916
#[cfg(target_has_atomic = "16")]
@@ -1906,6 +1925,7 @@ atomic_int! {
19061925
"#![feature(integer_atomics)]\n\n",
19071926
atomic_min, atomic_max,
19081927
2,
1928+
"AtomicI16::new(0)",
19091929
i16 AtomicI16 ATOMIC_I16_INIT
19101930
}
19111931
#[cfg(target_has_atomic = "16")]
@@ -1920,6 +1940,7 @@ atomic_int! {
19201940
"#![feature(integer_atomics)]\n\n",
19211941
atomic_umin, atomic_umax,
19221942
2,
1943+
"AtomicU16::new(0)",
19231944
u16 AtomicU16 ATOMIC_U16_INIT
19241945
}
19251946
#[cfg(target_has_atomic = "32")]
@@ -1934,6 +1955,7 @@ atomic_int! {
19341955
"#![feature(integer_atomics)]\n\n",
19351956
atomic_min, atomic_max,
19361957
4,
1958+
"AtomicI32::new(0)",
19371959
i32 AtomicI32 ATOMIC_I32_INIT
19381960
}
19391961
#[cfg(target_has_atomic = "32")]
@@ -1948,6 +1970,7 @@ atomic_int! {
19481970
"#![feature(integer_atomics)]\n\n",
19491971
atomic_umin, atomic_umax,
19501972
4,
1973+
"AtomicU32::new(0)",
19511974
u32 AtomicU32 ATOMIC_U32_INIT
19521975
}
19531976
#[cfg(target_has_atomic = "64")]
@@ -1962,6 +1985,7 @@ atomic_int! {
19621985
"#![feature(integer_atomics)]\n\n",
19631986
atomic_min, atomic_max,
19641987
8,
1988+
"AtomicI64::new(0)",
19651989
i64 AtomicI64 ATOMIC_I64_INIT
19661990
}
19671991
#[cfg(target_has_atomic = "64")]
@@ -1976,6 +2000,7 @@ atomic_int! {
19762000
"#![feature(integer_atomics)]\n\n",
19772001
atomic_umin, atomic_umax,
19782002
8,
2003+
"AtomicU64::new(0)",
19792004
u64 AtomicU64 ATOMIC_U64_INIT
19802005
}
19812006
#[cfg(target_has_atomic = "128")]
@@ -1990,6 +2015,7 @@ atomic_int! {
19902015
"#![feature(integer_atomics)]\n\n",
19912016
atomic_min, atomic_max,
19922017
16,
2018+
"AtomicI128::new(0)",
19932019
i128 AtomicI128 ATOMIC_I128_INIT
19942020
}
19952021
#[cfg(target_has_atomic = "128")]
@@ -2004,6 +2030,7 @@ atomic_int! {
20042030
"#![feature(integer_atomics)]\n\n",
20052031
atomic_umin, atomic_umax,
20062032
16,
2033+
"AtomicU128::new(0)",
20072034
u128 AtomicU128 ATOMIC_U128_INIT
20082035
}
20092036
#[cfg(target_pointer_width = "16")]
@@ -2030,6 +2057,7 @@ atomic_int!{
20302057
"",
20312058
atomic_min, atomic_max,
20322059
ptr_width!(),
2060+
"AtomicIsize::new(0)",
20332061
isize AtomicIsize ATOMIC_ISIZE_INIT
20342062
}
20352063
#[cfg(target_has_atomic = "ptr")]
@@ -2044,6 +2072,7 @@ atomic_int!{
20442072
"",
20452073
atomic_umin, atomic_umax,
20462074
ptr_width!(),
2075+
"AtomicUsize::new(0)",
20472076
usize AtomicUsize ATOMIC_USIZE_INIT
20482077
}
20492078

src/librustc/ich/impls_syntax.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ for ::syntax::attr::StabilityLevel {
147147
}
148148
}
149149

150-
impl_stable_hash_for!(struct ::syntax::attr::RustcDeprecation { since, reason });
150+
impl_stable_hash_for!(struct ::syntax::attr::RustcDeprecation { since, reason, suggestion });
151151

152152

153153
impl_stable_hash_for!(enum ::syntax::attr::IntType {

src/librustc/middle/stability.rs

+40-22
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use syntax::symbol::Symbol;
1616
use syntax_pos::{Span, MultiSpan};
1717
use syntax::ast;
1818
use syntax::ast::{NodeId, Attribute};
19+
use syntax::errors::Applicability;
1920
use syntax::feature_gate::{GateIssue, emit_feature_err};
2021
use syntax::attr::{self, Stability, Deprecation};
2122
use ty::{self, TyCtxt};
@@ -569,6 +570,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
569570
let lint_deprecated = |def_id: DefId,
570571
id: NodeId,
571572
note: Option<Symbol>,
573+
suggestion: Option<Symbol>,
572574
message: &str,
573575
lint: &'static Lint| {
574576
let msg = if let Some(note) = note {
@@ -577,7 +579,18 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
577579
format!("{}", message)
578580
};
579581

580-
self.lint_node(lint, id, span, &msg);
582+
let mut diag = self.struct_span_lint_node(lint, id, span, &msg);
583+
if let Some(suggestion) = suggestion {
584+
if let hir::Node::Expr(_) = self.hir().get(id) {
585+
diag.span_suggestion(
586+
span,
587+
&msg,
588+
suggestion.to_string(),
589+
Applicability::MachineApplicable,
590+
);
591+
}
592+
}
593+
diag.emit();
581594
if id == ast::DUMMY_NODE_ID {
582595
span_bug!(span, "emitted a {} lint with dummy node id: {:?}", lint.name, def_id);
583596
}
@@ -613,6 +626,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
613626
lint_deprecated(def_id,
614627
id,
615628
depr_entry.attr.note,
629+
None,
616630
&message,
617631
lint::builtin::DEPRECATED_IN_FUTURE);
618632
} else if !skip {
@@ -621,6 +635,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
621635
lint_deprecated(def_id,
622636
id,
623637
depr_entry.attr.note,
638+
None,
624639
&message,
625640
lint::builtin::DEPRECATED);
626641
}
@@ -639,27 +654,30 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
639654
debug!("stability: \
640655
inspecting def_id={:?} span={:?} of stability={:?}", def_id, span, stability);
641656

642-
if let Some(&Stability{rustc_depr: Some(attr::RustcDeprecation { reason, since }), ..})
643-
= stability {
644-
if let Some(id) = id {
645-
let path = self.item_path_str(def_id);
646-
if deprecation_in_effect(&since.as_str()) {
647-
let message = format!("use of deprecated item '{}'", path);
648-
lint_deprecated(def_id,
649-
id,
650-
Some(reason),
651-
&message,
652-
lint::builtin::DEPRECATED);
653-
} else {
654-
let message = format!("use of item '{}' \
655-
that will be deprecated in future version {}",
656-
path,
657-
since);
658-
lint_deprecated(def_id,
659-
id,
660-
Some(reason),
661-
&message,
662-
lint::builtin::DEPRECATED_IN_FUTURE);
657+
if let Some(id) = id {
658+
if let Some(stability) = stability {
659+
if let Some(depr) = &stability.rustc_depr {
660+
let path = self.item_path_str(def_id);
661+
if deprecation_in_effect(&depr.since.as_str()) {
662+
let message = format!("use of deprecated item '{}'", path);
663+
lint_deprecated(def_id,
664+
id,
665+
Some(depr.reason),
666+
depr.suggestion,
667+
&message,
668+
lint::builtin::DEPRECATED);
669+
} else {
670+
let message = format!("use of item '{}' \
671+
that will be deprecated in future version {}",
672+
path,
673+
depr.since);
674+
lint_deprecated(def_id,
675+
id,
676+
Some(depr.reason),
677+
depr.suggestion,
678+
&message,
679+
lint::builtin::DEPRECATED_IN_FUTURE);
680+
}
663681
}
664682
}
665683
}

src/libsyntax/attr/builtin.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ impl StabilityLevel {
158158
pub struct RustcDeprecation {
159159
pub since: Symbol,
160160
pub reason: Symbol,
161+
/// A text snippet used to completely replace any use of the deprecated item in an expression.
162+
pub suggestion: Option<Symbol>,
161163
}
162164

163165
/// Check if `attrs` contains an attribute like `#![feature(feature_name)]`.
@@ -274,13 +276,14 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
274276
continue 'outer
275277
}
276278

277-
get_meta!(since, reason);
279+
get_meta!(since, reason, suggestion);
278280

279281
match (since, reason) {
280282
(Some(since), Some(reason)) => {
281283
rustc_depr = Some(RustcDeprecation {
282284
since,
283285
reason,
286+
suggestion,
284287
})
285288
}
286289
(None, _) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
// compile-pass
3+
4+
#[allow(deprecated, unused_imports)]
5+
use std::sync::atomic::{AtomicIsize, ATOMIC_ISIZE_INIT};
6+
7+
#[allow(dead_code)]
8+
static FOO: AtomicIsize = AtomicIsize::new(0);
9+
//~^ WARN use of deprecated item
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
// compile-pass
3+
4+
#[allow(deprecated, unused_imports)]
5+
use std::sync::atomic::{AtomicIsize, ATOMIC_ISIZE_INIT};
6+
7+
#[allow(dead_code)]
8+
static FOO: AtomicIsize = ATOMIC_ISIZE_INIT;
9+
//~^ WARN use of deprecated item
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
warning: use of deprecated item 'std::sync::atomic::ATOMIC_ISIZE_INIT': the `new` function is now preferred
2+
--> $DIR/atomic_initializers.rs:8:27
3+
|
4+
LL | static FOO: AtomicIsize = ATOMIC_ISIZE_INIT;
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: #[warn(deprecated)] on by default
8+
help: use of deprecated item 'std::sync::atomic::ATOMIC_ISIZE_INIT': the `new` function is now preferred
9+
|
10+
LL | static FOO: AtomicIsize = AtomicIsize::new(0);
11+
| ^^^^^^^^^^^^^^^^^^^
12+

0 commit comments

Comments
 (0)