@@ -5,7 +5,7 @@ pub use self::StabilityLevel::*;
55
66use crate :: ty:: { self , TyCtxt } ;
77use 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 } ;
99use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
1010use rustc_errors:: { Applicability , DiagnosticBuilder } ;
1111use 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-
177182pub 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
197205pub 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