From b6e166f44d813632b7d0f4ee5158fdc6839f7736 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 5 Jan 2025 16:12:13 +0100 Subject: [PATCH] Include `@name` when searching for possible matches to `name` in the error message. #1779 --- releasenotes.md | 1 + src/compiler/sema_name_resolution.c | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/releasenotes.md b/releasenotes.md index a342fe78c..f5977e273 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -18,6 +18,7 @@ - Add 'validation' setting and make dead code a warning. - Allow compile time `$foreach` iteration over constant Strings and bytes. - Improved error message when accessing `@private` from other modules #1769. +- Include `@name` when searching for possible matches to `name` in the error message. #1779 ### Fixes - Fix case trying to initialize a `char[*]*` from a String. diff --git a/src/compiler/sema_name_resolution.c b/src/compiler/sema_name_resolution.c index 31b674ecf..84c203516 100644 --- a/src/compiler/sema_name_resolution.c +++ b/src/compiler/sema_name_resolution.c @@ -546,10 +546,18 @@ static void find_closest(const char *name, int name_len, Decl **decls, int *coun { int best_distance = *best_distance_ref; int count = *count_ref; + bool starts_at = name[0] == '@'; + Decl *at_match = NULL; FOREACH(Decl *, decl, decls) { if (decl->visibility != VISIBLE_PUBLIC) continue; - int dist = damerau_levenshtein_distance(name, name_len, decl->name, strlen(decl->name)); + const char *decl_name = decl->name; + if (!starts_at && decl_name[0] == '@' && str_eq(&decl_name[1], name)) + { + at_match = decl; + continue; + } + int dist = damerau_levenshtein_distance(name, name_len, decl_name, strlen(decl_name)); if (dist < best_distance) { matches[0] = decl; @@ -562,6 +570,17 @@ static void find_closest(const char *name, int name_len, Decl **decls, int *coun matches[count++] = decl; } } + if (at_match) + { + if (count == 3) + { + matches[0] = at_match; + } + else + { + matches[count++] = at_match; + } + } *count_ref = count; *best_distance_ref = best_distance; }