@@ -9,14 +9,14 @@ use rustc_attr::{
99 self as attr, ConstStability , DefaultBodyStability , DeprecatedSince , Deprecation , Stability ,
1010} ;
1111use rustc_data_structures:: unord:: UnordMap ;
12- use rustc_errors:: { Applicability , Diag } ;
12+ use rustc_errors:: { Applicability , Diag , EmissionGuarantee } ;
1313use rustc_feature:: GateIssue ;
1414use rustc_hir:: def:: DefKind ;
1515use rustc_hir:: def_id:: { DefId , LocalDefId , LocalDefIdMap } ;
1616use rustc_hir:: { self as hir, HirId } ;
1717use rustc_middle:: ty:: print:: with_no_trimmed_paths;
1818use rustc_session:: lint:: builtin:: { DEPRECATED , DEPRECATED_IN_FUTURE , SOFT_UNSTABLE } ;
19- use rustc_session:: lint:: { BuiltinLintDiag , Level , Lint , LintBuffer } ;
19+ use rustc_session:: lint:: { BuiltinLintDiag , DeprecatedSinceKind , Level , Lint , LintBuffer } ;
2020use rustc_session:: parse:: feature_err_issue;
2121use rustc_session:: Session ;
2222use rustc_span:: symbol:: { sym, Symbol } ;
@@ -124,90 +124,106 @@ pub fn report_unstable(
124124 }
125125}
126126
127- pub fn deprecation_suggestion (
128- diag : & mut Diag < ' _ , ( ) > ,
129- kind : & str ,
130- suggestion : Option < Symbol > ,
131- span : Span ,
132- ) {
133- if let Some ( suggestion) = suggestion {
134- diag. span_suggestion_verbose (
135- span,
136- format ! ( "replace the use of the deprecated {kind}" ) ,
137- suggestion,
138- Applicability :: MachineApplicable ,
139- ) ;
140- }
141- }
142-
143127fn deprecation_lint ( is_in_effect : bool ) -> & ' static Lint {
144128 if is_in_effect { DEPRECATED } else { DEPRECATED_IN_FUTURE }
145129}
146130
147- fn deprecation_message (
148- is_in_effect : bool ,
149- since : DeprecatedSince ,
150- note : Option < Symbol > ,
151- kind : & str ,
152- path : & str ,
153- ) -> String {
154- let message = if is_in_effect {
155- format ! ( "use of deprecated {kind} `{path}`" )
131+ #[ derive( Subdiagnostic ) ]
132+ #[ suggestion(
133+ middle_deprecated_suggestion,
134+ code = "{suggestion}" ,
135+ style = "verbose" ,
136+ applicability = "machine-applicable"
137+ ) ]
138+ pub struct DeprecationSuggestion {
139+ #[ primary_span]
140+ pub span : Span ,
141+
142+ pub suggestion : Symbol ,
143+ }
144+
145+ pub struct Deprecated {
146+ pub sub : Option < DeprecationSuggestion > ,
147+
148+ // FIXME: make this translatable
149+ pub kind : String ,
150+ pub path : String ,
151+ pub note : Option < Symbol > ,
152+ pub since_kind : DeprecatedSinceKind ,
153+ }
154+
155+ impl < ' a , G : EmissionGuarantee > rustc_errors:: LintDiagnostic < ' a , G > for Deprecated {
156+ fn decorate_lint < ' b > ( self , diag : & ' b mut Diag < ' a , G > ) {
157+ diag. arg ( "kind" , self . kind ) ;
158+ diag. arg ( "path" , self . path ) ;
159+ if let DeprecatedSinceKind :: InVersion ( version) = self . since_kind {
160+ diag. arg ( "version" , version) ;
161+ }
162+ if let Some ( note) = self . note {
163+ diag. arg ( "has_note" , true ) ;
164+ diag. arg ( "note" , note) ;
165+ } else {
166+ diag. arg ( "has_note" , false ) ;
167+ }
168+ if let Some ( sub) = self . sub {
169+ diag. subdiagnostic ( diag. dcx , sub) ;
170+ }
171+ }
172+
173+ fn msg ( & self ) -> rustc_errors:: DiagMessage {
174+ match self . since_kind {
175+ DeprecatedSinceKind :: InEffect => crate :: fluent_generated:: middle_deprecated,
176+ DeprecatedSinceKind :: InFuture => crate :: fluent_generated:: middle_deprecated_in_future,
177+ DeprecatedSinceKind :: InVersion ( _) => {
178+ crate :: fluent_generated:: middle_deprecated_in_version
179+ }
180+ }
181+ }
182+ }
183+
184+ fn deprecated_since_kind ( is_in_effect : bool , since : DeprecatedSince ) -> DeprecatedSinceKind {
185+ if is_in_effect {
186+ DeprecatedSinceKind :: InEffect
156187 } else {
157188 match since {
158- DeprecatedSince :: RustcVersion ( version) => format ! (
159- "use of {kind} `{path}` that will be deprecated in future version {version}"
160- ) ,
161- DeprecatedSince :: Future => {
162- format ! ( "use of {kind} `{path}` that will be deprecated in a future Rust version" )
189+ DeprecatedSince :: RustcVersion ( version) => {
190+ DeprecatedSinceKind :: InVersion ( version. to_string ( ) )
163191 }
192+ DeprecatedSince :: Future => DeprecatedSinceKind :: InFuture ,
164193 DeprecatedSince :: NonStandard ( _)
165194 | DeprecatedSince :: Unspecified
166195 | DeprecatedSince :: Err => {
167196 unreachable ! ( "this deprecation is always in effect; {since:?}" )
168197 }
169198 }
170- } ;
171-
172- match note {
173- Some ( reason) => format ! ( "{message}: {reason}" ) ,
174- None => message,
175199 }
176200}
177201
178- pub fn deprecation_message_and_lint (
179- depr : & Deprecation ,
180- kind : & str ,
181- path : & str ,
182- ) -> ( String , & ' static Lint ) {
183- let is_in_effect = depr. is_in_effect ( ) ;
184- (
185- deprecation_message ( is_in_effect, depr. since , depr. note , kind, path) ,
186- deprecation_lint ( is_in_effect) ,
187- )
188- }
189-
190- pub fn early_report_deprecation (
202+ pub fn early_report_macro_deprecation (
191203 lint_buffer : & mut LintBuffer ,
192- message : String ,
193- suggestion : Option < Symbol > ,
194- lint : & ' static Lint ,
204+ depr : & Deprecation ,
195205 span : Span ,
196206 node_id : NodeId ,
207+ path : String ,
197208) {
198209 if span. in_derive_expansion ( ) {
199210 return ;
200211 }
201212
202- let diag = BuiltinLintDiag :: DeprecatedMacro { suggestion, span, message } ;
203- lint_buffer. buffer_lint_with_diagnostic ( lint, node_id, span, diag) ;
213+ let is_in_effect = depr. is_in_effect ( ) ;
214+ let diag = BuiltinLintDiag :: DeprecatedMacro {
215+ suggestion : depr. suggestion ,
216+ suggestion_span : span,
217+ note : depr. note ,
218+ path,
219+ since_kind : deprecated_since_kind ( is_in_effect, depr. since . clone ( ) ) ,
220+ } ;
221+ lint_buffer. buffer_lint_with_diagnostic ( deprecation_lint ( is_in_effect) , node_id, span, diag) ;
204222}
205223
206224fn late_report_deprecation (
207225 tcx : TyCtxt < ' _ > ,
208- message : String ,
209- suggestion : Option < Symbol > ,
210- lint : & ' static Lint ,
226+ depr : & Deprecation ,
211227 span : Span ,
212228 method_span : Option < Span > ,
213229 hir_id : HirId ,
@@ -216,13 +232,22 @@ fn late_report_deprecation(
216232 if span. in_derive_expansion ( ) {
217233 return ;
218234 }
235+
236+ let def_path = with_no_trimmed_paths ! ( tcx. def_path_str( def_id) ) ;
237+ let def_kind = tcx. def_descr ( def_id) ;
238+ let is_in_effect = depr. is_in_effect ( ) ;
239+
219240 let method_span = method_span. unwrap_or ( span) ;
220- tcx. node_span_lint ( lint, hir_id, method_span, message, |diag| {
221- if let hir:: Node :: Expr ( _) = tcx. hir_node ( hir_id) {
222- let kind = tcx. def_descr ( def_id) ;
223- deprecation_suggestion ( diag, kind, suggestion, method_span) ;
224- }
225- } ) ;
241+ let suggestion =
242+ if let hir:: Node :: Expr ( _) = tcx. hir_node ( hir_id) { depr. suggestion } else { None } ;
243+ let diag = Deprecated {
244+ sub : suggestion. map ( |suggestion| DeprecationSuggestion { span : method_span, suggestion } ) ,
245+ kind : def_kind. to_owned ( ) ,
246+ path : def_path,
247+ note : depr. note ,
248+ since_kind : deprecated_since_kind ( is_in_effect, depr. since ) ,
249+ } ;
250+ tcx. emit_node_span_lint ( deprecation_lint ( is_in_effect) , hir_id, method_span, diag) ;
226251}
227252
228253/// Result of `TyCtxt::eval_stability`.
@@ -351,28 +376,9 @@ impl<'tcx> TyCtxt<'tcx> {
351376 // Calculating message for lint involves calling `self.def_path_str`.
352377 // Which by default to calculate visible path will invoke expensive `visible_parent_map` query.
353378 // So we skip message calculation altogether, if lint is allowed.
354- let is_in_effect = depr_attr. is_in_effect ( ) ;
355- let lint = deprecation_lint ( is_in_effect) ;
379+ let lint = deprecation_lint ( depr_attr. is_in_effect ( ) ) ;
356380 if self . lint_level_at_node ( lint, id) . 0 != Level :: Allow {
357- let def_path = with_no_trimmed_paths ! ( self . def_path_str( def_id) ) ;
358- let def_kind = self . def_descr ( def_id) ;
359-
360- late_report_deprecation (
361- self ,
362- deprecation_message (
363- is_in_effect,
364- depr_attr. since ,
365- depr_attr. note ,
366- def_kind,
367- & def_path,
368- ) ,
369- depr_attr. suggestion ,
370- lint,
371- span,
372- method_span,
373- id,
374- def_id,
375- ) ;
381+ late_report_deprecation ( self , depr_attr, span, method_span, id, def_id) ;
376382 }
377383 }
378384 } ;
0 commit comments