@@ -5,7 +5,7 @@ pub use self::StabilityLevel::*;
5
5
6
6
use crate :: ty:: { self , TyCtxt } ;
7
7
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 } ;
9
9
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
10
10
use rustc_errors:: { Applicability , DiagnosticBuilder } ;
11
11
use rustc_feature:: GateIssue ;
@@ -130,14 +130,26 @@ pub fn report_unstable(
130
130
131
131
/// Checks whether an item marked with `deprecated(since="X")` is currently
132
132
/// 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
+ } ;
134
146
fn parse_version ( ver : & str ) -> Vec < u32 > {
135
147
// We ignore non-integer components of the version (e.g., "nightly").
136
148
ver. split ( |c| c == '.' || c == '-' ) . flat_map ( |s| s. parse ( ) ) . collect ( )
137
149
}
138
150
139
151
if let Some ( rustc) = option_env ! ( "CFG_RELEASE" ) {
140
- let since: Vec < u32 > = parse_version ( since) ;
152
+ let since: Vec < u32 > = parse_version ( & since) ;
141
153
let rustc: Vec < u32 > = parse_version ( rustc) ;
142
154
// We simply treat invalid `since` attributes as relating to a previous
143
155
// Rust version, thus always displaying the warning.
@@ -167,31 +179,27 @@ pub fn deprecation_suggestion(
167
179
}
168
180
}
169
181
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
-
177
182
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
+ ) {
184
187
( format ! ( "use of deprecated item '{}'" , path) , DEPRECATED )
185
188
} else {
186
189
(
187
190
format ! (
188
191
"use of item '{}' that will be deprecated in future version {}" ,
189
- path, depr. since
192
+ path,
193
+ depr. since. unwrap( )
190
194
) ,
191
195
DEPRECATED_IN_FUTURE ,
192
196
)
193
197
} ;
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)
195
203
}
196
204
197
205
pub fn early_report_deprecation (
@@ -289,10 +297,23 @@ impl<'tcx> TyCtxt<'tcx> {
289
297
. lookup_deprecation_entry ( parent_def_id. to_def_id ( ) )
290
298
. map_or ( false , |parent_depr| parent_depr. same_origin ( & depr_entry) ) ;
291
299
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 {
293
307
let ( message, lint) =
294
308
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
+ ) ;
296
317
}
297
318
} ;
298
319
}
@@ -310,16 +331,6 @@ impl<'tcx> TyCtxt<'tcx> {
310
331
def_id, span, stability
311
332
) ;
312
333
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
-
323
334
// Only the cross-crate scenario matters when checking unstable APIs
324
335
let cross_crate = !def_id. is_local ( ) ;
325
336
if !cross_crate {
0 commit comments