Skip to content

Commit d6d28a9

Browse files
authored
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.
1 parent 6d59680 commit d6d28a9

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

compiler/semstmts.nim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,6 +1787,17 @@ proc typeSectionFinalPass(c: PContext, n: PNode) =
17871787
# check the style here after the pragmas have been processed:
17881788
styleCheckDef(c, s)
17891789
# compute the type's size and check for illegal recursions:
1790+
if a[0].kind == nkPragmaExpr:
1791+
let pragmas = a[0][1]
1792+
for i in 0 ..< pragmas.len:
1793+
if pragmas[i].kind == nkExprColonExpr and
1794+
pragmas[i][0].kind == nkIdent and
1795+
whichKeyword(pragmas[i][0].ident) == wSize:
1796+
if s.typ.kind != tyEnum and sfImportc notin s.flags:
1797+
# EventType* {.size: sizeof(uint32).} = enum
1798+
# AtomicFlag* {.importc: "atomic_flag", header: "<stdatomic.h>", size: 1.} = object
1799+
localError(c.config, pragmas[i].info, "size pragma only allowed for enum types and imported types")
1800+
17901801
if a[1].kind == nkEmpty:
17911802
var x = a[2]
17921803
if x.kind in nkCallKinds and nfSem in x.flags:

0 commit comments

Comments
 (0)