Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/devel' into pr_strictdefs
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout committed Nov 23, 2024
2 parents 15f8fb4 + 555191a commit d51c09a
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 15 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ rounding guarantees (via the
avoid conflicts with `system.default`, so named argument usage for this
parameter like `getOrDefault(..., default = ...)` will have to be changed.

- With `-d:nimPreviewCheckedClose`, the `close` function in the `std/syncio` module now raises an IO exception in case of an error.

## Standard library additions and changes

[//]: # "Additions:"
Expand Down
1 change: 1 addition & 0 deletions compiler/nim.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ define:nimPreviewCstringConversion
define:nimPreviewProcConversion
define:nimPreviewRangeDefault
define:nimPreviewNonVarDestructor
define:nimPreviewCheckedClose
threads:off

#import:"$projectpath/testability"
Expand Down
5 changes: 4 additions & 1 deletion compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1878,9 +1878,12 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
let target = f.genericHead
let targetKind = target.kind
var effectiveArgType = reduceToBase(a)
# the skipped child of tyBuiltInTypeClass can be structured differently,
# newConstraint constructs them with no children
let typeClassArg = effectiveArgType.kind == tyBuiltInTypeClass
effectiveArgType = effectiveArgType.skipTypes({tyBuiltInTypeClass})
if targetKind == effectiveArgType.kind:
if effectiveArgType.isEmptyContainer:
if not typeClassArg and effectiveArgType.isEmptyContainer:
return isNone
if targetKind == tyProc:
if target.flags * {tfIterator} != effectiveArgType.flags * {tfIterator}:
Expand Down
23 changes: 19 additions & 4 deletions lib/std/syncio.nim
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,26 @@ elif defined(windows):
const
BufSize = 4000

proc close*(f: File) {.tags: [], gcsafe, sideEffect.} =
template closeIgnoreError(f: File) =
## Closes the file.
if not f.isNil:
discard c_fclose(f)


when defined(nimPreviewCheckedClose):
proc close*(f: File) {.tags: [], gcsafe, sideEffect.} =
## Closes the file.
##
## Raises an IO exception in case of an error.
if not f.isNil:
let x = c_fclose(f)
if x < 0:
checkErr(f)
else:
proc close*(f: File) {.tags: [], gcsafe, sideEffect.} =
## Closes the file.
closeIgnoreError(f)

proc readChar*(f: File): char {.tags: [ReadIOEffect].} =
## Reads a single character from the stream `f`. Should not be used in
## performance sensitive code.
Expand Down Expand Up @@ -708,12 +723,12 @@ proc open*(f: var File, filename: string,
# be opened.
var res {.noinit.}: Stat
if c_fstat(getFileHandle(f2), res) >= 0'i32 and modeIsDir(res.st_mode):
close(f2)
closeIgnoreError(f2)
return false
when not defined(nimInheritHandles) and declared(setInheritable) and
NoInheritFlag.len == 0:
if not setInheritable(getOsFileHandle(f2), false):
close(f2)
closeIgnoreError(f2)
return false

result = true
Expand All @@ -736,7 +751,7 @@ proc reopen*(f: File, filename: string, mode: FileMode = fmRead): bool {.
when not defined(nimInheritHandles) and declared(setInheritable) and
NoInheritFlag.len == 0:
if not setInheritable(getOsFileHandle(f), false):
close(f)
closeIgnoreError(f)
return false
result = true

Expand Down
1 change: 1 addition & 0 deletions tests/config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ switch("define", "nimPreviewJsonutilsHoleyEnum")
switch("define", "nimPreviewHashRef")
switch("define", "nimPreviewRangeDefault")
switch("define", "nimPreviewNonVarDestructor")
switch("define", "nimPreviewCheckedClose")

switch("warningAserror", "UnnamedBreak")
when not defined(testsConciseTypeMismatch):
Expand Down
36 changes: 36 additions & 0 deletions tests/misc/tsizeof.nim
Original file line number Diff line number Diff line change
Expand Up @@ -740,3 +740,39 @@ type

doAssert sizeof(ChunkObj) == 1
doAssert offsetOf(ChunkObj, data) == 1


block: # bug #13945
template c_sizeof(T: typedesc): int =
block:
# proc needed pending https://github.com/nim-lang/Nim/issues/13943 D20200409T215527
proc fun2(): int =
{.emit:[result," = sizeof(", T, ");"].}
fun2()

macro test(body: untyped): untyped =
result = newStmtList()
for T in body:
result.add quote do:
let s1 = `T`.sizeof
let s2 = `T`.c_sizeof
doAssert s1 == s2

type FooEmpty = object
const N = 10
type FooEmptyArr = array[N, FooEmpty]
type Bar = object
x: FooEmpty
y: cint

type BarTup = (FooEmpty, cint)
type BarTup2 = (().type, cint)

test:
FooEmpty
FooEmptyArr
Bar
BarTup
BarTup2


25 changes: 15 additions & 10 deletions tests/overload/tvartypeclass.nim
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# issue #13302
block: # issue #13302
proc foo(x: object): int = x.i*2
proc foo(x: var object) = x.i*=2
type Foo = object
i: int
let x = Foo(i: 3)
var y = Foo(i: 4)
doAssert foo(x) == 6
foo(y)
doAssert y.i == 8

proc foo(x: object): int = x.i*2
proc foo(x: var object) = x.i*=2
type Foo = object
i: int
let x = Foo(i: 3)
var y = Foo(i: 4)
doAssert foo(x) == 6
foo(y)
doAssert y.i == 8
block: # issue #24449
proc p(x: var seq)= discard
proc p(x: seq)= discard
var s : seq[int]
p(s)

0 comments on commit d51c09a

Please sign in to comment.