@@ -52,30 +52,40 @@ impl Directive {
5252 }
5353 }
5454
55- pub fn evaluate_directive (
55+ pub fn eval (
5656 & self ,
57- trait_name : impl Debug ,
58- condition_options : & ConditionOptions ,
57+ condition_options : Option < & ConditionOptions > ,
5958 args : & FormatArgs ,
60- ) -> OnUnimplementedNote {
59+ ) -> CustomDiagnostic {
60+ let this = & args. this ;
61+ info ! ( "eval({self:?}, this={this}, options={condition_options:?}, args ={args:?})" ) ;
62+
63+ let Some ( condition_options) = condition_options else {
64+ debug_assert ! (
65+ !self . is_rustc_attr,
66+ "Directive::eval called for `rustc_on_unimplemented` without `condition_options`"
67+ ) ;
68+ return CustomDiagnostic {
69+ label : self . label . as_ref ( ) . map ( |l| l. 1 . format ( args) ) ,
70+ message : self . message . as_ref ( ) . map ( |m| m. 1 . format ( args) ) ,
71+ notes : self . notes . iter ( ) . map ( |n| n. format ( args) ) . collect ( ) ,
72+ parent_label : None ,
73+ } ;
74+ } ;
6175 let mut message = None ;
6276 let mut label = None ;
6377 let mut notes = Vec :: new ( ) ;
6478 let mut parent_label = None ;
65- info ! (
66- "evaluate_directive({:?}, trait_ref={:?}, options={:?}, args ={:?})" ,
67- self , trait_name, condition_options, args
68- ) ;
6979
7080 for command in self . subcommands . iter ( ) . chain ( Some ( self ) ) . rev ( ) {
7181 debug ! ( ?command) ;
7282 if let Some ( ref condition) = command. condition
7383 && !condition. matches_predicate ( condition_options)
7484 {
75- debug ! ( "evaluate_directive : skipping {:?} due to condition" , command ) ;
85+ debug ! ( "eval : skipping {command :?} due to condition" ) ;
7686 continue ;
7787 }
78- debug ! ( "evaluate_directive : {:?} succeeded" , command ) ;
88+ debug ! ( "eval : {command :?} succeeded" ) ;
7989 if let Some ( ref message_) = command. message {
8090 message = Some ( message_. clone ( ) ) ;
8191 }
@@ -91,7 +101,7 @@ impl Directive {
91101 }
92102 }
93103
94- OnUnimplementedNote {
104+ CustomDiagnostic {
95105 label : label. map ( |l| l. 1 . format ( args) ) ,
96106 message : message. map ( |m| m. 1 . format ( args) ) ,
97107 notes : notes. into_iter ( ) . map ( |n| n. format ( args) ) . collect ( ) ,
@@ -100,8 +110,9 @@ impl Directive {
100110 }
101111}
102112
113+ /// A custom diagnostic, created from a diagnostic attribute.
103114#[ derive( Default , Debug ) ]
104- pub struct OnUnimplementedNote {
115+ pub struct CustomDiagnostic {
105116 pub message : Option < String > ,
106117 pub label : Option < String > ,
107118 pub notes : Vec < String > ,
@@ -116,8 +127,13 @@ pub struct FormatString {
116127 pub span : Span ,
117128 pub pieces : ThinVec < Piece > ,
118129}
130+
119131impl FormatString {
120- pub fn format ( & self , args : & FormatArgs ) -> String {
132+ /// Formats the format string.
133+ ///
134+ /// This is a private method, use `Directive::eval` instead. A diagnostic attribute being used
135+ /// should issue a `tracing` event, which `Directive::eval` does.
136+ fn format ( & self , args : & FormatArgs ) -> String {
121137 let mut ret = String :: new ( ) ;
122138 for piece in & self . pieces {
123139 match piece {
@@ -147,7 +163,7 @@ impl FormatString {
147163 // It's only `rustc_onunimplemented` from here
148164 Piece :: Arg ( FormatArg :: This ) => ret. push_str ( & args. this ) ,
149165 Piece :: Arg ( FormatArg :: Trait ) => {
150- let _ = fmt:: write ( & mut ret, format_args ! ( "{}" , & args. trait_sugared ) ) ;
166+ let _ = fmt:: write ( & mut ret, format_args ! ( "{}" , & args. this_sugared ) ) ;
151167 }
152168 Piece :: Arg ( FormatArg :: ItemContext ) => ret. push_str ( args. item_context ) ,
153169 }
@@ -193,15 +209,15 @@ impl FormatString {
193209/// ```rust,ignore (just an example)
194210/// FormatArgs {
195211/// this: "FromResidual",
196- /// trait_sugared : "FromResidual<Option<Infallible>>",
212+ /// this_sugared : "FromResidual<Option<Infallible>>",
197213/// item_context: "an async function",
198214/// generic_args: [("Self", "u32"), ("R", "Option<Infallible>")],
199215/// }
200216/// ```
201217#[ derive( Debug ) ]
202218pub struct FormatArgs {
203219 pub this : String ,
204- pub trait_sugared : String ,
220+ pub this_sugared : String ,
205221 pub item_context : & ' static str ,
206222 pub generic_args : Vec < ( Symbol , String ) > ,
207223}
0 commit comments