forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

delegate.rs68 lines, 1.6 KiBHistoryRawDownload
1//! This implementation uses [`proc_macro::Diagnostic`], nightly only.
2
3use std::cell::Cell;
4
5use proc_macro::{Diagnostic as PDiag, Level as PLevel};
6
7use crate::{
8 abort_now, check_correctness,
9 diagnostic::{Diagnostic, Level, SuggestionKind},
10};
11
12pub fn abort_if_dirty() {
13 check_correctness();
14 if IS_DIRTY.with(|c| c.get()) {
15 abort_now()
16 }
17}
18
19pub(crate) fn cleanup() -> Vec<Diagnostic> {
20 IS_DIRTY.with(|c| c.set(false));
21 vec![]
22}
23
24pub(crate) fn emit_diagnostic(diag: Diagnostic) {
25 let Diagnostic {
26 level,
27 span_range,
28 msg,
29 suggestions,
30 children,
31 } = diag;
32
33 let span = span_range.collapse().unwrap();
34
35 let level = match level {
36 Level::Warning => PLevel::Warning,
37 Level::Error => {
38 IS_DIRTY.with(|c| c.set(true));
39 PLevel::Error
40 }
41 };
42
43 let mut res = PDiag::spanned(span, level, msg);
44
45 for (kind, msg, span) in suggestions {
46 res = match (kind, span) {
47 (SuggestionKind::Note, Some(span_range)) => {
48 res.span_note(span_range.collapse().unwrap(), msg)
49 }
50 (SuggestionKind::Help, Some(span_range)) => {
51 res.span_help(span_range.collapse().unwrap(), msg)
52 }
53 (SuggestionKind::Note, None) => res.note(msg),
54 (SuggestionKind::Help, None) => res.help(msg),
55 }
56 }
57
58 for (span_range, msg) in children {
59 let span = span_range.collapse().unwrap();
60 res = res.span_error(span, msg);
61 }
62
63 res.emit()
64}
65
66thread_local! {
67 static IS_DIRTY: Cell<bool> = Cell::new(false);
68}