tamer: diagnose::report::Report: {Mutable=>immutable} self reference
VisualReporter now uses interior mutability so that we can hold multiple references to it for upcoming lowering pipeline changes. DEV-13158main
parent
a6e72b87f7
commit
733f44a616
|
@ -102,7 +102,7 @@ fn copy_xml_to<'e, W: io::Write + 'e>(
|
|||
fn compile<R: Reporter>(
|
||||
src_path: &String,
|
||||
dest_path: &String,
|
||||
reporter: &mut R,
|
||||
reporter: &R,
|
||||
) -> Result<(), TamecError> {
|
||||
let dest = Path::new(&dest_path);
|
||||
let fout = BufWriter::new(fs::File::create(dest)?);
|
||||
|
@ -161,9 +161,9 @@ pub fn main() -> Result<(), TamecError> {
|
|||
|
||||
match parse_options(opts, args) {
|
||||
Ok(Command::Compile(src_path, _, dest_path)) => {
|
||||
let mut reporter = VisualReporter::new(FsSpanResolver);
|
||||
let reporter = VisualReporter::new(FsSpanResolver);
|
||||
|
||||
compile(&src_path, &dest_path, &mut reporter).or_else(
|
||||
compile(&src_path, &dest_path, &reporter).or_else(
|
||||
|e: TamecError| {
|
||||
// POC: Rendering to a string ensures buffering so that we don't
|
||||
// interleave output between processes.
|
||||
|
|
|
@ -59,7 +59,7 @@ pub fn main() -> Result<(), TameldError> {
|
|||
let usage =
|
||||
opts.usage(&format!("Usage: {} [OPTIONS] -o OUTPUT FILE", program));
|
||||
|
||||
let mut reporter = VisualReporter::new(FsSpanResolver);
|
||||
let reporter = VisualReporter::new(FsSpanResolver);
|
||||
|
||||
match parse_options(opts, args) {
|
||||
Ok(Command::Link(input, output, emit)) => match emit {
|
||||
|
|
|
@ -123,7 +123,7 @@ macro_rules! diagnostic_panic {
|
|||
($desc_data:expr, $($panic_args:tt)*) => {{
|
||||
use crate::diagnose::Reporter;
|
||||
|
||||
let mut reporter = crate::diagnose::panic::PanicReporter::new(
|
||||
let reporter = crate::diagnose::panic::PanicReporter::new(
|
||||
Default::default()
|
||||
);
|
||||
|
||||
|
|
|
@ -39,6 +39,7 @@ use super::{
|
|||
};
|
||||
use crate::span::{Context, Span, UNKNOWN_SPAN};
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
fmt::{self, Display, Write},
|
||||
num::NonZeroU32,
|
||||
ops::Add,
|
||||
|
@ -72,8 +73,7 @@ pub trait Reporter {
|
|||
/// ensuring both that the user is made aware of the problem
|
||||
/// and that we're not inadvertently suppressing the actual
|
||||
/// diagnostic messages that were requested.
|
||||
fn render<'d, D: Diagnostic>(&mut self, diagnostic: &'d D)
|
||||
-> Report<'d, D>;
|
||||
fn render<'d, D: Diagnostic>(&self, diagnostic: &'d D) -> Report<'d, D>;
|
||||
}
|
||||
|
||||
/// Render diagnostic report in a highly visual way.
|
||||
|
@ -86,22 +86,32 @@ pub trait Reporter {
|
|||
/// understanding why the error occurred and how to approach resolving
|
||||
/// it.
|
||||
pub struct VisualReporter<R: SpanResolver> {
|
||||
resolver: R,
|
||||
/// Span resolver.
|
||||
///
|
||||
/// This is responsible for resolving a span to a filename with line and
|
||||
/// column numbers.
|
||||
///
|
||||
/// This uses interior mutability since it is in our best interest to
|
||||
/// permit multiple references to a single resolver---the
|
||||
/// shared resolver caching is far more important than saving on a
|
||||
/// cell lock check.
|
||||
resolver: RefCell<R>,
|
||||
}
|
||||
|
||||
impl<R: SpanResolver> VisualReporter<R> {
|
||||
pub fn new(resolver: R) -> Self {
|
||||
Self { resolver }
|
||||
Self {
|
||||
resolver: RefCell::new(resolver),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: SpanResolver> Reporter for VisualReporter<R> {
|
||||
fn render<'d, D: Diagnostic>(
|
||||
&mut self,
|
||||
diagnostic: &'d D,
|
||||
) -> Report<'d, D> {
|
||||
let mspans =
|
||||
describe_resolved(|span| self.resolver.resolve(span), diagnostic);
|
||||
fn render<'d, D: Diagnostic>(&self, diagnostic: &'d D) -> Report<'d, D> {
|
||||
let mspans = describe_resolved(
|
||||
|span| self.resolver.borrow_mut().resolve(span),
|
||||
diagnostic,
|
||||
);
|
||||
|
||||
let mut report = Report::empty(Message(diagnostic));
|
||||
report.extend(mspans.map(Into::into));
|
||||
|
|
|
@ -140,7 +140,7 @@ macro_rules! assert_report {
|
|||
BufSpanResolver::new(Cursor::new(FILE_MANY_LINES), ctx_mny_lns),
|
||||
);
|
||||
|
||||
let mut sut = VisualReporter::new(resolver);
|
||||
let sut = VisualReporter::new(resolver);
|
||||
|
||||
assert_eq!(
|
||||
sut.render(&StubError($msg.into(), $aspans)).to_string(),
|
||||
|
|
Loading…
Reference in New Issue