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

Typed macro removes std / in imports #19904

Open
HugoGranstrom opened this issue Jun 17, 2022 · 3 comments
Open

Typed macro removes std / in imports #19904

HugoGranstrom opened this issue Jun 17, 2022 · 3 comments

Comments

@HugoGranstrom
Copy link
Contributor

When passing an import using the syntax:

import std / [jsonutils]
import karax / [kbase]

to a typed macro the first part of the import is removed resulting in this:

import jsonutils
import kbase

which gives an error like:

Error: cannot open file: jsonutils

Note: If you replace typed with untyped it works.

Example

import macros

macro identity(body: typed) =
  echo body.repr
  result = body
    
identity:
  import std/[jsonutils]

Current Output

import
  jsonutils

C:\Users\hugog\code\nim\nimib\a.nim(7, 1) template/generic instantiation of `identity` from here
C:\Users\hugog\code\nim\nimib\a.nim(8, 13) Error: cannot open file: jsonutils

Expected Output

import
  std / [jsonutils]

Possible Solution

  • No idea sadly

Additional Information

Have tried it on version 1.6.6, 1.6.0, 1.4.8, 1.4.0, and the latest devel. All give the same error.

$ nim -v
Nim Compiler Version 1.7.1 [Windows: amd64]
Compiled at 2022-06-17
Copyright (c) 2006-2022 by Andreas Rumpf

active boot switches: -d:release
@bung87
Copy link
Collaborator

bung87 commented Sep 27, 2022

weird, I just tried , if you change the module to other like jsonutils -> cstrutils , it will not error you.

@HugoGranstrom
Copy link
Contributor Author

@bung87

weird, I just tried , if you change the module to other like jsonutils -> cstrutils , it will not error you.

It's because some modules in the stdlib requires the std / prefix while others don't. jsonutils is one which requires it while cstrutils doesn't. Try this code for example:

import jsonutils
# Error: cannot open file: jsonutils

While this one works:

import cstrutils

@metagn
Copy link
Collaborator

metagn commented Sep 26, 2024

The compiler expects typed import AST to contain just sym nodes, creating them here. Maybe we could keep the last node as a sym node and the compiler always recurses to find it, or it could become something like import "fullpath" as name (this would also remove the ambiguity of things like import foo in templates when foo is captured as a non-module symbol). We could also support importing sym nodes but macros might still want the original AST.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants