From 53a88b8ce04b266fa1b62d942d8586043d8ccdaf Mon Sep 17 00:00:00 2001 From: metagn <10591326+metagn@users.noreply.github.com> Date: Mon, 18 Dec 2023 21:16:56 +0300 Subject: [PATCH 1/3] directly parse on/off in pragmas, ignore user override fixes #22841, fixes #23002 --- compiler/pragmas.nim | 16 ++++++++++------ tests/pragmas/monoff1.nim | 1 + tests/pragmas/tonoff1.nim | 8 ++++++++ tests/pragmas/tonoff2.nim | 10 ++++++++++ 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 tests/pragmas/monoff1.nim create mode 100644 tests/pragmas/tonoff1.nim create mode 100644 tests/pragmas/tonoff2.nim diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index 001af6ae7fec..77e57d623096 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -285,9 +285,15 @@ proc wordToCallConv(sw: TSpecialWord): TCallingConvention = proc isTurnedOn(c: PContext, n: PNode): bool = result = false if n.kind in nkPragmaCallKinds and n.len == 2: - let x = c.semConstBoolExpr(c, n[1]) - n[1] = x - if x.kind == nkIntLit: return x.intVal != 0 + let ident = getPIdent(n[1]) + if ident != nil and ident.id == ord(wOn): + return true + elif ident != nil and ident.id == ord(wOff): + return false + else: + let x = c.semConstBoolExpr(c, n[1]) + n[1] = x + if x.kind == nkIntLit: return x.intVal != 0 localError(c.config, n.info, "'on' or 'off' expected") proc onOff(c: PContext, n: PNode, op: TOptions, resOptions: var TOptions) = @@ -368,9 +374,7 @@ proc processNote(c: PContext, n: PNode) = let x = findStr(enumVals.a, enumVals.b, n[0][1].ident.s, errUnknown) if x != errUnknown: nk = TNoteKind(x) - let x = c.semConstBoolExpr(c, n[1]) - n[1] = x - if x.kind == nkIntLit and x.intVal != 0: incl(notes, nk) + if isTurnedOn(c, n): incl(notes, nk) else: excl(notes, nk) else: invalidPragma(c, n) diff --git a/tests/pragmas/monoff1.nim b/tests/pragmas/monoff1.nim new file mode 100644 index 000000000000..85d6c57b3b13 --- /dev/null +++ b/tests/pragmas/monoff1.nim @@ -0,0 +1 @@ +proc on*() = discard diff --git a/tests/pragmas/tonoff1.nim b/tests/pragmas/tonoff1.nim new file mode 100644 index 000000000000..20ba7def2374 --- /dev/null +++ b/tests/pragmas/tonoff1.nim @@ -0,0 +1,8 @@ +# issue #23002 + +import monoff1 + +proc test() = + {.warning[ProveInit]: on.} + +test() diff --git a/tests/pragmas/tonoff2.nim b/tests/pragmas/tonoff2.nim new file mode 100644 index 000000000000..cf0d94ce18e7 --- /dev/null +++ b/tests/pragmas/tonoff2.nim @@ -0,0 +1,10 @@ +# issue #22841 + +import unittest + +proc on() = + discard + +suite "some suite": + test "some test": + discard From 0f677108cd77bec65a3350d535d03582a23b15ed Mon Sep 17 00:00:00 2001 From: metagn <10591326+metagn@users.noreply.github.com> Date: Mon, 18 Dec 2023 23:03:25 +0300 Subject: [PATCH 2/3] set the node to the bool value --- compiler/pragmas.nim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index 77e57d623096..9c7bdefbaecd 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -287,8 +287,10 @@ proc isTurnedOn(c: PContext, n: PNode): bool = if n.kind in nkPragmaCallKinds and n.len == 2: let ident = getPIdent(n[1]) if ident != nil and ident.id == ord(wOn): + n[1] = newIntTypeNode(1, getSysType(c.graph, n.info, tyBool)) return true elif ident != nil and ident.id == ord(wOff): + n[1] = newIntTypeNode(0, getSysType(c.graph, n.info, tyBool)) return false else: let x = c.semConstBoolExpr(c, n[1]) From 5538830a4fa710cb864526884452b3c127806009 Mon Sep 17 00:00:00 2001 From: metagn <10591326+metagn@users.noreply.github.com> Date: Tue, 19 Dec 2023 01:02:30 +0300 Subject: [PATCH 3/3] enough to compile unittest test --- tests/pragmas/tonoff2.nim | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/pragmas/tonoff2.nim b/tests/pragmas/tonoff2.nim index cf0d94ce18e7..9dff5ef11a69 100644 --- a/tests/pragmas/tonoff2.nim +++ b/tests/pragmas/tonoff2.nim @@ -1,3 +1,7 @@ +discard """ + action: compile +""" + # issue #22841 import unittest