Skip to content

Commit

Permalink
borrowchecks: fix parameter borrow overlap check
Browse files Browse the repository at this point in the history
Only mutable borrows overlapping with other borrows is a problem.
  • Loading branch information
zerbina committed Sep 17, 2024
1 parent d5c8afd commit 7f9830f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/sem/borrowchecks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,8 @@ proc verifyParamBorrow(config: ConfigRef, c: ControlFlowGraph, start: int) =
of borrow, mborrow:
if instr.borrower.kind in nkCallKinds and instr.borrower != target:
discard "ignore other call's borrows; the mut/use instruction are what's relevant"
elif overlaps(path, instr.n):
elif overlaps(path, instr.n) and mborrow in {c[start].kind, instr.kind}:
# only mutable borrows overlapping with other borrows is a problem
config.localReport(instr.n):
SemReport(kind: rsemOverlappingParamBorrows, usage: path.info,
isProblemMutation: c[start].kind == mborrow)
Expand Down
10 changes: 10 additions & 0 deletions tests/lang_experimental/views/tborrow_checking_3.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ proc error() =
var s = @[1]
a(s, s) # error; mutable borrows of the same location

proc no_error() =
type Container = ref object
children: seq[Container]

proc p(a, b: Container) = discard

var c = Container(children: @[Container()])
p(c, c.children[0])
# not an error, even though this is a dangerous borrow of c.children[0]

proc error_2() =
type Container = object
children: seq[Container]
Expand Down

0 comments on commit 7f9830f

Please sign in to comment.