Skip to content

Commit de9369c

Browse files
authored
Start requiring f32/f64 in WIT by default (#1780)
This commit continues the transition started in #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.
1 parent ba02e8f commit de9369c

7 files changed

Lines changed: 67 additions & 11 deletions

File tree

crates/wit-component/src/printing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::mem;
55
use wit_parser::*;
66

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

1010
/// A utility for printing WebAssembly interface definitions to a string.
1111
pub struct WitPrinter {

crates/wit-parser/src/ast.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,13 +1772,15 @@ impl SourceMap {
17721772
bail!("{msg}")
17731773
}
17741774

1775-
if let Some(sort) = err.downcast_ref::<toposort::Error>() {
1776-
let span = match sort {
1777-
toposort::Error::NonexistentDep { span, .. }
1778-
| toposort::Error::Cycle { span, .. } => *span,
1779-
};
1780-
let msg = self.highlight_err(span.start, Some(span.end), sort);
1781-
bail!("{msg}")
1775+
if let Some(sort) = err.downcast_mut::<toposort::Error>() {
1776+
if sort.highlighted().is_none() {
1777+
let span = match sort {
1778+
toposort::Error::NonexistentDep { span, .. }
1779+
| toposort::Error::Cycle { span, .. } => *span,
1780+
};
1781+
let highlighted = self.highlight_err(span.start, Some(span.end), &sort);
1782+
sort.set_highlighted(highlighted);
1783+
}
17821784
}
17831785

17841786
Err(err)

crates/wit-parser/src/ast/lex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub enum Error {
116116
}
117117

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

121121
impl<'a> Tokenizer<'a> {
122122
pub fn new(

crates/wit-parser/src/ast/resolve.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ impl<'a> Resolver<'a> {
903903
}
904904
}
905905
}
906-
let order = toposort("type", &type_deps)?;
906+
let order = toposort("type", &type_deps).map_err(attach_old_float_type_context)?;
907907
for ty in order {
908908
let def = match type_defs.swap_remove(&ty).unwrap() {
909909
Some(def) => def,
@@ -922,7 +922,27 @@ impl<'a> Resolver<'a> {
922922
self.type_spans.push(def.name.span);
923923
self.define_interface_name(&def.name, TypeOrItem::Type(id))?;
924924
}
925-
Ok(())
925+
return Ok(());
926+
927+
fn attach_old_float_type_context(err: ast::toposort::Error) -> anyhow::Error {
928+
let name = match &err {
929+
ast::toposort::Error::NonexistentDep { name, .. } => name,
930+
_ => return err.into(),
931+
};
932+
let new = match name.as_str() {
933+
"float32" => "f32",
934+
"float64" => "f64",
935+
_ => return err.into(),
936+
};
937+
938+
let context = format!(
939+
"the `{name}` type has been renamed to `{new}` and is \
940+
no longer accepted, but the `WIT_REQUIRE_F32_F64=0` \
941+
environment variable can be used to temporarily \
942+
disable this error"
943+
);
944+
anyhow::Error::from(err).context(context)
945+
}
926946
}
927947

928948
fn resolve_use(&mut self, owner: TypeOwner, u: &ast::Use<'a>) -> Result<()> {

crates/wit-parser/src/ast/toposort.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub fn toposort<'a>(
5555
span: edge.span,
5656
name: edge.name.to_string(),
5757
kind: kind.to_string(),
58+
highlighted: None,
5859
})?;
5960
states[j].reverse_deps.push(i);
6061
}
@@ -115,6 +116,7 @@ pub fn toposort<'a>(
115116
span: dep.span,
116117
name: dep.name.to_string(),
117118
kind: kind.to_string(),
119+
highlighted: None,
118120
});
119121
}
120122
}
@@ -128,16 +130,38 @@ pub enum Error {
128130
span: Span,
129131
name: String,
130132
kind: String,
133+
highlighted: Option<String>,
131134
},
132135
Cycle {
133136
span: Span,
134137
name: String,
135138
kind: String,
139+
highlighted: Option<String>,
136140
},
137141
}
138142

143+
impl Error {
144+
pub(crate) fn highlighted(&self) -> Option<&str> {
145+
match self {
146+
Error::NonexistentDep { highlighted, .. } | Error::Cycle { highlighted, .. } => {
147+
highlighted.as_deref()
148+
}
149+
}
150+
}
151+
pub(crate) fn set_highlighted(&mut self, string: String) {
152+
match self {
153+
Error::NonexistentDep { highlighted, .. } | Error::Cycle { highlighted, .. } => {
154+
*highlighted = Some(string);
155+
}
156+
}
157+
}
158+
}
159+
139160
impl fmt::Display for Error {
140161
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
162+
if let Some(s) = self.highlighted() {
163+
return f.write_str(s);
164+
}
141165
match self {
142166
Error::NonexistentDep { kind, name, .. } => {
143167
write!(f, "{kind} `{name}` does not exist")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package foo:bar;
2+
3+
interface a {
4+
type t1 = float32;
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
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
2+
--> tests/ui/parse-fail/old-float-types.wit:4:13
3+
|
4+
4 | type t1 = float32;
5+
| ^------

0 commit comments

Comments
 (0)