Skip to content

Commit 0213c73

Browse files
authored
allow HSlice bounded by constants of distinct types (#19219) [backport:1.2]
When creating heterogenous slices of distinct types, the compiler does not initialize the internal type's `size` before accessing it. This then leads to this crash message: ``` compiler/int128.nim(594, 11) `false` masking only implemented for 1, 2, 4 and 8 bytes [AssertionError] ``` This patch initializes the `size` properly, fixing the problem.
1 parent 7e3da69 commit 0213c73

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

compiler/semfold.nim

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; idgen: IdGenerator; g: ModuleGraph): P
133133
of mCard: result = newIntNodeT(toInt128(nimsets.cardSet(g.config, a)), n, idgen, g)
134134
of mBitnotI:
135135
if n.typ.isUnsigned:
136-
result = newIntNodeT(bitnot(getInt(a)).maskBytes(int(n.typ.size)), n, idgen, g)
136+
result = newIntNodeT(bitnot(getInt(a)).maskBytes(int(getSize(g.config, n.typ))), n, idgen, g)
137137
else:
138138
result = newIntNodeT(bitnot(getInt(a)), n, idgen, g)
139139
of mLengthArray: result = newIntNodeT(lengthOrd(g.config, a.typ), n, idgen, g)
@@ -248,23 +248,23 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; idgen: IdGenerator; g: ModuleGraph): P
248248
of mBitorI, mOr: result = newIntNodeT(bitor(getInt(a), getInt(b)), n, idgen, g)
249249
of mBitxorI, mXor: result = newIntNodeT(bitxor(getInt(a), getInt(b)), n, idgen, g)
250250
of mAddU:
251-
let val = maskBytes(getInt(a) + getInt(b), int(n.typ.size))
251+
let val = maskBytes(getInt(a) + getInt(b), int(getSize(g.config, n.typ)))
252252
result = newIntNodeT(val, n, idgen, g)
253253
of mSubU:
254-
let val = maskBytes(getInt(a) - getInt(b), int(n.typ.size))
254+
let val = maskBytes(getInt(a) - getInt(b), int(getSize(g.config, n.typ)))
255255
result = newIntNodeT(val, n, idgen, g)
256256
# echo "subU: ", val, " n: ", n, " result: ", val
257257
of mMulU:
258-
let val = maskBytes(getInt(a) * getInt(b), int(n.typ.size))
258+
let val = maskBytes(getInt(a) * getInt(b), int(getSize(g.config, n.typ)))
259259
result = newIntNodeT(val, n, idgen, g)
260260
of mModU:
261-
let argA = maskBytes(getInt(a), int(a.typ.size))
262-
let argB = maskBytes(getInt(b), int(a.typ.size))
261+
let argA = maskBytes(getInt(a), int(getSize(g.config, a.typ)))
262+
let argB = maskBytes(getInt(b), int(getSize(g.config, a.typ)))
263263
if argB != Zero:
264264
result = newIntNodeT(argA mod argB, n, idgen, g)
265265
of mDivU:
266-
let argA = maskBytes(getInt(a), int(a.typ.size))
267-
let argB = maskBytes(getInt(b), int(a.typ.size))
266+
let argA = maskBytes(getInt(a), int(getSize(g.config, a.typ)))
267+
let argB = maskBytes(getInt(b), int(getSize(g.config, a.typ)))
268268
if argB != Zero:
269269
result = newIntNodeT(argA div argB, n, idgen, g)
270270
of mLeSet: result = newIntNodeT(toInt128(ord(containsSets(g.config, a, b))), n, idgen, g)

tests/slice/tdistinct.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type Foo = distinct uint64
2+
const slice = 0 ..< 42.Foo

0 commit comments

Comments
 (0)