From f34779c289398adb50e52064528b81fece02f952 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 8 Apr 2020 00:56:16 -0700 Subject: [PATCH] fix #13902 distinct uint64 type corruption on 32-bit with borrow (#13907) [backport:1.2] * fix #13902 distinct uint64 type corruption on 32-bit with borrow Co-authored-by: Timothee Cour (cherry picked from commit 95fd8ae93ce8732eb5a979d4cd63e0350594318d) --- compiler/ccgexprs.nim | 2 +- tests/ccgbugs/t13902.nim | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/ccgbugs/t13902.nim diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index fcedcf001e..d8f5d016b1 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -2156,7 +2156,7 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) = const opr: array[mInc..mDec, string] = ["+=", "-="] const fun64: array[mInc..mDec, string] = ["nimAddInt64", "nimSubInt64"] const fun: array[mInc..mDec, string] = ["nimAddInt","nimSubInt"] - let underlying = skipTypes(e[1].typ, {tyGenericInst, tyAlias, tySink, tyVar, tyLent, tyRange}) + let underlying = skipTypes(e[1].typ, {tyGenericInst, tyAlias, tySink, tyVar, tyLent, tyRange, tyDistinct}) if optOverflowCheck notin p.options or underlying.kind in {tyUInt..tyUInt64}: binaryStmt(p, e, d, opr[op]) else: diff --git a/tests/ccgbugs/t13902.nim b/tests/ccgbugs/t13902.nim new file mode 100644 index 0000000000..fd4f76d15e --- /dev/null +++ b/tests/ccgbugs/t13902.nim @@ -0,0 +1,12 @@ + +#issue #13902 +block: + type Slot = distinct uint64 + var s = Slot(1) + proc `$`(x: Slot): string {.borrow.} + proc `+=`(x: var Slot, y: uint64) {.borrow.} + # test was failing with either 0 or 2 echos but not with 1 echo + # echo "s = ", s + s += 1 + # echo "s = ", s + doAssert s.uint64 == 2, $s # was failing, showing 18419607611339964418