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

compiler crash on len of varargs[untyped] #24258

Closed
arnetheduck opened this issue Oct 8, 2024 · 6 comments · Fixed by #24307
Closed

compiler crash on len of varargs[untyped] #24258

arnetheduck opened this issue Oct 8, 2024 · 6 comments · Fixed by #24307

Comments

@arnetheduck
Copy link
Contributor

arnetheduck commented Oct 8, 2024

Description

template encodeList*(args: varargs[untyped]): seq[byte] =
  @[byte args.len]

let x = encodeList([22], 43)

Nim Version

2.0.8

Current Output

nim c -r "/home/arnetheduck/status/nimbus-eth1/_exper/testit.nim"
.......................................................................................................................assertions.nim(34)       raiseAssert
Error: unhandled exception: renderer.nim(1313, 7) `n.len == 2` 3 [AssertionDefect]

Expected Output

No response

Known Workarounds

No response

Additional Information

No response

@ringabout
Copy link
Member

ringabout commented Oct 8, 2024

You probably need to use varargsLen

template encodeList*(args: varargs[untyped]): seq[byte] =
  @[byte varargsLen(args)]

let x = encodeList([22], 43)
echo x

@arnetheduck
Copy link
Contributor Author

You probably need to use varargsLen

... but why? len is a perfectly fine name for this.

@arnetheduck
Copy link
Contributor Author

template x(v: varargs[int]) =
  echo v.len

x(2, 3, 4)

for comparison, this works

@ringabout
Copy link
Member

ringabout commented Oct 8, 2024

In addition to the untyped meta-type that prevents type checking, there is also varargs[untyped] so that not even the number of parameters is fixed:

varargs[int] type checks the parameters, which means 2, 3, 4 are passed as an array form. It equals to len([1, 2, 3]). varargs[untyped] passes the parameters as they are. So it is equal to len(1, 2, 3) which is not valid.

@arnetheduck
Copy link
Contributor Author

sure, but why a separate name for it? it's conceptually the same thing except for some implementation details.

@ringabout
Copy link
Member

I don't know. But in #12907

It said that

IMO len should be made to just work on varargs by making this case behave as the following:

as I mentioned above, it can't, it'd introduce inherent ambiguities due to the way templates work and varargs gets unboxed

@Araq Araq closed this as completed in 8b39b2d Oct 14, 2024
narimiran pushed a commit that referenced this issue Oct 24, 2024
fixes #24258

It uses conditionals to guard against ill formed AST to produce better
error messages, rather than crashing

(cherry picked from commit 8b39b2d)
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.

2 participants