Skip to content

Commit

Permalink
make proc not implicitly convert to pointer with a preview define (
Browse files Browse the repository at this point in the history
…nim-lang#21953)

* test `proc` not converting to `pointer`

* ignore define for now to test

* remove cstring

* fixes, changelog
  • Loading branch information
metagn authored and bung87 committed Jul 29, 2023
1 parent 9d9bfec commit bc16e8a
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 12 deletions.
3 changes: 3 additions & 0 deletions changelogs/changelog_2_0_0.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@

- Enabling `-d:nimPreviewCstringConversion`, `ptr char`, `ptr array[N, char]` and `ptr UncheckedArray[N, char]` don't support conversion to cstring anymore.

- Enabling `-d:nimPreviewProcConversion`, `proc` does not support conversion to
`pointer`. `cast` may be used instead.

- The `gc:v2` option is removed.

- The `mainmodule` and `m` options are removed.
Expand Down
1 change: 1 addition & 0 deletions compiler/nim.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ define:nimcore
define:nimPreviewFloatRoundtrip
define:nimPreviewSlimSystem
define:nimPreviewCstringConversion
define:nimPreviewProcConversion
define:nimPreviewRangeDefault
threads:off

Expand Down
5 changes: 4 additions & 1 deletion compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,10 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
result = isEqual
of tyNil: result = f.allowsNil
of tyProc:
if a.callConv != ccClosure: result = isConvertible
if isDefined(c.c.config, "nimPreviewProcConversion"):
result = isNone
else:
if a.callConv != ccClosure: result = isConvertible
of tyPtr:
# 'pointer' is NOT compatible to regionized pointers
# so 'dealloc(regionPtr)' fails:
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/hashes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ proc hash*[T: tuple | object | proc | iterator {.closure.}](x: T): Hash =
when T is "closure":
result = hash((rawProc(x), rawEnv(x)))
elif T is (proc):
result = hash(pointer(x))
result = hash(cast[pointer](x))
else:
result = 0
for f in fields(x):
Expand Down
2 changes: 1 addition & 1 deletion lib/wrappers/openssl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ when not useWinVersion and not defined(macosx) and not defined(android) and useN
if p != nil: deallocShared(p)

proc CRYPTO_malloc_init*() =
CRYPTO_set_mem_functions(allocWrapper, reallocWrapper, deallocWrapper)
CRYPTO_set_mem_functions(cast[pointer](allocWrapper), cast[pointer](reallocWrapper), cast[pointer](deallocWrapper))
else:
proc CRYPTO_malloc_init*() =
discard
Expand Down
6 changes: 3 additions & 3 deletions tests/dll/nimhcr_unit.nim
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,22 @@ macro carryOutTests(callingConv: untyped): untyped =
echo `procName`, " implementation #1 ", x
return x + 1

let fp1 = cast[F](hcrRegisterProc("dummy_module", `procName`, `p1`))
let fp1 = cast[F](hcrRegisterProc("dummy_module", `procName`, cast[pointer](`p1`)))
echo fp1(10)

proc `p2`(x: int): int {.placeholder.} =
echo `procName`, " implementation #2 ", x
return x + 2

let fp2 = cast[F](hcrRegisterProc("dummy_module", `procName`, `p2`))
let fp2 = cast[F](hcrRegisterProc("dummy_module", `procName`, cast[pointer](`p2`)))
echo fp1(20)
echo fp2(20)

proc `p3`(x: int): int {.placeholder.} =
echo `procName`, " implementation #3 ", x
return x + 3

let fp3 = cast[F](hcrRegisterProc("dummy_module", `procName`, `p3`))
let fp3 = cast[F](hcrRegisterProc("dummy_module", `procName`, cast[pointer](`p3`)))
echo fp1(30)
echo fp2(30)
echo fp3(30)
Expand Down
3 changes: 2 additions & 1 deletion tests/stdlib/config.nims
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
switch("styleCheck", "usages")
switch("styleCheck", "error")
switch("define", "nimPreviewSlimSystem")
switch("define", "nimPreviewCstringConversion")
switch("define", "nimPreviewCstringConversion")
switch("define", "nimPreviewProcConversion")
3 changes: 2 additions & 1 deletion tests/tools/config.nims
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--d:nimPreviewSlimSystem
--d:nimPreviewCstringConversion
--d:nimPreviewCstringConversion
--d:nimPreviewProcConversion
7 changes: 3 additions & 4 deletions tests/types/tissues_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ block t5648:
g.bar = 3

var
mainPtr1: pointer = main
mainPtr2 = pointer(main)
mainPtr3 = cast[pointer](main)
mainPtr = cast[pointer](main)
mainFromPtr = cast[typeof(main)](mainPtr)

doAssert mainPtr1 == mainPtr2 and mainPtr2 == mainPtr3
doAssert main == mainFromPtr

main()

Expand Down

0 comments on commit bc16e8a

Please sign in to comment.