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

Fix testing for errors during fuzzing #1814

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/wit-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub use ast::{parse_use_path, ParsedUsePath};
mod sizealign;
pub use sizealign::*;
mod resolve;
pub use resolve::{Package, PackageId, Remap, Resolve};
pub use resolve::{InvalidTransitiveDependency, Package, PackageId, Remap, Resolve};
mod live;
pub use live::{LiveTypes, TypeIdVisitor};

Expand Down
32 changes: 30 additions & 2 deletions crates/wit-parser/src/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt;
use std::mem;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -1888,8 +1889,7 @@ package {name} is defined in two different locations:\n\
// more refactoring, so it's left to a future date in the
// hopes that most folks won't actually run into this for
// the time being.
"interface `{name}` transitively depends on an interface in \
incompatible ways",
InvalidTransitiveDependency(name),
);
}
}
Expand Down Expand Up @@ -3218,6 +3218,34 @@ fn update_stability(from: &Stability, into: &mut Stability) -> Result<()> {
bail!("mismatch in stability attributes")
}

/// An error that can be returned during "world elaboration" during various
/// [`Resolve`] operations.
///
/// Methods on [`Resolve`] which mutate its internals, such as
/// [`Resolve::push_dir`] or [`Resolve::importize`] can fail if `world` imports
/// in WIT packages are invalid. This error indicates one of these situations
/// where an invalid dependency graph between imports and exports are detected.
///
/// Note that at this time this error is subtle and not easy to understand, and
/// work needs to be done to explain this better and additionally provide a
/// better error message. For now though this type enables callers to test for
/// the exact kind of error emitted.
#[derive(Debug, Clone)]
pub struct InvalidTransitiveDependency(String);

impl fmt::Display for InvalidTransitiveDependency {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"interface `{}` transitively depends on an interface in \
incompatible ways",
self.0
)
}
}

impl std::error::Error for InvalidTransitiveDependency {}

#[cfg(test)]
mod tests {
use crate::Resolve;
Expand Down
7 changes: 2 additions & 5 deletions crates/wit-smith/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! type structures.

use arbitrary::{Result, Unstructured};
use wit_parser::Resolve;
use wit_parser::{InvalidTransitiveDependency, Resolve};

mod config;
pub use self::config::Config;
Expand All @@ -25,10 +25,7 @@ pub fn smith(config: &Config, u: &mut Unstructured<'_>) -> Result<Vec<u8>> {
let id = match resolve.push_group(group) {
Ok(id) => id,
Err(e) => {
if e.to_string().contains(
"interface transitively depends on an interface in \
incompatible ways",
) {
if e.is::<InvalidTransitiveDependency>() {
return Err(arbitrary::Error::IncorrectFormat);
}
panic!("bad wit parse: {e:?}")
Expand Down