plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
7d5b4f8
— Peter John
2026-11-10T11:25:09+05:30
wasm codegen: a codegen error points at the node it came from
compiler/wasm-codegen/src/context.rs
CHANGED
|
@@ -322,6 +322,23 @@ pub(crate) fn withGen<R>(f: impl FnOnce(&mut GenState) -> R) -> R {
|
|
|
322
322
|
pub(crate) struct ErrInfo {
|
|
323
323
|
/// File of the fn being compiled (`expr!` crash messages name it).
|
|
324
324
|
pub(crate) file: String,
|
|
325
|
+
/// File and span of the innermost expression or statement a codegen error came out of
|
|
326
|
+
/// (`noteErrorAt`); the error's diagnostic points there.
|
|
327
|
+
pub(crate) at: Option<(String, ast::Span)>,
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/// Notes `span` (in the fn being compiled) as where the codegen error now unwinding came
|
|
331
|
+
/// from, unless a node inside it already did. Codegen never recovers from an error, so
|
|
332
|
+
/// the first note is the failing node.
|
|
333
|
+
pub(crate) fn noteErrorAt(span: &ast::Span) {
|
|
334
|
+
if span.startLine == 0 {
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
withGen(|s| {
|
|
338
|
+
if s.err_info.at.is_none() {
|
|
339
|
+
s.err_info.at = Some((s.err_info.file.clone(), *span));
|
|
340
|
+
}
|
|
341
|
+
});
|
|
325
342
|
}
|
|
326
343
|
|
|
327
344
|
/// Records `file` as the source file of the fn about to be compiled (when known).
|
compiler/wasm-codegen/src/driver.rs
CHANGED
|
@@ -62,6 +62,7 @@ pub(crate) fn resetGenState(source: &ast::Source) {
|
|
|
62
62
|
*s = GenState {
|
|
63
63
|
err_info: ErrInfo {
|
|
64
64
|
file: source.file.clone(),
|
|
65
|
+
at: None,
|
|
65
66
|
},
|
|
66
67
|
..GenState::default()
|
|
67
68
|
}
|
compiler/wasm-codegen/src/expr.rs
CHANGED
|
@@ -45,6 +45,15 @@ pub(crate) fn compileExpr(
|
|
|
45
45
|
body: &mut Vec<u8>,
|
|
46
46
|
ctx: &LocalCtx,
|
|
47
47
|
state: &mut ModuleState,
|
|
48
|
+
) -> Result<(), String> {
|
|
49
|
+
compileExprNode(expr, body, ctx, state).inspect_err(|_| noteErrorAt(&expr.span))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
fn compileExprNode(
|
|
53
|
+
expr: &ast::Expr,
|
|
54
|
+
body: &mut Vec<u8>,
|
|
55
|
+
ctx: &LocalCtx,
|
|
56
|
+
state: &mut ModuleState,
|
|
48
57
|
) -> Result<(), String> {
|
|
49
58
|
match expr {
|
|
50
59
|
// A block `if` / `match` value: its statements run inline, the last value is the result.
|
compiler/wasm-codegen/src/inference.rs
CHANGED
|
@@ -93,9 +93,14 @@ pub(crate) fn localType(name: &str, ctx: &LocalCtx) -> Option<PlumType> {
|
|
|
93
93
|
|
|
94
94
|
/// Type-checks `source` through the pass list (`plum_driver::check`), returning the
|
|
95
95
|
/// per-expression types codegen needs.
|
|
96
|
-
///
|
|
96
|
+
/// A codegen failure as a diagnostic, at the node it came out of when that is known.
|
|
97
97
|
pub(crate) fn codegenDiag(message: String) -> Vec<Diagnostic> {
|
|
98
|
+
let at = withGen(|s| s.err_info.at.clone());
|
|
99
|
+
vec![match at {
|
|
100
|
+
Some((file, sp)) => Diagnostic::new(DiagKind::Codegen, file, sp.startLine, sp.startColumn, message)
|
|
101
|
+
.with_end(sp.endLine, sp.endColumn),
|
|
98
|
-
|
|
102
|
+
None => Diagnostic::bare(DiagKind::Codegen, message),
|
|
103
|
+
}]
|
|
99
104
|
}
|
|
100
105
|
|
|
101
106
|
/// The `PlumType` of a declared parameter, including `fn(...)`-typed params as `TFun`;
|
compiler/wasm-codegen/src/stmts.rs
CHANGED
|
@@ -346,6 +346,15 @@ pub(crate) fn compileStmt(
|
|
|
346
346
|
body: &mut Vec<u8>,
|
|
347
347
|
ctx: &LocalCtx,
|
|
348
348
|
state: &mut ModuleState,
|
|
349
|
+
) -> Result<(), String> {
|
|
350
|
+
compileStmtNode(stmt, body, ctx, state).inspect_err(|_| noteErrorAt(&stmt.span))
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
fn compileStmtNode(
|
|
354
|
+
stmt: &ast::Stmt,
|
|
355
|
+
body: &mut Vec<u8>,
|
|
356
|
+
ctx: &LocalCtx,
|
|
357
|
+
state: &mut ModuleState,
|
|
349
358
|
) -> Result<(), String> {
|
|
350
359
|
match stmt {
|
|
351
360
|
ast::Stmt { kind: ast::StmtKind::Assign(a), .. } => {
|
compiler/wasm-codegen/tests/wasm_codegen/codegen_tests.rs
CHANGED
|
@@ -267,6 +267,18 @@ fn lastCaseOfAnExhaustiveMatchIsNotTested() {
|
|
|
267
267
|
assert_eq!(plum_test_support::runInt("lastcase", src), 13);
|
|
268
268
|
}
|
|
269
269
|
|
|
270
|
+
/// A codegen error points at the node it came out of: here the interpolation on line 3.
|
|
271
|
+
#[test]
|
|
272
|
+
fn codegenErrorCarriesThePosition() {
|
|
273
|
+
let src = "fun main() -> Str =\n x := 1.5\n \"{x}\"\n";
|
|
274
|
+
let source = parse(src);
|
|
275
|
+
let ds = compile(&source, &CompileOptions::default()).err().expect("an error");
|
|
276
|
+
assert_eq!(ds.len(), 1);
|
|
277
|
+
assert!(ds[0].message.contains("Float"), "{:?}", ds[0]);
|
|
278
|
+
assert_eq!((ds[0].line, ds[0].column), (3, 3), "{:?}", ds[0]);
|
|
279
|
+
assert!(ds[0].end_line >= 3, "{:?}", ds[0]);
|
|
280
|
+
}
|
|
281
|
+
|
|
270
282
|
/// String and float literal patterns compile (`tests/core/literal_patterns.plum` runs them).
|
|
271
283
|
#[test]
|
|
272
284
|
fn matchStringAndFloatPatternsCompile() {
|