Skip to content

Commit

Permalink
fix: schema resolver type value load attribute error messages (#1010)
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed Jan 26, 2024
1 parent 90f25ad commit 09aa50f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion kclvm/cmd/src/test_data/fuzz_match/main.k
Original file line number Diff line number Diff line change
Expand Up @@ -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`?
a = p.a # Error, attribute 'a' not found in `Person`, did you mean `aa`?
2 changes: 1 addition & 1 deletion kclvm/cmd/src/test_data/fuzz_match/main_unmatched.k
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ schema Person:
sd?: int

p = Person {}
a = p.a # Error, attribute 'a' not found in schema `Person`
a = p.a # Error, attribute 'a' not found in `Person`
5 changes: 2 additions & 3 deletions kclvm/cmd/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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\"]'?"))
}
}
}
Expand All @@ -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'"))
}
}
}
Expand Down
17 changes: 12 additions & 5 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 @@ -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

0 comments on commit 09aa50f

Please sign in to comment.