mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-27 07:14:20 +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.
45 lines
832 B
Mathematica
45 lines
832 B
Mathematica
:- module impure_method_impl.
|
|
:- interface.
|
|
|
|
:- import_module io.
|
|
|
|
:- pred main(io__state::di, io__state::uo) is det.
|
|
|
|
:- typeclass c(T) where [
|
|
pred m1(T::in, int::out) is det,
|
|
semipure pred m2(T::in, int::out) is det
|
|
].
|
|
|
|
:- type foo ---> foo.
|
|
|
|
:- semipure pred foo_m1(foo::in, int::out) is det.
|
|
:- impure pred foo_m2(foo::in, int::out) is det.
|
|
|
|
:- implementation.
|
|
|
|
:- instance c(foo) where [
|
|
pred(m1/2) is foo_m1,
|
|
pred(m2/2) is foo_m2
|
|
].
|
|
|
|
main -->
|
|
[].
|
|
|
|
:- pragma foreign_decl("C", "extern int foo_counter;").
|
|
:- pragma foreign_code("C", "int foo_counter = 0;").
|
|
|
|
:- pragma foreign_proc("C",
|
|
foo_m1(_F::in, Val::out),
|
|
[will_not_call_mercury, promise_semipure],
|
|
"
|
|
Val = foo_counter;
|
|
").
|
|
:- pragma foreign_proc("C",
|
|
foo_m2(_F::in, Val::out),
|
|
[will_not_call_mercury],
|
|
"
|
|
Val = foo_counter++;"
|
|
).
|
|
foo_m1(_, 0).
|
|
foo_m2(_, 0).
|