Skip to content

Commit be7fa11

Browse files
onbjergDaniPopes
andauthored
feat: InMemoryEmitter (#451)
Adds an `InMemoryEmitter` that pushes diagnostics to a shared buffer. Mapping spans to sources is done outside of the emitter itself. --------- Co-authored-by: DaniPopes <[email protected]>
1 parent 04b8466 commit be7fa11

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use crate::diagnostics::{Diag, Emitter};
2+
use solar_data_structures::sync::RwLock;
3+
use std::sync::Arc;
4+
5+
/// An in-memory diagnostics emitter.
6+
///
7+
/// Diagnostics are pushed to a shared buffer as-is.
8+
///
9+
/// # Warning
10+
///
11+
/// Do **NOT** hold a read lock on the buffer across compiler passes as this will prevent the
12+
/// compiler from pushing diagnostics.
13+
pub struct InMemoryEmitter {
14+
buffer: Arc<RwLock<Vec<Diag>>>,
15+
}
16+
17+
impl InMemoryEmitter {
18+
/// Creates a new emitter, returning the emitter itself and the buffer.
19+
pub fn new() -> (Self, Arc<RwLock<Vec<Diag>>>) {
20+
let buffer = Default::default();
21+
(Self { buffer: Arc::clone(&buffer) }, buffer)
22+
}
23+
}
24+
25+
impl Emitter for InMemoryEmitter {
26+
fn emit_diagnostic(&mut self, diagnostic: &crate::diagnostics::Diag) {
27+
self.buffer.write().push(diagnostic.clone());
28+
}
29+
}

crates/interface/src/diagnostics/emitter/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ mod json;
1010
#[cfg(feature = "json")]
1111
pub use json::JsonEmitter;
1212

13+
mod mem;
14+
pub use mem::InMemoryEmitter;
15+
1316
mod rustc;
1417

1518
/// Dynamic diagnostic emitter. See [`Emitter`].

crates/interface/src/diagnostics/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ mod emitter;
2020
#[cfg(feature = "json")]
2121
pub use emitter::JsonEmitter;
2222
pub use emitter::{
23-
DynEmitter, Emitter, HumanBufferEmitter, HumanEmitter, LocalEmitter, SilentEmitter,
23+
DynEmitter, Emitter, HumanBufferEmitter, HumanEmitter, InMemoryEmitter, LocalEmitter,
24+
SilentEmitter,
2425
};
2526

2627
mod message;

0 commit comments

Comments
 (0)