@@ -112,6 +112,22 @@ pub trait EmissionGuarantee: Sized {
112
112
fn emit_producing_guarantee ( db : & mut DiagnosticBuilder < ' _ , Self > ) -> Self :: EmitResult ;
113
113
}
114
114
115
+ impl < ' a , G : EmissionGuarantee > DiagnosticBuilder < ' a , G > {
116
+ /// Most `emit_producing_guarantee` functions use this as a starting point.
117
+ fn emit_producing_nothing ( & mut self ) {
118
+ match self . inner . state {
119
+ // First `.emit()` call, the `&DiagCtxt` is still available.
120
+ DiagnosticBuilderState :: Emittable ( dcx) => {
121
+ self . inner . state = DiagnosticBuilderState :: AlreadyEmittedOrDuringCancellation ;
122
+
123
+ dcx. emit_diagnostic_without_consuming ( & mut self . inner . diagnostic ) ;
124
+ }
125
+ // `.emit()` was previously called, disallowed from repeating it.
126
+ DiagnosticBuilderState :: AlreadyEmittedOrDuringCancellation => { }
127
+ }
128
+ }
129
+ }
130
+
115
131
impl < ' a > DiagnosticBuilder < ' a , ErrorGuaranteed > {
116
132
/// Discard the guarantee `.emit()` would return, in favor of having the
117
133
/// type `DiagnosticBuilder<'a, ()>`. This may be necessary whenever there
@@ -124,6 +140,7 @@ impl<'a> DiagnosticBuilder<'a, ErrorGuaranteed> {
124
140
// FIXME(eddyb) make `ErrorGuaranteed` impossible to create outside `.emit()`.
125
141
impl EmissionGuarantee for ErrorGuaranteed {
126
142
fn emit_producing_guarantee ( db : & mut DiagnosticBuilder < ' _ , Self > ) -> Self :: EmitResult {
143
+ // Contrast this with `emit_producing_nothing`.
127
144
match db. inner . state {
128
145
// First `.emit()` call, the `&DiagCtxt` is still available.
129
146
DiagnosticBuilderState :: Emittable ( dcx) => {
@@ -165,16 +182,7 @@ impl EmissionGuarantee for ErrorGuaranteed {
165
182
// FIXME(eddyb) should there be a `Option<ErrorGuaranteed>` impl as well?
166
183
impl EmissionGuarantee for ( ) {
167
184
fn emit_producing_guarantee ( db : & mut DiagnosticBuilder < ' _ , Self > ) -> Self :: EmitResult {
168
- match db. inner . state {
169
- // First `.emit()` call, the `&DiagCtxt` is still available.
170
- DiagnosticBuilderState :: Emittable ( dcx) => {
171
- db. inner . state = DiagnosticBuilderState :: AlreadyEmittedOrDuringCancellation ;
172
-
173
- dcx. emit_diagnostic_without_consuming ( & mut db. inner . diagnostic ) ;
174
- }
175
- // `.emit()` was previously called, disallowed from repeating it.
176
- DiagnosticBuilderState :: AlreadyEmittedOrDuringCancellation => { }
177
- }
185
+ db. emit_producing_nothing ( ) ;
178
186
}
179
187
}
180
188
@@ -187,17 +195,7 @@ impl EmissionGuarantee for BugAbort {
187
195
type EmitResult = !;
188
196
189
197
fn emit_producing_guarantee ( db : & mut DiagnosticBuilder < ' _ , Self > ) -> Self :: EmitResult {
190
- match db. inner . state {
191
- // First `.emit()` call, the `&DiagCtxt` is still available.
192
- DiagnosticBuilderState :: Emittable ( dcx) => {
193
- db. inner . state = DiagnosticBuilderState :: AlreadyEmittedOrDuringCancellation ;
194
-
195
- dcx. emit_diagnostic_without_consuming ( & mut db. inner . diagnostic ) ;
196
- }
197
- // `.emit()` was previously called, disallowed from repeating it.
198
- DiagnosticBuilderState :: AlreadyEmittedOrDuringCancellation => { }
199
- }
200
- // Then panic. No need to return the marker type.
198
+ db. emit_producing_nothing ( ) ;
201
199
panic:: panic_any ( ExplicitBug ) ;
202
200
}
203
201
}
@@ -211,34 +209,14 @@ impl EmissionGuarantee for FatalAbort {
211
209
type EmitResult = !;
212
210
213
211
fn emit_producing_guarantee ( db : & mut DiagnosticBuilder < ' _ , Self > ) -> Self :: EmitResult {
214
- match db. inner . state {
215
- // First `.emit()` call, the `&DiagCtxt` is still available.
216
- DiagnosticBuilderState :: Emittable ( dcx) => {
217
- db. inner . state = DiagnosticBuilderState :: AlreadyEmittedOrDuringCancellation ;
218
-
219
- dcx. emit_diagnostic_without_consuming ( & mut db. inner . diagnostic ) ;
220
- }
221
- // `.emit()` was previously called, disallowed from repeating it.
222
- DiagnosticBuilderState :: AlreadyEmittedOrDuringCancellation => { }
223
- }
224
- // Then fatally error, returning `!`
212
+ db. emit_producing_nothing ( ) ;
225
213
crate :: FatalError . raise ( )
226
214
}
227
215
}
228
216
229
217
impl EmissionGuarantee for rustc_span:: fatal_error:: FatalError {
230
218
fn emit_producing_guarantee ( db : & mut DiagnosticBuilder < ' _ , Self > ) -> Self :: EmitResult {
231
- match db. inner . state {
232
- // First `.emit()` call, the `&DiagCtxt` is still available.
233
- DiagnosticBuilderState :: Emittable ( dcx) => {
234
- db. inner . state = DiagnosticBuilderState :: AlreadyEmittedOrDuringCancellation ;
235
-
236
- dcx. emit_diagnostic_without_consuming ( & mut db. inner . diagnostic ) ;
237
- }
238
- // `.emit()` was previously called, disallowed from repeating it.
239
- DiagnosticBuilderState :: AlreadyEmittedOrDuringCancellation => { }
240
- }
241
- // Then fatally error..
219
+ db. emit_producing_nothing ( ) ;
242
220
rustc_span:: fatal_error:: FatalError
243
221
}
244
222
}
0 commit comments