Skip to content

Commit

Permalink
Start requiring f32/f64 in WIT by default
Browse files Browse the repository at this point in the history
This commit continues the transition started in bytecodealliance#1364 to rename the
`float32 `and `float64` types in WIT to `f32` and `f64`. Now that
support has been present for some time to parse `f32` and `f64` this
commit flips the WIT parser to requiring it by default. The error
message for `float32` and `float64` has additionally been updated to
explain a bit more about this transition.
  • Loading branch information
alexcrichton committed Sep 12, 2024
1 parent ba02e8f commit a0431b9
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crates/wit-component/src/printing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::mem;
use wit_parser::*;

// NB: keep in sync with `crates/wit-parser/src/ast/lex.rs`
const PRINT_F32_F64_DEFAULT: bool = false;
const PRINT_F32_F64_DEFAULT: bool = true;

/// A utility for printing WebAssembly interface definitions to a string.
pub struct WitPrinter {
Expand Down
16 changes: 9 additions & 7 deletions crates/wit-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1772,13 +1772,15 @@ impl SourceMap {
bail!("{msg}")
}

if let Some(sort) = err.downcast_ref::<toposort::Error>() {
let span = match sort {
toposort::Error::NonexistentDep { span, .. }
| toposort::Error::Cycle { span, .. } => *span,
};
let msg = self.highlight_err(span.start, Some(span.end), sort);
bail!("{msg}")
if let Some(sort) = err.downcast_mut::<toposort::Error>() {
if sort.highlighted().is_none() {
let span = match sort {
toposort::Error::NonexistentDep { span, .. }
| toposort::Error::Cycle { span, .. } => *span,
};
let highlighted = self.highlight_err(span.start, Some(span.end), &sort);
sort.set_highlighted(highlighted);
}
}

Err(err)
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-parser/src/ast/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub enum Error {
}

// NB: keep in sync with `crates/wit-component/src/printing.rs`.
const REQUIRE_F32_F64_BY_DEFAULT: bool = false;
const REQUIRE_F32_F64_BY_DEFAULT: bool = true;

impl<'a> Tokenizer<'a> {
pub fn new(
Expand Down
24 changes: 22 additions & 2 deletions crates/wit-parser/src/ast/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ impl<'a> Resolver<'a> {
}
}
}
let order = toposort("type", &type_deps)?;
let order = toposort("type", &type_deps).map_err(attach_old_float_type_context)?;
for ty in order {
let def = match type_defs.swap_remove(&ty).unwrap() {
Some(def) => def,
Expand All @@ -922,7 +922,27 @@ impl<'a> Resolver<'a> {
self.type_spans.push(def.name.span);
self.define_interface_name(&def.name, TypeOrItem::Type(id))?;
}
Ok(())
return Ok(());

fn attach_old_float_type_context(err: ast::toposort::Error) -> anyhow::Error {
let name = match &err {
ast::toposort::Error::NonexistentDep { name, .. } => name,
_ => return err.into(),
};
let new = match name.as_str() {
"float32" => "f32",
"float64" => "f64",
_ => return err.into(),
};

let context = format!(
"the `{name}` type has been renamed to `{new}` and is \
no longer accepted, but the `WIT_REQUIRE_F32_F64=0` \
environment variable can be used to temporarily \
disable this error"
);
anyhow::Error::from(err).context(context)
}
}

fn resolve_use(&mut self, owner: TypeOwner, u: &ast::Use<'a>) -> Result<()> {
Expand Down
24 changes: 24 additions & 0 deletions crates/wit-parser/src/ast/toposort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn toposort<'a>(
span: edge.span,
name: edge.name.to_string(),
kind: kind.to_string(),
highlighted: None,
})?;
states[j].reverse_deps.push(i);
}
Expand Down Expand Up @@ -115,6 +116,7 @@ pub fn toposort<'a>(
span: dep.span,
name: dep.name.to_string(),
kind: kind.to_string(),
highlighted: None,
});
}
}
Expand All @@ -128,16 +130,38 @@ pub enum Error {
span: Span,
name: String,
kind: String,
highlighted: Option<String>,
},
Cycle {
span: Span,
name: String,
kind: String,
highlighted: Option<String>,
},
}

impl Error {
pub(crate) fn highlighted(&self) -> Option<&str> {
match self {
Error::NonexistentDep { highlighted, .. } | Error::Cycle { highlighted, .. } => {
highlighted.as_deref()
}
}
}
pub(crate) fn set_highlighted(&mut self, string: String) {
match self {
Error::NonexistentDep { highlighted, .. } | Error::Cycle { highlighted, .. } => {
*highlighted = Some(string);
}
}
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(s) = self.highlighted() {
return f.write_str(s);
}
match self {
Error::NonexistentDep { kind, name, .. } => {
write!(f, "{kind} `{name}` does not exist")
Expand Down
5 changes: 5 additions & 0 deletions crates/wit-parser/tests/ui/parse-fail/old-float-types.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package foo:bar;

interface a {
type t1 = float32;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
the `float32` type has been renamed to `f32` and is no longer accepted, but the `WIT_REQUIRE_F32_F64=0` environment variable can be used to temporarily disable this error: type `float32` does not exist
--> tests/ui/parse-fail/old-float-types.wit:4:13
|
4 | type t1 = float32;
| ^------

0 comments on commit a0431b9

Please sign in to comment.