Skip to content

Commit 25ac009

Browse files
authored
assume/assert pragmas, wip (#817)
* assume/assert pragmas, wip * important bugfix * critical bugfix * update tests
1 parent b8b6ab0 commit 25ac009

19 files changed

+356
-218
lines changed

doc/tags.md

+2
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@
203203
| `(noinit)` | NimonyPragma | `noinit` pragma |
204204
| `(requires X)` | NimonyPragma | `requires` pragma |
205205
| `(ensures X)` | NimonyPragma | `ensures` pragma |
206+
| `(assume X)` | NimonyPragma, NimonyStmt | `assume` pragma |
207+
| `(assert X)` | NimonyPragma, NimonyStmt | `assert` pragma |
206208
| `(build X)`; `(build STR STR STR)` | NimonyPragma, NifIndexKind | `build` pragma |
207209
| `(string)` | NimonyPragma | `string` pragma |
208210
| `(view)` | NimonyPragma | `view` pragma |

src/hastur.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ proc controlflowTests(overwrite: bool) =
321321
failure c, t, "controlflow exitcode " & $exitcode, msgs
322322
let expected = t.changeFileExt(".expected.nif")
323323
if overwrite:
324-
writeFile(dest, msgs)
324+
moveFile(dest, expected)
325325
elif expected.fileExists():
326326
let expectedOutput = readFile(expected).strip
327327
let destContent = readFile(dest).strip

src/hexer/mover.nim

+2-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ proc singlePath(pc: Cursor; nested: int; x: Cursor; pcs: var seq[Cursor]; otherU
216216
ImportasS, ExportexceptS, BindS, MixinS, UsingS,
217217
UnpackDeclS, StaticstmtS, AsmS, DeferS:
218218
raiseAssert "BUG: statement not eliminated: " & $pc.stmtKind
219-
of ProcS, FuncS, IteratorS, ConverterS, MethodS, MacroS, TemplateS, TypeS:
219+
of ProcS, FuncS, IteratorS, ConverterS, MethodS, MacroS, TemplateS, TypeS,
220+
AssumeS, AssertS:
220221
# declarative junk we don't care about:
221222
skip pc
222223
return true

src/hexer/nifcgen.nim

+2-2
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ proc parsePragmas(c: var EContext; n: var Cursor): CollectedPragmas =
631631
expectIntLit c, n
632632
result.bits = n.intId
633633
inc n
634-
of RequiresP, EnsuresP, StringP, RaisesP, ErrorP:
634+
of RequiresP, EnsuresP, StringP, RaisesP, ErrorP, AssumeP, AssertP:
635635
skip n
636636
continue
637637
of BuildP, EmitP:
@@ -1465,7 +1465,7 @@ proc traverseStmt(c: var EContext; n: var Cursor; mode = TraverseAll) =
14651465
traverseTypeDecl c, n
14661466
of ContinueS, WhenS:
14671467
error c, "unreachable: ", n
1468-
of PragmasS:
1468+
of PragmasS, AssumeS, AssertS:
14691469
skip n
14701470
else:
14711471
error c, "statement expected, but got: ", n

src/hexer/xelim.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ proc trStmt(c: var Context; dest: var TokenBuf; n: var Cursor) =
384384
trBlock c, dest, n, tar
385385
of IteratorS, TemplateS, TypeS, EmitS, BreakS, ContinueS,
386386
ForS, IncludeS, ImportS, FromimportS, ImportExceptS,
387-
ExportS, CommentS,
387+
ExportS, CommentS, AssumeS, AssertS,
388388
PragmasS, ImportasS, ExportexceptS, BindS, MixinS, UsingS:
389389
takeTree dest, n
390390
of ScopeS, StaticstmtS:

src/lib/lineinfos.nim

+3-2
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ proc unpack*(m: LineInfoManager; info: PackedLineInfo): LineInfoUnpacked =
8080
result = LineInfoUnpacked(file: FileId((i shr 1'u32) and FileMax.uint32),
8181
line: int32((i shr uint32(AsideBit + FileBits)) and LineMax.uint32),
8282
col: int32((i shr uint32(AsideBit + FileBits + LineBits)) and ColMax.uint32))
83-
else:
84-
assert(not isPayload(info))
83+
elif not isPayload(info):
8584
result = m.aside[int(i shr 2'u32)]
85+
else:
86+
result = LineInfoUnpacked(file: NoFile)
8687

8788
proc getPayload*(i: PackedLineInfo): uint32 {.inline.} =
8889
assert isPayload(i)

src/models/nimony_tags.nim

+6-2
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ type
158158
TryS = (ord(TryTagId), "try") ## `try` statement
159159
RaiseS = (ord(RaiseTagId), "raise") ## `raise` statement
160160
UnpackdeclS = (ord(UnpackdeclTagId), "unpackdecl") ## unpack var/let/const declaration
161+
AssumeS = (ord(AssumeTagId), "assume") ## `assume` pragma
162+
AssertS = (ord(AssertTagId), "assert") ## `assert` pragma
161163
StaticstmtS = (ord(StaticstmtTagId), "staticstmt") ## `static` statement
162164
BindS = (ord(BindTagId), "bind") ## `bind` statement
163165
MixinS = (ord(MixinTagId), "mixin") ## `mixin` statement
@@ -166,7 +168,7 @@ type
166168
DeferS = (ord(DeferTagId), "defer") ## `defer` statement
167169

168170
proc rawTagIsNimonyStmt*(raw: TagEnum): bool {.inline.} =
169-
raw in {CallTagId, CmdTagId, GvarTagId, TvarTagId, VarTagId, ConstTagId, ResultTagId, GletTagId, TletTagId, LetTagId, CursorTagId, ProcTagId, FuncTagId, IteratorTagId, ConverterTagId, MethodTagId, MacroTagId, TemplateTagId, TypeTagId, BlockTagId, EmitTagId, AsgnTagId, ScopeTagId, IfTagId, WhenTagId, BreakTagId, ContinueTagId, ForTagId, WhileTagId, CaseTagId, RetTagId, YldTagId, StmtsTagId, PragmasTagId, InclTagId, ExclTagId, IncludeTagId, ImportTagId, ImportasTagId, FromimportTagId, ImportexceptTagId, ExportTagId, ExportexceptTagId, CommentTagId, DiscardTagId, TryTagId, RaiseTagId, UnpackdeclTagId, StaticstmtTagId, BindTagId, MixinTagId, UsingTagId, AsmTagId, DeferTagId}
171+
raw in {CallTagId, CmdTagId, GvarTagId, TvarTagId, VarTagId, ConstTagId, ResultTagId, GletTagId, TletTagId, LetTagId, CursorTagId, ProcTagId, FuncTagId, IteratorTagId, ConverterTagId, MethodTagId, MacroTagId, TemplateTagId, TypeTagId, BlockTagId, EmitTagId, AsgnTagId, ScopeTagId, IfTagId, WhenTagId, BreakTagId, ContinueTagId, ForTagId, WhileTagId, CaseTagId, RetTagId, YldTagId, StmtsTagId, PragmasTagId, InclTagId, ExclTagId, IncludeTagId, ImportTagId, ImportasTagId, FromimportTagId, ImportexceptTagId, ExportTagId, ExportexceptTagId, CommentTagId, DiscardTagId, TryTagId, RaiseTagId, UnpackdeclTagId, AssumeTagId, AssertTagId, StaticstmtTagId, BindTagId, MixinTagId, UsingTagId, AsmTagId, DeferTagId}
170172

171173
type
172174
NimonyType* = enum
@@ -274,6 +276,8 @@ type
274276
NoinitP = (ord(NoinitTagId), "noinit") ## `noinit` pragma
275277
RequiresP = (ord(RequiresTagId), "requires") ## `requires` pragma
276278
EnsuresP = (ord(EnsuresTagId), "ensures") ## `ensures` pragma
279+
AssumeP = (ord(AssumeTagId), "assume") ## `assume` pragma
280+
AssertP = (ord(AssertTagId), "assert") ## `assert` pragma
277281
BuildP = (ord(BuildTagId), "build") ## `build` pragma
278282
StringP = (ord(StringTagId), "string") ## `string` pragma
279283
ViewP = (ord(ViewTagId), "view") ## `view` pragma
@@ -282,7 +286,7 @@ type
282286
ErrorP = (ord(ErrorTagId), "error") ## `error` pragma
283287

284288
proc rawTagIsNimonyPragma*(raw: TagEnum): bool {.inline.} =
285-
raw in {EmitTagId, InlineTagId, NoinlineTagId, VarargsTagId, SelectanyTagId, AlignTagId, BitsTagId, NodeclTagId, RaisesTagId, UntypedTagId, MagicTagId, ImportcTagId, ImportcppTagId, ExportcTagId, HeaderTagId, ThreadvarTagId, GlobalTagId, DiscardableTagId, NoreturnTagId, BorrowTagId, NoSideEffectTagId, NodestroyTagId, PluginTagId, BycopyTagId, ByrefTagId, NoinitTagId, RequiresTagId, EnsuresTagId, BuildTagId, StringTagId, ViewTagId, InjectTagId, GensymTagId, ErrorTagId}
289+
raw in {EmitTagId, InlineTagId, NoinlineTagId, VarargsTagId, SelectanyTagId, AlignTagId, BitsTagId, NodeclTagId, RaisesTagId, UntypedTagId, MagicTagId, ImportcTagId, ImportcppTagId, ExportcTagId, HeaderTagId, ThreadvarTagId, GlobalTagId, DiscardableTagId, NoreturnTagId, BorrowTagId, NoSideEffectTagId, NodestroyTagId, PluginTagId, BycopyTagId, ByrefTagId, NoinitTagId, RequiresTagId, EnsuresTagId, AssumeTagId, AssertTagId, BuildTagId, StringTagId, ViewTagId, InjectTagId, GensymTagId, ErrorTagId}
286290

287291
type
288292
NimonySym* = enum

src/models/tags.nim

+69-65
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ type
206206
NoinitTagId
207207
RequiresTagId
208208
EnsuresTagId
209+
AssumeTagId
210+
AssertTagId
209211
BuildTagId
210212
StringTagId
211213
ViewTagId
@@ -477,69 +479,71 @@ const
477479
("noinit", 201),
478480
("requires", 202),
479481
("ensures", 203),
480-
("build", 204),
481-
("string", 205),
482-
("view", 206),
483-
("quoted", 207),
484-
("hderef", 208),
485-
("ddot", 209),
486-
("haddr", 210),
487-
("newref", 211),
488-
("newobj", 212),
489-
("tup", 213),
490-
("tupconstr", 214),
491-
("setconstr", 215),
492-
("tabconstr", 216),
493-
("ashr", 217),
494-
("oconv", 218),
495-
("hconv", 219),
496-
("dconv", 220),
497-
("callstrlit", 221),
498-
("infix", 222),
499-
("prefix", 223),
500-
("hcall", 224),
501-
("compiles", 225),
502-
("declared", 226),
503-
("defined", 227),
504-
("high", 228),
505-
("low", 229),
506-
("typeof", 230),
507-
("unpack", 231),
508-
("enumtostr", 232),
509-
("ismainmodule", 233),
510-
("defaultobj", 234),
511-
("defaulttup", 235),
512-
("expr", 236),
513-
("do", 237),
514-
("arrat", 238),
515-
("tupat", 239),
516-
("plusset", 240),
517-
("minusset", 241),
518-
("mulset", 242),
519-
("xorset", 243),
520-
("eqset", 244),
521-
("leset", 245),
522-
("ltset", 246),
523-
("inset", 247),
524-
("card", 248),
525-
("emove", 249),
526-
("destroy", 250),
527-
("dup", 251),
528-
("copy", 252),
529-
("wasmoved", 253),
530-
("sinkh", 254),
531-
("trace", 255),
532-
("errv", 256),
533-
("staticstmt", 257),
534-
("bind", 258),
535-
("mixin", 259),
536-
("using", 260),
537-
("asm", 261),
538-
("defer", 262),
539-
("index", 263),
540-
("public", 264),
541-
("private", 265),
542-
("inject", 266),
543-
("gensym", 267),
544-
("error", 268)
482+
("assume", 204),
483+
("assert", 205),
484+
("build", 206),
485+
("string", 207),
486+
("view", 208),
487+
("quoted", 209),
488+
("hderef", 210),
489+
("ddot", 211),
490+
("haddr", 212),
491+
("newref", 213),
492+
("newobj", 214),
493+
("tup", 215),
494+
("tupconstr", 216),
495+
("setconstr", 217),
496+
("tabconstr", 218),
497+
("ashr", 219),
498+
("oconv", 220),
499+
("hconv", 221),
500+
("dconv", 222),
501+
("callstrlit", 223),
502+
("infix", 224),
503+
("prefix", 225),
504+
("hcall", 226),
505+
("compiles", 227),
506+
("declared", 228),
507+
("defined", 229),
508+
("high", 230),
509+
("low", 231),
510+
("typeof", 232),
511+
("unpack", 233),
512+
("enumtostr", 234),
513+
("ismainmodule", 235),
514+
("defaultobj", 236),
515+
("defaulttup", 237),
516+
("expr", 238),
517+
("do", 239),
518+
("arrat", 240),
519+
("tupat", 241),
520+
("plusset", 242),
521+
("minusset", 243),
522+
("mulset", 244),
523+
("xorset", 245),
524+
("eqset", 246),
525+
("leset", 247),
526+
("ltset", 248),
527+
("inset", 249),
528+
("card", 250),
529+
("emove", 251),
530+
("destroy", 252),
531+
("dup", 253),
532+
("copy", 254),
533+
("wasmoved", 255),
534+
("sinkh", 256),
535+
("trace", 257),
536+
("errv", 258),
537+
("staticstmt", 259),
538+
("bind", 260),
539+
("mixin", 261),
540+
("using", 262),
541+
("asm", 263),
542+
("defer", 264),
543+
("index", 265),
544+
("public", 266),
545+
("private", 267),
546+
("inject", 268),
547+
("gensym", 269),
548+
("error", 270)
545549
]

0 commit comments

Comments
 (0)