Skip to content

Commit

Permalink
Fix reachable unreachable... XD
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniosarosi committed Dec 11, 2024
1 parent 511b8ea commit a912fc9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type One = int
// Unexpected keyword.
typpe Two = float

// Unknown identifier.
type Three = i

// error: Error validating: Unexpected keyword used in assignment: typpe
// --> class/invalid_type_aliases.baml:9
// |
Expand All @@ -20,3 +23,9 @@ typpe Two = float
// 5 | // Already existing name.
// 6 | type One = int
// |
// error: Error validating: Type alias points to unknown identifier `i`
// --> class/invalid_type_aliases.baml:12
// |
// 11 | // Unknown identifier.
// 12 | type Three = i
// |
6 changes: 5 additions & 1 deletion engine/baml-lib/parser-database/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,11 @@ fn visit_type_alias<'db>(
match item {
FieldType::Symbol(_, ident, _) => {
let Some(string_id) = ctx.interner.lookup(ident.name()) else {
unreachable!("Visiting alias `{ident}` that does not exist in the interner");
ctx.push_error(DatamodelError::new_validation_error(
&format!("Type alias points to unknown identifier `{ident}`"),
item.span().clone(),
));
return;
};

let Some(top_id) = ctx.names.tops.get(&string_id) else {
Expand Down
35 changes: 35 additions & 0 deletions engine/baml-schema-wasm/tests/test_file_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,39 @@ test Two {

assert!(diagnostics.errors().is_empty());
}

#[wasm_bindgen_test]
fn test_type_alias() {
wasm_logger::init(wasm_logger::Config::new(log::Level::Info));
let sample_baml_content = r##"
type Foo = i
"##;
let mut files = HashMap::new();
files.insert("error.baml".to_string(), sample_baml_content.to_string());
let files_js = to_value(&files).unwrap();
let project = WasmProject::new("baml_src", files_js)
.map_err(JsValue::from)
.unwrap();

let env_vars = [("OPENAI_API_KEY", "12345")]
.iter()
.cloned()
.collect::<HashMap<_, _>>();
let env_vars_js = to_value(&env_vars).unwrap();

let Err(js_error) = project.runtime(env_vars_js) else {
panic!("Expected error, got Ok");
};

assert!(js_error.is_object());

assert_eq!(
js_error,
serde_wasm_bindgen::to_value::<HashMap<String, Vec<String>>>(&HashMap::from_iter([(
"all_files".to_string(),
vec!["error.baml".to_string()]
)]))
.unwrap()
);
}
}

0 comments on commit a912fc9

Please sign in to comment.