Skip to content

Commit 8e695a5

Browse files
committed
allow HSlice bounded by constants of distinct types
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 faacd63 commit 8e695a5

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

compiler/semfold.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +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+
computeSizeAlign(g.config, n.typ)
136137
result = newIntNodeT(bitnot(getInt(a)).maskBytes(int(n.typ.size)), n, idgen, g)
137138
else:
138139
result = newIntNodeT(bitnot(getInt(a)), n, idgen, g)
@@ -248,21 +249,26 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; idgen: IdGenerator; g: ModuleGraph): P
248249
of mBitorI, mOr: result = newIntNodeT(bitor(getInt(a), getInt(b)), n, idgen, g)
249250
of mBitxorI, mXor: result = newIntNodeT(bitxor(getInt(a), getInt(b)), n, idgen, g)
250251
of mAddU:
252+
computeSizeAlign(g.config, n.typ)
251253
let val = maskBytes(getInt(a) + getInt(b), int(n.typ.size))
252254
result = newIntNodeT(val, n, idgen, g)
253255
of mSubU:
256+
computeSizeAlign(g.config, n.typ)
254257
let val = maskBytes(getInt(a) - getInt(b), int(n.typ.size))
255258
result = newIntNodeT(val, n, idgen, g)
256259
# echo "subU: ", val, " n: ", n, " result: ", val
257260
of mMulU:
261+
computeSizeAlign(g.config, n.typ)
258262
let val = maskBytes(getInt(a) * getInt(b), int(n.typ.size))
259263
result = newIntNodeT(val, n, idgen, g)
260264
of mModU:
265+
computeSizeAlign(g.config, a.typ)
261266
let argA = maskBytes(getInt(a), int(a.typ.size))
262267
let argB = maskBytes(getInt(b), int(a.typ.size))
263268
if argB != Zero:
264269
result = newIntNodeT(argA mod argB, n, idgen, g)
265270
of mDivU:
271+
computeSizeAlign(g.config, a.typ)
266272
let argA = maskBytes(getInt(a), int(a.typ.size))
267273
let argB = maskBytes(getInt(b), int(a.typ.size))
268274
if argB != Zero:

compiler/sizealignoffsetimpl.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ proc finish(arg: var OffsetAccum): int =
7373
result = align(arg.offset, arg.maxAlign) - arg.offset
7474
arg.offset += result
7575

76-
proc computeSizeAlign(conf: ConfigRef; typ: PType)
76+
proc computeSizeAlign*(conf: ConfigRef; typ: PType)
7777

7878
proc computeSubObjectAlign(conf: ConfigRef; n: PNode): BiggestInt =
7979
## returns object alignment
@@ -195,7 +195,7 @@ proc computeUnionObjectOffsetsFoldFunction(conf: ConfigRef; n: PNode; packed: bo
195195
accum.maxAlign = szUnknownSize
196196
accum.offset = szUnknownSize
197197

198-
proc computeSizeAlign(conf: ConfigRef; typ: PType) =
198+
proc computeSizeAlign*(conf: ConfigRef; typ: PType) =
199199
template setSize(typ, s) =
200200
typ.size = s
201201
typ.align = s

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)