Skip to content

Refactor Diagnostic Creation#770

Merged
InsertCreativityHere merged 9 commits intoicerpc:mainfrom
InsertCreativityHere:refactor-diagnostic-creation
Apr 15, 2026
Merged

Refactor Diagnostic Creation#770
InsertCreativityHere merged 9 commits intoicerpc:mainfrom
InsertCreativityHere:refactor-diagnostic-creation

Conversation

@InsertCreativityHere
Copy link
Copy Markdown
Member

This PR:

  • Removes the implement_diagnostic_functions macro we used to implement functions for diagnostic types. Now we just implement the functions like normal.
  • Adds more typed constructors to create diagnostics, in anticipation of adding a new Info diagnostic kind, which will be very different from the existing Error and Lints.

None of the actual error messages or codes should be changed. Just moved the implementation from a macro to functions.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors how diagnostics are constructed and identified across slicec, replacing the prior macro-based implementation with explicit constructors/methods and updating call sites (including tests) to use the new typed constructors.

Changes:

  • Replaces Diagnostic::new(...) usage with typed constructors Diagnostic::error(...) / Diagnostic::lint(...) throughout validators, parsers, patchers, CLI, and tests.
  • Removes the macro that generated diagnostic code/message functions and implements these as regular methods on Error/Lint.
  • Centralizes allowable lint identifiers into a shared constant for attribute/CLI validation.

Reviewed changes

Copilot reviewed 52 out of 52 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
slicec/src/diagnostics/diagnostic.rs Introduces typed constructors and adjusts code/level behavior.
slicec/src/diagnostics/errors.rs Replaces macro-generated code/message with explicit methods.
slicec/src/diagnostics/lints.rs Replaces macro-generated name/message/default-level with explicit methods.
slicec/src/diagnostics/mod.rs Removes macro and reorganizes imports/exports accordingly.
slicec/src/grammar/attributes/allow.rs Adds shared ALLOWABLE_LINT_IDENTIFIERS constant and updates validation.
slicec/src/slice_options.rs Updates CLI --allow parser to use shared allowable-lints constant.
slicec/src/utils/file_util.rs Updates duplicate-file lint + IO error creation to typed constructors.
slicec/src/main.rs Updates generator-response diagnostics to typed constructors.
slicec/src/validators/type_aliases.rs Updates error construction to Diagnostic::error.
slicec/src/validators/structs.rs Updates error construction to Diagnostic::error.
slicec/src/validators/parameters.rs Updates streamed-member errors to Diagnostic::error.
slicec/src/validators/operations.rs Updates doc-comment lint construction to Diagnostic::lint.
slicec/src/validators/members.rs Updates tag-related errors to Diagnostic::error.
slicec/src/validators/identifiers.rs Updates shadowing/redefinition errors to Diagnostic::error.
slicec/src/validators/enums.rs Updates enum validation errors to Diagnostic::error.
slicec/src/validators/dictionary.rs Updates dictionary key-type errors to Diagnostic::error.
slicec/src/validators/cycle_detection.rs Updates cycle error construction to Diagnostic::error.
slicec/src/validators/comments.rs Updates doc-comment lint construction to Diagnostic::lint.
slicec/src/validators/attribute.rs Updates attribute error construction to Diagnostic::error.
slicec/src/patchers/type_ref_patcher.rs Updates mapped errors and deprecation lints to typed constructors.
slicec/src/patchers/mod.rs Updates unknown-attribute errors to Diagnostic::error.
slicec/src/patchers/comment_link_patcher.rs Updates broken-link lints to Diagnostic::lint.
slicec/src/parsers/mod.rs Minor reordering + updates module-level syntax error construction to Diagnostic::error.
slicec/src/parsers/slice/mod.rs Updates parse error conversion to Diagnostic::error.
slicec/src/parsers/slice/grammar.rs Updates parser-emitted errors to Diagnostic::error.
slicec/src/parsers/preprocessor/mod.rs Updates preprocessor parse error conversion to Diagnostic::error.
slicec/src/parsers/comments/mod.rs Updates comment-parse lints to Diagnostic::lint.
slicec/src/grammar/attributes/mod.rs Updates attribute error construction and imports to match refactor.
slicec/src/grammar/attributes/sliced_format.rs Updates invalid-argument error construction to Diagnostic::error.
slicec/src/grammar/attributes/compress.rs Updates invalid-argument error construction to Diagnostic::error.
slicec/tests/typealias_tests.rs Updates tests to use Diagnostic::error.
slicec/tests/tag_tests.rs Updates tests to use Diagnostic::error.
slicec/tests/structs/tags.rs Updates tests to use Diagnostic::error.
slicec/tests/structs/container.rs Updates tests to use Diagnostic::error.
slicec/tests/sequence_tests.rs Updates tests to use Diagnostic::error.
slicec/tests/scope_resolution_tests.rs Updates tests to use Diagnostic::error.
slicec/tests/redefinition_tests.rs Updates tests to use Diagnostic::error.
slicec/tests/preprocessor_tests.rs Updates tests to use Diagnostic::error.
slicec/tests/parser_tests.rs Updates tests to use Diagnostic::error.
slicec/tests/module_tests.rs Updates tests to use Diagnostic::error.
slicec/tests/interfaces/operations.rs Updates tests to use Diagnostic::error.
slicec/tests/interfaces/mod.rs Updates tests to use Diagnostic::error.
slicec/tests/interfaces/inheritance.rs Updates tests to use Diagnostic::error.
slicec/tests/identifier_tests.rs Updates tests to use Diagnostic::error.
slicec/tests/files/io.rs Updates tests to use Diagnostic::lint.
slicec/tests/enums/mod.rs Updates tests to use Diagnostic::error.
slicec/tests/enums/container.rs Updates tests to use Diagnostic::error.
slicec/tests/dictionaries/value_type.rs Updates tests to use Diagnostic::error.
slicec/tests/dictionaries/key_type.rs Updates tests to use Diagnostic::error.
slicec/tests/cycle_tests.rs Updates tests to use Diagnostic::error.
slicec/tests/comment_tests.rs Updates tests to use Diagnostic::error / Diagnostic::lint.
slicec/tests/attribute_tests.rs Updates tests to use Diagnostic::error / Diagnostic::lint.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread slicec/src/grammar/attributes/allow.rs
Comment thread slicec/src/diagnostics/lints.rs
Comment thread slicec/src/diagnostics/lints.rs
Comment thread slicec/src/diagnostics/lints.rs
Comment thread slicec/src/diagnostics/diagnostic.rs
Comment thread slicec/src/diagnostics/errors.rs

use crate::slice_file::Span;
use serde::Serialize;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conventional ordering for top-level Rust statements is pub mod, mod, pub use, use.
So I moved these use statements to where they should be.


/// A macro that implements the `code` and `message` functions for [Lint] and [Error] enums.
macro_rules! implement_diagnostic_functions {
(Lint, $(($kind:ident, $message:expr $(, $variant:ident)* )),*) => {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the fanciest macro we had in slicec.

use super::*;

/// All the valid arguments that can be supplied to the `allow` metadata, or the `--allow` command line option.
pub const ALLOWABLE_LINT_IDENTIFIERS: [&str; 6] = [
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This used to be generated by the macro, now we just write it by hand.
It does live in a different module though, so had to update it's scoping where we use it.

pub use sliced_format::*;

use super::Attributables;
use crate::diagnostics::{Diagnostic, Diagnostics, Error, Lint};
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint was in scope to see the ALLOWABLE_LINT_IDENTIFIERS. Now that it lives in this module ancestry, no longer needed.

Comment on lines -108 to -118
impl From<Error> for DiagnosticKind {
fn from(error: Error) -> Self {
DiagnosticKind::Error(error)
}
}

impl From<Lint> for DiagnosticKind {
fn from(lint: Lint) -> Self {
DiagnosticKind::Lint(lint)
}
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are no longer needed now that we have dedicated constructors for error and lint, instead of using From/Into casts.

Comment thread slicec/src/parsers/mod.rs
Comment on lines +10 to +13
// We only export the parsers and keep all the other logic private.
pub use self::comments::parser::CommentParser;
pub use self::preprocessor::parser::Preprocessor;
pub use self::slice::parser::Parser;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pub use should come after mod statements.

Comment on lines +75 to +80
Self::MalformedDocComment { message } => format!("malformed doc comment: {message}"),

Self::IncorrectDocComment { message } => format!("incorrect doc comment: {message}"),

Self::BrokenDocLink { message } => format!("broken doc link: {message}"),
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the one place where the messages changed. I added short little prefixes before emitting the messages. Before we just emitted {message} verbatim. No prefix.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 53 out of 53 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread slicec/src/diagnostics/lints.rs
Comment thread slicec/src/diagnostics/errors.rs Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Member

@externl externl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Comment thread slicec/src/diagnostics/diagnostic.rs Outdated

/// Creates a new error `Diagnostic` from the provided [`Error`].
/// The newly created `Diagnostic` has no `span`, `scope`, or `notes` set.
pub fn error(error: Error) -> Self {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these be something like from_error and from_info?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you're right. My current names are not very Rusty...
Fixed!

@InsertCreativityHere InsertCreativityHere merged commit c63d0a5 into icerpc:main Apr 15, 2026
7 checks passed
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

Successfully merging this pull request may close these issues.

3 participants