From d6d28a9c79b1f7d700458ee3aa56a38e1216bb41 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 24 Jan 2025 03:10:14 +0800 Subject: [PATCH] fixes #24623; fixes #23692; size pragma only allowed for imported types and enum types (#24640) fixes #24623 fixes #23692 ref https://nim-lang.org/docs/manual.html#implementation-specific-pragmas-size-pragma confines `size` pragma to `enums` and imported `objects` for now The `typeDefLeftSidePass` carries out the check for pragmas, but the type is not complete yet. So the `size` pragma checking is postponed at the final pass. --- compiler/semstmts.nim | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 75b327afb403..41154f596278 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 != 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") + if a[1].kind == nkEmpty: var x = a[2] if x.kind in nkCallKinds and nfSem in x.flags: