summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2024-06-06 02:54:00 +0800
committerGitHub <noreply@github.com>2024-06-05 20:54:00 +0200
commit2d1533f34f74d69978f20d09be28740be2ec721c (patch)
tree3760c76fd676b714a280ed556bb508b548559e95
parent42e8472ca6eab740c0879428bd119ec94e70fe74 (diff)
downloadNim-2d1533f34f74d69978f20d09be28740be2ec721c.tar.gz
fixes #5901 #21211; don't fold cast function types because of gcc 14 (#23683)
follow up https://github.com/nim-lang/Nim/pull/6265

fixes #5901
fixes #21211

It causes many problems with gcc14 if we fold the cast function types.
Let's check what it will break
-rw-r--r--compiler/semfold.nim3
-rw-r--r--tests/misc/tcast.nim7
2 files changed, 8 insertions, 2 deletions
diff --git a/compiler/semfold.nim b/compiler/semfold.nim
index f7da4ea75..466df2e4e 100644
--- a/compiler/semfold.nim
+++ b/compiler/semfold.nim
@@ -771,7 +771,8 @@ proc getConstExpr(m: PSym, n: PNode; idgen: IdGenerator; g: ModuleGraph): PNode
   of nkCast:
     var a = getConstExpr(m, n[1], idgen, g)
     if a == nil: return
-    if n.typ != nil and n.typ.kind in NilableTypes:
+    if n.typ != nil and n.typ.kind in NilableTypes and
+        not (n.typ.kind == tyProc and a.typ.kind == tyProc):
       # we allow compile-time 'cast' for pointer types:
       result = a
       result.typ = n.typ
diff --git a/tests/misc/tcast.nim b/tests/misc/tcast.nim
index 6d67b1c52..73196e76c 100644
--- a/tests/misc/tcast.nim
+++ b/tests/misc/tcast.nim
@@ -1,6 +1,7 @@
 discard """
   output: '''
 Hello World
+Hello World
 Hello World'''
   joinable: false
 """
@@ -8,7 +9,7 @@ type MyProc = proc() {.cdecl.}
 type MyProc2 = proc() {.nimcall.}
 type MyProc3 = proc() #{.closure.} is implicit
 
-proc testProc()  = echo "Hello World"
+proc testProc() {.exportc:"foo".} = echo "Hello World"
 
 template reject(x) = doAssert(not compiles(x))
 
@@ -23,6 +24,10 @@ proc callPointer(p: pointer) =
   ffunc0()
   ffunc1()
 
+    # bug #5901
+  proc foo() {.importc.}
+  (cast[proc(a: int) {.cdecl.}](foo))(5)
+
 callPointer(cast[pointer](testProc))
 
 reject: discard cast[enum](0)