[m-rev.] for post-commit review: make diagnostic about coerce fails give the reason

Peter Wang novalazy at gmail.com
Tue May 28 12:35:34 AEST 2024


On Mon, 27 May 2024 22:39:22 +1000 "Zoltan Somogyi" <zoltan.somogyi at runbox.com> wrote:
> For review by Peter.
> 
> Zoltan.

> Make diagnostics about coerce failures more detailed.
> 
> compiler/typecheck_errors.m:
>     After saying that the user is not allowed to coerce from type T1
>     to type T2, say *the reason* for this prohibition.
> 
> tests/invalid/coerce_infer.err_exp:
> tests/invalid/coerce_non_du.err_exp:
>     Expect updated diagnostics.

> diff --git a/compiler/typecheck_errors.m b/compiler/typecheck_errors.m
> index da1e89dc1..7bb6893a0 100644
> --- a/compiler/typecheck_errors.m
> +++ b/compiler/typecheck_errors.m
> @@ -241,12 +241,110 @@ report_invalid_coerce_from_to(ClauseContext, Context, TVarSet,
>      InClauseForPieces = in_clause_for_pieces(ClauseContext),
>      FromTypeStr = mercury_type_to_string(TVarSet, print_num_only, FromType),
>      ToTypeStr = mercury_type_to_string(TVarSet, print_num_only, ToType),
> +    OnlyDuPieces = [words("You can only coerce"),
> +        words("from one discriminated union type to another, and")],
> +    ( if FromTypeStr = ToTypeStr then
> +        describe_if_non_du_type(FromType, FromTypeNonDuPieces),
> +        (
> +            FromTypeNonDuPieces = [],
> +            CausePieces = [words("You cannot coerce")] ++
> +                color_as_incorrect([words("from a type to the same type.")])
> +        ;
> +            FromTypeNonDuPieces = [_ | _],
> +            CausePieces = OnlyDuPieces ++
> +                [quote(FromTypeStr), words("is a")] ++
> +                color_as_incorrect(FromTypeNonDuPieces ++ [suffix(".")]) ++
> +                [nl] ++
> +                [words("Also, you cannot coerce")] ++
> +                color_as_incorrect([words("from a type to the same type.")])
> +        )

Actually, you can coerce from a type to the same type. I have been
considering adding a warning about such redundant coercions, though.

That looks fine otherwise.

Peter


More information about the reviews mailing list