Skip to content

Commit

Permalink
fixes #19051 [backport:1.6] (#19133)
Browse files Browse the repository at this point in the history
(cherry picked from commit c6fc3b2)
  • Loading branch information
Araq authored and narimiran committed Nov 17, 2021
1 parent 73366c0 commit 167881b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
19 changes: 17 additions & 2 deletions compiler/sizealignoffsetimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ proc computeUnionObjectOffsetsFoldFunction(conf: ConfigRef; n: PNode; packed: bo
accum.offset = szUnknownSize

proc computeSizeAlign(conf: ConfigRef; typ: PType) =
template setSize(typ, s) =
typ.size = s
typ.align = s
typ.paddingAtEnd = 0

## computes and sets ``size`` and ``align`` members of ``typ``
assert typ != nil
let hasSize = typ.size != szUncomputedSize
Expand Down Expand Up @@ -258,14 +263,14 @@ proc computeSizeAlign(conf: ConfigRef; typ: PType) =

of tyArray:
computeSizeAlign(conf, typ[1])
let elemSize = typ[1].size
let elemSize = typ[1].size
let len = lengthOrd(conf, typ[0])
if elemSize < 0:
typ.size = elemSize
typ.align = int16(elemSize)
elif len < 0:
typ.size = szUnknownSize
typ.align = szUnknownSize
typ.align = szUnknownSize
else:
typ.size = toInt64Checked(len * int32(elemSize), szTooBigSize)
typ.align = typ[1].align
Expand Down Expand Up @@ -445,6 +450,16 @@ proc computeSizeAlign(conf: ConfigRef; typ: PType) =
typ.size = szUnknownSize
typ.align = szUnknownSize
typ.paddingAtEnd = szUnknownSize
of tyInt, tyUInt:
setSize typ, conf.target.intSize.int16
of tyBool, tyChar, tyUInt8, tyInt8:
setSize typ, 1
of tyInt16, tyUInt16:
setSize typ, 2
of tyInt32, tyUInt32:
setSize typ, 4
of tyInt64, tyUInt64:
setSize typ, 8
else:
typ.size = szUnknownSize
typ.align = szUnknownSize
Expand Down
9 changes: 8 additions & 1 deletion tests/ccgbugs/t5296.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
discard """
cmd: "nim c -d:release $file"
output: 1
output: '''1
-1'''
"""

proc bug() : void =
Expand All @@ -12,3 +13,9 @@ proc bug() : void =
echo x

bug()

# bug #19051
type GInt[T] = int

var a = 1
echo -a

0 comments on commit 167881b

Please sign in to comment.