Skip to content

Commit

Permalink
Disallow coercion of sets into multidimensional arrays
Browse files Browse the repository at this point in the history
Fixes #656
  • Loading branch information
cyderize committed Mar 28, 2023
1 parent 029e647 commit c522052
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Bug fixes:
- Fix handling of domains for tuple and record types.
- Fix index set checking for tuples and records which contain arrays.
- Fix the handling of domain and index set expressions in aliases.
- Fix incorrect coercion of sets into multidimensional arrays (:bugref:`656`).

.. _v2.7.0:

Expand Down
7 changes: 6 additions & 1 deletion lib/typecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1447,13 +1447,18 @@ KeepAlive add_coercion(EnvI& env, Model* m, Expression* e, const Type& funarg_t)
}
GCLock lock;
Call* c = nullptr;
if (e->type().dim() == 0 && funarg_t.dim() != 0) {
if (e->type().isSet() && funarg_t.dim() != 0) {
if (e->type().isvar()) {
throw TypeError(env, e->loc(), "cannot coerce var set into array");
}
if (e->type().isOpt()) {
throw TypeError(env, e->loc(), "cannot coerce opt set into array");
}
if (funarg_t.dim() > 1) {
std::stringstream ss;
ss << "cannot coerce set into " << funarg_t.dim() << "-dimensional array";
throw TypeError(env, e->loc(), ss.str());
}
std::vector<Expression*> set2a_args(1);
set2a_args[0] = e;
Call* set2a = Call::a(e->loc(), ASTString("set2array"), set2a_args);
Expand Down
10 changes: 10 additions & 0 deletions tests/spec/unit/regression/github_656.mzn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/***
!Test
solvers:
- gecode
expected: !Error
regex: .*cannot coerce set into 2-dimensional array.*
***/

% Previously sets were incorrectly allowed to coerce to multidimensional arrays.
output [show2d(1..2)];

0 comments on commit c522052

Please sign in to comment.