Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic dispatch broken if base method returns generic var type #6777

Closed
hohlerde opened this issue Nov 18, 2017 · 1 comment
Closed

Dynamic dispatch broken if base method returns generic var type #6777

hohlerde opened this issue Nov 18, 2017 · 1 comment

Comments

@hohlerde
Copy link

Code example:

type
  Command* = ref object of RootObj
    d*: seq[cuchar]

  CmdSeparation* = ref object of Command
  BaseJob* = ref object of RootObj
  SeparationJob* = ref object of BaseJob

var
  workerThread: Thread[tuple[dummy:int]]
  

proc getJob*[T: BaseJob](t: typedesc[T]): T =
  result = new(T)
  

method serialize*(self: Command): var seq[cuchar] {.base, gcsafe.} =
  echo "Command"
  # the lines below cause warning: use {.base.} for base methods; baseless methods are deprecated [UseBase] for all other serialize methods
  self.d = newSeq[cuchar](10)
  result = self.d


method serialize*(self: CmdSeparation): var seq[cuchar] =
  echo "CmdSeparation"
  result = procCall Command(self).serialize()


proc doCmdWork(cmd: Command) =
  discard cmd.serialize()


method execute*(self: BaseJob) {.base, gcsafe.} =
  echo "BaseJob"


method execute*(self: SeparationJob) = 
  echo "SeparationJob"
  var cmd = CmdSeparation()
  doCmdWork(cmd)
  procCall BaseJob(self).execute()


proc doJobWork(job: BaseJob) =
  job.execute()


proc workerRun(params: tuple[dummy:int]) {.thread.} =
  var job = getJob(SeparationJob)
  doJobWork(job)


proc main*() =
  createThread(workerThread, workerRun, (dummy: 0))
  joinThread(workerThread)


when isMainModule:
  main()

Compiler gives a warning:

test.nim(24, 8) Warning: use {.base.} for base methods; baseless methods are deprecated [UseBase]

Expected console output:

SeparationJob
CmdSeparation
Command
BaseJob

Actual console output:

SeparationJob
Command
BaseJob
@hohlerde
Copy link
Author

Nim version used:

Nim Compiler Version 0.17.3 (2017-11-14) [Windows: i386]
Copyright (c) 2006-2017 by Andreas Rumpf

git hash: 22ceab0fbb29241f72149fe4d53749d57861ba6f
active boot switches: -d:release

Also happens to be the same with amd64 on windows and also seen the same behaviour on a Raspberry Pi running raspbian.

@zah zah added the Generics label Nov 20, 2017
Araq added a commit that referenced this issue May 28, 2019
@Araq Araq closed this as completed in e68adca May 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants