Skip to content

Commit 935f71a

Browse files
authored
Switch from failure to anyhow (#1851)
This commit switches all of `wasm-bindgen` from the `failure` crate to `anyhow`. The `anyhow` crate should serve all the purposes that we previously used `failure` for but has a few advantages: * It's based on the standard `Error` trait rather than a custom `Fail` trait, improving ecosystem compatibility. * We don't need a `#[derive(Fail)]`, which means that's less code to compile for `wasm-bindgen`. This notably helps the compile time of `web-sys` itself. * Using `Result<()>` in `fn main` with `anyhow::Error` produces human-readable output, so we can use that natively.
1 parent 913fdbc commit 935f71a

37 files changed

+131
-239
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,6 @@ wasm-bindgen = { path = '.' }
9393
wasm-bindgen-futures = { path = 'crates/futures' }
9494
js-sys = { path = 'crates/js-sys' }
9595
web-sys = { path = 'crates/web-sys' }
96+
97+
walrus = { git = 'https://github.com/rustwasm/walrus' }
98+
wasm-webidl-bindings = { git = 'https://github.com/rustwasm/wasm-webidl-bindings' }

crates/anyref-xform/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ Internal anyref transformations for wasm-bindgen
1212
edition = '2018'
1313

1414
[dependencies]
15-
failure = "0.1"
16-
walrus = "0.12.0"
15+
anyhow = "1.0"
16+
walrus = "0.13.0"

crates/anyref-xform/src/lib.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
//! goal at least is to have valid wasm modules coming in that don't use
1616
//! `anyref` and valid wasm modules going out which use `anyref` at the fringes.
1717
18-
use failure::{bail, format_err, Error};
18+
use anyhow::{anyhow, bail, Error};
1919
use std::cmp;
2020
use std::collections::{BTreeMap, HashMap, HashSet};
2121
use walrus::ir::*;
22-
use walrus::{ExportId, ImportId, TypeId};
22+
use walrus::{ExportId, ImportId, InstrLocId, TypeId};
2323
use walrus::{FunctionId, GlobalId, InitExpr, Module, TableId, ValType};
2424

2525
// must be kept in sync with src/lib.rs and ANYREF_HEAP_START
@@ -185,9 +185,8 @@ impl Context {
185185
_ => {}
186186
}
187187
}
188-
let heap_alloc = heap_alloc.ok_or_else(|| format_err!("failed to find heap alloc"))?;
189-
let heap_dealloc =
190-
heap_dealloc.ok_or_else(|| format_err!("failed to find heap dealloc"))?;
188+
let heap_alloc = heap_alloc.ok_or_else(|| anyhow!("failed to find heap alloc"))?;
189+
let heap_dealloc = heap_dealloc.ok_or_else(|| anyhow!("failed to find heap dealloc"))?;
191190

192191
// Create a shim function that looks like:
193192
//
@@ -624,7 +623,7 @@ impl Transform<'_> {
624623
impl VisitorMut for Rewrite<'_, '_> {
625624
fn start_instr_seq_mut(&mut self, seq: &mut InstrSeq) {
626625
for i in (0..seq.instrs.len()).rev() {
627-
let call = match &mut seq.instrs[i] {
626+
let call = match &mut seq.instrs[i].0 {
628627
Instr::Call(call) => call,
629628
_ => continue,
630629
};
@@ -644,24 +643,26 @@ impl Transform<'_> {
644643
match intrinsic {
645644
Intrinsic::TableGrow => {
646645
// Switch this to a `table.grow` instruction...
647-
seq.instrs[i] = TableGrow {
646+
seq.instrs[i].0 = TableGrow {
648647
table: self.xform.table,
649648
}
650649
.into();
651650
// ... and then insert a `ref.null` before the
652651
// preceding instruction as the value to grow the
653652
// table with.
654-
seq.instrs.insert(i - 1, RefNull {}.into());
653+
seq.instrs
654+
.insert(i - 1, (RefNull {}.into(), InstrLocId::default()));
655655
}
656656
Intrinsic::TableSetNull => {
657657
// Switch this to a `table.set` instruction...
658-
seq.instrs[i] = TableSet {
658+
seq.instrs[i].0 = TableSet {
659659
table: self.xform.table,
660660
}
661661
.into();
662662
// ... and then insert a `ref.null` as the
663663
// preceding instruction
664-
seq.instrs.insert(i, RefNull {}.into());
664+
seq.instrs
665+
.insert(i, (RefNull {}.into(), InstrLocId::default()));
665666
}
666667
Intrinsic::DropRef => call.func = self.xform.heap_dealloc,
667668
Intrinsic::CloneRef => call.func = self.xform.clone_ref,

crates/cli-support/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ edition = '2018'
1313

1414
[dependencies]
1515
base64 = "0.9"
16-
failure = "0.1.2"
16+
anyhow = "1.0"
1717
log = "0.4"
1818
rustc-demangle = "0.1.13"
1919
serde_json = "1.0"
2020
tempfile = "3.0"
21-
walrus = "0.12.0"
21+
walrus = "0.13.0"
2222
wasm-bindgen-anyref-xform = { path = '../anyref-xform', version = '=0.2.53' }
2323
wasm-bindgen-shared = { path = "../shared", version = '=0.2.53' }
2424
wasm-bindgen-multi-value-xform = { path = '../multi-value-xform', version = '=0.2.53' }

crates/cli-support/src/anyref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::webidl::{NonstandardIncoming, NonstandardOutgoing};
22
use crate::webidl::{NonstandardWebidlSection, WasmBindgenAux};
3-
use failure::Error;
3+
use anyhow::Error;
44
use std::collections::HashSet;
55
use walrus::Module;
66
use wasm_bindgen_anyref_xform::Context;

crates/cli-support/src/descriptors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! functions.
1212
1313
use crate::descriptor::{Closure, Descriptor};
14-
use failure::Error;
14+
use anyhow::Error;
1515
use std::borrow::Cow;
1616
use std::collections::{HashMap, HashSet};
1717
use walrus::ImportId;

crates/cli-support/src/js/binding.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::js::incoming;
88
use crate::js::outgoing;
99
use crate::js::Context;
1010
use crate::webidl::Binding;
11-
use failure::{bail, Error};
11+
use anyhow::{bail, Error};
1212
use std::collections::HashSet;
1313
use wasm_webidl_bindings::ast;
1414

crates/cli-support/src/js/incoming.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::descriptor::VectorKind;
88
use crate::js::binding::JsBuilder;
99
use crate::js::Context;
1010
use crate::webidl::NonstandardIncoming;
11-
use failure::{bail, Error};
11+
use anyhow::{bail, Error};
1212
use wasm_webidl_bindings::ast;
1313

1414
pub struct Incoming<'a, 'b> {

crates/cli-support/src/js/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::webidl::{AuxEnum, AuxExport, AuxExportKind, AuxImport, AuxStruct};
55
use crate::webidl::{AuxValue, Binding};
66
use crate::webidl::{JsImport, JsImportName, NonstandardWebidlSection, WasmBindgenAux};
77
use crate::{Bindgen, EncodeInto, OutputMode};
8-
use failure::{bail, Error, ResultExt};
8+
use anyhow::{bail, Context as _, Error};
99
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
1010
use std::fs;
1111
use std::path::{Path, PathBuf};
@@ -1888,7 +1888,7 @@ impl<'a> Context<'a> {
18881888
check_duplicated_getter_and_setter_names(&pairs)?;
18891889
for (id, export) in pairs {
18901890
self.generate_export(*id, export, bindings)
1891-
.with_context(|_| {
1891+
.with_context(|| {
18921892
format!(
18931893
"failed to generate bindings for Rust export `{}`",
18941894
export.debug_name,
@@ -1901,7 +1901,7 @@ impl<'a> Context<'a> {
19011901
let catch = aux.imports_with_catch.contains(&id);
19021902
let assert_no_shim = aux.imports_with_assert_no_shim.contains(&id);
19031903
self.generate_import(*id, import, bindings, variadic, catch, assert_no_shim)
1904-
.with_context(|_| {
1904+
.with_context(|| {
19051905
format!("failed to generate bindings for import `{:?}`", import,)
19061906
})?;
19071907
}

crates/cli-support/src/js/outgoing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::descriptor::VectorKind;
66
use crate::js::binding::JsBuilder;
77
use crate::js::Context;
88
use crate::webidl::NonstandardOutgoing;
9-
use failure::{bail, Error};
9+
use anyhow::{bail, Error};
1010
use wasm_webidl_bindings::ast;
1111

1212
pub struct Outgoing<'a, 'b> {

crates/cli-support/src/lib.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-cli-support/0.2")]
22

3-
use failure::{bail, Error, ResultExt};
3+
use anyhow::{bail, Context, Error};
44
use std::collections::{BTreeMap, BTreeSet, HashMap};
55
use std::env;
66
use std::fs;
@@ -256,7 +256,7 @@ impl Bindgen {
256256
}
257257
Input::Path(ref path) => {
258258
let contents = fs::read(&path)
259-
.with_context(|_| format!("failed to read `{}`", path.display()))?;
259+
.with_context(|| format!("failed to read `{}`", path.display()))?;
260260
let module = walrus::ModuleConfig::new()
261261
// Skip validation of the module as LLVM's output is
262262
// generally already well-formed and so we won't gain much
@@ -307,7 +307,7 @@ impl Bindgen {
307307

308308
self.threads
309309
.run(&mut module)
310-
.with_context(|_| "failed to prepare module for threading")?;
310+
.with_context(|| "failed to prepare module for threading")?;
311311

312312
// If requested, turn all mangled symbols into prettier unmangled
313313
// symbols with the help of `rustc-demangle`.
@@ -373,10 +373,10 @@ impl Bindgen {
373373
.context("failed to transform return pointers into multi-value Wasm")?;
374374
}
375375
webidl::standard::add_section(&mut module, &aux, &bindings)
376-
.with_context(|_| "failed to generate a standard wasm bindings custom section")?;
376+
.with_context(|| "failed to generate a standard wasm bindings custom section")?;
377377
} else {
378378
if self.multi_value {
379-
failure::bail!(
379+
anyhow::bail!(
380380
"Wasm multi-value is currently only available when \
381381
Wasm interface types is also enabled"
382382
);
@@ -565,11 +565,11 @@ impl Output {
565565
&self.module
566566
}
567567

568-
pub fn emit(&self, out_dir: impl AsRef<Path>) -> Result<(), Error> {
568+
pub fn emit(&mut self, out_dir: impl AsRef<Path>) -> Result<(), Error> {
569569
self._emit(out_dir.as_ref())
570570
}
571571

572-
fn _emit(&self, out_dir: &Path) -> Result<(), Error> {
572+
fn _emit(&mut self, out_dir: &Path) -> Result<(), Error> {
573573
let wasm_name = if self.wasm_interface_types {
574574
self.stem.clone()
575575
} else {
@@ -579,7 +579,7 @@ impl Output {
579579
fs::create_dir_all(out_dir)?;
580580
let wasm_bytes = self.module.emit_wasm();
581581
fs::write(&wasm_path, wasm_bytes)
582-
.with_context(|_| format!("failed to write `{}`", wasm_path.display()))?;
582+
.with_context(|| format!("failed to write `{}`", wasm_path.display()))?;
583583

584584
if self.wasm_interface_types {
585585
return Ok(());
@@ -593,15 +593,15 @@ impl Output {
593593
let path = out_dir.join("snippets").join(identifier).join(name);
594594
fs::create_dir_all(path.parent().unwrap())?;
595595
fs::write(&path, js)
596-
.with_context(|_| format!("failed to write `{}`", path.display()))?;
596+
.with_context(|| format!("failed to write `{}`", path.display()))?;
597597
}
598598
}
599599

600600
for (path, contents) in self.local_modules.iter() {
601601
let path = out_dir.join("snippets").join(path);
602602
fs::create_dir_all(path.parent().unwrap())?;
603603
fs::write(&path, contents)
604-
.with_context(|_| format!("failed to write `{}`", path.display()))?;
604+
.with_context(|| format!("failed to write `{}`", path.display()))?;
605605
}
606606

607607
if self.npm_dependencies.len() > 0 {
@@ -623,26 +623,26 @@ impl Output {
623623
};
624624
let js_path = out_dir.join(&self.stem).with_extension(extension);
625625
fs::write(&js_path, reset_indentation(&self.js))
626-
.with_context(|_| format!("failed to write `{}`", js_path.display()))?;
626+
.with_context(|| format!("failed to write `{}`", js_path.display()))?;
627627

628628
if self.typescript {
629629
let ts_path = js_path.with_extension("d.ts");
630630
fs::write(&ts_path, &self.ts)
631-
.with_context(|_| format!("failed to write `{}`", ts_path.display()))?;
631+
.with_context(|| format!("failed to write `{}`", ts_path.display()))?;
632632
}
633633

634634
if self.mode.nodejs() {
635635
let js_path = wasm_path.with_extension(extension);
636636
let shim = self.generate_node_wasm_import(&self.module, &wasm_path);
637637
fs::write(&js_path, shim)
638-
.with_context(|_| format!("failed to write `{}`", js_path.display()))?;
638+
.with_context(|| format!("failed to write `{}`", js_path.display()))?;
639639
}
640640

641641
if self.typescript {
642642
let ts_path = wasm_path.with_extension("d.ts");
643643
let ts = wasm2es6js::typescript(&self.module)?;
644644
fs::write(&ts_path, ts)
645-
.with_context(|_| format!("failed to write `{}`", ts_path.display()))?;
645+
.with_context(|| format!("failed to write `{}`", ts_path.display()))?;
646646
}
647647

648648
Ok(())

crates/cli-support/src/wasm2es6js.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use failure::{bail, Error};
1+
use anyhow::{bail, Error};
22
use std::collections::HashSet;
33
use walrus::Module;
44

crates/cli-support/src/webidl/bindings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::descriptor::Function;
1414
use crate::webidl::incoming::IncomingBuilder;
1515
use crate::webidl::outgoing::OutgoingBuilder;
1616
use crate::webidl::{Binding, NonstandardWebidlSection};
17-
use failure::{format_err, Error};
17+
use anyhow::{format_err, Error};
1818
use walrus::{FunctionId, Module, ValType};
1919
use wasm_webidl_bindings::ast;
2020

crates/cli-support/src/webidl/incoming.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! the `outgoing.rs` module.
1212
1313
use crate::descriptor::{Descriptor, VectorKind};
14-
use failure::{bail, format_err, Error};
14+
use anyhow::{bail, format_err, Error};
1515
use walrus::ValType;
1616
use wasm_webidl_bindings::ast;
1717

crates/cli-support/src/webidl/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::decode;
2727
use crate::descriptor::{Descriptor, Function};
2828
use crate::descriptors::WasmBindgenDescriptorsSection;
2929
use crate::intrinsic::Intrinsic;
30-
use failure::{bail, format_err, Error};
30+
use anyhow::{anyhow, bail, Error};
3131
use std::borrow::Cow;
3232
use std::collections::{HashMap, HashSet};
3333
use std::path::PathBuf;
@@ -1340,7 +1340,7 @@ impl<'a> Context<'a> {
13401340
walrus::ExportItem::Function(f) => f == bind.func,
13411341
_ => false,
13421342
})
1343-
.ok_or_else(|| format_err!("missing export function for webidl binding"))?;
1343+
.ok_or_else(|| anyhow!("missing export function for webidl binding"))?;
13441344
let id = export.id();
13451345
self.standard_export(binding, id)?;
13461346
}
@@ -1363,7 +1363,7 @@ impl<'a> Context<'a> {
13631363
let binding: &ast::FunctionBinding = std
13641364
.bindings
13651365
.get(bind.binding)
1366-
.ok_or_else(|| format_err!("bad binding id"))?;
1366+
.ok_or_else(|| anyhow!("bad binding id"))?;
13671367
let (wasm_ty, webidl_ty, incoming, outgoing) = match binding {
13681368
ast::FunctionBinding::Export(e) => (
13691369
e.wasm_ty,

crates/cli-support/src/webidl/outgoing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
use crate::descriptor::{Descriptor, VectorKind};
1717
use crate::webidl::NonstandardWebidlSection;
18-
use failure::{bail, format_err, Error};
18+
use anyhow::{bail, format_err, Error};
1919
use walrus::{Module, ValType};
2020
use wasm_webidl_bindings::ast;
2121

crates/cli-support/src/webidl/standard.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::descriptor::VectorKind;
2020
use crate::webidl::{AuxExportKind, AuxImport, AuxValue, JsImport, JsImportName};
2121
use crate::webidl::{NonstandardIncoming, NonstandardOutgoing};
2222
use crate::webidl::{NonstandardWebidlSection, WasmBindgenAux};
23-
use failure::{bail, Error, ResultExt};
23+
use anyhow::{bail, Context, Error};
2424
use walrus::Module;
2525
use wasm_bindgen_multi_value_xform as multi_value_xform;
2626
use wasm_bindgen_wasm_conventions as wasm_conventions;
@@ -210,14 +210,14 @@ pub fn add_section(
210210
}
211211

212212
let name = &module.exports.get(*export).name;
213-
let params = extract_incoming(&binding.incoming).with_context(|_| {
213+
let params = extract_incoming(&binding.incoming).with_context(|| {
214214
format!(
215215
"failed to map arguments for export `{}` to standard \
216216
binding expressions",
217217
name
218218
)
219219
})?;
220-
let result = extract_outgoing(&binding.outgoing).with_context(|_| {
220+
let result = extract_outgoing(&binding.outgoing).with_context(|| {
221221
format!(
222222
"failed to map return value for export `{}` to standard \
223223
binding expressions",
@@ -252,14 +252,14 @@ pub fn add_section(
252252
let import = module.imports.get(*import);
253253
(&import.module, &import.name)
254254
};
255-
let params = extract_outgoing(&binding.outgoing).with_context(|_| {
255+
let params = extract_outgoing(&binding.outgoing).with_context(|| {
256256
format!(
257257
"failed to map arguments of import `{}::{}` to standard \
258258
binding expressions",
259259
module_name, name,
260260
)
261261
})?;
262-
let result = extract_incoming(&binding.incoming).with_context(|_| {
262+
let result = extract_incoming(&binding.incoming).with_context(|| {
263263
format!(
264264
"failed to map return value of import `{}::{}` to standard \
265265
binding expressions",

crates/cli/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ edition = '2018'
1717
curl = "0.4.13"
1818
docopt = "1.0"
1919
env_logger = "0.7"
20-
failure = "0.1.2"
20+
anyhow = "1.0"
2121
log = "0.4"
2222
openssl = { version = '0.10.11', optional = true }
2323
rouille = { version = "3.0.0", default-features = false }
2424
serde = { version = "1.0", features = ['derive'] }
2525
serde_derive = "1.0"
2626
serde_json = "1.0"
27-
walrus = { version = "0.12.0", features = ['parallel'] }
27+
walrus = { version = "0.13.0", features = ['parallel'] }
2828
wasm-bindgen-cli-support = { path = "../cli-support", version = "=0.2.53" }
2929
wasm-bindgen-shared = { path = "../shared", version = "=0.2.53" }
3030

0 commit comments

Comments
 (0)