Skip to content

Commit

Permalink
fixes #10912 (#11317)
Browse files Browse the repository at this point in the history
* fixes #10912

* update the tutorial examples
  • Loading branch information
Araq committed May 24, 2019
1 parent d67a9f0 commit ef8ddef
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
6 changes: 3 additions & 3 deletions compiler/cgmeth.nim
Expand Up @@ -56,7 +56,7 @@ proc methodCall*(n: PNode; conf: ConfigRef): PNode =
type
MethodResult = enum No, Invalid, Yes

proc sameMethodBucket(a, b: PSym): MethodResult =
proc sameMethodBucket(a, b: PSym; multiMethods: bool): MethodResult =
if a.name.id != b.name.id: return
if sonsLen(a.typ) != sonsLen(b.typ):
return
Expand All @@ -75,7 +75,7 @@ proc sameMethodBucket(a, b: PSym): MethodResult =
if sameType(a.typ.sons[i], b.typ.sons[i]):
if aa.kind == tyObject and result != Invalid:
result = Yes
elif aa.kind == tyObject and bb.kind == tyObject:
elif aa.kind == tyObject and bb.kind == tyObject and (i == 1 or multiMethods):
let diff = inheritanceDiff(bb, aa)
if diff < 0:
if result != Invalid:
Expand Down Expand Up @@ -162,7 +162,7 @@ proc methodDef*(g: ModuleGraph; s: PSym, fromCache: bool) =
var witness: PSym
for i in 0 ..< L:
let disp = g.methods[i].dispatcher
case sameMethodBucket(disp, s)
case sameMethodBucket(disp, s, multimethods = optMultiMethods in g.config.globalOptions)
of Yes:
add(g.methods[i].methods, s)
attachDispatcher(s, disp.ast[dispatcherPos])
Expand Down
6 changes: 3 additions & 3 deletions doc/tut2.rst
Expand Up @@ -277,7 +277,7 @@ Procedures always use static dispatch. For dynamic dispatch replace the
a, b: Expression
# watch out: 'eval' relies on dynamic binding
method eval(e: Expression): int =
method eval(e: Expression): int {.base.} =
# override this base method
quit "to override!"
Expand All @@ -300,7 +300,7 @@ In a multi-method all parameters that have an object type are used for the
dispatching:

.. code-block:: nim
:test: "nim c $1"
:test: "nim c --multiMethods:on $1"
type
Thing = ref object of RootObj
Expand Down Expand Up @@ -611,7 +611,7 @@ To pass a block of statements to a template, use 'untyped' for the last paramete
:test: "nim c $1"
template withFile(f: untyped, filename: string, mode: FileMode,
body: untyped): typed =
body: untyped) =
let fn = filename
var f: File
if open(f, fn, mode):
Expand Down
48 changes: 48 additions & 0 deletions tests/method/tsingle_methods.nim
@@ -0,0 +1,48 @@
discard """
cmd: "nim c --multimethods:off $file"
output: '''base
base
base
base
base
base
'''
"""

# bug #10912

type
X = ref object of RootObj

type
A* = ref object of RootObj
B* = ref object of A
C* = ref object of A
D* = ref object of A
E* = ref object of A
F* = ref object of A

method resolve(self: var X, stmt: A) {.base.} = echo "base"

proc resolveSeq*(self: var X, statements: seq[A]) =
for statement in statements:
resolve(self, statement)

method resolve(self: var X, stmt: B) =
echo "B"

method resolve(self: var X, stmt: D) =
echo "D"

method resolve(self: var X, stmt: E) =
echo "E"

method resolve(self: var X, stmt: C) =
echo "C"

method resolve(self: var X, stmt: F) =
echo "F"

var x = X()
var a = @[A(), B(), C(), D(), E(), F()]
resolveSeq(x, a)

0 comments on commit ef8ddef

Please sign in to comment.