Skip to content

Unexpected Behavior of return in Switch Block  #20739

@danstur

Description

@danstur

Prerequisites

Steps to reproduce

Executing the following script in PowerShell 7.4.0 results in an unexpected behavior which also differs from PowerShell 5.1

function foo($x) {
    $val = switch ($x){
        1 { 5 }
        default {
            return 10 
        }
    }
    Write-Host "Val is $val" 
    $val + 1
}

Write-Host "foo 1: $(foo 1)"
Write-Host "foo 2: $(foo 2)"

PS 5.1 output:

Val is 5
foo 1: 6
foo 2: 10

PS 7.4.0 output:

Val is 5
foo 1: 6    
foo 2:

It's not clear if this is a bug or simply a documente change from classic PowerShell, but it certainly behaves in a way I don't find intuitive.

Expected behavior

PS> <execute script>
Val is 5
foo 1: 6
foo 2: 10

Actual behavior

PS> <execute script>
Val is 5
foo 1: 6
foo 2:

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


Name                           Value
----                           -----
PSVersion                      5.1.19041.3570
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.3570
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    7.4-regressionRegression in 7.4In-PRIndicates that a PR is out for the issueNeeds-InvestigationThe behavior reported in the issue is unexpected and needs further investigation.WG-Enginecore PowerShell engine, interpreter, and runtimeWG-NeedsReviewNeeds a review by the labeled Working Group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions