Files
mercury/tests/invalid/overload_resolution_preference.m
Zoltan Somogyi f32a26b4b3 Make the from_int method in enum cless a predicate.
library/enum.m:
    As above.

NEWS.md:
    Announce this breaking change.

library/bool.m:
library/char.m:
library/int.m:
library/term.m:
library/uint.m:
tests/hard_coded/deep_copy_exist.m:
tests/typeclasses/extract_typeinfo2.m:
    Conform to the change in enum.m.

compiler/pred_table.m:
    When typechecking leaves am ambiguity unresolved, a predicate
    in this module used to pick one of the candidate matches effectively
    at random to "resolve" it.

    After the change to enum.m, this led to a compiler abort when compiling
    uint.m. The ambiguity was between uint.from_int/2 and the new
    enum.from_int/2. The old algorithm picked enum.from_int/2, and tried
    to look up its class constraints in the current class context; this failed
    with an abort, because the current class context was empty.

    Fix this by choosing the match (or *a* match) with the fewest typeclass
    constraints to "resolve" any ambiguities.

tests/invalid/overload_resolution_preference.{m,err_exp}:
    A test case for the change to pred_table.m.

tests/invalid/Mmakefile:
    Enable the new test case.

tests/invalid_submodules/unresolved_overloading.err_exp:
    Update this expected output after the change to pred_table.m.
2024-08-09 21:43:03 +02:00

33 lines
963 B
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
%
% This is a regression test. It tests whether overload resolution will pick
% a match *with* typeclass constraints over a match *without* such constraints.
%
:- module overload_resolution_preference.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module enum.
:- import_module list.
:- import_module string.
:- import_module uint.
main(!IO) :-
% This call to from_int matches both enum.from_int/2 and uint.from_int/2.
% Having overload resolution pick enum.from_int/2 leads to a compiler
% abort, due to the class constraints on that predicate not being found.
( if from_int(42, U) then
io.format("U = %u\n", [u(U)], !IO)
else
io.write_string("from_int failed.\n", !IO)
).