tmethod_issues.nim 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. discard """
  2. matrix: "--mm:arc; --mm:refc"
  3. output: '''
  4. wof!
  5. wof!
  6. type A
  7. type B
  8. '''
  9. """
  10. # bug #1659
  11. type Animal {.inheritable.} = ref object
  12. type Dog = ref object of Animal
  13. method say(a: Animal): auto {.base.} = "wat!"
  14. method say(a: Dog): auto = "wof!"
  15. proc saySomething(a: Animal): auto = a.say()
  16. method ec(a: Animal): auto {.base.} = echo "wat!"
  17. method ec(a: Dog): auto = echo "wof!"
  18. proc ech(a: Animal): auto = a.ec()
  19. var a = Dog()
  20. echo saySomething(a)
  21. ech a
  22. # bug #2401
  23. type MyClass = ref object of RootObj
  24. method HelloWorld*(obj: MyClass) {.base.} =
  25. when defined(myPragma):
  26. echo("Hello World")
  27. # discard # with this line enabled it works
  28. var obj = MyClass()
  29. obj.HelloWorld()
  30. # bug #5432
  31. type
  32. Iterator[T] = ref object of RootObj
  33. # base methods with `T` in the return type are okay
  34. method methodThatWorks*[T](i: Iterator[T]): T {.base.} =
  35. discard
  36. # base methods without `T` (void or basic types) fail
  37. method methodThatFails*[T](i: Iterator[T]) {.base.} =
  38. discard
  39. type
  40. SpecificIterator1 = ref object of Iterator[string]
  41. SpecificIterator2 = ref object of Iterator[int]
  42. # bug #3431
  43. type
  44. Lexer = object
  45. buf*: string
  46. pos*: int
  47. lastchar*: char
  48. ASTNode = object
  49. method init*(self: var Lexer; buf: string) {.base.} =
  50. self.buf = buf
  51. self.pos = 0
  52. self.lastchar = self.buf[0]
  53. method init*(self: var ASTNode; val: string) =
  54. discard
  55. # bug #3370
  56. type
  57. RefTestA*[T] = ref object of RootObj
  58. data*: T
  59. method tester*[S](self: S): bool =
  60. true
  61. type
  62. RefTestB* = RefTestA[(string, int)]
  63. method tester*(self: RefTestB): bool =
  64. true
  65. type
  66. RefTestC = RefTestA[string]
  67. method tester*(self: RefTestC): bool =
  68. false
  69. # bug #3468
  70. type X = ref object of RootObj
  71. type Y = ref object of RootObj
  72. method draw*(x: X) {.base.} = discard
  73. method draw*(y: Y) {.base.} = discard
  74. # bug #3550
  75. type
  76. BaseClass = ref object of RootObj
  77. Class1 = ref object of BaseClass
  78. Class2 = ref object of BaseClass
  79. method test(obj: Class1, obj2: BaseClass) =
  80. discard
  81. method test(obj: Class2, obj2: BaseClass) =
  82. discard
  83. var obj1 = Class1()
  84. var obj2 = Class2()
  85. obj1.test(obj2)
  86. obj2.test(obj1)
  87. # -------------------------------------------------------
  88. # issue #16516
  89. type
  90. A = ref object of RootObj
  91. x: int
  92. B = ref object of A
  93. method foo(v: sink A, lst: var seq[A]) {.base,locks:0.} =
  94. echo "type A"
  95. lst.add v
  96. method foo(v: sink B, lst: var seq[A]) =
  97. echo "type B"
  98. lst.add v
  99. proc main() =
  100. let
  101. a = A(x: 5)
  102. b: A = B(x: 5)
  103. var lst: seq[A]
  104. foo(a, lst)
  105. foo(b, lst)
  106. main()
  107. block: # bug #20391
  108. type Container[T] = ref object of RootRef
  109. item: T
  110. let a = Container[int]()
  111. doAssert a of Container[int]
  112. doAssert not (a of Container[string])