diff --git a/kclvm/sema/src/resolver/attr.rs b/kclvm/sema/src/resolver/attr.rs index 0892cf786..83420dbcd 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::*; @@ -64,7 +64,7 @@ impl<'ctx> Resolver<'ctx> { TypeKind::Union(_) => (true, self.any_ty()), TypeKind::Schema(schema_ty) => { let (result, schema_attr_ty) = self.schema_load_attr(schema_ty, attr); - if result { + if result && schema_ty.is_instance { (result, schema_attr_ty) } else if schema_ty.is_member_functions(attr) { ( @@ -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 diff --git a/kclvm/sema/src/resolver/test_fail_data/schema_type_value_attr.k b/kclvm/sema/src/resolver/test_fail_data/schema_type_value_attr.k new file mode 100644 index 000000000..e094d9b12 --- /dev/null +++ b/kclvm/sema/src/resolver/test_fail_data/schema_type_value_attr.k @@ -0,0 +1,5 @@ +schema Data: + name: str + type: str + +data = Data.name diff --git a/kclvm/sema/src/resolver/tests.rs b/kclvm/sema/src/resolver/tests.rs index b6da99b43..abc773113 100644 --- a/kclvm/sema/src/resolver/tests.rs +++ b/kclvm/sema/src/resolver/tests.rs @@ -153,6 +153,7 @@ fn test_resolve_program_fail() { "module_optional_select.k", "mutable_error_0.k", "mutable_error_1.k", + "schema_type_value_attr.k", "unique_key_error_0.k", "unique_key_error_1.k", "unmatched_index_sign_default_value.k",