Skip to content

Commit dd4cd22

Browse files
committed
feat: improve transpiler errors
1 parent 1afeef1 commit dd4cd22

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

src/transpile/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub enum TranspileError {
1919
#[error("{message}")]
2020
UnsupportedType {
2121
message: String,
22+
ty: String,
2223
#[source_code]
2324
src: String,
2425
#[label = "{message}"]
@@ -28,6 +29,7 @@ pub enum TranspileError {
2829
#[error("{message}")]
2930
UnsupportedExpr {
3031
message: String,
32+
expr: String,
3133
#[source_code]
3234
src: String,
3335
#[label = "{message}"]

src/transpile/expr.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ pub(crate) fn expr_span<'de>(expr: &Expr<'de>) -> Option<crate::SourceSpan<'de>>
1717
}
1818

1919
/// Build a [`TranspileError::UnsupportedExpr`] from an expression.
20-
fn unsupported_from_expr(message: &str, expr: &Expr<'_>) -> TranspileError {
20+
pub(crate) fn unsupported_from_expr(message: &str, expr: &Expr<'_>) -> TranspileError {
2121
match expr_span(expr) {
2222
Some(span) => TranspileError::UnsupportedExpr {
2323
message: message.to_owned(),
24+
expr: format!("{expr:?}"),
2425
src: span.full_source().to_owned(),
2526
err_span: span.into(),
2627
},
2728
None => TranspileError::UnsupportedExpr {
2829
message: message.to_owned(),
30+
expr: format!("{expr:?}"),
2931
src: String::new(),
3032
err_span: miette::SourceSpan::new(0.into(), 0),
3133
},
@@ -43,7 +45,8 @@ impl<'de> Transpile for Expr<'de> {
4345
Expr::Lit(lit) => {
4446
let rust_expr: syn::Expr = syn::parse_str(lit.span.src()).map_err(|_| {
4547
TranspileError::UnsupportedExpr {
46-
message: format!("cannot parse literal `{}`", lit.span.src()),
48+
message: "cannot parse literal".to_owned(),
49+
expr: lit.span.src().to_owned(),
4750
src: lit.span.full_source().to_owned(),
4851
err_span: lit.span.into(),
4952
}

src/transpile/ty.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,10 @@ impl TypeMapper {
106106
let inner = self.map_type(&a.element)?;
107107
match &a.size {
108108
Some(Expr::Lit(lit)) if lit.kind == LitKind::Integer => {
109-
let n: usize = lit.span.src().parse().map_err(|_| {
110-
TranspileError::UnsupportedType {
111-
message: format!("invalid array size literal `{}`", lit.span.src()),
112-
src: lit.span.full_source().to_owned(),
113-
err_span: lit.span.into(),
114-
}
115-
})?;
109+
let n: usize =
110+
lit.span.src().parse().map_err(|_| {
111+
unsupported_from_type("invalid array size literal", ty)
112+
})?;
116113
let lit_n =
117114
syn::LitInt::new(&n.to_string(), proc_macro2::Span::call_site());
118115
Ok(syn::parse_quote!([#inner; #lit_n]))
@@ -159,11 +156,10 @@ impl TypeMapper {
159156
}
160157
Ok(result)
161158
}
162-
Type::Auto(a) => Err(TranspileError::UnsupportedType {
163-
message: "auto type cannot be mapped to Rust".to_owned(),
164-
src: a.span.full_source().to_owned(),
165-
err_span: a.span.into(),
166-
}),
159+
Type::Auto(_) => Err(unsupported_from_type(
160+
"auto type cannot be mapped to Rust",
161+
ty,
162+
)),
167163
Type::Decltype(_) => Err(unsupported_from_type(
168164
"decltype cannot be mapped to Rust",
169165
ty,
@@ -271,11 +267,13 @@ fn unsupported_from_type(message: &str, ty: &Type<'_>) -> TranspileError {
271267
match type_span(ty) {
272268
Some(span) => TranspileError::UnsupportedType {
273269
message: message.to_owned(),
270+
ty: format!("{ty:?}"),
274271
src: span.full_source().to_owned(),
275272
err_span: span.into(),
276273
},
277274
None => TranspileError::UnsupportedType {
278275
message: message.to_owned(),
276+
ty: format!("{ty:?}"),
279277
src: String::new(),
280278
err_span: miette::SourceSpan::new(0.into(), 0),
281279
},
@@ -355,6 +353,7 @@ impl<'de> Transpile for ItemStatic<'de> {
355353
.as_ref()
356354
.ok_or_else(|| TranspileError::UnsupportedExpr {
357355
message: "Rust statics require an initializer".to_owned(),
356+
expr: "missing initializer".to_owned(),
358357
src: name.span.full_source().to_owned(),
359358
err_span: name.span.into(),
360359
})?;

0 commit comments

Comments
 (0)