From a912fc910d04fb01ab719fb41102a3b1acf57ccc Mon Sep 17 00:00:00 2001 From: Antonio Sarosi Date: Wed, 11 Dec 2024 19:47:47 +0100 Subject: [PATCH] Fix reachable unreachable... XD --- .../class/invalid_type_aliases.baml | 9 +++++ .../baml-lib/parser-database/src/types/mod.rs | 6 +++- .../tests/test_file_manager.rs | 35 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/engine/baml-lib/baml/tests/validation_files/class/invalid_type_aliases.baml b/engine/baml-lib/baml/tests/validation_files/class/invalid_type_aliases.baml index a8e4a7c3c..0a13664ba 100644 --- a/engine/baml-lib/baml/tests/validation_files/class/invalid_type_aliases.baml +++ b/engine/baml-lib/baml/tests/validation_files/class/invalid_type_aliases.baml @@ -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 // | @@ -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 +// | diff --git a/engine/baml-lib/parser-database/src/types/mod.rs b/engine/baml-lib/parser-database/src/types/mod.rs index 966757b3a..3fe3f46b8 100644 --- a/engine/baml-lib/parser-database/src/types/mod.rs +++ b/engine/baml-lib/parser-database/src/types/mod.rs @@ -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 { diff --git a/engine/baml-schema-wasm/tests/test_file_manager.rs b/engine/baml-schema-wasm/tests/test_file_manager.rs index 8de0e0743..ff0f8b1db 100644 --- a/engine/baml-schema-wasm/tests/test_file_manager.rs +++ b/engine/baml-schema-wasm/tests/test_file_manager.rs @@ -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::>(); + 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::from_iter([( + "all_files".to_string(), + vec!["error.baml".to_string()] + )])) + .unwrap() + ); + } }