mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-20 03:43:51 +00:00
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.
58 lines
1.4 KiB
Mathematica
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
|
|
).
|