@@ -112,6 +112,22 @@ pub trait EmissionGuarantee: Sized {
112112 fn emit_producing_guarantee ( db : & mut DiagnosticBuilder < ' _ , Self > ) -> Self :: EmitResult ;
113113}
114114
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+
115131impl < ' a > DiagnosticBuilder < ' a , ErrorGuaranteed > {
116132 /// Discard the guarantee `.emit()` would return, in favor of having the
117133 /// type `DiagnosticBuilder<'a, ()>`. This may be necessary whenever there
@@ -124,6 +140,7 @@ impl<'a> DiagnosticBuilder<'a, ErrorGuaranteed> {
124140// FIXME(eddyb) make `ErrorGuaranteed` impossible to create outside `.emit()`.
125141impl EmissionGuarantee for ErrorGuaranteed {
126142 fn emit_producing_guarantee ( db : & mut DiagnosticBuilder < ' _ , Self > ) -> Self :: EmitResult {
143+ // Contrast this with `emit_producing_nothing`.
127144 match db. inner . state {
128145 // First `.emit()` call, the `&DiagCtxt` is still available.
129146 DiagnosticBuilderState :: Emittable ( dcx) => {
@@ -165,16 +182,7 @@ impl EmissionGuarantee for ErrorGuaranteed {
165182// FIXME(eddyb) should there be a `Option<ErrorGuaranteed>` impl as well?
166183impl EmissionGuarantee for ( ) {
167184 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 ( ) ;
178186 }
179187}
180188
@@ -187,17 +195,7 @@ impl EmissionGuarantee for BugAbort {
187195 type EmitResult = !;
188196
189197 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 ( ) ;
201199 panic:: panic_any ( ExplicitBug ) ;
202200 }
203201}
@@ -211,34 +209,14 @@ impl EmissionGuarantee for FatalAbort {
211209 type EmitResult = !;
212210
213211 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 ( ) ;
225213 crate :: FatalError . raise ( )
226214 }
227215}
228216
229217impl EmissionGuarantee for rustc_span:: fatal_error:: FatalError {
230218 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 ( ) ;
242220 rustc_span:: fatal_error:: FatalError
243221 }
244222}
0 commit comments