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

Symbol resolution bug when template that uses method call syntax is used from another module #11733

Closed
johnnovak opened this issue Jul 14, 2019 · 4 comments · Fixed by #22111
Closed

Comments

@johnnovak
Copy link
Contributor

When a template that uses the method call syntax is called from another module, there is a compilation error saying that the referenced method could not be found. If the call is rewritten using the regular function call syntax (object in the first argument), the error goes away.

Example

main.nim

type ObjA* = object

proc foo(o: var ObjA) =
  echo "foo"

proc bar(o: var ObjA, arg: Natural) =
  echo "bar ", arg

# Case 1
template privateTemplate(o: var ObjA, arg: Natural, doStuff: untyped) =
  o.foo()
  doStuff
  o.bar(arg)

# Case 2
proc wrappedPrivateTemplate*(o: var ObjA, arg: Natural) =
  o.privateTemplate(arg, echo "  doing wrapped private stuff")

# Case 3
template publicTemplate*(o: var ObjA, arg: Natural, doStuff: untyped) =
  foo(o)
  doStuff
  bar(o, arg)

# Case 4
template publicTemplateObjSyntax*(o: var ObjA, arg: Natural, doStuff: untyped) =
  o.foo()
  doStuff
  o.bar(arg)


var a: ObjA
a.privateTemplate(21, echo "  doing private stuff")
a.wrappedPrivateTemplate(84)
a.publicTemplate(42, echo "  doing public stuff")
a.publicTemplateObjSyntax(42, echo "  doing public obj syntax stuff")

submodule.nim

import main

var a: ObjA
a.wrappedPrivateTemplate(84)
a.publicTemplate(42, echo "  doing public stuff")
a.publicTemplateObjSyntax(42, echo "  doing public obj syntax stuff")   # DOES NOT COMPILE!

Current Output

nim c -r main.nim

Note that all cases work correctly when called from a single file, which is expected and good:

  • private template using method call syntax - OK
  • private template using method call syntax wrapped in a public function - OK
  • public template that uses the function call syntax - OK
  • public template that uses the method call syntax - OK
foo
  doing private stuff
bar 21
foo
  doing wrapped private stuff
bar 84
foo
  doing public stuff
bar 42
foo
  doing public obj syntax stuff
bar 42

nim c -r submodule.nim

D:\Work\Code\easywave\submodule.nim(6, 2) template/generic instantiation of `publicTemplateObjSyntax` from here
D:\Work\Code\easywave\main.nim(27, 4) Error: attempting to call undeclared routine: 'foo'

Note when the templates are referenced from another module, using the template directly that uses the method call syntax fails at compilation time. When the template is rewritten to use the function call syntax, using the template from another module works again. Interestingly, calling a public method that wraps the private template that uses the method call syntax works usually too (but not always, see comment below).

So to summarise, when calling the templates from another module:

  • private template using method call syntax - NOT APPLICABLE
  • private template using method call syntax wrapped in a public function - OK
  • public template that uses the function call syntax - OK
  • public template that uses the method call syntax - COMPILATION ERROR

It seems a bit complicated but the descriptions and the code should make the issue clear.

There are even more complicated cases when the wrapper function approach doesn't work (couldn't isolate that one, sorry), in that case the only workaround is to use the function call syntax in the template.

Expected Output

Calling the public functions/templates should behave the exact same way, regardless whether they are called from the same module or from another module.

Additional Information

  • Tested on Nim 0.20.0, but I remember hitting the same issue with much earlier versions years ago.
Nim Compiler Version 0.20.0 [Windows: amd64]
Compiled at 2019-06-06
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: e7471cebae2a404f3e4239f199f5a0c422484aac
active boot switches: -d:release
@mratsim
Copy link
Collaborator

mratsim commented Jul 15, 2019

This has been a long standing issue, probably since forever:

Given the details you gave, maybe we should close the other issue in favor of this one? cc @nitely.

@Araq
Copy link
Member

Araq commented Jul 15, 2019

I doubt this is the same as #1258 or #7085

@Araq Araq added the Severe label Jul 15, 2019
@johnnovak
Copy link
Contributor Author

I'll see if I can replicate the more complex issue I hinted at, where wrapping the template in a public function doesn't do the trick and you MUST use the function syntax for stuff to compile again.

@metagn
Copy link
Collaborator

metagn commented Apr 22, 2020

#7223 should also be mentioned with the others, similar issue

metagn added a commit to metagn/Nim that referenced this issue Jun 16, 2023
Araq pushed a commit that referenced this issue Jun 16, 2023
add test to close #7223, close #11733

closes #7223, closes #11733, were fixed by #22076
bung87 pushed a commit to bung87/Nim that referenced this issue Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants