Skip to content

Commit eca2759

Browse files
committed
feat(typecheck): enhance instanceof type checking with support for primitive types and enums
1 parent 98a7262 commit eca2759

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

engine/baml-compiler/src/thir/typecheck.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
///
1818
/// However, the current implementation is simple and ad-hoc, likely wrong
1919
/// in several places. Bidirectional typing is the target.
20-
use std::{borrow::Cow, sync::Arc};
20+
use std::{borrow::Cow, str::FromStr, sync::Arc};
2121

2222
use baml_types::{
2323
ir_type::{ArrowGeneric, TypeIR},
2424
BamlMap, BamlMediaType, BamlValueWithMeta, TypeValue,
2525
};
26+
use baml_vm::types::Type;
2627
use internal_baml_ast::ast::WithSpan;
2728
use internal_baml_diagnostics::{DatamodelError, Diagnostics, Span};
2829

@@ -1371,6 +1372,7 @@ pub fn typecheck_expression(
13711372
"image" | "audio" | "video" | "pdf" | "baml" => {}
13721373

13731374
cls if context.classes.contains_key(cls) => {}
1375+
p if TypeValue::from_str(p).is_ok() => {}
13741376

13751377
_ => {
13761378
diagnostics.push_error(DatamodelError::new_validation_error(
@@ -2487,17 +2489,22 @@ pub fn typecheck_expression(
24872489
thir::Expr::Var(name, _) => {
24882490
if context.classes.get(name).is_some() {
24892491
Some(TypeIR::bool())
2492+
} else if context.enums.get(name).is_some() {
2493+
Some(TypeIR::bool())
2494+
} else if TypeValue::from_str(name).is_ok() {
2495+
Some(TypeIR::bool())
24902496
} else {
2497+
// TODO: Check type aliases (may be recursive)
24912498
diagnostics.push_error(DatamodelError::new_validation_error(
2492-
&format!("Class {name} not found"),
2499+
&format!("Type {name} not found"),
24932500
span.clone(),
24942501
));
24952502
None
24962503
}
24972504
}
24982505
_ => {
24992506
diagnostics.push_error(DatamodelError::new_validation_error(
2500-
"Invalid binary operation (instanceof): right operand must be a class",
2507+
"Invalid binary operation (instanceof): right operand must be a type",
25012508
span.clone(),
25022509
));
25032510
None

integ-tests/baml_src/test-files/vm/expr_funcs.baml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ class DummyJsonTodo {
154154
userId int
155155
}
156156

157+
enum DummyEnum {
158+
POSITIVE
159+
NEGATIVE
160+
NEUTRAL
161+
}
162+
157163
function ExecFetchAs(url: string) -> DummyJsonTodo {
158164
let todo = baml.fetch_as<DummyJsonTodo>(url);
159165

@@ -165,3 +171,15 @@ test Fib5() {
165171
args { n 5 }
166172
@@assert( {{ this == 5 }} )
167173
}
174+
175+
function InstanceofWithPrimitives(n: int) -> bool {
176+
n instanceof int
177+
}
178+
179+
function InstanceofWithClasses(dummy: DummyJsonTodo) -> bool {
180+
dummy instanceof DummyJsonTodo
181+
}
182+
183+
function InstanceofWithEnums(e: DummyEnum) -> bool {
184+
e instanceof DummyEnum
185+
}

0 commit comments

Comments
 (0)