Skip to content

Commit 43ebcb8

Browse files
authored
Remove usage of gen as an identifier (bytecodealliance#9958)
This commit is a start to some preparation for the Rust 2024 edition for Wasmtime and this workspace. The `rust-2024-compatibility` lint group in rustc, currently off-by-default, is intended to assist with migrating code to prepare for the 2024 edition. Some of those lints though are, in my opinion, far too noisy to be turned on so this PR doesn't turn on the whole group. Instead though I plan on enabling individual lints over time in our `Cargo.toml` before the 2024 edition is enabled. This should hopefully provide a relatively smooth and less churn-y path to enabling the 2024 edition in the future. The first lint enabled here in this commit is the `keyword_idents_2024` lint which warns against usage of identifiers that will become keywords in the 2024 edition. The only one affecting Wasmtime is the `gen` identifier (soon to be keyword) and we had quite a few instances of it. Where possible I've renamed to `generator` or `generate` or `generated` but when used as methods from upstream crate traits (e.g. `RngCore::gen`) we're forced to use `r#gen`. The `rand` crate will be updated in 0.9.0 to avoid this keyword so this shouldn't be permanent.
1 parent a8c767a commit 43ebcb8

File tree

14 files changed

+268
-237
lines changed

14 files changed

+268
-237
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ unused_import_braces = 'warn'
179179
unused-lifetimes = 'warn'
180180
unused-macro-rules = 'warn'
181181

182+
# Lints that are part of the `rust-2024-compatibility` group. This group is a
183+
# bit too noisy to enable wholesale but some selective items are ones we want to
184+
# opt-in to.
185+
keyword_idents_2024 = 'warn'
186+
182187
# Don't warn about unknown cfgs for pulley
183188
[workspace.lints.rust.unexpected_cfgs]
184189
level = "warn"

crates/environ/src/component/dfg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ impl LinearizeDfg<'_> {
697697
&mut self,
698698
key: K,
699699
map: impl Fn(&mut Self) -> &mut HashMap<K, V>,
700-
gen: impl FnOnce(&mut Self, K) -> T,
700+
generate: impl FnOnce(&mut Self, K) -> T,
701701
init: impl FnOnce(V, T) -> GlobalInitializer,
702702
) -> V
703703
where
@@ -707,7 +707,7 @@ impl LinearizeDfg<'_> {
707707
if let Some(val) = map(self).get(&key) {
708708
return *val;
709709
}
710-
let tmp = gen(self, key);
710+
let tmp = generate(self, key);
711711
let index = V::new(map(self).len());
712712
self.initializers.push(init(index, tmp));
713713
let prev = map(self).insert(key, index);

crates/fuzzing/src/single_module_fuzzer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,10 @@ mod tests {
311311
let mutate = mutate::<u32>;
312312
let run2 = run_config::<(u32, u32)>;
313313

314-
if let Ok((module, known_valid)) = execute(&buf[..seed_size], run1, gen) {
314+
if let Ok((module, known_valid)) = execute(&buf[..seed_size], run1, generate) {
315315
assert_eq!(known_valid, KnownValid::Yes);
316-
let new_size = mutate(&mut buf, seed_size, max_size, gen, noop_mutate);
317-
if let Ok((module2, known_valid)) = execute(&buf[..new_size], run2, gen) {
316+
let new_size = mutate(&mut buf, seed_size, max_size, generate, noop_mutate);
317+
if let Ok((module2, known_valid)) = execute(&buf[..new_size], run2, generate) {
318318
assert_eq!(known_valid, KnownValid::No);
319319
compares += 1;
320320
if module != module2 {
@@ -340,7 +340,7 @@ mod tests {
340340
Ok((data.to_vec(), known_valid))
341341
}
342342

343-
fn gen<T>(_: &mut T, u: &mut Unstructured<'_>) -> Result<(Vec<u8>, KnownValid)>
343+
fn generate<T>(_: &mut T, u: &mut Unstructured<'_>) -> Result<(Vec<u8>, KnownValid)>
344344
where
345345
T: for<'a> Arbitrary<'a>,
346346
{

crates/misc/component-fuzz-util/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ impl<T, const L: u32, const H: u32> VecInRange<T, L, H> {
6868
fn new<'a>(
6969
input: &mut Unstructured<'a>,
7070
fuel: &mut u32,
71-
gen: impl Fn(&mut Unstructured<'a>, &mut u32) -> arbitrary::Result<T>,
71+
generate: impl Fn(&mut Unstructured<'a>, &mut u32) -> arbitrary::Result<T>,
7272
) -> arbitrary::Result<Self> {
7373
let mut ret = Vec::new();
7474
input.arbitrary_loop(Some(L), Some(H), |input| {
7575
if *fuel > 0 {
7676
*fuel = *fuel - 1;
77-
ret.push(gen(input, fuel)?);
77+
ret.push(generate(input, fuel)?);
7878
Ok(std::ops::ControlFlow::Continue(()))
7979
} else {
8080
Ok(std::ops::ControlFlow::Break(()))

crates/wasi-common/src/sync/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl WasiCtxBuilder {
131131

132132
pub fn random_ctx() -> Box<dyn RngCore + Send + Sync> {
133133
let mut rng = cap_rand::thread_rng(cap_rand::ambient_authority());
134-
Box::new(cap_rand::rngs::StdRng::from_seed(rng.gen()))
134+
Box::new(cap_rand::rngs::StdRng::from_seed(rng.r#gen()))
135135
}
136136

137137
#[cfg(feature = "wasmtime")]

crates/wasi-nn/src/wit.rs

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//! This module exports its [`types`] for use throughout the crate and the
88
//! [`ML`] object, which exposes [`ML::add_to_linker`]. To implement all of
99
//! this, this module proceeds in steps:
10-
//! 1. generate all of the WIT glue code into a `gen::*` namespace
11-
//! 2. wire up the `gen::*` glue to the context state, delegating actual
10+
//! 1. generate all of the WIT glue code into a `generated::*` namespace
11+
//! 2. wire up the `generated::*` glue to the context state, delegating actual
1212
//! computation to a [`Backend`]
1313
//! 3. convert some types
1414
//!
@@ -115,7 +115,7 @@ pub enum ErrorCode {
115115
}
116116

117117
/// Generate the traits and types from the `wasi-nn` WIT specification.
118-
mod gen_ {
118+
mod generated_ {
119119
wasmtime::component::bindgen!({
120120
world: "ml",
121121
path: "wit/wasi-nn.wit",
@@ -133,35 +133,35 @@ mod gen_ {
133133
},
134134
});
135135
}
136-
use gen_::wasi::nn::{self as gen}; // Shortcut to the module containing the types we need.
136+
use generated_::wasi::nn::{self as generated}; // Shortcut to the module containing the types we need.
137137

138138
// Export the `types` used in this crate as well as `ML::add_to_linker`.
139139
pub mod types {
140-
use super::gen;
141-
pub use gen::errors::Error;
142-
pub use gen::graph::{ExecutionTarget, Graph, GraphBuilder, GraphEncoding};
143-
pub use gen::inference::GraphExecutionContext;
144-
pub use gen::tensor::{Tensor, TensorType};
140+
use super::generated;
141+
pub use generated::errors::Error;
142+
pub use generated::graph::{ExecutionTarget, Graph, GraphBuilder, GraphEncoding};
143+
pub use generated::inference::GraphExecutionContext;
144+
pub use generated::tensor::{Tensor, TensorType};
145145
}
146-
pub use gen::graph::{ExecutionTarget, Graph, GraphBuilder, GraphEncoding};
147-
pub use gen::inference::GraphExecutionContext;
148-
pub use gen::tensor::{Tensor, TensorData, TensorDimensions, TensorType};
149-
pub use gen_::Ml as ML;
146+
pub use generated::graph::{ExecutionTarget, Graph, GraphBuilder, GraphEncoding};
147+
pub use generated::inference::GraphExecutionContext;
148+
pub use generated::tensor::{Tensor, TensorData, TensorDimensions, TensorType};
149+
pub use generated_::Ml as ML;
150150

151151
/// Add the WIT-based version of the `wasi-nn` API to a
152152
/// [`wasmtime::component::Linker`].
153153
pub fn add_to_linker<T>(
154154
l: &mut wasmtime::component::Linker<T>,
155155
f: impl Fn(&mut T) -> WasiNnView<'_> + Send + Sync + Copy + 'static,
156156
) -> anyhow::Result<()> {
157-
gen::graph::add_to_linker_get_host(l, f)?;
158-
gen::tensor::add_to_linker_get_host(l, f)?;
159-
gen::inference::add_to_linker_get_host(l, f)?;
160-
gen::errors::add_to_linker_get_host(l, f)?;
157+
generated::graph::add_to_linker_get_host(l, f)?;
158+
generated::tensor::add_to_linker_get_host(l, f)?;
159+
generated::inference::add_to_linker_get_host(l, f)?;
160+
generated::errors::add_to_linker_get_host(l, f)?;
161161
Ok(())
162162
}
163163

164-
impl gen::graph::Host for WasiNnView<'_> {
164+
impl generated::graph::Host for WasiNnView<'_> {
165165
fn load(
166166
&mut self,
167167
builders: Vec<GraphBuilder>,
@@ -210,7 +210,7 @@ impl gen::graph::Host for WasiNnView<'_> {
210210
}
211211
}
212212

213-
impl gen::graph::HostGraph for WasiNnView<'_> {
213+
impl generated::graph::HostGraph for WasiNnView<'_> {
214214
fn init_execution_context(
215215
&mut self,
216216
graph: Resource<Graph>,
@@ -235,7 +235,7 @@ impl gen::graph::HostGraph for WasiNnView<'_> {
235235
}
236236
}
237237

238-
impl gen::inference::HostGraphExecutionContext for WasiNnView<'_> {
238+
impl generated::inference::HostGraphExecutionContext for WasiNnView<'_> {
239239
fn set_input(
240240
&mut self,
241241
exec_context: Resource<GraphExecutionContext>,
@@ -291,7 +291,7 @@ impl gen::inference::HostGraphExecutionContext for WasiNnView<'_> {
291291
}
292292
}
293293

294-
impl gen::tensor::HostTensor for WasiNnView<'_> {
294+
impl generated::tensor::HostTensor for WasiNnView<'_> {
295295
fn new(
296296
&mut self,
297297
dimensions: TensorDimensions,
@@ -328,17 +328,19 @@ impl gen::tensor::HostTensor for WasiNnView<'_> {
328328
}
329329
}
330330

331-
impl gen::errors::HostError for WasiNnView<'_> {
332-
fn code(&mut self, error: Resource<Error>) -> wasmtime::Result<gen::errors::ErrorCode> {
331+
impl generated::errors::HostError for WasiNnView<'_> {
332+
fn code(&mut self, error: Resource<Error>) -> wasmtime::Result<generated::errors::ErrorCode> {
333333
let error = self.table.get(&error)?;
334334
match error.code {
335-
ErrorCode::InvalidArgument => Ok(gen::errors::ErrorCode::InvalidArgument),
336-
ErrorCode::InvalidEncoding => Ok(gen::errors::ErrorCode::InvalidEncoding),
337-
ErrorCode::Timeout => Ok(gen::errors::ErrorCode::Timeout),
338-
ErrorCode::RuntimeError => Ok(gen::errors::ErrorCode::RuntimeError),
339-
ErrorCode::UnsupportedOperation => Ok(gen::errors::ErrorCode::UnsupportedOperation),
340-
ErrorCode::TooLarge => Ok(gen::errors::ErrorCode::TooLarge),
341-
ErrorCode::NotFound => Ok(gen::errors::ErrorCode::NotFound),
335+
ErrorCode::InvalidArgument => Ok(generated::errors::ErrorCode::InvalidArgument),
336+
ErrorCode::InvalidEncoding => Ok(generated::errors::ErrorCode::InvalidEncoding),
337+
ErrorCode::Timeout => Ok(generated::errors::ErrorCode::Timeout),
338+
ErrorCode::RuntimeError => Ok(generated::errors::ErrorCode::RuntimeError),
339+
ErrorCode::UnsupportedOperation => {
340+
Ok(generated::errors::ErrorCode::UnsupportedOperation)
341+
}
342+
ErrorCode::TooLarge => Ok(generated::errors::ErrorCode::TooLarge),
343+
ErrorCode::NotFound => Ok(generated::errors::ErrorCode::NotFound),
342344
ErrorCode::Trap => Err(anyhow!(error.data.to_string())),
343345
}
344346
}
@@ -354,7 +356,7 @@ impl gen::errors::HostError for WasiNnView<'_> {
354356
}
355357
}
356358

357-
impl gen::errors::Host for WasiNnView<'_> {
359+
impl generated::errors::Host for WasiNnView<'_> {
358360
fn convert_error(&mut self, err: Error) -> wasmtime::Result<Error> {
359361
if matches!(err.code, ErrorCode::Trap) {
360362
Err(err.data)
@@ -364,18 +366,18 @@ impl gen::errors::Host for WasiNnView<'_> {
364366
}
365367
}
366368

367-
impl gen::tensor::Host for WasiNnView<'_> {}
368-
impl gen::inference::Host for WasiNnView<'_> {}
369+
impl generated::tensor::Host for WasiNnView<'_> {}
370+
impl generated::inference::Host for WasiNnView<'_> {}
369371

370-
impl Hash for gen::graph::GraphEncoding {
372+
impl Hash for generated::graph::GraphEncoding {
371373
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
372374
self.to_string().hash(state)
373375
}
374376
}
375377

376-
impl fmt::Display for gen::graph::GraphEncoding {
378+
impl fmt::Display for generated::graph::GraphEncoding {
377379
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
378-
use gen::graph::GraphEncoding::*;
380+
use generated::graph::GraphEncoding::*;
379381
match self {
380382
Openvino => write!(f, "openvino"),
381383
Onnx => write!(f, "onnx"),
@@ -388,16 +390,16 @@ impl fmt::Display for gen::graph::GraphEncoding {
388390
}
389391
}
390392

391-
impl FromStr for gen::graph::GraphEncoding {
393+
impl FromStr for generated::graph::GraphEncoding {
392394
type Err = GraphEncodingParseError;
393395
fn from_str(s: &str) -> Result<Self, Self::Err> {
394396
match s.to_lowercase().as_str() {
395-
"openvino" => Ok(gen::graph::GraphEncoding::Openvino),
396-
"onnx" => Ok(gen::graph::GraphEncoding::Onnx),
397-
"pytorch" => Ok(gen::graph::GraphEncoding::Pytorch),
398-
"tensorflow" => Ok(gen::graph::GraphEncoding::Tensorflow),
399-
"tensorflowlite" => Ok(gen::graph::GraphEncoding::Tensorflowlite),
400-
"autodetect" => Ok(gen::graph::GraphEncoding::Autodetect),
397+
"openvino" => Ok(generated::graph::GraphEncoding::Openvino),
398+
"onnx" => Ok(generated::graph::GraphEncoding::Onnx),
399+
"pytorch" => Ok(generated::graph::GraphEncoding::Pytorch),
400+
"tensorflow" => Ok(generated::graph::GraphEncoding::Tensorflow),
401+
"tensorflowlite" => Ok(generated::graph::GraphEncoding::Tensorflowlite),
402+
"autodetect" => Ok(generated::graph::GraphEncoding::Autodetect),
401403
_ => Err(GraphEncodingParseError(s.into())),
402404
}
403405
}

0 commit comments

Comments
 (0)