From 8bb43ec583e6cb24e542767a1d2e193f93026269 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 8 Jan 2024 16:52:25 -0800 Subject: [PATCH 1/2] Add migration code for `f32`/`f64`. Add a `WIT_REQUIRE_F32_F64` environment variable controlling whether the `float32` and `float64` syntax is acccepted, similar to the transition for semicolons. --- crates/wit-component/src/printing.rs | 22 ++++++++++++++++++++-- crates/wit-parser/src/ast/lex.rs | 15 +++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/crates/wit-component/src/printing.rs b/crates/wit-component/src/printing.rs index 1ae09295e9..449d7e0373 100644 --- a/crates/wit-component/src/printing.rs +++ b/crates/wit-component/src/printing.rs @@ -6,6 +6,7 @@ use wit_parser::*; // NB: keep in sync with `crates/wit-parser/src/ast/lex.rs` const PRINT_SEMICOLONS_DEFAULT: bool = true; +const PRINT_F32_F64_DEFAULT: bool = true; /// A utility for printing WebAssembly interface definitions to a string. pub struct WitPrinter { @@ -19,6 +20,7 @@ pub struct WitPrinter { emit_docs: bool, print_semicolons: bool, + print_f32_f64: bool, } impl Default for WitPrinter { @@ -31,6 +33,10 @@ impl Default for WitPrinter { Ok(s) => s == "1", Err(_) => PRINT_SEMICOLONS_DEFAULT, }, + print_f32_f64: match std::env::var("WIT_REQUIRE_F32_F64") { + Ok(s) => s == "1", + Err(_) => PRINT_F32_F64_DEFAULT, + }, } } } @@ -437,8 +443,20 @@ impl WitPrinter { Type::S16 => self.output.push_str("s16"), Type::S32 => self.output.push_str("s32"), Type::S64 => self.output.push_str("s64"), - Type::Float32 => self.output.push_str("float32"), - Type::Float64 => self.output.push_str("float64"), + Type::Float32 => { + if self.print_f32_f64 { + self.output.push_str("f32") + } else { + self.output.push_str("float32") + } + } + Type::Float64 => { + if self.print_f32_f64 { + self.output.push_str("f64") + } else { + self.output.push_str("float64") + } + } Type::Char => self.output.push_str("char"), Type::String => self.output.push_str("string"), diff --git a/crates/wit-parser/src/ast/lex.rs b/crates/wit-parser/src/ast/lex.rs index c300392f53..8bcead10d7 100644 --- a/crates/wit-parser/src/ast/lex.rs +++ b/crates/wit-parser/src/ast/lex.rs @@ -13,6 +13,7 @@ pub struct Tokenizer<'a> { span_offset: u32, chars: CrlfFold<'a>, require_semicolons: bool, + require_f32_f64: bool, } #[derive(Clone)] @@ -118,12 +119,14 @@ pub enum Error { // NB: keep in sync with `crates/wit-component/src/printing.rs`. const REQUIRE_SEMICOLONS_BY_DEFAULT: bool = true; +const REQUIRE_F32_F64_BY_DEFAULT: bool = true; impl<'a> Tokenizer<'a> { pub fn new( input: &'a str, span_offset: u32, require_semicolons: Option, + require_f32_f64: Option, ) -> Result> { detect_invalid_input(input)?; @@ -139,6 +142,12 @@ impl<'a> Tokenizer<'a> { Err(_) => REQUIRE_SEMICOLONS_BY_DEFAULT, } }), + require_f32_f64: require_f32_f64.unwrap_or_else(|| { + match std::env::var("WIT_REQUIRE_F32_F64") { + Ok(s) => s == "1", + Err(_) => REQUIRE_F32_F64_BY_DEFAULT, + } + }), }; // Eat utf-8 BOM t.eatc('\u{feff}'); @@ -285,8 +294,10 @@ impl<'a> Tokenizer<'a> { "s16" => S16, "s32" => S32, "s64" => S64, - "f32" | "float32" => Float32, - "f64" | "float64" => Float64, + "f32" => Float32, + "f64" => Float64, + "float32" if !self.require_f32_f64 => Float32, + "float64" if !self.require_f32_f64 => Float64, "char" => Char, "resource" => Resource, "own" => Own, From a0226e44f7f8197a61d8c8fc1d413bea80ed14b1 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 8 Jan 2024 17:38:50 -0800 Subject: [PATCH 2/2] Enable `float32`/`float64` by default. --- crates/wit-component/src/printing.rs | 2 +- crates/wit-parser/src/ast.rs | 17 ++++++++++++++--- crates/wit-parser/src/ast/lex.rs | 4 ++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/crates/wit-component/src/printing.rs b/crates/wit-component/src/printing.rs index 449d7e0373..6a4074c274 100644 --- a/crates/wit-component/src/printing.rs +++ b/crates/wit-component/src/printing.rs @@ -6,7 +6,7 @@ use wit_parser::*; // NB: keep in sync with `crates/wit-parser/src/ast/lex.rs` const PRINT_SEMICOLONS_DEFAULT: bool = true; -const PRINT_F32_F64_DEFAULT: bool = true; +const PRINT_F32_F64_DEFAULT: bool = false; /// A utility for printing WebAssembly interface definitions to a string. pub struct WitPrinter { diff --git a/crates/wit-parser/src/ast.rs b/crates/wit-parser/src/ast.rs index 3df0a7c57b..694519b3c3 100644 --- a/crates/wit-parser/src/ast.rs +++ b/crates/wit-parser/src/ast.rs @@ -1160,6 +1160,7 @@ pub struct SourceMap { sources: Vec, offset: u32, require_semicolons: Option, + require_f32_f64: Option, } #[derive(Clone)] @@ -1180,6 +1181,11 @@ impl SourceMap { self.require_semicolons = Some(enable); } + #[doc(hidden)] // NB: only here for a transitionary period + pub fn set_require_f32_f64(&mut self, enable: bool) { + self.require_f32_f64 = Some(enable); + } + /// Reads the file `path` on the filesystem and appends its contents to this /// [`SourceMap`]. pub fn push_file(&mut self, path: &Path) -> Result<()> { @@ -1214,8 +1220,13 @@ impl SourceMap { let mut srcs = self.sources.iter().collect::>(); srcs.sort_by_key(|src| &src.path); for src in srcs { - let mut tokens = Tokenizer::new(&src.contents, src.offset, self.require_semicolons) - .with_context(|| format!("failed to tokenize path: {}", src.path.display()))?; + let mut tokens = Tokenizer::new( + &src.contents, + src.offset, + self.require_semicolons, + self.require_f32_f64, + ) + .with_context(|| format!("failed to tokenize path: {}", src.path.display()))?; let ast = Ast::parse(&mut tokens)?; resolver.push(ast).with_context(|| { format!("failed to start resolving path: {}", src.path.display()) @@ -1324,7 +1335,7 @@ pub(crate) enum AstUsePath { } pub(crate) fn parse_use_path(s: &str) -> Result { - let mut tokens = Tokenizer::new(s, 0, Some(true))?; + let mut tokens = Tokenizer::new(s, 0, Some(true), None)?; let path = UsePath::parse(&mut tokens)?; if tokens.next()?.is_some() { bail!("trailing tokens in path specifier"); diff --git a/crates/wit-parser/src/ast/lex.rs b/crates/wit-parser/src/ast/lex.rs index 8bcead10d7..78483c57d6 100644 --- a/crates/wit-parser/src/ast/lex.rs +++ b/crates/wit-parser/src/ast/lex.rs @@ -119,7 +119,7 @@ pub enum Error { // NB: keep in sync with `crates/wit-component/src/printing.rs`. const REQUIRE_SEMICOLONS_BY_DEFAULT: bool = true; -const REQUIRE_F32_F64_BY_DEFAULT: bool = true; +const REQUIRE_F32_F64_BY_DEFAULT: bool = false; impl<'a> Tokenizer<'a> { pub fn new( @@ -665,7 +665,7 @@ fn test_validate_id() { #[test] fn test_tokenizer() { fn collect(s: &str) -> Result> { - let mut t = Tokenizer::new(s, 0, Some(true))?; + let mut t = Tokenizer::new(s, 0, Some(true), None)?; let mut tokens = Vec::new(); while let Some(token) = t.next()? { tokens.push(token.1);