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

Create a new scope in collect body #19288

Closed
wants to merge 3 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Dec 24, 2021

Fixes a part of #19287, previously collect didn't create a new scope so all variables defined inside of collect call were available outside.

Also changed newTree(nnkStmtList) to newStmtList for shorter code

@ghost
Copy link
Author

ghost commented Dec 24, 2021

I'm confused about newBlockStmt - since in this case, we need the block to return a value should we maybe use nnkBlockExpr instead? But I don't see any info about it.

@metagn
Copy link
Collaborator

metagn commented Dec 25, 2021

I'm confused about newBlockStmt - since in this case, we need the block to return a value should we maybe use nnkBlockExpr instead? But I don't see any info about it.

I don't think there's a semantic difference between them (this should be documented if it hasn't already), so you can't use them to error if it doesn't return an expression. They are just reflective of the syntax the parser encounters. For example this compiles and runs fine:

(if true:
  echo "a"
else:
  echo "b")

But is parsed as:

StmtListExpr
  IfStmt
    ElifExpr
      Ident "true"
      StmtList
        Command
          Ident "echo"
          StrLit "a"
    ElseExpr
      StmtList
        Command
          Ident "echo"
          StrLit "b"

In this case using Expr helps describe what the macro is outputting, but in general I wouldn't use the Expr variants for macro outputs as they do not seem to be the "default" versions. But it doesn't matter.

@Araq
Copy link
Member

Araq commented Dec 25, 2021

The reason it was done this way is that you can easily introduce a new scope via block on your own, but you cannot easily remove it when the macro does it for you. It's not a bug.

@SolitudeSF
Copy link
Contributor

it looks like a bug, and i bet no one would expect it not to open new scope.

@stale
Copy link

stale bot commented Dec 26, 2022

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.

@konsumlamm
Copy link
Contributor

The reason it was done this way is that you can easily introduce a new scope via block on your own, but you cannot easily remove it when the macro does it for you.

If you want to use variables from inside the collect: block, you can just declare them outside of it, so I don't really buy this argument.

Copy link
Contributor

This pull request is stale because it has been open for 1 year with no activity. Contribute more commits on the pull request and rebase it on the latest devel, or it will be closed in 30 days. Thank you for your contributions.

@github-actions github-actions bot added the stale Staled PR/issues; remove the label after fixing them label Mar 11, 2024
Copy link
Contributor

This pull request has been marked as stale and closed due to inactivity after 395 days.

@github-actions github-actions bot closed this Apr 14, 2024
@Araq Araq reopened this Apr 14, 2024
@Araq Araq added merge_when_passes_CI mergeable once green and removed stale Staled PR/issues; remove the label after fixing them labels Apr 14, 2024
@ghost ghost closed this by deleting the head repository Jun 7, 2024
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge_when_passes_CI mergeable once green
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants