From 3d692d08f74e41588fa157b006e882f142bd77d4 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 16 Nov 2022 14:36:14 +0800 Subject: [PATCH] fixes a long-standing ARC bug (#20849) * fixes an ARC bug * add a testcase --- compiler/aliasanalysis.nim | 8 ++++++-- tests/arc/tarcmisc.nim | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/compiler/aliasanalysis.nim b/compiler/aliasanalysis.nim index 22a66e4df9..f14b815243 100644 --- a/compiler/aliasanalysis.nim +++ b/compiler/aliasanalysis.nim @@ -63,6 +63,8 @@ proc aliases*(obj, field: PNode): AliasKind = # x.f -> x: false # x.f -> x.f: true # x.f -> x.v: false + # x -> x[]: true + # x[] -> x: false # x -> x[0]: true # x[0] -> x: false # x[0] -> x[0]: true @@ -76,11 +78,11 @@ proc aliases*(obj, field: PNode): AliasKind = var n = n while true: case n.kind - of PathKinds0 - {nkDotExpr, nkBracketExpr}: + of PathKinds0 - {nkDotExpr, nkBracketExpr, nkDerefExpr, nkHiddenDeref}: n = n[0] of PathKinds1: n = n[1] - of nkDotExpr, nkBracketExpr: + of nkDotExpr, nkBracketExpr, nkDerefExpr, nkHiddenDeref: result.add n n = n[0] of nkSym: @@ -114,6 +116,8 @@ proc aliases*(obj, field: PNode): AliasKind = if currFieldPath.sym != currObjPath.sym: return no of nkDotExpr: if currFieldPath[1].sym != currObjPath[1].sym: return no + of nkDerefExpr, nkHiddenDeref: + discard of nkBracketExpr: if currFieldPath[1].kind in nkLiterals and currObjPath[1].kind in nkLiterals: if currFieldPath[1].intVal != currObjPath[1].intVal: diff --git a/tests/arc/tarcmisc.nim b/tests/arc/tarcmisc.nim index 3160d8a4df..6435f5e941 100644 --- a/tests/arc/tarcmisc.nim +++ b/tests/arc/tarcmisc.nim @@ -546,3 +546,15 @@ proc fooz(sec: var InputSectionBase) = var sec = create(InputSection) sec[] = InputSection(relocations: newSeq[int]()) fooz sec[] + +block: + type + Data = ref object + id: int + proc main = + var x = Data(id: 99) + var y = x + x[] = Data(id: 778)[] + doAssert y.id == 778 + doAssert x[].id == 778 + main()