web-infra-dev / rspack

The fast Rust-based web bundler with webpack-compatible API 🦀️
https://rspack.dev
MIT License
10.23k stars 579 forks source link

`rspack_error::Diagnostic` should not be a struct. #5265

Closed bvanjoi closed 10 months ago

bvanjoi commented 11 months ago

What problem does this feature solve?

Diagnostic should not contain concrete fields and it should be a trait that guides a series of actions.

What does the proposed API of configuration look like?

The error handler in rspack_error:

// rspack_error

struct DiagnosticItem {
     span: Span,
     level: Level,
     msg: String,
     note: String,
     // ....
}

struct DiagnosticHandlerItem {
      items: Vec<DiagnosticItem>
}

pub prco_macro Diagnostic; 

Different stages have their own unique errors, for example:

// in parse:
#[derive(Diagnostic)]
#[msg = "should meet comma"]
struct ShouldMeetComma {
   span: Span
}

if self.next_token() != Token::Comma {
   context.sess.emit_error(ShouldMeetComma {span});
}

// Alternatively, we might be able to collect the errors and emit them at the end:
// in code splitting:
struct CodeSplitting {
   // ....
   errors: Vec<dyn Diagnostic>)
}

// at the ended of code splitting:
if !errors.is_empty() {
   contxt.sess.error_hanlde.push(errors.map(Into))
}
h-a-n-a commented 11 months ago

miette::Diagnostic maybe the case for fitting your need ;-).

Boshen commented 10 months ago

It's unfortunate that we called it Diagnostic as well, but if you need to store multiple errors, use miette::Diagnostic instead.

errors: Vec<dyn miette::Diagnostic>)