Skip to content

Commit 3f413d2

Browse files
committed
sess: add create_{err,warning}
Currently, the only API for creating errors from a diagnostic derive will emit it immediately. This makes it difficult to add subdiagnostics to diagnostics from the derive, so add `create_{err,warning}` functions that return the diagnostic without emitting it. Signed-off-by: David Wood <[email protected]>
1 parent 859079f commit 3f413d2

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

compiler/rustc_session/src/parse.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,26 @@ impl ParseSess {
289289
self.proc_macro_quoted_spans.lock().clone()
290290
}
291291

292+
pub fn create_err<'a>(
293+
&'a self,
294+
err: impl SessionDiagnostic<'a>,
295+
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
296+
err.into_diagnostic(self)
297+
}
298+
292299
pub fn emit_err<'a>(&'a self, err: impl SessionDiagnostic<'a>) -> ErrorGuaranteed {
293-
err.into_diagnostic(self).emit()
300+
self.create_err(err).emit()
301+
}
302+
303+
pub fn create_warning<'a>(
304+
&'a self,
305+
warning: impl SessionDiagnostic<'a, ()>,
306+
) -> DiagnosticBuilder<'a, ()> {
307+
warning.into_diagnostic(self)
294308
}
295309

296310
pub fn emit_warning<'a>(&'a self, warning: impl SessionDiagnostic<'a, ()>) {
297-
warning.into_diagnostic(self).emit()
311+
self.create_warning(warning).emit()
298312
}
299313

300314
pub fn struct_err(

compiler/rustc_session/src/session.rs

+12
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,21 @@ impl Session {
413413
pub fn err(&self, msg: impl Into<DiagnosticMessage>) -> ErrorGuaranteed {
414414
self.diagnostic().err(msg)
415415
}
416+
pub fn create_err<'a>(
417+
&'a self,
418+
err: impl SessionDiagnostic<'a>,
419+
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
420+
self.parse_sess.create_err(err)
421+
}
416422
pub fn emit_err<'a>(&'a self, err: impl SessionDiagnostic<'a>) -> ErrorGuaranteed {
417423
self.parse_sess.emit_err(err)
418424
}
425+
pub fn create_warning<'a>(
426+
&'a self,
427+
err: impl SessionDiagnostic<'a, ()>,
428+
) -> DiagnosticBuilder<'a, ()> {
429+
self.parse_sess.create_warning(err)
430+
}
419431
pub fn emit_warning<'a>(&'a self, warning: impl SessionDiagnostic<'a, ()>) {
420432
self.parse_sess.emit_warning(warning)
421433
}

0 commit comments

Comments
 (0)