-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
74: Nonref object inheritance hints and converts to base in varargs a… r=saem a=beef331 …nd on assignment This should make it so non ref objects can be used as one expected. `varargs[Base]` can accept `[Base, Derived, Base, Derived2]` and so forth. It also makes it so any object conversion up or down(Though down conversion is explicit so no clue why it checks) emits a hint to inform about loss of possible information. `var a: Base = Derived(field2: 300)` emits `'Derived(field2: 300)' implicitly converted to 'Base' [/home/jason/nimskull/bin/abuse.nim(26, 22)] [ImplicitObjConv]` Co-authored-by: Jason <[email protected]>
- Loading branch information
Showing
11 changed files
with
146 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
discard """ | ||
cmd: "nim check --hints:off $file" | ||
action: reject | ||
nimout: ''' | ||
tinheritance_arrays.nim(20, 18) Error: type mismatch: got <B> but expected 'A = object' | ||
tinheritance_arrays.nim(20, 23) Error: type mismatch: got <C> but expected 'A = object' | ||
tinheritance_arrays.nim(21, 18) Error: type mismatch: got <B> but expected 'C = object' | ||
tinheritance_arrays.nim(21, 23) Error: type mismatch: got <A> but expected 'C = object' | ||
tinheritance_arrays.nim(22, 23) Error: type mismatch: got <A> but expected 'B = object' | ||
''' | ||
""" | ||
|
||
|
||
block: # Value test | ||
type | ||
A = object of RootObj | ||
B = object of A | ||
C = object of A | ||
|
||
discard [A(), B(), C()] | ||
discard [C(), B(), A()] | ||
discard [B(), B(), A()] | ||
discard [B(), B(), B()] | ||
discard [A(), A(), A()] | ||
|
||
block: # ref test | ||
type | ||
A = ref object of RootObj | ||
B = ref object of A | ||
C = ref object of A | ||
|
||
discard [A(), B(), C()] | ||
discard [C(), B(), A()] | ||
discard [B(), B(), A()] | ||
discard [B(), B(), B()] | ||
discard [A(), A(), A()] | ||
|
||
block: # ptr test | ||
type | ||
A = object of RootObj | ||
B = object of A | ||
C = object of A | ||
|
||
template make(t: typedesc): ptr t = | ||
let res = createU(t) | ||
res[] = t() | ||
res | ||
|
||
discard [make A, make B, make C] | ||
discard [make C, make B, make A] | ||
discard [make B, make B, make A] | ||
discard [make B, make B, make B] | ||
discard [make A, make A, make A] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
discard """ | ||
cmd: "nim c --hint[Conf]:off --verbosity:0 $file" | ||
nimout: ''' | ||
Hint: Implicit conversion: Receiver 'Base' will not receive fields of sub-type 'Derived' [tinheritance_conversion.nim(30, 15)] [ImplicitObjConv] | ||
Hint: Implicit conversion: Receiver 'Base' will not receive fields of sub-type 'Derived' [tinheritance_conversion.nim(30, 34)] [ImplicitObjConv] | ||
Hint: Implicit conversion: Receiver 'Base' will not receive fields of sub-type 'Derived2' [tinheritance_conversion.nim(38, 3)] [ImplicitObjConv] | ||
''' | ||
""" | ||
|
||
|
||
type | ||
Base {.inheritable.} = object | ||
field: int | ||
|
||
Derived = object of Base | ||
field2: int | ||
field3: int | ||
Derived2 = object of Base | ||
|
||
block: # Value tests | ||
proc test(args: varargs[Base]) = | ||
for x in args: | ||
assert x.field == 0 | ||
|
||
proc test2(base: var Base) = base.field = 400 | ||
proc test3(base: Base) = discard | ||
var a: Derived = Derived(Base()) | ||
a = Derived(Base(Derived2())) | ||
test(Derived(), Base(), Derived()) | ||
a.field2 = 300 | ||
test2(a) | ||
assert a.field == 400 | ||
assert a.field2 == 300 | ||
var b = Derived2(field: 800) | ||
b.test2() | ||
assert b.field == 400 | ||
b.test3() | ||
|
||
|
||
|
||
block: # Ref tests | ||
type | ||
Base = ref object of RootObj | ||
field: int | ||
Derived = ref object of Base | ||
field2: int | ||
Derived2 = ref object of Base | ||
|
||
var a: Base = Derived() | ||
assert Derived(a) is Derived | ||
doAssertRaises(ObjectConversionDefect): discard Derived2(a)[] | ||
doAssertRaises(ObjectConversionDefect): discard Base(Derived2()).Derived | ||
assert Base(Derived()) is Base | ||
assert Derived2(Base(Derived2())) is Derived2 | ||
assert Derived(Base(Derived())) is Derived | ||
|
||
block: # Pointer tests | ||
template make(t: typedesc): ptr t = | ||
let res = createU(t) | ||
res[] = t() | ||
res | ||
var a: ptr Base = make(Derived) | ||
assert (ptr Derived)(a) is (ptr Derived) | ||
doAssertRaises(ObjectConversionDefect): discard (ptr Derived2)(a)[] | ||
doAssertRaises(ObjectConversionDefect): | ||
var a = make(Derived2) | ||
discard (ptr Derived)((ptr Base)(a)) | ||
assert (ptr Base)(make(Derived)) is (ptr Base) | ||
assert (ptr Derived2)((ptr Base)(make(Derived2))) is (ptr Derived2) | ||
assert (ptr Derived)((ptr Base)(make(Derived))) is (ptr Derived) |