mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-29 10:43:57 +00:00
Field checks for everybody (#8957)
* Field checks for JS backend * Clean nkCall nodes with no arguments Generating a nkEmpty in place of no arguments makes no sense form the AST point of view and also trips up the VM codegen. * Field checks for VM backend * Test case for #6612 This patchset fixes #6612 * Add test case for LHS double evaluation * Prevent LHS double-eval for JS backend * Prevent double evaluation in VM backend
This commit is contained in:
24
tests/js/t6612.nim
Normal file
24
tests/js/t6612.nim
Normal file
@@ -0,0 +1,24 @@
|
||||
discard """
|
||||
action: "run"
|
||||
"""
|
||||
|
||||
proc fillWith(sq: var seq[int], n: int, unused: string) =
|
||||
sq = @[n]
|
||||
|
||||
type
|
||||
Object = object of RootObj
|
||||
case hasNums: bool
|
||||
of true:
|
||||
numbers: seq[int]
|
||||
of false:
|
||||
discard
|
||||
always: seq[int]
|
||||
|
||||
var obj = Object(hasNums: true)
|
||||
|
||||
obj.always.fillWith(5, "unused")
|
||||
doAssert obj.always == @[5]
|
||||
|
||||
obj.numbers.fillWith(3, "unused")
|
||||
doAssert obj.numbers == @[3]
|
||||
doAssert obj.always == @[5]
|
||||
48
tests/js/tfieldchecks.nim
Normal file
48
tests/js/tfieldchecks.nim
Normal file
@@ -0,0 +1,48 @@
|
||||
discard """
|
||||
output: '''
|
||||
foo
|
||||
C
|
||||
3.14
|
||||
foo
|
||||
3.14
|
||||
3.14
|
||||
'''
|
||||
"""
|
||||
|
||||
type
|
||||
V = enum
|
||||
A, B, C
|
||||
X = object
|
||||
f0: string
|
||||
case f1: V
|
||||
of A: f2: string
|
||||
of B: discard
|
||||
of C: f3: float
|
||||
|
||||
var obj = X(f0: "foo", f1: C, f3: 3.14)
|
||||
|
||||
block:
|
||||
echo obj.f0
|
||||
echo obj.f1
|
||||
doAssertRaises(FieldError): echo obj.f2
|
||||
echo obj.f3
|
||||
|
||||
block:
|
||||
let a0 = addr(obj.f0)
|
||||
echo a0[]
|
||||
# let a1 = unsafeAddr(obj.f1)
|
||||
# echo a1[]
|
||||
doAssertRaises(FieldError):
|
||||
let a2 = addr(obj.f2)
|
||||
echo a2[]
|
||||
let a3 = addr(obj.f3)
|
||||
echo a3[]
|
||||
|
||||
# Prevent double evaluation of LHS
|
||||
block:
|
||||
var flag = false
|
||||
proc wrap(x: X): X =
|
||||
doAssert flag == false
|
||||
flag = true
|
||||
result = x
|
||||
echo wrap(obj).f3
|
||||
Reference in New Issue
Block a user