Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create api for diagnostics ensure with no need for lowering group #6440

33 changes: 22 additions & 11 deletions crates/cairo-lang-compiler/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub struct DiagnosticsReporter<'a> {
crate_ids: Vec<CrateId>,
/// If true, compilation will not fail due to warnings.
allow_warnings: bool,
/// If the diagnostics from lowering group should be included.
include_lowering_diagnostics: bool,
}

impl DiagnosticsReporter<'static> {
Expand All @@ -54,12 +56,13 @@ impl DiagnosticsReporter<'static> {
crate_ids: vec![],
ignore_warnings_crate_ids: vec![],
allow_warnings: false,
include_lowering_diagnostics: true,
}
}

/// Create a reporter which prints all diagnostics to [`std::io::Stderr`].
pub fn stderr() -> Self {
Self::callback(|diagnostic| eprint!("{diagnostic}"))
Self::callback(|diagnostic| eprint!("{diagnostic}"), true)
}
}

Expand All @@ -68,7 +71,10 @@ impl<'a> DiagnosticsReporter<'a> {
// impl<F> DiagnosticCallback for F where F: FnMut(Severity,String)
// and `new` could accept regular functions without need for this separate method.
/// Create a reporter which calls `callback` for each diagnostic.
pub fn callback(callback: impl FnMut(FormattedDiagnosticEntry) + 'a) -> Self {
pub fn callback(
callback: impl FnMut(FormattedDiagnosticEntry) + 'a,
include_lowering_diagnostics: bool,
) -> Self {
struct Func<F>(F);

impl<F> DiagnosticCallback for Func<F>
Expand All @@ -80,23 +86,27 @@ impl<'a> DiagnosticsReporter<'a> {
}
}

Self::new(Func(callback))
Self::new(Func(callback), include_lowering_diagnostics)
}

/// Create a reporter which appends all diagnostics to provided string.
pub fn write_to_string(string: &'a mut String) -> Self {
Self::callback(|diagnostic| {
write!(string, "{diagnostic}").unwrap();
})
Self::callback(
|diagnostic| {
write!(string, "{diagnostic}").unwrap();
},
true,
)
}

/// Create a reporter which calls [`DiagnosticCallback::on_diagnostic`].
fn new(callback: impl DiagnosticCallback + 'a) -> Self {
fn new(callback: impl DiagnosticCallback + 'a, include_lowering_diagnostics: bool) -> Self {
Self {
callback: Some(Box::new(callback)),
crate_ids: vec![],
ignore_warnings_crate_ids: vec![],
allow_warnings: false,
include_lowering_diagnostics,
}
}

Expand Down Expand Up @@ -178,10 +188,11 @@ impl<'a> DiagnosticsReporter<'a> {
found_diagnostics |=
self.check_diag_group(db.upcast(), group, ignore_warnings_in_crate);
}

if let Ok(group) = db.module_lowering_diagnostics(*module_id) {
found_diagnostics |=
self.check_diag_group(db.upcast(), group, ignore_warnings_in_crate);
if self.include_lowering_diagnostics {
if let Ok(group) = db.module_lowering_diagnostics(*module_id) {
found_diagnostics |=
self.check_diag_group(db.upcast(), group, ignore_warnings_in_crate);
}
}
}
}
Expand Down