@@ -15,19 +15,14 @@ use tracing::debug;
15
15
/// extending `HandlerFlags`, accessed via `self.handler.flags`.
16
16
#[ must_use]
17
17
#[ derive( Clone ) ]
18
- pub struct DiagnosticBuilder < ' a > ( Box < DiagnosticBuilderInner < ' a > > ) ;
19
-
20
- /// This is a large type, and often used as a return value, especially within
21
- /// the frequently-used `PResult` type. In theory, return value optimization
22
- /// (RVO) should avoid unnecessary copying. In practice, it does not (at the
23
- /// time of writing). The split between `DiagnosticBuilder` and
24
- /// `DiagnosticBuilderInner` exists to avoid many `memcpy` calls.
25
- #[ must_use]
26
- #[ derive( Clone ) ]
27
- struct DiagnosticBuilderInner < ' a > {
18
+ pub struct DiagnosticBuilder < ' a > {
28
19
handler : & ' a Handler ,
29
- diagnostic : Diagnostic ,
30
- allow_suggestions : bool ,
20
+
21
+ /// `Diagnostic` is a large type, and `DiagnosticBuilder` is often used as a
22
+ /// return value, especially within the frequently-used `PResult` type.
23
+ /// In theory, return value optimization (RVO) should avoid unnecessary
24
+ /// copying. In practice, it does not (at the time of writing).
25
+ diagnostic : Box < Diagnostic > ,
31
26
}
32
27
33
28
/// In general, the `DiagnosticBuilder` uses deref to allow access to
@@ -60,7 +55,7 @@ macro_rules! forward {
60
55
$( #[ $attrs] ) *
61
56
#[ doc = concat!( "See [`Diagnostic::" , stringify!( $n) , "()`]." ) ]
62
57
pub fn $n( & mut self , $( $name: $ty) ,* ) -> & mut Self {
63
- self . 0 . diagnostic. $n( $( $name) ,* ) ;
58
+ self . diagnostic. $n( $( $name) ,* ) ;
64
59
self
65
60
}
66
61
} ;
@@ -77,7 +72,7 @@ macro_rules! forward {
77
72
$( #[ $attrs] ) *
78
73
#[ doc = concat!( "See [`Diagnostic::" , stringify!( $n) , "()`]." ) ]
79
74
pub fn $n<$( $generic: $bound) ,* >( & mut self , $( $name: $ty) ,* ) -> & mut Self {
80
- self . 0 . diagnostic. $n( $( $name) ,* ) ;
75
+ self . diagnostic. $n( $( $name) ,* ) ;
81
76
self
82
77
}
83
78
} ;
@@ -87,20 +82,20 @@ impl<'a> Deref for DiagnosticBuilder<'a> {
87
82
type Target = Diagnostic ;
88
83
89
84
fn deref ( & self ) -> & Diagnostic {
90
- & self . 0 . diagnostic
85
+ & self . diagnostic
91
86
}
92
87
}
93
88
94
89
impl < ' a > DerefMut for DiagnosticBuilder < ' a > {
95
90
fn deref_mut ( & mut self ) -> & mut Diagnostic {
96
- & mut self . 0 . diagnostic
91
+ & mut self . diagnostic
97
92
}
98
93
}
99
94
100
95
impl < ' a > DiagnosticBuilder < ' a > {
101
96
/// Emit the diagnostic.
102
97
pub fn emit ( & mut self ) {
103
- self . 0 . handler . emit_diagnostic ( & self ) ;
98
+ self . handler . emit_diagnostic ( & self ) ;
104
99
self . cancel ( ) ;
105
100
}
106
101
@@ -130,19 +125,19 @@ impl<'a> DiagnosticBuilder<'a> {
130
125
/// Converts the builder to a `Diagnostic` for later emission,
131
126
/// unless handler has disabled such buffering.
132
127
pub fn into_diagnostic ( mut self ) -> Option < ( Diagnostic , & ' a Handler ) > {
133
- if self . 0 . handler . flags . dont_buffer_diagnostics
134
- || self . 0 . handler . flags . treat_err_as_bug . is_some ( )
128
+ if self . handler . flags . dont_buffer_diagnostics
129
+ || self . handler . flags . treat_err_as_bug . is_some ( )
135
130
{
136
131
self . emit ( ) ;
137
132
return None ;
138
133
}
139
134
140
- let handler = self . 0 . handler ;
135
+ let handler = self . handler ;
141
136
142
137
// We must use `Level::Cancelled` for `dummy` to avoid an ICE about an
143
138
// unused diagnostic.
144
139
let dummy = Diagnostic :: new ( Level :: Cancelled , "" ) ;
145
- let diagnostic = std:: mem:: replace ( & mut self . 0 . diagnostic , dummy) ;
140
+ let diagnostic = std:: mem:: replace ( & mut * self . diagnostic , dummy) ;
146
141
147
142
// Logging here is useful to help track down where in logs an error was
148
143
// actually emitted.
@@ -169,7 +164,7 @@ impl<'a> DiagnosticBuilder<'a> {
169
164
/// locally in whichever way makes the most sense.
170
165
pub fn delay_as_bug ( & mut self ) {
171
166
self . level = Level :: Bug ;
172
- self . 0 . handler . delay_as_bug ( self . 0 . diagnostic . clone ( ) ) ;
167
+ self . handler . delay_as_bug ( ( * self . diagnostic ) . clone ( ) ) ;
173
168
self . cancel ( ) ;
174
169
}
175
170
@@ -186,7 +181,7 @@ impl<'a> DiagnosticBuilder<'a> {
186
181
/// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is
187
182
/// primary.
188
183
pub fn span_label ( & mut self , span : Span , label : impl Into < String > ) -> & mut Self {
189
- self . 0 . diagnostic . span_label ( span, label) ;
184
+ self . diagnostic . span_label ( span, label) ;
190
185
self
191
186
}
192
187
@@ -199,7 +194,7 @@ impl<'a> DiagnosticBuilder<'a> {
199
194
) -> & mut Self {
200
195
let label = label. as_ref ( ) ;
201
196
for span in spans {
202
- self . 0 . diagnostic . span_label ( span, label) ;
197
+ self . diagnostic . span_label ( span, label) ;
203
198
}
204
199
self
205
200
}
@@ -244,164 +239,79 @@ impl<'a> DiagnosticBuilder<'a> {
244
239
) -> & mut Self ) ;
245
240
forward ! ( pub fn set_is_lint( & mut self , ) -> & mut Self ) ;
246
241
247
- /// See [`Diagnostic::multipart_suggestion()`].
248
- pub fn multipart_suggestion (
242
+ forward ! ( pub fn disable_suggestions( & mut self , ) -> & mut Self ) ;
243
+
244
+ forward ! ( pub fn multipart_suggestion(
249
245
& mut self ,
250
246
msg: & str ,
251
247
suggestion: Vec <( Span , String ) >,
252
248
applicability: Applicability ,
253
- ) -> & mut Self {
254
- if !self . 0 . allow_suggestions {
255
- return self ;
256
- }
257
- self . 0 . diagnostic . multipart_suggestion ( msg, suggestion, applicability) ;
258
- self
259
- }
260
-
261
- /// See [`Diagnostic::multipart_suggestion()`].
262
- pub fn multipart_suggestion_verbose (
249
+ ) -> & mut Self ) ;
250
+ forward ! ( pub fn multipart_suggestion_verbose(
263
251
& mut self ,
264
252
msg: & str ,
265
253
suggestion: Vec <( Span , String ) >,
266
254
applicability: Applicability ,
267
- ) -> & mut Self {
268
- if !self . 0 . allow_suggestions {
269
- return self ;
270
- }
271
- self . 0 . diagnostic . multipart_suggestion_verbose ( msg, suggestion, applicability) ;
272
- self
273
- }
274
-
275
- /// See [`Diagnostic::tool_only_multipart_suggestion()`].
276
- pub fn tool_only_multipart_suggestion (
255
+ ) -> & mut Self ) ;
256
+ forward ! ( pub fn tool_only_multipart_suggestion(
277
257
& mut self ,
278
258
msg: & str ,
279
259
suggestion: Vec <( Span , String ) >,
280
260
applicability: Applicability ,
281
- ) -> & mut Self {
282
- if !self . 0 . allow_suggestions {
283
- return self ;
284
- }
285
- self . 0 . diagnostic . tool_only_multipart_suggestion ( msg, suggestion, applicability) ;
286
- self
287
- }
288
-
289
- /// See [`Diagnostic::span_suggestion()`].
290
- pub fn span_suggestion (
261
+ ) -> & mut Self ) ;
262
+ forward ! ( pub fn span_suggestion(
291
263
& mut self ,
292
264
sp: Span ,
293
265
msg: & str ,
294
266
suggestion: String ,
295
267
applicability: Applicability ,
296
- ) -> & mut Self {
297
- if !self . 0 . allow_suggestions {
298
- return self ;
299
- }
300
- self . 0 . diagnostic . span_suggestion ( sp, msg, suggestion, applicability) ;
301
- self
302
- }
303
-
304
- /// See [`Diagnostic::span_suggestions()`].
305
- pub fn span_suggestions (
268
+ ) -> & mut Self ) ;
269
+ forward ! ( pub fn span_suggestions(
306
270
& mut self ,
307
271
sp: Span ,
308
272
msg: & str ,
309
273
suggestions: impl Iterator <Item = String >,
310
274
applicability: Applicability ,
311
- ) -> & mut Self {
312
- if !self . 0 . allow_suggestions {
313
- return self ;
314
- }
315
- self . 0 . diagnostic . span_suggestions ( sp, msg, suggestions, applicability) ;
316
- self
317
- }
318
-
319
- /// See [`Diagnostic::multipart_suggestions()`].
320
- pub fn multipart_suggestions (
275
+ ) -> & mut Self ) ;
276
+ forward ! ( pub fn multipart_suggestions(
321
277
& mut self ,
322
278
msg: & str ,
323
279
suggestions: impl Iterator <Item = Vec <( Span , String ) >>,
324
280
applicability: Applicability ,
325
- ) -> & mut Self {
326
- if !self . 0 . allow_suggestions {
327
- return self ;
328
- }
329
- self . 0 . diagnostic . multipart_suggestions ( msg, suggestions, applicability) ;
330
- self
331
- }
332
-
333
- /// See [`Diagnostic::span_suggestion_short()`].
334
- pub fn span_suggestion_short (
281
+ ) -> & mut Self ) ;
282
+ forward ! ( pub fn span_suggestion_short(
335
283
& mut self ,
336
284
sp: Span ,
337
285
msg: & str ,
338
286
suggestion: String ,
339
287
applicability: Applicability ,
340
- ) -> & mut Self {
341
- if !self . 0 . allow_suggestions {
342
- return self ;
343
- }
344
- self . 0 . diagnostic . span_suggestion_short ( sp, msg, suggestion, applicability) ;
345
- self
346
- }
347
-
348
- /// See [`Diagnostic::span_suggestion_verbose()`].
349
- pub fn span_suggestion_verbose (
288
+ ) -> & mut Self ) ;
289
+ forward ! ( pub fn span_suggestion_verbose(
350
290
& mut self ,
351
291
sp: Span ,
352
292
msg: & str ,
353
293
suggestion: String ,
354
294
applicability: Applicability ,
355
- ) -> & mut Self {
356
- if !self . 0 . allow_suggestions {
357
- return self ;
358
- }
359
- self . 0 . diagnostic . span_suggestion_verbose ( sp, msg, suggestion, applicability) ;
360
- self
361
- }
362
-
363
- /// See [`Diagnostic::span_suggestion_hidden()`].
364
- pub fn span_suggestion_hidden (
295
+ ) -> & mut Self ) ;
296
+ forward ! ( pub fn span_suggestion_hidden(
365
297
& mut self ,
366
298
sp: Span ,
367
299
msg: & str ,
368
300
suggestion: String ,
369
301
applicability: Applicability ,
370
- ) -> & mut Self {
371
- if !self . 0 . allow_suggestions {
372
- return self ;
373
- }
374
- self . 0 . diagnostic . span_suggestion_hidden ( sp, msg, suggestion, applicability) ;
375
- self
376
- }
377
-
378
- /// See [`Diagnostic::tool_only_span_suggestion()`] for more information.
379
- pub fn tool_only_span_suggestion (
302
+ ) -> & mut Self ) ;
303
+ forward ! ( pub fn tool_only_span_suggestion(
380
304
& mut self ,
381
305
sp: Span ,
382
306
msg: & str ,
383
307
suggestion: String ,
384
308
applicability: Applicability ,
385
- ) -> & mut Self {
386
- if !self . 0 . allow_suggestions {
387
- return self ;
388
- }
389
- self . 0 . diagnostic . tool_only_span_suggestion ( sp, msg, suggestion, applicability) ;
390
- self
391
- }
309
+ ) -> & mut Self ) ;
392
310
393
311
forward ! ( pub fn set_primary_message<M : Into <String >>( & mut self , msg: M ) -> & mut Self ) ;
394
312
forward ! ( pub fn set_span<S : Into <MultiSpan >>( & mut self , sp: S ) -> & mut Self ) ;
395
313
forward ! ( pub fn code( & mut self , s: DiagnosticId ) -> & mut Self ) ;
396
314
397
- /// Allow attaching suggestions this diagnostic.
398
- /// If this is set to `false`, then any suggestions attached with the `span_suggestion_*`
399
- /// methods after this is set to `false` will be ignored.
400
- pub fn allow_suggestions ( & mut self , allow : bool ) -> & mut Self {
401
- self . 0 . allow_suggestions = allow;
402
- self
403
- }
404
-
405
315
/// Convenience function for internal use, clients should use one of the
406
316
/// `struct_*` methods on [`Handler`].
407
317
crate fn new ( handler : & ' a Handler , level : Level , message : & str ) -> DiagnosticBuilder < ' a > {
@@ -424,17 +334,13 @@ impl<'a> DiagnosticBuilder<'a> {
424
334
/// diagnostic.
425
335
crate fn new_diagnostic ( handler : & ' a Handler , diagnostic : Diagnostic ) -> DiagnosticBuilder < ' a > {
426
336
debug ! ( "Created new diagnostic" ) ;
427
- DiagnosticBuilder ( Box :: new ( DiagnosticBuilderInner {
428
- handler,
429
- diagnostic,
430
- allow_suggestions : true ,
431
- } ) )
337
+ DiagnosticBuilder { handler, diagnostic : Box :: new ( diagnostic) }
432
338
}
433
339
}
434
340
435
341
impl < ' a > Debug for DiagnosticBuilder < ' a > {
436
342
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
437
- self . 0 . diagnostic . fmt ( f)
343
+ self . diagnostic . fmt ( f)
438
344
}
439
345
}
440
346
@@ -444,7 +350,7 @@ impl<'a> Drop for DiagnosticBuilder<'a> {
444
350
fn drop ( & mut self ) {
445
351
if !panicking ( ) && !self . cancelled ( ) {
446
352
let mut db = DiagnosticBuilder :: new (
447
- self . 0 . handler ,
353
+ self . handler ,
448
354
Level :: Bug ,
449
355
"the following error was constructed but not emitted" ,
450
356
) ;
0 commit comments