From dbd9a8bad04f87e572fde1dccb078e958298f8e0 Mon Sep 17 00:00:00 2001 From: peefy Date: Fri, 26 Jan 2024 21:56:15 +0800 Subject: [PATCH] fix: schema resolver type value load attribute error messages Signed-off-by: peefy --- kclvm/cmd/src/test_data/fuzz_match/main.k | 2 +- .../src/test_data/fuzz_match/main_unmatched.k | 2 +- kclvm/cmd/src/tests.rs | 5 ++--- kclvm/sema/src/resolver/attr.rs | 17 ++++++++++++----- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/kclvm/cmd/src/test_data/fuzz_match/main.k b/kclvm/cmd/src/test_data/fuzz_match/main.k index 7cf458d62..1a5682818 100644 --- a/kclvm/cmd/src/test_data/fuzz_match/main.k +++ b/kclvm/cmd/src/test_data/fuzz_match/main.k @@ -3,4 +3,4 @@ schema Person: aaa?: int p = Person {} -a = p.a # Error, attribute 'a' not found in schema `Person`, did you mean `aa`? \ No newline at end of file +a = p.a # Error, attribute 'a' not found in `Person`, did you mean `aa`? \ No newline at end of file diff --git a/kclvm/cmd/src/test_data/fuzz_match/main_unmatched.k b/kclvm/cmd/src/test_data/fuzz_match/main_unmatched.k index 105956175..f802a7d70 100644 --- a/kclvm/cmd/src/test_data/fuzz_match/main_unmatched.k +++ b/kclvm/cmd/src/test_data/fuzz_match/main_unmatched.k @@ -3,4 +3,4 @@ schema Person: sd?: int p = Person {} -a = p.a # Error, attribute 'a' not found in schema `Person` \ No newline at end of file +a = p.a # Error, attribute 'a' not found in `Person` \ No newline at end of file diff --git a/kclvm/cmd/src/tests.rs b/kclvm/cmd/src/tests.rs index 2348e62f0..d0c392ce5 100644 --- a/kclvm/cmd/src/tests.rs +++ b/kclvm/cmd/src/tests.rs @@ -587,8 +587,7 @@ fn test_error_message_fuzz_matched() { { Ok(_) => panic!("unreachable code."), Err(msg) => { - assert!(msg - .contains("attribute 'a' not found in schema 'Person', did you mean '[\"aa\"]'?")) + assert!(msg.contains("attribute 'a' not found in 'Person', did you mean '[\"aa\"]'?")) } } } @@ -608,7 +607,7 @@ fn test_error_message_fuzz_unmatched() { { Ok(_) => panic!("unreachable code."), Err(msg) => { - assert!(msg.contains("attribute 'a' not found in schema 'Person'")) + assert!(msg.contains("attribute 'a' not found in 'Person'")) } } } diff --git a/kclvm/sema/src/resolver/attr.rs b/kclvm/sema/src/resolver/attr.rs index 0892cf786..25d2dff38 100644 --- a/kclvm/sema/src/resolver/attr.rs +++ b/kclvm/sema/src/resolver/attr.rs @@ -4,7 +4,7 @@ use crate::builtin::system_module::{get_system_module_members, UNITS, UNITS_NUMB use crate::builtin::{get_system_member_function_ty, STRING_MEMBER_FUNCTIONS}; use crate::resolver::Resolver; use crate::ty::TypeKind::Schema; -use crate::ty::{DictType, ModuleKind, Type, TypeKind, TypeRef}; +use crate::ty::{DictType, ModuleKind, Type, TypeKind, TypeRef, SCHEMA_MEMBER_FUNCTIONS}; use kclvm_error::diagnostic::Range; use kclvm_error::*; @@ -119,10 +119,17 @@ impl<'ctx> Resolver<'ctx> { ("[missing name]", "".to_string()) } else { let mut suggestion = String::new(); - // Calculate the closests miss attributes. + // Calculate the closest miss attributes. if let Schema(schema_ty) = &obj.kind { - // Get all the attrbuets of the schema. - let attrs = schema_ty.attrs.keys().cloned().collect::>(); + // Get all the attributes of the schema. + let attrs = if schema_ty.is_instance { + schema_ty.attrs.keys().cloned().collect::>() + } else { + SCHEMA_MEMBER_FUNCTIONS + .iter() + .map(|s| s.to_string()) + .collect::>() + }; let suggs = suggestions::provide_suggestions(attr, &attrs); if suggs.len() > 0 { suggestion = format!(", did you mean '{:?}'?", suggs); @@ -133,7 +140,7 @@ impl<'ctx> Resolver<'ctx> { self.handler.add_type_error( &format!( - "attribute '{}' not found in schema '{}'{}", + "attribute '{}' not found in '{}'{}", attr, obj.ty_str(), suggestion