[m-rev.] for post-commit review: "did you mean" messages for singleton variables

Peter Wang novalazy at gmail.com
Mon Dec 9 18:15:25 AEDT 2024


On Mon, 09 Dec 2024 04:24:52 +1100 "Zoltan Somogyi" <zoltan.somogyi at runbox.com> wrote:
> Extend "did you mean" messages to singleton vars.

> diff --git a/compiler/error_spec.m b/compiler/error_spec.m
> index 87e0dd53f..713fd267a 100644
> --- a/compiler/error_spec.m
> +++ b/compiler/error_spec.m
> @@ -1096,13 +1096,16 @@ do_maybe_construct_did_you_mean_pieces(BaseName, CandidateNames,
>              BestNames, CloseEnoughBestNames),
>          CloseEnoughBestNames = [_ | _]
>      then
> -        % For hand-written code, having more than ten names
> -        % equally close to BaseName should be vanishingly rare,
> -        % so the limit we impose here should not matter.
> -        % But programs that automatically generate Mercury code
> -        % may use naming schemes that make such occurrences
> -        % much more likely, and for these, avoiding the generation
> -        % of far-too-long error messages may be important.
> +        BaseNameChars = string.to_char_list(BaseName),
> +        list.map(char.to_lower, BaseNameChars, BaseNameLowerChars),

Could use string.to_lower.

> +        is_any_suggestion_same_but_for_case(BaseNameLowerChars,
> +            CloseEnoughBestNames, SameButForCase),
> +        % For hand-written code, having more than ten names equally close
> +        % to BaseName should be vanishingly rare, so the limit we impose here
> +        % should not matter. But programs that generate Mercury code
> +        % automatically may use naming schemes that make such occurrences
> +        % much more likely, and for these, avoiding the generation of
> +        % far-too-long error messages may be important.
>          list.split_upto(10, CloseEnoughBestNames,
>              SuggestedNames0, NonSuggestedNames),
>          (

> @@ -1165,6 +1180,25 @@ name_is_close_enough(Cost, Query, QueryLenU, Name) :-
>      ),
>      Cost =< MaxAcceptableCost.
>  
> +:- type same_but_for_case
> +    --->    all_have_non_case_difference
> +    ;       some_have_only_case_difference.
> +
> +:- pred is_any_suggestion_same_but_for_case(list(char)::in, list(string)::in,
> +    same_but_for_case::out) is det.
> +
> +is_any_suggestion_same_but_for_case(_, [], all_have_non_case_difference).
> +is_any_suggestion_same_but_for_case(BaseNameLowerChars, [Name | Names],
> +        SameButForCase) :-
> +    NameChars = string.to_char_list(Name),
> +    list.map(char.to_lower, NameChars, NameLowerChars),
> +    ( if BaseNameLowerChars = NameLowerChars then

Could use string.compare_ignore_case_ascii.

> +        SameButForCase = some_have_only_case_difference
> +    else
> +        is_any_suggestion_same_but_for_case(BaseNameLowerChars, Names,
> +            SameButForCase)
> +    ).

That looks fine.

Peter


More information about the reviews mailing list