From 597eaa08730b38fe4d0051dd22f710d037699b68 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 f8b16f86..e3b29113 100644 --- a/nac3core/src/toplevel/type_annotation.rs +++ b/nac3core/src/toplevel/type_annotation.rs @@ -101,7 +101,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() @@ -156,12 +162,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()