Skip to content

Commit 8454ee8

Browse files
Migrate rustc_depr uses to use deprecation attribute
This should not be a change in behavior.
1 parent 21a3d29 commit 8454ee8

File tree

7 files changed

+138
-181
lines changed

7 files changed

+138
-181
lines changed

src/librustc_attr/builtin.rs

+2-52
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op
129129
pub struct Stability {
130130
pub level: StabilityLevel,
131131
pub feature: Symbol,
132-
pub rustc_depr: Option<RustcDeprecation>,
133132
}
134133

135134
/// Represents the `#[rustc_const_unstable]` and `#[rustc_const_stable]` attributes.
@@ -162,15 +161,6 @@ impl StabilityLevel {
162161
}
163162
}
164163

165-
#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Copy, Clone, Debug, Eq, Hash)]
166-
#[derive(HashStable_Generic)]
167-
pub struct RustcDeprecation {
168-
pub since: Symbol,
169-
pub reason: Symbol,
170-
/// A text snippet used to completely replace any use of the deprecated item in an expression.
171-
pub suggestion: Option<Symbol>,
172-
}
173-
174164
/// Checks if `attrs` contains an attribute like `#![feature(feature_name)]`.
175165
/// This will not perform any "sanity checks" on the form of the attributes.
176166
pub fn contains_feature_attr(attrs: &[Attribute], feature_name: Symbol) -> bool {
@@ -204,7 +194,6 @@ where
204194
use StabilityLevel::*;
205195

206196
let mut stab: Option<Stability> = None;
207-
let mut rustc_depr: Option<RustcDeprecation> = None;
208197
let mut const_stab: Option<ConstStability> = None;
209198
let mut promotable = false;
210199
let mut allow_const_fn_ptr = false;
@@ -256,45 +245,6 @@ where
256245
}
257246
};
258247

259-
macro_rules! get_meta {
260-
($($name:ident),+) => {
261-
$(
262-
let mut $name = None;
263-
)+
264-
for meta in metas {
265-
if let Some(mi) = meta.meta_item() {
266-
match mi.name_or_empty() {
267-
$(
268-
sym::$name => if !get(mi, &mut $name) { continue 'outer },
269-
)+
270-
_ => {
271-
let expected = &[ $( stringify!($name) ),+ ];
272-
handle_errors(
273-
sess,
274-
mi.span,
275-
AttrError::UnknownMetaItem(
276-
pprust::path_to_string(&mi.path),
277-
expected,
278-
),
279-
);
280-
continue 'outer
281-
}
282-
}
283-
} else {
284-
handle_errors(
285-
sess,
286-
meta.span(),
287-
AttrError::UnsupportedLiteral(
288-
"unsupported literal",
289-
false,
290-
),
291-
);
292-
continue 'outer
293-
}
294-
}
295-
}
296-
}
297-
298248
let meta_name = meta.name_or_empty();
299249
match meta_name {
300250
sym::rustc_const_unstable | sym::unstable => {
@@ -398,7 +348,7 @@ where
398348
(Some(feature), reason, Some(_)) => {
399349
let level = Unstable { reason, issue: issue_num, is_soft };
400350
if sym::unstable == meta_name {
401-
stab = Some(Stability { level, feature, rustc_depr: None });
351+
stab = Some(Stability { level, feature });
402352
} else {
403353
const_stab = Some(ConstStability {
404354
level,
@@ -470,7 +420,7 @@ where
470420
(Some(feature), Some(since)) => {
471421
let level = Stable { since };
472422
if sym::stable == meta_name {
473-
stab = Some(Stability { level, feature, rustc_depr: None });
423+
stab = Some(Stability { level, feature });
474424
} else {
475425
const_stab = Some(ConstStability {
476426
level,

src/librustc_middle/middle/stability.rs

+41-30
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub use self::StabilityLevel::*;
55

66
use crate::ty::{self, TyCtxt};
77
use rustc_ast::ast::CRATE_NODE_ID;
8-
use rustc_attr::{self as attr, ConstStability, Deprecation, RustcDeprecation, Stability};
8+
use rustc_attr::{self as attr, ConstStability, Deprecation, Stability};
99
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1010
use rustc_errors::{Applicability, DiagnosticBuilder};
1111
use rustc_feature::GateIssue;
@@ -130,14 +130,26 @@ pub fn report_unstable(
130130

131131
/// Checks whether an item marked with `deprecated(since="X")` is currently
132132
/// deprecated (i.e., whether X is not greater than the current rustc version).
133-
pub fn deprecation_in_effect(since: &str) -> bool {
133+
pub fn deprecation_in_effect(is_since_rustc_version: bool, since: Option<&str>) -> bool {
134+
let since = if let Some(since) = since {
135+
if is_since_rustc_version {
136+
since
137+
} else {
138+
// We assume that the deprecation is in effect if it's not a
139+
// rustc version.
140+
return true;
141+
}
142+
} else {
143+
// If since attribute is not set, then we're definitely in effect.
144+
return true;
145+
};
134146
fn parse_version(ver: &str) -> Vec<u32> {
135147
// We ignore non-integer components of the version (e.g., "nightly").
136148
ver.split(|c| c == '.' || c == '-').flat_map(|s| s.parse()).collect()
137149
}
138150

139151
if let Some(rustc) = option_env!("CFG_RELEASE") {
140-
let since: Vec<u32> = parse_version(since);
152+
let since: Vec<u32> = parse_version(&since);
141153
let rustc: Vec<u32> = parse_version(rustc);
142154
// We simply treat invalid `since` attributes as relating to a previous
143155
// Rust version, thus always displaying the warning.
@@ -167,31 +179,27 @@ pub fn deprecation_suggestion(
167179
}
168180
}
169181

170-
fn deprecation_message_common(message: String, reason: Option<Symbol>) -> String {
171-
match reason {
172-
Some(reason) => format!("{}: {}", message, reason),
173-
None => message,
174-
}
175-
}
176-
177182
pub fn deprecation_message(depr: &Deprecation, path: &str) -> (String, &'static Lint) {
178-
let message = format!("use of deprecated item '{}'", path);
179-
(deprecation_message_common(message, depr.note), DEPRECATED)
180-
}
181-
182-
pub fn rustc_deprecation_message(depr: &RustcDeprecation, path: &str) -> (String, &'static Lint) {
183-
let (message, lint) = if deprecation_in_effect(&depr.since.as_str()) {
183+
let (message, lint) = if deprecation_in_effect(
184+
depr.is_since_rustc_version,
185+
depr.since.map(Symbol::as_str).as_deref(),
186+
) {
184187
(format!("use of deprecated item '{}'", path), DEPRECATED)
185188
} else {
186189
(
187190
format!(
188191
"use of item '{}' that will be deprecated in future version {}",
189-
path, depr.since
192+
path,
193+
depr.since.unwrap()
190194
),
191195
DEPRECATED_IN_FUTURE,
192196
)
193197
};
194-
(deprecation_message_common(message, Some(depr.reason)), lint)
198+
let message = match depr.note {
199+
Some(reason) => format!("{}: {}", message, reason),
200+
None => message,
201+
};
202+
(message, lint)
195203
}
196204

197205
pub fn early_report_deprecation(
@@ -289,10 +297,23 @@ impl<'tcx> TyCtxt<'tcx> {
289297
.lookup_deprecation_entry(parent_def_id.to_def_id())
290298
.map_or(false, |parent_depr| parent_depr.same_origin(&depr_entry));
291299

292-
if !skip {
300+
// #[deprecated] doesn't emit a notice if we're not on the
301+
// topmost deprecation. For example, if a struct is deprecated,
302+
// the use of a field won't be linted.
303+
//
304+
// #[rustc_deprecated] however wants to emit down the whole
305+
// hierarchy.
306+
if !skip || depr_entry.attr.is_since_rustc_version {
293307
let (message, lint) =
294308
deprecation_message(&depr_entry.attr, &self.def_path_str(def_id));
295-
late_report_deprecation(self, &message, None, lint, span, id);
309+
late_report_deprecation(
310+
self,
311+
&message,
312+
depr_entry.attr.suggestion,
313+
lint,
314+
span,
315+
id,
316+
);
296317
}
297318
};
298319
}
@@ -310,16 +331,6 @@ impl<'tcx> TyCtxt<'tcx> {
310331
def_id, span, stability
311332
);
312333

313-
if let Some(id) = id {
314-
if let Some(stability) = stability {
315-
if let Some(depr) = &stability.rustc_depr {
316-
let (message, lint) =
317-
rustc_deprecation_message(depr, &self.def_path_str(def_id));
318-
late_report_deprecation(self, &message, depr.suggestion, lint, span, id);
319-
}
320-
}
321-
}
322-
323334
// Only the cross-crate scenario matters when checking unstable APIs
324335
let cross_crate = !def_id.is_local();
325336
if !cross_crate {

0 commit comments

Comments
 (0)