icerpc / slicec

The Slice compiler library
Apache License 2.0
13 stars 5 forks source link

Centralize Diagnostic Emission Logic #646

Closed InsertCreativityHere closed 1 year ago

InsertCreativityHere commented 1 year ago

Currently the logic for emitting diagnostics is split between 2 files: "DiagnosticReporter" and "CompilationState". This PR pulls all this logic into its own centralized file: "DiagnosticEmitter", and then renames "DiagnosticReporter" to just "Diagnostics". Also no "CompilationState" isn't as cluttered with this emission code.

I chose this instead of merging them into the existing "DiagnosticReporter" because really, these are 2 different tasks: storing and emitting. We never use both functionalities in the same place.

Now the validators, parsers, etc. only need to deal with Diagnostics. The wrapper is trivial to construct and easy to combine together, unlike DiagnosticReporter. So, now we can use function-chains to report errors in more places, and functions are free to return a Diagnostics instead of needing to take &mut DiagnosticReporter.

DiagnosticEmitter can be created on the fly whenever you want to emit diagnostics (really only at the end of compilation). Simply construct it, then call emit_diagnostics(diagnostics). Clean and simple, and easier to test with!


To the reviewer: this PR isn't as large as it looks. Most of the changes were just renaming diagnostic_reporter to reporter and moving functions between files.