Skip to content

Commit

Permalink
revert #10814; unsafeAddr is the same as addr
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Apr 5, 2024
1 parent 0fdb4d3 commit 8b0eb9d
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 20 deletions.
8 changes: 0 additions & 8 deletions compiler/astalgo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -758,14 +758,6 @@ proc iiTablePut(t: var TIITable, key, val: int) =
iiTableRawInsert(t.data, key, val)
inc(t.counter)

proc isAddrNode*(n: PNode): bool =
case n.kind
of nkAddr, nkHiddenAddr: true
of nkCallKinds:
if n[0].kind == nkSym and n[0].sym.magic == mAddr: true
else: false
else: false

proc listSymbolNames*(symbols: openArray[PSym]): string =
result = ""
for sym in symbols:
Expand Down
7 changes: 2 additions & 5 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2448,9 +2448,7 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags; expectedType: P
of mAddr:
markUsed(c, n.info, s)
checkSonsLen(n, 2, c.config)
result = newTreeI(nkAddr, n.info)
result.add semAddrArg(c, n[1])
result.typ = makePtrType(c, result[0].typ)
result = semAddr(c, n[1])
of mTypeOf:
markUsed(c, n.info, s)
result = semTypeOf(c, n)
Expand Down Expand Up @@ -3335,8 +3333,7 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}, expectedType: PType
of nkAddr:
result = n
checkSonsLen(n, 1, c.config)
result[0] = semAddrArg(c, n[0])
result.typ = makePtrType(c, result[0].typ)
result = semAddr(c, n[0])
of nkHiddenAddr, nkHiddenDeref:
checkSonsLen(n, 1, c.config)
n[0] = semExpr(c, n[0], flags, expectedType)
Expand Down
10 changes: 5 additions & 5 deletions compiler/semmagic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ proc addDefaultFieldForNew(c: PContext, n: PNode): PNode =
if asgnExpr.sons.len > 1:
result = newTree(nkAsgn, result[1], asgnExpr)

proc semAddrArg(c: PContext; n: PNode): PNode =
proc semAddr(c: PContext; n: PNode): PNode =
result = newNodeI(nkAddr, n.info)
let x = semExprWithType(c, n)
if x.kind == nkSym:
x.sym.flags.incl(sfAddrTaken)
if isAssignable(c, x) notin {arLValue, arLocalLValue, arAddressableConst, arLentValue}:
localError(c.config, n.info, errExprHasNoAddress)
result = x
result.add x
result.typ = makePtrType(c, x.typ)

proc semTypeOf(c: PContext; n: PNode): PNode =
var m = BiggestInt 1 # typeOfIter
Expand Down Expand Up @@ -561,9 +563,7 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
case n[0].sym.magic
of mAddr:
checkSonsLen(n, 2, c.config)
result = newTreeI(nkAddr, n.info)
result.add semAddrArg(c, n[1])
result.typ = makePtrType(c, result[0].typ)
result = semAddr(c, n[1])
of mTypeOf:
result = semTypeOf(c, n)
of mSizeOf:
Expand Down
2 changes: 1 addition & 1 deletion compiler/sempass2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ proc notNilCheck(tracked: PEffects, n: PNode, paramType: PType) =
if paramType != nil and tfNotNil in paramType.flags and n.typ != nil:
let ntyp = n.typ.skipTypesOrNil({tyVar, tyLent, tySink})
if ntyp != nil and tfNotNil notin ntyp.flags:
if isAddrNode(n):
if n.kind in {nkAddr, nkHiddenAddr}:
# addr(x[]) can't be proven, but addr(x) can:
if not containsNode(n, {nkDerefExpr, nkHiddenDeref}): return
elif (n.kind == nkSym and n.sym.kind in routineKinds) or
Expand Down
2 changes: 1 addition & 1 deletion tests/macros/tmacrostmt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static:
let fn4s = "proc fn4(x: int): int =\n if x mod 2 == 0:\n return x + 2\n else:\n return 0\n"
let fn5s = "proc fn5(a, b: float): float =\n result = -a * a / (b * b)\n"
let fn6s = "proc fn6() =\n var a = @[1.0, 2.0]\n let z = a{0, 1}\n a{2} = 5.0\n"
let fnAddr = "proc fn_unsafeaddr(x: int): int =\n result = cast[int](unsafeAddr(x))\n"
let fnAddr = "proc fn_unsafeaddr(x: int): int =\n result = cast[int](addr(x))\n"

doAssert fn1.repr_to_string == fn1s
doAssert fn2.repr_to_string == fn2s
Expand Down

0 comments on commit 8b0eb9d

Please sign in to comment.