From 7fe5e4097328ce9d6e8aa162c0ca665c48db0f5b Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 23 Jan 2025 16:45:13 +0800 Subject: [PATCH 1/4] size pragma only allowed for objects and enums --- compiler/semstmts.nim | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 75b327afb403..2fc4a486c450 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -1787,6 +1787,17 @@ proc typeSectionFinalPass(c: PContext, n: PNode) = # check the style here after the pragmas have been processed: styleCheckDef(c, s) # compute the type's size and check for illegal recursions: + if a[0].kind == nkPragmaExpr: + let pragmas = a[0][1] + for i in 0 ..< pragmas.len: + if pragmas[i].kind == nkExprColonExpr and + pragmas[i][0].kind == nkIdent and + whichKeyword(pragmas[i][0].ident) == wSize: + if s.typ.kind notin {tyObject, tyEnum}: + # EventType* {.size: sizeof(uint32).} = enum + # AtomicFlag* {.importc: "atomic_flag", header: "", size: 1.} = object + localError(c.config, pragmas[i].info, "size pragma only allowed for objects and enums") + if a[1].kind == nkEmpty: var x = a[2] if x.kind in nkCallKinds and nfSem in x.flags: From 59ebc328cf9770d4e419776303284838e4969c7a Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 23 Jan 2025 17:00:39 +0800 Subject: [PATCH 2/4] imported objects --- compiler/semstmts.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 2fc4a486c450..5c2eb24edba4 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -1793,10 +1793,10 @@ proc typeSectionFinalPass(c: PContext, n: PNode) = if pragmas[i].kind == nkExprColonExpr and pragmas[i][0].kind == nkIdent and whichKeyword(pragmas[i][0].ident) == wSize: - if s.typ.kind notin {tyObject, tyEnum}: + if s.typ.kind != tyEnum and sfImportC notin s.flags: # EventType* {.size: sizeof(uint32).} = enum # AtomicFlag* {.importc: "atomic_flag", header: "", size: 1.} = object - localError(c.config, pragmas[i].info, "size pragma only allowed for objects and enums") + localError(c.config, pragmas[i].info, "size pragma only allowed for imported objects and enums") if a[1].kind == nkEmpty: var x = a[2] From 1a036c9345b749fa84542574d1f19c98a6a71960 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 23 Jan 2025 17:02:45 +0800 Subject: [PATCH 3/4] progress --- compiler/semstmts.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 5c2eb24edba4..d32c923dbce7 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -1796,7 +1796,7 @@ proc typeSectionFinalPass(c: PContext, n: PNode) = if s.typ.kind != tyEnum and sfImportC notin s.flags: # EventType* {.size: sizeof(uint32).} = enum # AtomicFlag* {.importc: "atomic_flag", header: "", size: 1.} = object - localError(c.config, pragmas[i].info, "size pragma only allowed for imported objects and enums") + localError(c.config, pragmas[i].info, "size pragma only allowed for enum types and imported types") if a[1].kind == nkEmpty: var x = a[2] From 2478d0e20fc6d19aa3349cabc323c819683f1c03 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 23 Jan 2025 17:05:39 +0800 Subject: [PATCH 4/4] oops --- compiler/semstmts.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index d32c923dbce7..41154f596278 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -1793,7 +1793,7 @@ proc typeSectionFinalPass(c: PContext, n: PNode) = if pragmas[i].kind == nkExprColonExpr and pragmas[i][0].kind == nkIdent and whichKeyword(pragmas[i][0].ident) == wSize: - if s.typ.kind != tyEnum and sfImportC notin s.flags: + if s.typ.kind != tyEnum and sfImportc notin s.flags: # EventType* {.size: sizeof(uint32).} = enum # AtomicFlag* {.importc: "atomic_flag", header: "", size: 1.} = object localError(c.config, pragmas[i].info, "size pragma only allowed for enum types and imported types")