diff --git a/compiler/sem/borrowchecks.nim b/compiler/sem/borrowchecks.nim index 23fdc5f52d..5e8c2f30e2 100644 --- a/compiler/sem/borrowchecks.nim +++ b/compiler/sem/borrowchecks.nim @@ -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) diff --git a/tests/lang_experimental/views/tborrow_checking_3.nim b/tests/lang_experimental/views/tborrow_checking_3.nim index 5c6bd36c66..0c7d342452 100644 --- a/tests/lang_experimental/views/tborrow_checking_3.nim +++ b/tests/lang_experimental/views/tborrow_checking_3.nim @@ -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]