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
return
on the RHS of an assignment ignores the assignment
#13146
Comments
The RHS is executed before any assignment happens. Works the same as any other flow control statement like |
As far as I know, assignment has always allowed full statements on the RHS. Because of this arrangement of the grammar, it would be very difficult, if not impossible, to turn this into a parse error. If nothing else, it would turn the RHS of assignments into a strange and confusing subset of the allowed statement grammar. I'd say this is most like the old |
Thanks, @SeeminglyScience and @rjmholt - I'm personally happy with a "won't-fix" / "as-designed" classification , but I wanted the potential pitfall recorded. Perhaps a candidate for the docs wiki? |
I've updated the SO answer based on the feedback, so I'm happy to close this. As an aside, @rjmholt: there is a second answer there that summarizes (and links to) our discussion in #10967 (using statements in pipelines / as expressions). |
I could have sworn there was a time back in the dark ages of v2 or 1 where you couldn't have an |
@SeeminglyScience You are right, and it was relaxed to allow statement on the right hand side of assignment in v3. |
Hmm... I just tried However, something related is indeed broken, all the way up to v5.1 (fine since v6), although the scenario is very specific: # Breaks in *Windows PowerShell* - OK in PowerShell Core.
@{
Foo = if ($true) { "yes" } # Value is an `if` statement that LACKS AN `else` and there's NO OTHER ENTRY on the SAME LINE
Bar = 'Baz' # !! Any key AFTER triggers the error
} |
See that's what I thought but then:
I tried it too with At least I know I'm not crazy 😀 |
That's good to know ( |
Full disclosure, that's what I've heard. I don't remember anything specific or having ran into anything personally (though I also don't use it super often) so ymmv |
Ah, then I must remember it wrong. It could be a v1 -> v2 change. I doubt it would be ported back to v2 if it was done in v3, as v3 was a huge rewritten of a lot stuff.
I think this one was fix by #7002 |
Thanks, @daxian-dbw. Can you tell us definitely if there can ever be a difference between running a later version with |
I don't think there is any difference between running a stand-alone v2 version and running I believe the v2 assemblies that are in-box for |
Note: This may be an exotic scenario that is easily avoided (no reason to ever use
return
this way), but I just came across it on Stack Overflow, and the behavior is surprising and unhelpful.Steps to reproduce
Expected behavior
The test should succeed.
Actual behavior
The test fails, because the assignment is ignored (the variable isn't even created) due to use of
return
.The output goes straight to the success output stream and therefore prints to the display here.
Environment data
The text was updated successfully, but these errors were encountered: