strictly typecheck expressions in bracketed emit (#22074)

* strictly typecheck expressions in bracketed `emit`

* use nim check in test
This commit is contained in:
metagn
2023-06-13 13:04:24 +03:00
committed by GitHub
parent 2e12d3e26b
commit fda8b6f193
4 changed files with 15 additions and 1 deletions

View File

@@ -629,7 +629,7 @@ proc pragmaEmit(c: PContext, n: PNode) =
if n1.kind == nkBracket:
var b = newNodeI(nkBracket, n1.info, n1.len)
for i in 0..<n1.len:
b[i] = c.semExpr(c, n1[i])
b[i] = c.semExprWithType(c, n1[i], {efTypeAllowed})
n[1] = b
else:
n[1] = c.semConstExpr(c, n1)

View File

@@ -678,6 +678,7 @@ proc preparePContext*(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PCo
if result.p != nil: internalError(graph.config, module.info, "sem.preparePContext")
result.semConstExpr = semConstExpr
result.semExpr = semExpr
result.semExprWithType = semExprWithType
result.semTryExpr = tryExpr
result.semTryConstExpr = tryConstExpr
result.computeRequiresInit = computeRequiresInit

View File

@@ -126,6 +126,7 @@ type
libs*: seq[PLib] # all libs used by this module
semConstExpr*: proc (c: PContext, n: PNode; expectedType: PType = nil): PNode {.nimcall.} # for the pragmas
semExpr*: proc (c: PContext, n: PNode, flags: TExprFlags = {}, expectedType: PType = nil): PNode {.nimcall.}
semExprWithType*: proc (c: PContext, n: PNode, flags: TExprFlags = {}, expectedType: PType = nil): PNode {.nimcall.}
semTryExpr*: proc (c: PContext, n: PNode, flags: TExprFlags = {}): PNode {.nimcall.}
semTryConstExpr*: proc (c: PContext, n: PNode; expectedType: PType = nil): PNode {.nimcall.}
computeRequiresInit*: proc (c: PContext, t: PType): bool {.nimcall.}

View File

@@ -0,0 +1,12 @@
discard """
cmd: "nim check $options $file" # use check to assure error is pre-codegen
matrix: "; --backend:js" # backend shouldn't matter but at least check js
"""
proc foo(x: int) = discard
proc foo(x: float) = discard
{.emit: ["// ", foo].} #[tt.Error
^ ambiguous identifier 'foo' -- use one of the following:
tambiguousemit.foo: proc (x: int){.noSideEffect, gcsafe.}
tambiguousemit.foo: proc (x: float){.noSideEffect, gcsafe.}]#