From 5337dbe23bc19dca99ad93e59f0019e895865184 Mon Sep 17 00:00:00 2001 From: abdul124 Date: Fri, 30 Aug 2024 18:04:17 +0800 Subject: [PATCH] core/toplevel: add python-like error messages for class definition --- nac3core/src/toplevel/type_annotation.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/nac3core/src/toplevel/type_annotation.rs b/nac3core/src/toplevel/type_annotation.rs index a3af0b7f7..e1b20eb5d 100644 --- a/nac3core/src/toplevel/type_annotation.rs +++ b/nac3core/src/toplevel/type_annotation.rs @@ -100,7 +100,13 @@ pub fn parse_ast_to_type_annotation_kinds( Ok(TypeAnnotation::CustomClass { id: PrimDef::Exception.id(), params: Vec::default() }) } else if let Ok(obj_id) = resolver.get_identifier_def(*id) { let type_vars = { - let def_read = top_level_defs[obj_id.0].try_read(); + let Some(top_level_def) = top_level_defs.get(obj_id.0) else { + return Err(HashSet::from([format!( + "NameError: name '{id}' is not defined (at {})", + expr.location + )])); + }; + let def_read = top_level_def.try_read(); if let Some(def_read) = def_read { if let TopLevelDef::Class { type_vars, .. } = &*def_read { type_vars.clone() @@ -155,12 +161,17 @@ pub fn parse_ast_to_type_annotation_kinds( } let obj_id = resolver.get_identifier_def(*id)?; let type_vars = { - let def_read = top_level_defs[obj_id.0].try_read(); + let Some(top_level_def) = top_level_defs.get(obj_id.0) else { + return Err(HashSet::from([format!( + "NameError: name '{id}' is not defined (at {})", + expr.location + )])); + }; + let def_read = top_level_def.try_read(); if let Some(def_read) = def_read { let TopLevelDef::Class { type_vars, .. } = &*def_read else { unreachable!("must be class here") }; - type_vars.clone() } else { locked.get(&obj_id).unwrap().clone()