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

Add Stable APIs to emit diagnostic messages #59

Open
celinval opened this issue Jan 4, 2024 · 4 comments
Open

Add Stable APIs to emit diagnostic messages #59

celinval opened this issue Jan 4, 2024 · 4 comments

Comments

@celinval
Copy link
Contributor

celinval commented Jan 4, 2024

Provide a mechanism to emit diagnostic messages using StableMIR's Span. Maybe provide methods to emit error, warning, and note.
One possibility would be to provide an API similar to what is being proposed for proc_macro diagnostics:

// crate stable_mir

pub fn span_error(span: impl Spans, message: impl Message) -> proc_macro::Diagnostic;
pub fn span_warn(span: impl Spans, message: impl Message) -> proc_macro::Diagnostic;
pub fn span_note(span: impl Spans, message: impl Message) -> proc_macro::Diagnostic;

/// This could become an alias to proc_macro::Message once it gets added.
pub trait Message;

pub trait Spans {
    /// Converts `self` into a `Vec<Span>`.
    fn into_spans(self) -> Vec<Span>;
}

impl Message for String;
impl Message for &str;
impl Spans for Span;
impl<I: IntoIterator<Item = Span>> Spans for I;
impl<T> proc_macro::MultiSpan for T where T: Spans;

We should probably also add a function abort_if_errors() to allow tool developers to end some validation stage if any error was detected.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 4, 2024

We could attempt do this entirely on crates.io with just the span information. There are crates that render diagnostics in the rustc style.

I'll build a small demo.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 11, 2024

Here's a very hacky example of how to use annotate_snippets to render a diagnostic: https://github.com/oli-obk/smir_diagnostics

the demo produces the following output:

image

@celinval
Copy link
Contributor Author

That's awesome! I didn't know the annotate-snippets crate before.

@celinval
Copy link
Contributor Author

Maybe we can add methods to retrieve the byte range of a span and the file content, so users can easily integrate with a diagnostic crate such as annotate-snippets or codespan-reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants