@@ -15,20 +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
- // FIXME(eddyb) try having two pointers in `DiagnosticBuilder`, by only boxing
26
- // `Diagnostic` (i.e. `struct DiagnosticBuilder(&Handler, Box<Diagnostic>);`).
27
- #[ must_use]
28
- #[ derive( Clone ) ]
29
- struct DiagnosticBuilderInner < ' a > {
18
+ pub struct DiagnosticBuilder < ' a > {
30
19
handler : & ' a Handler ,
31
- diagnostic : Diagnostic ,
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 > ,
32
26
}
33
27
34
28
/// In general, the `DiagnosticBuilder` uses deref to allow access to
@@ -61,7 +55,7 @@ macro_rules! forward {
61
55
$( #[ $attrs] ) *
62
56
#[ doc = concat!( "See [`Diagnostic::" , stringify!( $n) , "()`]." ) ]
63
57
pub fn $n( & mut self , $( $name: $ty) ,* ) -> & mut Self {
64
- self . 0 . diagnostic. $n( $( $name) ,* ) ;
58
+ self . diagnostic. $n( $( $name) ,* ) ;
65
59
self
66
60
}
67
61
} ;
@@ -78,7 +72,7 @@ macro_rules! forward {
78
72
$( #[ $attrs] ) *
79
73
#[ doc = concat!( "See [`Diagnostic::" , stringify!( $n) , "()`]." ) ]
80
74
pub fn $n<$( $generic: $bound) ,* >( & mut self , $( $name: $ty) ,* ) -> & mut Self {
81
- self . 0 . diagnostic. $n( $( $name) ,* ) ;
75
+ self . diagnostic. $n( $( $name) ,* ) ;
82
76
self
83
77
}
84
78
} ;
@@ -88,20 +82,20 @@ impl<'a> Deref for DiagnosticBuilder<'a> {
88
82
type Target = Diagnostic ;
89
83
90
84
fn deref ( & self ) -> & Diagnostic {
91
- & self . 0 . diagnostic
85
+ & self . diagnostic
92
86
}
93
87
}
94
88
95
89
impl < ' a > DerefMut for DiagnosticBuilder < ' a > {
96
90
fn deref_mut ( & mut self ) -> & mut Diagnostic {
97
- & mut self . 0 . diagnostic
91
+ & mut self . diagnostic
98
92
}
99
93
}
100
94
101
95
impl < ' a > DiagnosticBuilder < ' a > {
102
96
/// Emit the diagnostic.
103
97
pub fn emit ( & mut self ) {
104
- self . 0 . handler . emit_diagnostic ( & self ) ;
98
+ self . handler . emit_diagnostic ( & self ) ;
105
99
self . cancel ( ) ;
106
100
}
107
101
@@ -131,19 +125,19 @@ impl<'a> DiagnosticBuilder<'a> {
131
125
/// Converts the builder to a `Diagnostic` for later emission,
132
126
/// unless handler has disabled such buffering.
133
127
pub fn into_diagnostic ( mut self ) -> Option < ( Diagnostic , & ' a Handler ) > {
134
- if self . 0 . handler . flags . dont_buffer_diagnostics
135
- || 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 ( )
136
130
{
137
131
self . emit ( ) ;
138
132
return None ;
139
133
}
140
134
141
- let handler = self . 0 . handler ;
135
+ let handler = self . handler ;
142
136
143
137
// We must use `Level::Cancelled` for `dummy` to avoid an ICE about an
144
138
// unused diagnostic.
145
139
let dummy = Diagnostic :: new ( Level :: Cancelled , "" ) ;
146
- let diagnostic = std:: mem:: replace ( & mut self . 0 . diagnostic , dummy) ;
140
+ let diagnostic = std:: mem:: replace ( & mut * self . diagnostic , dummy) ;
147
141
148
142
// Logging here is useful to help track down where in logs an error was
149
143
// actually emitted.
@@ -170,7 +164,7 @@ impl<'a> DiagnosticBuilder<'a> {
170
164
/// locally in whichever way makes the most sense.
171
165
pub fn delay_as_bug ( & mut self ) {
172
166
self . level = Level :: Bug ;
173
- self . 0 . handler . delay_as_bug ( self . 0 . diagnostic . clone ( ) ) ;
167
+ self . handler . delay_as_bug ( ( * self . diagnostic ) . clone ( ) ) ;
174
168
self . cancel ( ) ;
175
169
}
176
170
@@ -187,7 +181,7 @@ impl<'a> DiagnosticBuilder<'a> {
187
181
/// ["primary span"][`MultiSpan`]; only the `Span` supplied when creating the diagnostic is
188
182
/// primary.
189
183
pub fn span_label ( & mut self , span : Span , label : impl Into < String > ) -> & mut Self {
190
- self . 0 . diagnostic . span_label ( span, label) ;
184
+ self . diagnostic . span_label ( span, label) ;
191
185
self
192
186
}
193
187
@@ -200,7 +194,7 @@ impl<'a> DiagnosticBuilder<'a> {
200
194
) -> & mut Self {
201
195
let label = label. as_ref ( ) ;
202
196
for span in spans {
203
- self . 0 . diagnostic . span_label ( span, label) ;
197
+ self . diagnostic . span_label ( span, label) ;
204
198
}
205
199
self
206
200
}
@@ -340,13 +334,13 @@ impl<'a> DiagnosticBuilder<'a> {
340
334
/// diagnostic.
341
335
crate fn new_diagnostic ( handler : & ' a Handler , diagnostic : Diagnostic ) -> DiagnosticBuilder < ' a > {
342
336
debug ! ( "Created new diagnostic" ) ;
343
- DiagnosticBuilder ( Box :: new ( DiagnosticBuilderInner { handler, diagnostic } ) )
337
+ DiagnosticBuilder { handler, diagnostic : Box :: new ( diagnostic ) }
344
338
}
345
339
}
346
340
347
341
impl < ' a > Debug for DiagnosticBuilder < ' a > {
348
342
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
349
- self . 0 . diagnostic . fmt ( f)
343
+ self . diagnostic . fmt ( f)
350
344
}
351
345
}
352
346
@@ -356,7 +350,7 @@ impl<'a> Drop for DiagnosticBuilder<'a> {
356
350
fn drop ( & mut self ) {
357
351
if !panicking ( ) && !self . cancelled ( ) {
358
352
let mut db = DiagnosticBuilder :: new (
359
- self . 0 . handler ,
353
+ self . handler ,
360
354
Level :: Bug ,
361
355
"the following error was constructed but not emitted" ,
362
356
) ;
0 commit comments