Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure the Nim compiler works with --experimental:strictFuncs --exper… #15737

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion compiler/varpartitions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@ proc borrowingCall(c: var Partitions; destType: PType; n: PNode; i: int) =
localError(c.config, n[i].info, "cannot determine the target of the borrow")

proc borrowingAsgn(c: var Partitions; dest, src: PNode) =
proc mutableParameter(n: PNode): bool {.inline.} =
result = n.kind == nkSym and n.sym.kind == skParam and n.sym.typ.kind == tyVar

if dest.kind == nkSym:
if directViewType(dest.typ) != noView:
borrowFrom(c, dest.sym, src)
Expand All @@ -559,7 +562,11 @@ proc borrowingAsgn(c: var Partitions; dest, src: PNode) =
if vid >= 0:
c.s[vid].flags.incl viewDoesMutate
of immutableView:
localError(c.config, dest.info, "attempt to mutate a borrowed location from an immutable view")
if dest.kind == nkBracketExpr and dest[0].kind == nkHiddenDeref and
mutableParameter(dest[0][0]):
discard "remains a mutable location anyhow"
else:
localError(c.config, dest.info, "attempt to mutate a borrowed location from an immutable view")
of noView: discard "nothing to do"

proc containsPointer(t: PType): bool =
Expand Down
4 changes: 4 additions & 0 deletions tests/views/tcan_compile_nim.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
discard """
cmd: "nim check --hints:on --experimental:strictFuncs --experimental:views compiler/nim.nim"
action: "compile"
"""
11 changes: 11 additions & 0 deletions tests/views/tdont_mutate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,14 @@ proc main() =
echo i, ": ", x

main()

# This has to continue to work:

type
PNode = ref object
TSrcGen = object
comStack: seq[PNode]

proc pushCom(g: var TSrcGen, n: PNode) =
setLen(g.comStack, g.comStack.len + 1)
g.comStack[^1] = n