Skip to content

[wit-parser] Migrate to structured errors in the AST/package parser#2465

Open
PhoebeSzmucer wants to merge 6 commits intobytecodealliance:mainfrom
PhoebeSzmucer:structured-parsing-errors
Open

[wit-parser] Migrate to structured errors in the AST/package parser#2465
PhoebeSzmucer wants to merge 6 commits intobytecodealliance:mainfrom
PhoebeSzmucer:structured-parsing-errors

Conversation

@PhoebeSzmucer
Copy link
Contributor

This PR replaces anyhow error handling in the AST/parser laye rwith structured PackageParseErrors and PackageParseErrorKind types. Errors now carry span information directly, enabling better error diagnostics and better integration with downstream tools that use wit-parser programatically.

Part of a larger change: #2460 (see that link for API discussion).

}

#[derive(Debug)]
pub enum Error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thsi went away now in favor of just using PackageParseError


#[non_exhaustive]
#[derive(Debug, PartialEq, Eq)]
pub enum PackageParseErrorKind {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note - I named it "package parse", because in the future we might want to expose a dedicated error for the "file parse" stage (a single WIT package can consist of many files).

Parsing is also usually thought of as converting a single file into an AST, so I think mentioning that this is about parsing a whole package makes things a bit clearer.

///
/// On failure returns `Err((self, e))` so the caller can use the source
/// map for error formatting if needed.
pub fn parse(self) -> Result<UnresolvedPackageGroup, (Self, PackageParseErrors)> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that we're now returning the source map as well. Otherwise the consumers would need to clone the source map if they want to use it for resolving spans (since parse consumes self).

/// Runs `f` and, on error, attempts to add source highlighting to resolver
/// error types that still use `anyhow`. Only needed until the resolver is
/// migrated to structured errors.
pub(crate) fn rewrite_error<F, T>(&self, f: F) -> anyhow::Result<T>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will go away once the resolver is also migrated to structured errors.

@@ -1,4 +1,4 @@
name `nonexistent` is not defined
name `nonexistent` does not exist
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously we were doing a mix of "does not exist" and "is not defined". I'm going to standarize on "does not exist".

Once the structured error migration is done we might want to audit all error messages and see if anything could be better (now that we can track rich metadata easily).

@PhoebeSzmucer PhoebeSzmucer marked this pull request as ready for review March 13, 2026 10:36
@PhoebeSzmucer PhoebeSzmucer requested a review from a team as a code owner March 13, 2026 10:36
@PhoebeSzmucer PhoebeSzmucer requested review from alexcrichton and removed request for a team March 13, 2026 10:36
}

#[derive(Debug, PartialEq, Eq)]
pub struct PackageParseErrors(Box<PackageParseErrorKind>);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to change this to a struct with #[repr(transparent)] if that's better (is there a practical difference between them?)

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.

1 participant