From c536ff6399d4a6709441000aa1772d464986c26d Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sun, 13 Oct 2024 16:51:16 +0800 Subject: [PATCH 1/3] templates/macros use return types as expected types --- compiler/sem.nim | 2 +- tests/types/ttopdowninference.nim | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/compiler/sem.nim b/compiler/sem.nim index 699bcbb948bc8..2e6b2c0cb19d0 100644 --- a/compiler/sem.nim +++ b/compiler/sem.nim @@ -494,7 +494,7 @@ proc semAfterMacroCall(c: PContext, call, macroResult: PNode, if retType.kind == tyVoid: result = semStmt(c, result, flags) else: - result = semExpr(c, result, flags, expectedType) + result = semExpr(c, result, flags, retType) result = fitNode(c, retType, result, result.info) #globalError(s.info, errInvalidParamKindX, typeToString(s.typ.returnType)) dec(c.config.evalTemplateCounter) diff --git a/tests/types/ttopdowninference.nim b/tests/types/ttopdowninference.nim index 765761e993ffd..3e4ab29382c45 100644 --- a/tests/types/ttopdowninference.nim +++ b/tests/types/ttopdowninference.nim @@ -331,3 +331,29 @@ block: # issue #24164, related regression template bar(x: untyped = nil) = foo(x) bar() + +block: # bug #24296 + # Either changing the template to `proc`/`func` or using `$""`, not a string + # literal alone, allows any version of Nim 2.x to compile this. + template g(): string = "" + + # Alternatively: don't retrieve the string through g(), but directly, also + # allows compilation across Nim 2.x versions. + const d: cstring = "" + const f: cstring = $"" + const b = cstring g() + const m = cstring "" + const p = cstring $"" + + # But this does not compile across Nim 2.x/devel. + const c: cstring = g() + let d: cstring = g() + +block: # bug #24295 + template g(_: int): string = "" + const c: cstring = 0.g() + +block: # bug #24295 + template g(_: int): cstring = "" + let c: string = 0.g() + From 2928b5fb71675843a0de5be9cd2667e650063f96 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sun, 13 Oct 2024 18:24:03 +0800 Subject: [PATCH 2/3] fixes some tests --- tests/types/ttopdowninference.nim | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/types/ttopdowninference.nim b/tests/types/ttopdowninference.nim index 3e4ab29382c45..303effdf7bd8d 100644 --- a/tests/types/ttopdowninference.nim +++ b/tests/types/ttopdowninference.nim @@ -347,13 +347,9 @@ block: # bug #24296 # But this does not compile across Nim 2.x/devel. const c: cstring = g() - let d: cstring = g() + let e: cstring = g() block: # bug #24295 template g(_: int): string = "" const c: cstring = 0.g() -block: # bug #24295 - template g(_: int): cstring = "" - let c: string = 0.g() - From 93d8fa9f79b44202d42fbcb6bd6e8af0faa846cb Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sun, 13 Oct 2024 18:58:13 +0800 Subject: [PATCH 3/3] fixes for int literals --- compiler/sem.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/sem.nim b/compiler/sem.nim index 2e6b2c0cb19d0..1866d85fcbe7e 100644 --- a/compiler/sem.nim +++ b/compiler/sem.nim @@ -494,7 +494,7 @@ proc semAfterMacroCall(c: PContext, call, macroResult: PNode, if retType.kind == tyVoid: result = semStmt(c, result, flags) else: - result = semExpr(c, result, flags, retType) + result = semExpr(c, result, flags) result = fitNode(c, retType, result, result.info) #globalError(s.info, errInvalidParamKindX, typeToString(s.typ.returnType)) dec(c.config.evalTemplateCounter)