Skip to content

Commit f9c8775

Browse files
authored
conv needs to be picky about aliases and introduces a temp for addr conv (#24818)
ref #24817 ref #24815 ref status-im/nim-eth#784 ```nim {.emit:""" void foo(unsigned long long* x) { } """.} proc foo(x: var culonglong) {.importc: "foo", nodecl.} proc main(x: var uint64) = # var s: culonglong = u # TODO: var m = uint64(12) # var s = culonglong(m) foo(culonglong m) var u = uint64(12) main(u) ``` Notes that this code gives incompatible errors in 2.0.0, 2.2.0 and the devel branch. With this PR, `conv` is kept, but it seems to go back to #24807
1 parent 0f5732b commit f9c8775

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

compiler/ccgcalls.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ proc withTmpIfNeeded(p: BProc, a: TLoc, needsTmp: bool): TLoc =
338338
else:
339339
result = a
340340

341-
proc literalsNeedsTmp(p: BProc, a: TLoc): TLoc =
341+
proc expressionsNeedsTmp(p: BProc, a: TLoc): TLoc =
342342
result = getTemp(p, a.lode.typ, needsInit=false)
343343
genAssignment(p, result, a, {})
344344

@@ -358,7 +358,7 @@ proc genArg(p: BProc, n: PNode, param: PSym; call: PNode; result: var Builder; n
358358
(optByRef notin param.options or not p.module.compileToCpp):
359359
a = initLocExpr(p, n)
360360
if n.kind in {nkCharLit..nkNilLit}:
361-
addAddrLoc(p.config, literalsNeedsTmp(p, a), result)
361+
addAddrLoc(p.config, expressionsNeedsTmp(p, a), result)
362362
else:
363363
addAddrLoc(p.config, withTmpIfNeeded(p, a, needsTmp), result)
364364
elif p.module.compileToCpp and param.typ.kind in {tyVar} and

compiler/ccgexprs.nim

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,11 @@ proc cowBracket(p: BProc; n: PNode) =
962962
proc cow(p: BProc; n: PNode) {.inline.} =
963963
if n.kind == nkHiddenAddr: cowBracket(p, n[0])
964964

965+
template ignoreConv(e: PNode): bool =
966+
let destType = e.typ.skipTypes({tyVar, tyLent, tyGenericInst, tyAlias, tySink})
967+
let srcType = e[1].typ.skipTypes({tyVar, tyLent, tyGenericInst, tyAlias, tySink})
968+
sameBackendTypePickyAliases(destType, srcType)
969+
965970
proc genAddr(p: BProc, e: PNode, d: var TLoc) =
966971
# careful 'addr(myptrToArray)' needs to get the ampersand:
967972
if e[0].typ.skipTypes(abstractInstOwned).kind in {tyRef, tyPtr}:
@@ -974,7 +979,11 @@ proc genAddr(p: BProc, e: PNode, d: var TLoc) =
974979
d.lode = e
975980
else:
976981
var a: TLoc = initLocExpr(p, e[0])
977-
putIntoDest(p, d, e, addrLoc(p.config, a), a.storage)
982+
if e[0].kind in {nkHiddenStdConv, nkHiddenSubConv, nkConv} and not ignoreConv(e[0]):
983+
# addr (conv x) introduces a temp because `conv x` is not a rvalue
984+
putIntoDest(p, d, e, addrLoc(p.config, expressionsNeedsTmp(p, a)), a.storage)
985+
else:
986+
putIntoDest(p, d, e, addrLoc(p.config, a), a.storage)
978987

979988
template inheritLocation(d: var TLoc, a: TLoc) =
980989
if d.k == locNone: d.storage = a.storage
@@ -2637,9 +2646,7 @@ proc genRangeChck(p: BProc, n: PNode, d: var TLoc) =
26372646
putIntoDest(p, d, n, cCast(destType, wrapPar(val)), a.storage)
26382647

26392648
proc genConv(p: BProc, e: PNode, d: var TLoc) =
2640-
let destType = e.typ.skipTypes({tyVar, tyLent, tyGenericInst, tyAlias, tySink})
2641-
let srcType = e[1].typ.skipTypes({tyVar, tyLent, tyGenericInst, tyAlias, tySink})
2642-
if sameBackendTypeIgnoreRange(destType, srcType):
2649+
if ignoreConv(e):
26432650
expr(p, e[1], d)
26442651
else:
26452652
genSomeCast(p, e, d)

compiler/types.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ proc sameBackendTypeIgnoreRange*(x, y: PType): bool =
14201420

14211421
proc sameBackendTypePickyAliases*(x, y: PType): bool =
14221422
var c = initSameTypeClosure()
1423-
c.flags.incl {IgnoreTupleFields, PickyCAliases, PickyBackendAliases}
1423+
c.flags.incl {IgnoreTupleFields, IgnoreRangeShallow, PickyCAliases, PickyBackendAliases}
14241424
c.cmp = dcEqIgnoreDistinct
14251425
result = sameTypeAux(x, y, c)
14261426

tests/ccgbugs/taddrconvs.nim

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
discard """
2+
targets: "c cpp"
3+
matrix: "--mm:refc; --mm:orc"
4+
"""
5+
6+
{.emit:"""
7+
void foo(unsigned long long* x)
8+
{
9+
}
10+
""".}
11+
12+
block:
13+
proc foo(x: var culonglong) {.importc: "foo", nodecl.}
14+
15+
proc main(x: var uint64) =
16+
foo(culonglong x)
17+
18+
var u = uint64(12)
19+
main(u)
20+
21+
block:
22+
proc foo(x: var culonglong) {.importc: "foo", nodecl.}
23+
24+
proc main() =
25+
var m = uint64(12)
26+
foo(culonglong(m))
27+
main()

0 commit comments

Comments
 (0)