Skip to content

Commit 1917ebf

Browse files
committed
minor refactorings
1 parent 649bf32 commit 1917ebf

File tree

5 files changed

+34
-22
lines changed

5 files changed

+34
-22
lines changed

compiler/ast.nim

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,3 +1832,21 @@ proc addParam*(procType: PType; param: PSym) =
18321832
template destructor*(t: PType): PSym = t.attachedOps[attachedDestructor]
18331833
template assignment*(t: PType): PSym = t.attachedOps[attachedAsgn]
18341834
template asink*(t: PType): PSym = t.attachedOps[attachedSink]
1835+
1836+
const magicsThatCanRaise = {
1837+
mNone, mSlurp, mStaticExec, mParseExprToAst, mParseStmtToAst}
1838+
1839+
proc canRaiseConservative*(fn: PNode): bool =
1840+
if fn.kind == nkSym and fn.sym.magic notin magicsThatCanRaise:
1841+
result = false
1842+
else:
1843+
result = true
1844+
1845+
proc canRaise*(fn: PNode): bool =
1846+
if fn.kind == nkSym and (fn.sym.magic notin magicsThatCanRaise or
1847+
{sfImportc, sfInfixCall} * fn.sym.flags == {sfImportc}):
1848+
result = false
1849+
else:
1850+
result = fn.typ != nil and ((fn.typ.n[0].len < effectListLen) or
1851+
(fn.typ.n[0][exceptionEffects] != nil and
1852+
fn.typ.n[0][exceptionEffects].safeLen > 0))

compiler/cgen.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,17 @@ proc getModuleDllPath(m: BModule, s: PSym): Rope =
104104

105105
import macros
106106

107-
proc cgFormatValue(result: var string; value: Rope): void =
107+
proc cgFormatValue(result: var string; value: Rope) =
108108
for str in leaves(value):
109109
result.add str
110110

111-
proc cgFormatValue(result: var string; value: string): void =
111+
proc cgFormatValue(result: var string; value: string) =
112112
result.add value
113113

114-
proc cgFormatValue(result: var string; value: BiggestInt): void =
114+
proc cgFormatValue(result: var string; value: BiggestInt) =
115115
result.addInt value
116116

117-
proc cgFormatValue(result: var string; value: Int128): void =
117+
proc cgFormatValue(result: var string; value: Int128) =
118118
result.addInt128 value
119119

120120
# TODO: please document

compiler/dfa.nim

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -693,14 +693,6 @@ proc genDef(c: var Con; n: PNode) =
693693
elif isAnalysableFieldAccess(n, c.owner):
694694
c.code.add Instr(n: n, kind: def, sym: nil)
695695

696-
proc canRaise(fn: PNode): bool =
697-
const magicsThatCanRaise = {
698-
mNone, mSlurp, mStaticExec, mParseExprToAst, mParseStmtToAst}
699-
if fn.kind == nkSym and fn.sym.magic notin magicsThatCanRaise:
700-
result = false
701-
else:
702-
result = true
703-
704696
proc genCall(c: var Con; n: PNode) =
705697
gen(c, n[0])
706698
var t = n[0].typ
@@ -715,7 +707,7 @@ proc genCall(c: var Con; n: PNode) =
715707
# optimizer.
716708
genDef(c, n[i])
717709
# every call can potentially raise:
718-
if c.inTryStmt > 0 and canRaise(n[0]):
710+
if c.inTryStmt > 0 and canRaiseConservative(n[0]):
719711
# we generate the instruction sequence:
720712
# fork lab1
721713
# goto exceptionHandler (except or finally)

compiler/sempass2.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,8 @@ proc track(tracked: PEffects, n: PNode) =
708708
of nkCallKinds:
709709
# p's effects are ours too:
710710
var a = n[0]
711+
#if canRaise(a):
712+
# echo "this can raise ", tracked.config $ n.info
711713
let op = a.typ
712714
if n.typ != nil:
713715
if tracked.owner.kind != skMacro and n.typ.skipTypes(abstractVar).kind != tyOpenArray:

lib/system/io.nim

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ proc open*(f: var File, filename: string,
522522
##
523523
## Default mode is readonly. Returns true iff the file could be opened.
524524
## This throws no exception if the file could not be opened.
525-
var p: pointer = fopen(filename, FormatOpen[mode])
525+
var p = fopen(filename, FormatOpen[mode])
526526
if p != nil:
527527
when defined(posix) and not defined(nimscript):
528528
# How `fopen` handles opening a directory is not specified in ISO C and
@@ -547,15 +547,13 @@ proc reopen*(f: File, filename: string, mode: FileMode = fmRead): bool {.
547547
## file variables.
548548
##
549549
## Default mode is readonly. Returns true iff the file could be reopened.
550-
var p: pointer = freopen(filename, FormatOpen[mode], f)
551-
result = p != nil
550+
result = freopen(filename, FormatOpen[mode], f) != nil
552551
553552
proc open*(f: var File, filehandle: FileHandle,
554553
mode: FileMode = fmRead): bool {.tags: [], raises: [], benign.} =
555554
## Creates a ``File`` from a `filehandle` with given `mode`.
556555
##
557556
## Default mode is readonly. Returns true iff the file could be opened.
558-
559557
f = c_fdopen(filehandle, FormatOpen[mode])
560558
result = f != nil
561559
@@ -582,7 +580,7 @@ proc getFilePos*(f: File): int64 {.benign.} =
582580
583581
proc getFileSize*(f: File): int64 {.tags: [ReadIOEffect], benign.} =
584582
## retrieves the file size (in bytes) of `f`.
585-
var oldPos = getFilePos(f)
583+
let oldPos = getFilePos(f)
586584
discard c_fseek(f, 0, 2) # seek the end of the file
587585
result = getFilePos(f)
588586
setFilePos(f, oldPos)
@@ -639,7 +637,7 @@ when defined(windows) and not defined(nimscript):
639637
importc: when defined(bcc): "setmode" else: "_setmode",
640638
header: "<io.h>".}
641639
var
642-
O_BINARY {.importc: "_O_BINARY", header:"<fcntl.h>".}: cint
640+
O_BINARY {.importc: "_O_BINARY", header: "<fcntl.h>".}: cint
643641
644642
# we use binary mode on Windows:
645643
c_setmode(c_fileno(stdin), O_BINARY)
@@ -731,9 +729,11 @@ iterator lines*(filename: string): TaintedString {.tags: [ReadIOEffect].} =
731729
## buffer.add(line.replace("a", "0") & '\x0A')
732730
## writeFile(filename, buffer)
733731
var f = open(filename, bufSize=8000)
734-
defer: close(f)
735-
var res = TaintedString(newStringOfCap(80))
736-
while f.readLine(res): yield res
732+
try:
733+
var res = TaintedString(newStringOfCap(80))
734+
while f.readLine(res): yield res
735+
finally:
736+
close(f)
737737
738738
iterator lines*(f: File): TaintedString {.tags: [ReadIOEffect].} =
739739
## Iterate over any line in the file `f`.

0 commit comments

Comments
 (0)