Files
mercury/tests/valid/preferred_switch_var.m
Zoltan Somogyi 9f7d8305a6 Prefer switching on the named variable in require_switch_* scopes.
compiler/switch_detection.m:
    As above.

tests/valid/preferred_switch_var.m:
    A new test case. With the compiler before this change, it did not compile,
    because

    (a) switch detection transformed the disjunction in the
        "require_switch_arms_semidet [T]" scope into a switch on U instead
        of T, because the switch on U is cannot_fail while the switch on T
        is can_fail, and

    (b) having a switch on U inside the "require_switch_arms_semidet [T]" scope
        got an error about the goal not being a switch on T.

tests/valid/Mmakefile:
    Enable the new test case.
2016-09-01 07:09:34 +10:00

58 lines
1.4 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
%
% The following program tests whether the presence of a require_switch_arms_X
% scope can get the compiler to recognize the switch inside the scope as
% being an incomplete switch on the named variable, even though it could
% also recognize it as a *complete* switch on another variable.
%
% Normally, switch detection prefers complete switches to incomplete ones,
% but a scope that programmers use to specify the variable that they *expect*
% the switch to be switch on should trump that.
%
%---------------------------------------------------------------------------%
:- module preferred_switch_var.
:- interface.
:- type t
---> t1
; t2
; t3
; t4
; t5
; t6
; t7
; t8.
:- type u
---> u1
; u2.
:- pred p(t::in, u::in, int::out) is semidet.
%---------------------------------------------------------------------------%
:- implementation.
p(T, U, N) :-
require_switch_arms_semidet [T]
(
T = t1,
U = u1,
N = 11
;
T = t2,
U = u2,
N = 22
;
T = t3,
U = u1,
N = 31
;
T = t4,
U = u2,
N = 42
).