mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-27 15:24:00 +00:00
Estimated hours taken: 6
Branches: main, release
Make it an error for the (promised) purity of a foreign clause to disagree
with the declared purity of the corresponding predicate or function
declaration. We only perform this check in the absence of a
promise_{pure,semipure} pragma for the predicate or function.
Previously this situation was sometimes picked up by purity analysis but not
in all cases. For example, if a predicate was declared impure but the
foreign_proc was promised pure it wasn't reported. In that particular case
it was a problem because if the foreign_proc did not have any outputs, then
simplify.m might have optimised its body away (which is how I noticed this).
compiler/add_pramga.m:
In the absence of promise_{pure,semipure} pragmas emit error messages
about mismatches between the declared purity of a procedure and the
(promised) purity of a foreign clause for it.
compiler/mode_errors.m:
Fix a typo in an error message: s/becaise/because/
compiler/purity.m:
Fix a bug reported by Ian. Inconsistent purity annotation were being
treated as both a warning and an error. Make it into an error.
library/private_builtin.m:
library/solutions.m:
Delete bogus purity promises from foreign_proc attributes reported by
the new check.
tests/invalid/Mmakefile:
tests/invalid/foreign_purity_mismatch.{m,err_exp}:
Test case for the new error.
compiler/simplify.m:
compiler/prog_io_pragma.m:
Fix some formatting.
tests/*/*:
Fix purity errors picked up by the new check.
62 lines
1.7 KiB
Mathematica
62 lines
1.7 KiB
Mathematica
:- module multimode_syntax.
|
|
:- interface.
|
|
:- import_module io.
|
|
|
|
:- pred main(state::di, state::uo) is det.
|
|
|
|
:- implementation.
|
|
|
|
main --> [].
|
|
|
|
:- func func0 = string.
|
|
:- mode func0 = out is det.
|
|
func0 = "func0 = out" :: out. % missing parentheses
|
|
|
|
:- func func1(int) = string.
|
|
:- mode func1(in) = out is det.
|
|
:- mode func1(out) = out is det.
|
|
func1(_::in) = "func1(in) = out". % missing mode annotation on return value
|
|
func1(0) = ("func1(out) = out" :: out). % missing mode annotation on argument
|
|
|
|
:- func func2(int, int) = string.
|
|
:- mode func2(in, in) = out is det.
|
|
func2(_::in, _::in) = (R::out) :-
|
|
R = "func2(in, in) = out".
|
|
func2(_::in, 0::out) = (R::out) :- % reference to undeclared mode
|
|
R = "func2(in, out) = out".
|
|
func2(0::out, _::in) = (R::out) :-
|
|
R = "func2(out, in) = out".
|
|
func2(0::out, 0::out) = (R::out) :-
|
|
R = "func2(out, out) = out".
|
|
|
|
:- func func2b(int, int) = string.
|
|
func2b(_::in, _::out) = (R::out) :- % another reference to undeclared mode
|
|
R = "func3(in, out) = out".
|
|
|
|
:- impure pred pred2b(int, int).
|
|
pred2b(_::in, 0::out) :- % another reference to undeclared mode
|
|
impure puts("func3(in, out) = out").
|
|
|
|
:- impure pred test2(int, int).
|
|
:- mode test2(in, in) is det.
|
|
:- mode test2(in, out) is det.
|
|
:- mode test2(out, in) is det.
|
|
:- mode test2(out, out) is det.
|
|
test2(_::in, _) :- % missing mode annotation on 2nd arg
|
|
impure puts("test2(in, in)").
|
|
test2(_, 0::out) :- % missing mode annotation on 1st arg
|
|
impure puts("test2(in, out)").
|
|
test2(0::out, _::in) :-
|
|
impure puts("test2(out, in)").
|
|
test2(0::out, 0::out) :-
|
|
impure puts("test2(out, out)").
|
|
|
|
:- impure pred puts(string::in) is det.
|
|
:- pragma foreign_proc("C",
|
|
puts(S::in),
|
|
[will_not_call_mercury],
|
|
"
|
|
puts(S);
|
|
").
|
|
puts(_).
|