Skip to content

Commit

Permalink
fix: schema resolver type value load attribute error messages
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Jan 26, 2024
1 parent a54cf0c commit 8bf7da8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
19 changes: 13 additions & 6 deletions kclvm/sema/src/resolver/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -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) {
(
Expand Down Expand Up @@ -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::<Vec<String>>();
// Get all the attributes of the schema.
let attrs = if schema_ty.is_instance {
schema_ty.attrs.keys().cloned().collect::<Vec<String>>()
} else {
SCHEMA_MEMBER_FUNCTIONS
.iter()
.map(|s| s.to_string())
.collect::<Vec<String>>()
};
let suggs = suggestions::provide_suggestions(attr, &attrs);
if suggs.len() > 0 {
suggestion = format!(", did you mean '{:?}'?", suggs);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
schema Data:
name: str
type: str

data = Data.name
1 change: 1 addition & 0 deletions kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8bf7da8

Please sign in to comment.