Skip to content

Commit

Permalink
feat: add a suggestion for 'name not found' (#731)
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Sep 26, 2023
1 parent eeff00e commit 5642bb1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
18 changes: 17 additions & 1 deletion kclvm/sema/src/resolver/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,24 @@ impl<'ctx> Resolver<'ctx> {
match self.find_type_in_scope(name) {
Some(ty) => ty,
None => {
let mut suggestion = String::new();
let names = self
.scope
.borrow()
.all_usable_objects()
.keys()
.cloned()
.collect::<Vec<String>>();
let suggs = suggestions::provide_suggestions(name, &names);
if suggs.len() > 0 {
suggestion = format!(", did you mean '{:?}'?", suggs);
}
self.handler.add_compile_error(
&format!("name '{}' is not defined", name.replace('@', "")),
&format!(
"name '{}' is not defined{}",
name.replace('@', ""),
suggestion
),
range,
);
self.any_ty()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import sub as s1

main = s.sub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sub = "sub"
23 changes: 23 additions & 0 deletions kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,26 @@ fn test_system_package() {
.ty
.is_func());
}

#[test]
fn test_resolve_program_import_suggest() {
let sess = Arc::new(ParseSession::default());
let mut program = load_program(
sess.clone(),
&["./src/resolver/test_fail_data/not_found_suggest/main.k"],
None,
)
.unwrap();
let scope = resolve_program(&mut program);
assert_eq!(scope.handler.diagnostics.len(), 2);
let diag = &scope.handler.diagnostics[0];
assert_eq!(
diag.code,
Some(DiagnosticId::Error(ErrorKind::CompileError))
);
assert_eq!(diag.messages.len(), 1);
assert_eq!(
diag.messages[0].message,
"name 's' is not defined, did you mean '[\"s1\"]'?"
);
}

0 comments on commit 5642bb1

Please sign in to comment.