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

toOpenArray fails when inside nkStmtListExpr #12042

Closed
dom96 opened this issue Aug 25, 2019 · 0 comments
Closed

toOpenArray fails when inside nkStmtListExpr #12042

dom96 opened this issue Aug 25, 2019 · 0 comments

Comments

@dom96
Copy link
Contributor

dom96 commented Aug 25, 2019

I'm trying to use toOpenArray for a library I'm working on, in order to get around the limitation that procedures cannot return an openarray I am using templates. The problem is that as soon as I introduce any more statements to my template I start getting errors.

Example

template newOpenArray(x: var string, size: int): openArray[char] =
  var z = 1
  toOpenArray(x, z, size)

template doSomethingAndCreate(x: var string): openArray[char] =
  let size = 12
  newOpenArray(x, size)

var x = "asdqweqweqweqwrqwrqwrqwr"
# echo toOpenArray(x, 0, 2)
# echo newOpenArray(x, 12)
echo doSomethingAndCreate(x)

Current Output

a3.nim(13, 26) Error: invalid context for 'toOpenArray';  'toOpenArray' is only valid within a call expression

Expected Output

['s', 'd', 'q', 'w', 'e', 'q', 'w', 'e', 'q', 'w', 'e', 'q']

Possible Solution

ccgcalls.openArrayLoc only checks for the mSlice magic if it appears at the top-level, it doesn't check if it's nested inside a nkStmtListExpr. The code instead moves into the second branch and calls initLocExpr which eventually enters code that raises the exception.

I was able to come up with some really hacky workaround in the compiler code. I'm curious what the correct way to fix this is though, where should this nkStmtListExpr be processed?

Additional Information

$ nim -v
Nim Compiler Version 0.20.99 [MacOSX: amd64]
Compiled at 2019-08-06
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: 8407a574992ebd6bccec647a902cf54a4de8db18
active boot switches: -d:release
@Araq Araq added this to the v1 milestone Aug 27, 2019
@Araq Araq added the Severe label Aug 27, 2019
Araq added a commit that referenced this issue Aug 29, 2019
@Araq Araq closed this as completed in 2a3b056 Aug 29, 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

2 participants