Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: schema resolver type value load attribute error messages #1010

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading