Skip to content

Commit

Permalink
Merge #544
Browse files Browse the repository at this point in the history
544: disallow `sink openArray[T]` types r=saem a=zerbina

## Summary
No specification for how `sink openArray[T]` should work exists, and the current implementation only works without issues in the simple case where the argument is a literal array-constructor expression that is not constant.

For most other cases, either the `seq` or `string` used as the argument are not cleaned up or, for constant expression, a segmentation fault occurs at run-time when trying to mutate the `openArray`.

Passing an immutable `openArray` to a `sink openArray`, while in theory supported, also results in a run-time error.

## Details
- remove the "cannot create implicit openArray` report; it's unused now
- disable the test that made use of `sink openArray`



Co-authored-by: zerbina <[email protected]>
  • Loading branch information
bors[bot] and zerbina authored Feb 15, 2023
2 parents b4f72b4 + 5330cd7 commit 26e87bd
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 14 deletions.
1 change: 0 additions & 1 deletion compiler/ast/report_enums.nim
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,6 @@ type
rsemCannotMixTypesAndValuesInTuple
rsemExpectedTypelessDeferBody
rsemInvalidBindContext
rsemCannotCreateImplicitOpenarray
rsemCannotAssignToDiscriminantWithCustomDestructor
rsemUnavailableTypeBound

Expand Down
3 changes: 0 additions & 3 deletions compiler/front/cli_reporter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2056,9 +2056,6 @@ proc reportBody*(conf: ConfigRef, r: SemReport): string =
"destructor.\nIt is best to factor out piece of object that needs " &
"custom destructor into separate object or not use discriminator assignment"

of rsemCannotCreateImplicitOpenarray:
result = "cannot create an implicit openArray copy to be passed to a sink parameter"

of rsemWrongNumberOfQuoteArguments:
assert false, "UNUSED"

Expand Down
4 changes: 0 additions & 4 deletions compiler/sem/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,6 @@ proc passCopyToSink(n: PNode; c: var Con; s: var Scope): PNode =
if c.graph.config.selectedGC in {gcArc, gcOrc}:
assert(not containsManagedMemory(n.typ))

if n.typ.skipTypes(abstractInst).kind in {tyOpenArray, tyVarargs}:
localReport(c.graph.config, n.info, reportAst(
rsemCannotCreateImplicitOpenarray, n))

result.add newTree(nkAsgn, tmp, p(n, c, s, normal))
# Since we know somebody will take over the produced copy, there is
# no need to destroy it.
Expand Down
3 changes: 2 additions & 1 deletion compiler/sem/typeallowed.nim
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind,
result = typeAllowedAux(marker, t[0], kind, c, flags+{taIsOpenArray})
of tySink:
# you cannot nest openArrays/sinks/etc.
if kind != skParam or taIsOpenArray in flags or t[0].kind in {tySink, tyLent, tyVar}:
if kind != skParam or taIsOpenArray in flags or
t[0].kind in {tySink, tyLent, tyVar, tyOpenArray}:
result = t
else:
result = typeAllowedAux(marker, t[0], kind, c, flags)
Expand Down
10 changes: 5 additions & 5 deletions tests/lang_objects/destructor/tmove_objconstr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ type
TableNonCopyable = object
x: seq[(string, MySeqNonCopyable)]

proc toTable(pairs: sink openArray[(string, MySeqNonCopyable)]): TableNonCopyable =
discard


let mytable = {"a": newMySeq(2, 5.0)}.toTable
# XXX: ``sink openArray[T]`` is currently disallowed
when false:
proc toTable(pairs: sink openArray[(string, MySeqNonCopyable)]): TableNonCopyable =
discard

let mytable = {"a": newMySeq(2, 5.0)}.toTable

0 comments on commit 26e87bd

Please sign in to comment.