diff options
Diffstat (limited to 'tests/cpp/tconstructor.nim')
-rw-r--r-- | tests/cpp/tconstructor.nim | 84 |
1 files changed, 80 insertions, 4 deletions
diff --git a/tests/cpp/tconstructor.nim b/tests/cpp/tconstructor.nim index d4d6a7ccf..922ee54fd 100644 --- a/tests/cpp/tconstructor.nim +++ b/tests/cpp/tconstructor.nim @@ -3,6 +3,20 @@ discard """ cmd: "nim cpp $file" output: ''' 1 +0 +123 +0 +123 +___ +0 +777 +10 +123 +0 +777 +10 +123 +() ''' """ @@ -33,7 +47,7 @@ type NimClassNoNarent* = object x: int32 proc makeNimClassNoParent(x:int32): NimClassNoNarent {. constructor.} = - this.x = x + result.x = x discard let nimClassNoParent = makeNimClassNoParent(1) @@ -45,11 +59,73 @@ var nimClassNoParentDef {.used.}: NimClassNoNarent #test has a default construc type NimClass* = object of CppClass proc makeNimClass(x:int32): NimClass {. constructor:"NimClass('1 #1) : CppClass(0, #1) ".} = - this.x = x + result.x = x #optinially define the default constructor so we get rid of the cpp warn and we can declare the obj (note: default constructor of 'tyObject_NimClass__apRyyO8cfRsZtsldq1rjKA' is implicitly deleted because base class 'CppClass' has no default constructor) proc makeCppClass(): NimClass {. constructor: "NimClass() : CppClass(0, 0) ".} = - this.x = 1 + result.x = 1 let nimClass = makeNimClass(1) -var nimClassDef {.used.}: NimClass #since we explictly defined the default constructor we can declare the obj \ No newline at end of file +var nimClassDef {.used.}: NimClass #since we explictly defined the default constructor we can declare the obj + +#bug: 22662 +type + BugClass* = object + x: int # Not initialized + +proc makeBugClass(): BugClass {.constructor.} = + discard + +proc main = + for i in 0 .. 1: + var n = makeBugClass() + echo n.x + n.x = 123 + echo n.x + +main() +#bug: +echo "___" +type + NimClassWithDefault = object + x: int + y = 777 + case kind: bool = true + of true: + z: int = 10 + else: discard + +proc makeNimClassWithDefault(): NimClassWithDefault {.constructor.} = + result = NimClassWithDefault() + +proc init = + for i in 0 .. 1: + var n = makeNimClassWithDefault() + echo n.x + echo n.y + echo n.z + n.x = 123 + echo n.x + +init() + +#tests that the ctor is not declared with nodecl. +#nodelc also prevents the creation of a default one when another is created. +type Foo {.exportc.} = object + +proc makeFoo(): Foo {.used, constructor, nodecl.} = discard + +echo $Foo() + +type Boo = object +proc `=copy`(dest: var Boo; src: Boo) = discard + +proc makeBoo(): Boo {.constructor.} = Boo() +proc makeBoo2(): Boo = Boo() + +block: + proc main = + var b = makeBoo() + var b2 = makeBoo2() + + main() \ No newline at end of file |