Skip to content

Commit 8d66b80

Browse files
authored
deeply typecheck class constructors (#2000)
<!-- ELLIPSIS_HIDDEN --> > [!IMPORTANT] > Enhance type checking for nested fields in class constructors and add corresponding tests. > > - **Behavior**: > - In `expr_typecheck.rs`, `typecheck_in_context` now checks nested fields in `Expr::ClassConstructor`. > - Adds a call to `typecheck_in_context` for each field value in class constructors. > - **Tests**: > - Adds `constructors_nested.baml` to test nested class constructor type checking. > - Verifies type mismatch error for incorrect nested field types. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup> for b6d0603. You can [customize](https://app.ellipsis.dev/BoundaryML/settings/summaries) this summary. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
1 parent fb87426 commit 8d66b80

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

engine/baml-lib/baml-core/src/validate/validation_pipeline/validations/expr_typecheck.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,12 @@ pub fn typecheck_in_context(
250250
meta.0.clone(),
251251
));
252252
}
253+
typecheck_in_context(ir, diagnostics, typing_context, field_value)?;
253254
}
254255
}
255256
}
256257
}
258+
257259
let spread_type = spread.as_ref().and_then(|s| s.meta().1.clone());
258260
if !compatible_as_subtype(ir, &meta.1, &spread_type) {
259261
diagnostics.push_error(DatamodelError::new_validation_error(
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Foo {
2+
bar Bar
3+
}
4+
5+
class Bar {
6+
name string
7+
}
8+
9+
let x = Foo { bar: Bar { name: 10 }};
10+
11+
// error: Error validating: Type mismatch in class constructor
12+
// --> expr/constructors_nested.baml:9
13+
// |
14+
// 8 |
15+
// 9 | let x = Foo { bar: Bar { name: 10 }};
16+
// |

0 commit comments

Comments
 (0)