@@ -15,12 +15,11 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
15
15
use rustc_hir:: { self , HirId } ;
16
16
use rustc_middle:: ty:: print:: with_no_trimmed_paths;
17
17
use rustc_session:: lint:: builtin:: { DEPRECATED , DEPRECATED_IN_FUTURE , SOFT_UNSTABLE } ;
18
- use rustc_session:: lint:: { BuiltinLintDiagnostics , Lint , LintBuffer } ;
18
+ use rustc_session:: lint:: { BuiltinLintDiagnostics , Level , Lint , LintBuffer } ;
19
19
use rustc_session:: parse:: feature_err_issue;
20
20
use rustc_session:: { DiagnosticMessageId , Session } ;
21
21
use rustc_span:: symbol:: { sym, Symbol } ;
22
22
use rustc_span:: { MultiSpan , Span } ;
23
-
24
23
use std:: num:: NonZeroU32 ;
25
24
26
25
#[ derive( PartialEq , Clone , Copy , Debug ) ]
@@ -125,7 +124,11 @@ pub fn report_unstable(
125
124
126
125
/// Checks whether an item marked with `deprecated(since="X")` is currently
127
126
/// deprecated (i.e., whether X is not greater than the current rustc version).
128
- pub fn deprecation_in_effect ( is_since_rustc_version : bool , since : Option < & str > ) -> bool {
127
+ pub fn deprecation_in_effect ( depr : & Deprecation ) -> bool {
128
+ let is_since_rustc_version = depr. is_since_rustc_version ;
129
+ let since = depr. since . map ( Symbol :: as_str) ;
130
+ let since = since. as_deref ( ) ;
131
+
129
132
fn parse_version ( ver : & str ) -> Vec < u32 > {
130
133
// We ignore non-integer components of the version (e.g., "nightly").
131
134
ver. split ( |c| c == '.' || c == '-' ) . flat_map ( |s| s. parse ( ) ) . collect ( )
@@ -175,33 +178,50 @@ pub fn deprecation_suggestion(
175
178
}
176
179
}
177
180
178
- pub fn deprecation_message ( depr : & Deprecation , kind : & str , path : & str ) -> ( String , & ' static Lint ) {
179
- let since = depr. since . map ( Symbol :: as_str) ;
180
- let ( message, lint) = if deprecation_in_effect ( depr. is_since_rustc_version , since. as_deref ( ) ) {
181
- ( format ! ( "use of deprecated {} `{}`" , kind, path) , DEPRECATED )
181
+ fn deprecation_lint ( is_in_effect : bool ) -> & ' static Lint {
182
+ if is_in_effect { DEPRECATED } else { DEPRECATED_IN_FUTURE }
183
+ }
184
+
185
+ fn deprecation_message (
186
+ is_in_effect : bool ,
187
+ since : Option < Symbol > ,
188
+ note : Option < Symbol > ,
189
+ kind : & str ,
190
+ path : & str ,
191
+ ) -> String {
192
+ let message = if is_in_effect {
193
+ format ! ( "use of deprecated {} `{}`" , kind, path)
182
194
} else {
183
- (
184
- if since. as_deref ( ) == Some ( "TBD" ) {
185
- format ! (
186
- "use of {} `{}` that will be deprecated in a future Rust version" ,
187
- kind, path
188
- )
189
- } else {
190
- format ! (
191
- "use of {} `{}` that will be deprecated in future version {}" ,
192
- kind,
193
- path,
194
- since. unwrap( )
195
- )
196
- } ,
197
- DEPRECATED_IN_FUTURE ,
198
- )
195
+ let since = since. map ( Symbol :: as_str) ;
196
+
197
+ if since. as_deref ( ) == Some ( "TBD" ) {
198
+ format ! ( "use of {} `{}` that will be deprecated in a future Rust version" , kind, path)
199
+ } else {
200
+ format ! (
201
+ "use of {} `{}` that will be deprecated in future version {}" ,
202
+ kind,
203
+ path,
204
+ since. unwrap( )
205
+ )
206
+ }
199
207
} ;
200
- let message = match depr. note {
208
+
209
+ match note {
201
210
Some ( reason) => format ! ( "{}: {}" , message, reason) ,
202
211
None => message,
203
- } ;
204
- ( message, lint)
212
+ }
213
+ }
214
+
215
+ pub fn deprecation_message_and_lint (
216
+ depr : & Deprecation ,
217
+ kind : & str ,
218
+ path : & str ,
219
+ ) -> ( String , & ' static Lint ) {
220
+ let is_in_effect = deprecation_in_effect ( depr) ;
221
+ (
222
+ deprecation_message ( is_in_effect, depr. since , depr. note , kind, path) ,
223
+ deprecation_lint ( is_in_effect) ,
224
+ )
205
225
}
206
226
207
227
pub fn early_report_deprecation (
@@ -303,20 +323,34 @@ impl<'tcx> TyCtxt<'tcx> {
303
323
//
304
324
// #[rustc_deprecated] however wants to emit down the whole
305
325
// hierarchy.
306
- if !skip || depr_entry. attr . is_since_rustc_version {
307
- let path = & with_no_trimmed_paths ( || self . def_path_str ( def_id) ) ;
308
- let kind = self . def_kind ( def_id) . descr ( def_id) ;
309
- let ( message, lint) = deprecation_message ( & depr_entry. attr , kind, path) ;
310
- late_report_deprecation (
311
- self ,
312
- & message,
313
- depr_entry. attr . suggestion ,
314
- lint,
315
- span,
316
- method_span,
317
- id,
318
- def_id,
319
- ) ;
326
+ let depr_attr = & depr_entry. attr ;
327
+ if !skip || depr_attr. is_since_rustc_version {
328
+ // Calculating message for lint involves calling `self.def_path_str`.
329
+ // Which by default to calculate visible path will invoke expensive `visible_parent_map` query.
330
+ // So we skip message calculation altogether, if lint is allowed.
331
+ let is_in_effect = deprecation_in_effect ( depr_attr) ;
332
+ let lint = deprecation_lint ( is_in_effect) ;
333
+ if self . lint_level_at_node ( lint, id) . 0 != Level :: Allow {
334
+ let def_path = & with_no_trimmed_paths ( || self . def_path_str ( def_id) ) ;
335
+ let def_kind = self . def_kind ( def_id) . descr ( def_id) ;
336
+
337
+ late_report_deprecation (
338
+ self ,
339
+ & deprecation_message (
340
+ is_in_effect,
341
+ depr_attr. since ,
342
+ depr_attr. note ,
343
+ def_kind,
344
+ def_path,
345
+ ) ,
346
+ depr_attr. suggestion ,
347
+ lint,
348
+ span,
349
+ method_span,
350
+ id,
351
+ def_id,
352
+ ) ;
353
+ }
320
354
}
321
355
} ;
322
356
}
0 commit comments