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

Callsite as experimental #11864

Closed
wants to merge 7 commits into from
Closed

Conversation

krux02
Copy link
Contributor

@krux02 krux02 commented Aug 1, 2019

This PR pushes callsite() to the experimental state. This means that the compiler only needs to generate any of the deepCopy tries for nOrig, when the feature callsite is actually requested. This PR also refactors a lot of code, so that it does not require callsite anymore.

These are the two major reasons to remove it.

related branch: callsite-lineinfo

Edit:

A common use case is to use callsite just to get line information from the macro invocation. When callsite disappears entirely, code like that breaks. I think a better solution would be if callsite would return an empty node, but with the line information that is requested.

@timotheecour
Copy link
Member

timotheecour commented Aug 1, 2019

I'm all for removing redundant features, but as I was mentioning here #11805 (comment) there are cases where callsite is needed, as it provides additional information not available otherwise.

Before #11822 (which needed a compiler patch to implement alias that works with all symbols) here's how I was implementing alias in library code (looks rather ugly/complicated but anything simpler would fail in more cases, I can share my test suite if needed)

it relied on callsite to disambiguate:

  template aliasImpl(extra, exp, name) =
    template exp2()=exp
    extra

    macro name(): untyped = # overload with 0 args
      let c = callsite() # here's where I needed `callsite`
      result = getAst(exp2())
      if kind(c) == nnkCall: result = newCall(result)
      else: doAssert kind(c) == nnkIdent

    macro name(arg0: untyped, args: varargs[untyped]): untyped = # overload with > 0 args
      result = newCall(getAst(exp2()), arg0)
      for ai in items(args): result.add ai

  macro alias*(def: untyped): untyped = # entry point
    let (name, exp) = splitDefinition(def) # from #11824
    var extra: NimNode
    # caveat: doesn't work with, for example; `macro foo(a=1, b=21): untyped`: compiles(foo) fails
    if exp.kind == nnkIdent:
      extra = quote do:
        static: assert declared(`exp`)
    else:
      extra = quote do:
        static: assert compiles(`exp`)
    result = getAst(aliasImpl(extra, exp, name))

Note:
the simple: template name: untyped = exp wouldn't work as well, for example in following case:

  proc t():auto=42
  alias:ta=t
  # assert ta() == t() # what we would like to have
  assert ta()() == t() # what we have instead: violates what an alias should be: something that
    # can be used in same way as original symbol

note

even that implementation wasn't working in important cases (and probably has non-negligible compile time cost), hence the need for #11822 which works in all cases and should have 0 compile time overhead. Obviously if #11822 is merged I won't miss callsite as much but there may be other use cases where the extra information it provides is needed

would the following be possible

  • un-deprecate callsite and remove the wording: This feature is on the way out of the laguage. Only use it for legacy code, but require the use of {.experimental:"callsiteAccess" .} (that you've introduced) to use it, so that code that needs the information it provides can continue to use it in the future

@narimiran
Copy link
Member

narimiran commented Nov 11, 2019

@krux02 can you rebase this onto the latest devel?

compiler/semcall.nim Outdated Show resolved Hide resolved
@stale
Copy link

stale bot commented Apr 18, 2021

This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions.

@stale stale bot added the stale Staled PR/issues; remove the label after fixing them label Apr 18, 2021
@stale stale bot closed this May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Staled PR/issues; remove the label after fixing them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants