Skip to content

Commit 3290879

Browse files
Render error (#2560)
* Add render_error and rename highlight to render * Shorten docstrings
1 parent 2f82130 commit 3290879

10 files changed

Lines changed: 41 additions & 38 deletions

File tree

crates/wit-component/tests/components.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn run_test(path: &Path) -> Result<()> {
178178
assert_output(&wit, &component_wit_path)?;
179179

180180
UnresolvedPackageGroup::parse(&component_wit_path, &wit)
181-
.map_err(|(map, e)| anyhow::anyhow!("{}", e.highlight(&map)))
181+
.map_err(|(map, e)| anyhow::anyhow!("{}", e.render(&map)))
182182
.context("failed to parse printed WIT")?;
183183

184184
// Check that the producer data got piped through properly

crates/wit-component/tests/interfaces.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn assert_print(resolve: &Resolve, pkg_id: PackageId, path: &Path, is_dir: bool)
9999
assert_output(&expected, &output)?;
100100

101101
UnresolvedPackageGroup::parse("foo.wit", &output)
102-
.map_err(|(map, e)| anyhow::anyhow!("{}", e.highlight(&map)))
102+
.map_err(|(map, e)| anyhow::anyhow!("{}", e.render(&map)))
103103
.context("failed to parse printed output")?;
104104
Ok(())
105105
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ impl ParseError {
9090
&mut self.0
9191
}
9292

93-
/// Format this error with source context (file:line:col + snippet)
94-
pub fn highlight(&self, source_map: &SourceMap) -> String {
93+
/// Renders this error with source context (file:line:col + snippet).
94+
///
95+
/// `source_map` must be the map this error's spans are valid in.
96+
pub fn render(&self, source_map: &SourceMap) -> String {
9597
let e = self.kind();
9698
source_map
9799
.highlight_span(e.span(), e)

crates/wit-parser/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,23 @@ pub fn validate_id(s: &str) -> anyhow::Result<()> {
7070
/// Renders an [`anyhow::Error`] chain produced by this crate, substituting
7171
/// snippet-bearing output for any [`ResolveError`] or [`ParseError`] layers.
7272
///
73-
/// For each layer in the chain, this calls [`ResolveError::highlight`] or
74-
/// [`ParseError::highlight`] to format typed errors with file/line/column and
73+
/// For each layer in the chain, this calls [`ResolveError::render`] or
74+
/// [`ParseError::render`] to format typed errors with file/line/column and
7575
/// a source snippet. Other layers are formatted via their [`fmt::Display`]
7676
/// impl. Layers are joined with `": "`, matching `format!("{err:#}")`.
7777
///
7878
/// `source_map` must be the [`SourceMap`] in which every typed error's spans
7979
/// are valid; combining typed errors from different source maps in one chain
80-
/// is unsupported.
80+
/// is unsupported. For errors from [`Resolve`] methods, prefer
81+
/// [`Resolve::render_error`].
8182
#[cfg(feature = "std")]
8283
pub fn render_anyhow_error(err: &anyhow::Error, source_map: &SourceMap) -> String {
8384
err.chain()
8485
.map(|layer| {
8586
if let Some(re) = layer.downcast_ref::<ResolveError>() {
86-
re.highlight(source_map)
87+
re.render(source_map)
8788
} else if let Some(pe) = layer.downcast_ref::<ParseError>() {
88-
pe.highlight(source_map)
89+
pe.render(source_map)
8990
} else {
9091
layer.to_string()
9192
}
@@ -328,7 +329,7 @@ impl UnresolvedPackageGroup {
328329
///
329330
/// On failure the constructed [`SourceMap`] is returned alongside the
330331
/// typed [`ParseError`] so the caller can render a snippet via
331-
/// [`ParseError::highlight`] or merge the source map elsewhere.
332+
/// [`ParseError::render`] or merge the source map elsewhere.
332333
#[cfg(feature = "std")]
333334
pub fn parse(
334335
path: impl AsRef<Path>,
@@ -350,7 +351,7 @@ impl UnresolvedPackageGroup {
350351
let mut map = SourceMap::default();
351352
map.push_dir(path.as_ref())?;
352353
map.parse().map_err(|(map, e)| {
353-
let rendered = e.highlight(&map);
354+
let rendered = e.render(&map);
354355
anyhow::Error::from(e).context(rendered)
355356
})
356357
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ impl ResolveError {
129129
&mut self.0
130130
}
131131

132-
/// Format this error with source context (file:line:col + snippet).
133-
pub fn highlight(&self, source_map: &SourceMap) -> String {
132+
/// Renders this error with source context (file:line:col + snippet).
133+
///
134+
/// `source_map` must be the map this error's spans are valid in.
135+
pub fn render(&self, source_map: &SourceMap) -> String {
134136
let e = self.kind();
135137
let msg = e.to_string();
136138
match e {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,13 @@ impl Resolve {
441441
self.source_map.render_location(span)
442442
}
443443

444+
/// Renders an error returned by this [`Resolve`]'s `push_*` methods with
445+
/// source context (file:line:col + snippet).
446+
#[cfg(feature = "std")]
447+
pub fn render_error(&self, err: &anyhow::Error) -> String {
448+
crate::render_anyhow_error(err, &self.source_map)
449+
}
450+
444451
pub fn all_bits_valid(&self, ty: &Type) -> bool {
445452
match ty {
446453
Type::U8
@@ -5928,7 +5935,7 @@ interface iface {
59285935
};
59295936
let mut resolve = Resolve::default();
59305937
let err = resolve.push_groups(a, Vec::from([b])).unwrap_err();
5931-
let msg = err.highlight(&resolve.source_map);
5938+
let msg = err.render(&resolve.source_map);
59325939
assert!(
59335940
msg.contains("file:///"),
59345941
"cycle error should contain a file URI, got: {msg}"

crates/wit-parser/tests/all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Runner {
8787
"some generic platform-agnostic error message",
8888
);
8989
}
90-
render_anyhow_error(&e, &resolve.source_map)
90+
resolve.render_error(&e)
9191
}
9292
}
9393
} else {

crates/wit-parser/tests/downcast.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ fn push_str_parse_error_is_downcastable() {
6969
.expect_err("expected parse to fail");
7070
let pe = parse_error(&err);
7171
// Spans must be valid against `resolve.source_map` after the failure-path
72-
// merge, so highlighting against it should not panic and should include
72+
// merge, so rendering against it should not panic and should include
7373
// the file and line.
74-
let rendered = pe.highlight(&resolve.source_map);
74+
let rendered = pe.render(&resolve.source_map);
7575
assert!(
7676
rendered.contains("test.wit:2:"),
77-
"expected highlighted error to reference test.wit:2:..., got:\n{rendered}"
77+
"expected rendered error to reference test.wit:2:..., got:\n{rendered}"
7878
);
7979
assert!(
8080
matches!(pe.kind(), ParseErrorKind::Syntax { .. }),
@@ -103,10 +103,10 @@ fn parse_error_spans_are_adjusted_after_earlier_pushes() {
103103
// `resolve.source_map` because `first.wit` was merged before it, so this
104104
// only points at the right file and line if the error's spans were
105105
// adjusted during the failure-path merge.
106-
let rendered = pe.highlight(&resolve.source_map);
106+
let rendered = pe.render(&resolve.source_map);
107107
assert!(
108108
rendered.contains("second.wit:2:"),
109-
"expected highlighted error to reference second.wit:2:..., got:\n{rendered}"
109+
"expected rendered error to reference second.wit:2:..., got:\n{rendered}"
110110
);
111111
assert!(
112112
rendered.contains("not-a-keyword"),

src/bin/wasm-tools/component.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -967,12 +967,9 @@ impl WitOpts {
967967
if input.is_dir() {
968968
let mut resolve =
969969
WitResolve::resolve_with_features(&self.features, self.all_features);
970-
let (pkg_id, _) = resolve.push_dir(&input).map_err(|e| {
971-
anyhow::anyhow!(
972-
"{}",
973-
wit_parser::render_anyhow_error(&e, &resolve.source_map)
974-
)
975-
})?;
970+
let (pkg_id, _) = resolve
971+
.push_dir(&input)
972+
.map_err(|e| anyhow::anyhow!("{}", resolve.render_error(&e)))?;
976973
return Ok(DecodedWasm::WitPackage(resolve, pkg_id));
977974
}
978975
}
@@ -1029,12 +1026,9 @@ impl WitOpts {
10291026
};
10301027
let mut resolve =
10311028
WitResolve::resolve_with_features(&self.features, self.all_features);
1032-
let id = resolve.push_str(path, input).map_err(|e| {
1033-
anyhow::anyhow!(
1034-
"{}",
1035-
wit_parser::render_anyhow_error(&e, &resolve.source_map)
1036-
)
1037-
})?;
1029+
let id = resolve
1030+
.push_str(path, input)
1031+
.map_err(|e| anyhow::anyhow!("{}", resolve.render_error(&e)))?;
10381032
Ok(DecodedWasm::WitPackage(resolve, id))
10391033
}
10401034
}

src/wit.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,9 @@ impl WitResolve {
4242

4343
pub fn load(&self) -> Result<(Resolve, PackageId)> {
4444
let mut resolve = Self::resolve_with_features(&self.features, self.all_features);
45-
let (pkg_id, _) = resolve.push_path(&self.wit).map_err(|e| {
46-
anyhow::anyhow!(
47-
"{}",
48-
wit_parser::render_anyhow_error(&e, &resolve.source_map)
49-
)
50-
})?;
45+
let (pkg_id, _) = resolve
46+
.push_path(&self.wit)
47+
.map_err(|e| anyhow::anyhow!("{}", resolve.render_error(&e)))?;
5148
Ok((resolve, pkg_id))
5249
}
5350
}

0 commit comments

Comments
 (0)