Skip to content

Commit

Permalink
Fix second unreachable
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniosarosi committed Dec 18, 2024
1 parent 7f92dae commit 2253947
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ typpe Two = float
// Unknown identifier.
type Three = i

// Unknown identifier in union.
type Four = int | string | b

// error: Error validating: Unexpected keyword used in assignment: typpe
// --> class/invalid_type_aliases.baml:9
// |
Expand All @@ -29,3 +32,9 @@ type Three = i
// 11 | // Unknown identifier.
// 12 | type Three = i
// |
// error: Error validating: Type alias points to unknown identifier `b`
// --> class/invalid_type_aliases.baml:15
// |
// 14 | // Unknown identifier in union.
// 15 | type Four = int | string | b
// |
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 @@ -518,7 +518,11 @@ fn visit_type_alias<'db>(
};

let Some(top_id) = ctx.names.tops.get(&string_id) else {
unreachable!("Alias name `{ident}` is not registered in the context");
ctx.push_error(DatamodelError::new_validation_error(
&format!("Type alias points to unknown identifier `{ident}`"),
item.span().clone(),
));
return;
};

// Add alias to the graph.
Expand Down
74 changes: 73 additions & 1 deletion engine/baml-schema-wasm/tests/test_file_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ test Two {
}

#[wasm_bindgen_test]
fn test_type_alias() {
fn test_type_alias_pointing_to_unknown_identifier() {
wasm_logger::init(wasm_logger::Config::new(log::Level::Info));
let sample_baml_content = r##"
type Foo = i
Expand Down Expand Up @@ -265,4 +265,76 @@ test Two {
// .unwrap()
// );
}

#[wasm_bindgen_test]
fn test_type_alias_pointing_to_union_with_unknown_identifier() {
wasm_logger::init(wasm_logger::Config::new(log::Level::Info));
let sample_baml_content = r##"
type Foo = int | 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());

// TODO: Don't know how to build 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()
// );
}

#[wasm_bindgen_test]
fn test_type_alias_pointing_to_union_with_unknown_identifier_in_union() {
wasm_logger::init(wasm_logger::Config::new(log::Level::Info));
let sample_baml_content = r##"
type Four = int | string | b
"##;
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());

// TODO: Don't know how to build 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 2253947

Please sign in to comment.