mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-01 17:41:17 +00:00
fixes #25637 This pull request refactors the way the `sfInjectDestructors` flag is set on symbols during lambda lifting in the Nim compiler. The main change is the introduction of a helper procedure to encapsulate the logic for marking symbols that require destructor injection, improving code clarity and maintainability. Refactoring and code quality improvements: * Introduced the `markInjectDestructors` procedure to encapsulate the logic for marking a symbol with the `sfInjectDestructors` flag, ensuring that `backendEnsureMutable` is always called before modifying the symbol's flags. * Replaced direct flag manipulation (`owner.incl sfInjectDestructors` and `prc.incl sfInjectDestructors`) with calls to the new `markInjectDestructors` procedure in multiple locations, including `makeClosure`, `createTypeBoundOpsLL`, and `rawClosureCreation`. [[1]](diffhunk://#diff-19193904ba011a2bcc1e1a9768a7eb57cac57a274cad73d388149776ec2901e6L231-R235) [[2]](diffhunk://#diff-19193904ba011a2bcc1e1a9768a7eb57cac57a274cad73d388149776ec2901e6L243-R247) [[3]](diffhunk://#diff-19193904ba011a2bcc1e1a9768a7eb57cac57a274cad73d388149776ec2901e6L639-R643)
94 lines
1.5 KiB
Nim
94 lines
1.5 KiB
Nim
discard """
|
|
output: '''
|
|
42
|
|
5
|
|
3
|
|
2
|
|
1.0
|
|
2.0
|
|
55
|
|
@[1, 2]
|
|
'''
|
|
"""
|
|
|
|
# Object variant / case object
|
|
type
|
|
NodeKind = enum
|
|
nkInt, nkStr, nkAdd
|
|
|
|
Node = object
|
|
case kind: NodeKind
|
|
of nkInt: intVal: int
|
|
of nkStr: strVal: string
|
|
of nkAdd: left, right: ref Node
|
|
|
|
proc newInt(v: int): ref Node =
|
|
new(result)
|
|
result[] = Node(kind: nkInt, intVal: v)
|
|
|
|
let n = newInt(42)
|
|
echo n.intVal
|
|
|
|
# Sink and move semantics
|
|
type
|
|
BigObj = object
|
|
data: seq[int]
|
|
|
|
proc consume(x: sink BigObj) =
|
|
echo x.data.len
|
|
|
|
var b = BigObj(data: @[1, 2, 3, 4, 5])
|
|
consume(move b)
|
|
|
|
proc divmod(a, b: int): (int, int) =
|
|
(a div b, a mod b)
|
|
|
|
|
|
let (q, r) = divmod(17, 5)
|
|
echo q
|
|
echo r
|
|
|
|
|
|
# Shallow object with seq (trigger GC interaction)
|
|
type
|
|
Matrix = object
|
|
rows, cols: int
|
|
data: seq[float]
|
|
|
|
proc newMatrix(r, c: int): Matrix =
|
|
Matrix(rows: r, cols: c, data: newSeq[float](r * c))
|
|
|
|
proc `[]`(m: Matrix, r, c: int): float =
|
|
m.data[r * m.cols + c]
|
|
|
|
proc `[]=`(m: var Matrix, r, c: int, v: float) =
|
|
m.data[r * m.cols + c] = v
|
|
|
|
var m = newMatrix(2, 2)
|
|
m[0, 0] = 1.0
|
|
m[1, 1] = 2.0
|
|
echo m[0, 0]
|
|
echo m[1, 1]
|
|
|
|
template compute(body: untyped): int =
|
|
block:
|
|
body
|
|
let x = compute:
|
|
var sum = 0
|
|
for i in 1..10: sum += i
|
|
sum
|
|
|
|
echo x
|
|
|
|
# Crash: bridge.nim(206, 5) `allowEmpty` unexpected nkEmpty [AssertionDefect]
|
|
# Bare closure iterator type alias
|
|
type IntIter = iterator(): int {.closure.}
|
|
proc run(it: IntIter): seq[int] =
|
|
result = @[]
|
|
for x in it():
|
|
result.add(x)
|
|
let gen: IntIter = iterator(): int {.closure.} =
|
|
yield 1
|
|
yield 2
|
|
echo run(gen)
|