remove {.this.} pragma, deprecated since 0.19 (#20201)
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
parent
3dbf2ac946
commit
f6eb1d4d7d
13 changed files with 28 additions and 158 deletions
|
|
@ -1,15 +0,0 @@
|
|||
|
||||
# bug #4177
|
||||
|
||||
type
|
||||
Parent = object of RootObj
|
||||
parentField: int
|
||||
Child = object of Parent
|
||||
childField: int
|
||||
|
||||
{.this: self.}
|
||||
proc sumFields(self: Child): int =
|
||||
result = parentField + childField # Error: undeclared identifier: 'parentField'
|
||||
|
||||
proc sumFieldsWorks(self: Child): int =
|
||||
result = self.parentField + childField
|
||||
|
|
@ -1,88 +0,0 @@
|
|||
discard """
|
||||
output: "Using test.Closing test."
|
||||
"""
|
||||
|
||||
import
|
||||
macros
|
||||
|
||||
# This macro mimics the using statement from C#
|
||||
#
|
||||
# It's kept only as a test for the macro system
|
||||
# Nim's destructors offer a mechanism for automatic
|
||||
# disposal of resources.
|
||||
#
|
||||
macro autoClose(args: varargs[untyped]): untyped =
|
||||
let e = callsite()
|
||||
if e.len != 3:
|
||||
error "Using statement: unexpected number of arguments. Got " &
|
||||
$e.len & ", expected: 1 or more variable assignments and a block"
|
||||
|
||||
var args = e
|
||||
var body = e[2]
|
||||
|
||||
var
|
||||
variables : seq[NimNode]
|
||||
closingCalls : seq[NimNode]
|
||||
|
||||
newSeq(variables, 0)
|
||||
newSeq(closingCalls, 0)
|
||||
|
||||
for i in countup(1, args.len-2):
|
||||
if args[i].kind == nnkExprEqExpr:
|
||||
var varName = args[i][0]
|
||||
var varValue = args[i][1]
|
||||
|
||||
var varAssignment = newNimNode(nnkIdentDefs)
|
||||
varAssignment.add(varName)
|
||||
varAssignment.add(newNimNode(nnkEmpty)) # empty means no type
|
||||
varAssignment.add(varValue)
|
||||
variables.add(varAssignment)
|
||||
|
||||
closingCalls.add(newCall(newIdentNode("close"), varName))
|
||||
else:
|
||||
error "Using statement: Unexpected expression. Got " &
|
||||
$args[i].kind & " instead of assignment."
|
||||
|
||||
var varSection = newNimNode(nnkVarSection)
|
||||
varSection.add(variables)
|
||||
|
||||
var finallyBlock = newNimNode(nnkStmtList)
|
||||
finallyBlock.add(closingCalls)
|
||||
|
||||
# XXX: Use a template here once getAst is working properly
|
||||
var targetAst = parseStmt"""block:
|
||||
var
|
||||
x = foo()
|
||||
y = bar()
|
||||
|
||||
try:
|
||||
body()
|
||||
|
||||
finally:
|
||||
close x
|
||||
close y
|
||||
"""
|
||||
|
||||
targetAst[0][1][0] = varSection
|
||||
targetAst[0][1][1][0] = body
|
||||
targetAst[0][1][1][1][0] = finallyBlock
|
||||
|
||||
result = targetAst
|
||||
|
||||
type
|
||||
TResource* = object
|
||||
field*: string
|
||||
|
||||
proc openResource(param: string): TResource =
|
||||
result.field = param
|
||||
|
||||
proc close(r: var TResource) =
|
||||
write(stdout, "Closing " & r.field & ".")
|
||||
|
||||
proc use(r: var TResource) =
|
||||
write(stdout, "Using " & r.field & ".")
|
||||
|
||||
autoClose(r = openResource("test")):
|
||||
use r
|
||||
|
||||
write stdout, "\n"
|
||||
16
tests/usingstmt/tusingstmt.nim
Normal file
16
tests/usingstmt/tusingstmt.nim
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
type
|
||||
Foo = object
|
||||
|
||||
using
|
||||
c: Foo
|
||||
x, y: int
|
||||
|
||||
proc usesSig(c) = discard
|
||||
|
||||
proc foobar(c, y) = discard
|
||||
|
||||
usesSig(Foo())
|
||||
foobar(Foo(), 123)
|
||||
doAssert not compiles(usesSig(123))
|
||||
doAssert not compiles(foobar(Foo(), Foo()))
|
||||
doAssert not compiles(foobar(123, 123))
|
||||
Loading…
Add table
Add a link
Reference in a new issue