[ 
https://issues.apache.org/jira/browse/GROOVY-12447?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Daniel Sun resolved GROOVY-12447.
---------------------------------
    Fix Version/s: 7.0.0-alpha-1
         Assignee: Daniel Sun
       Resolution: Fixed

> STC: treat a complete native sealed type-case set as an exhaustive switch 
> expression
> ------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12447
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12447
>             Project: Groovy
>          Issue Type: Improvement
>            Reporter: Daniel Sun
>            Assignee: Daniel Sun
>            Priority: Major
>             Fix For: 7.0.0-alpha-1
>
>
> h2. Context
> GROOVY-12255 compiles switch expressions as first-class AST ([JEP 
> 361|https://openjdk.org/jeps/361]). Under {{@TypeChecked}} and 
> {{@CompileStatic}}, a switch expression is exhaustive when it has a 
> {{default}} arm, or when the selector is an enum and every constant is listed.
> Native sealed types (GROOVY-10148, GROOVY-10233, [JEP 
> 409|https://openjdk.org/jeps/409]) are a closed hierarchy when the target 
> bytecode is JDK 17+ and the sealed mode is native. Their permitted subclasses 
> are known at compile time, the same way enum constants are.
> Groovy already matches {{case Circle}} with {{isCase}} 
> ({{Class.isInstance}}). That type-case form is what this issue uses.
> h2. Problem
> STC rejects a switch expression that names every permitted subclass of a 
> native sealed selector:
> {code:groovy}
> sealed interface Shape permits Circle, Square {}
> final class Circle implements Shape {}
> final class Square implements Shape {}
> String name(Shape s) {
>     return switch (s) {
>         case Circle -> 'c'
>         case Square -> 's'
>     }
> }
> {code}
> The error is {{the switch expression does not cover all possible input 
> values}}. The same program is legal in Java with type patterns ([JEP 
> 441|https://openjdk.org/jeps/441]). Enums already receive this treatment in 
> Groovy.
> h2. Contract
> A switch expression with no {{default}} is exhaustive when the selector type 
> is native sealed and every remaining inhabitant is covered by a {{case Type}} 
> label.
> Inhabitants of the selector are its permitted subclasses, plus the selector 
> type itself when that type is a concrete class.
> A type-case label that names a member of the hierarchy removes remaining 
> inhabitants that are that type or a subtype of it. {{case Circle, Square}} is 
> two labels (the parser emits one {{CaseStatement}} per value). {{case Rect}} 
> covers nested {{Square}} and {{Triangle}}. {{case Square}} and {{case 
> Triangle}} leave {{Rect}} remaining.
> A label outside the hierarchy, including {{case Object}}, is ordinary 
> {{isCase}} and leaves the remaining set unchanged.
> Native sealed is detected as follows:
> * source: {{SealedASTTransformation.wouldBeNativeSealed}} (JDK 17+ target 
> bytecode and native mode), independent of transform order
> * class file: a non-empty permitted-subclass list (the 
> {{PermittedSubclasses}} attribute). Emulated sealed class files keep a 
> runtime {{@Sealed}} and leave that list empty.
> Duplicate permitted-subclass labels reuse the GROOVY-12289 / GROOVY-12406 
> check. The key is the pair (selector type, binary name), so {{case Circle}} 
> and {{case 'Circle'}} are distinct. That check already runs for switch 
> expressions and switch statements.
> ||Selector and labels||Today||This change||
> |native sealed interface, every permitted type|error|exhaustive|
> |one permitted type omitted|error|error|
> |emulated sealed, every permitted type|error|error|
> |concrete {{sealed class}}, subclasses only|error|error|
> |{{case Shape}} on a {{Shape}} selector|error|exhaustive|
> |{{case Object}} only|error|error|
> |{{case Circle}} and {{case Rect}} ({{Rect}} permits {{Square}}, 
> {{Triangle}})|error|exhaustive|
> |{{case Circle}}, {{case Square}}, {{case Triangle}} ({{Rect}} still 
> permitted)|error|error|
> |native sealed type from a class file, every permitted type|error|exhaustive|
> |switch statement, partial type cases|compiles|compiles|
> |enum, every constant (GROOVY-12255)|exhaustive|exhaustive|
> |duplicate {{case Circle}}|compiles|error: {{Duplicate case label: Circle}}|
> A concrete sealed class is itself an inhabitant:
> {code:groovy}
> sealed class Shape permits Circle, Square {}
> final class Circle extends Shape {}
> final class Square extends Shape {}
> String name(Shape s) {
>     return switch (s) {
>         case Circle -> 'c'
>         case Square -> 's'
>     }
> }
> {code}
> STC still reports {{the switch expression does not cover all possible input 
> values}}, because {{new Shape()}} is legal. {{case Shape}} covers the whole 
> hierarchy.
> Switch statements may be partial. Emulated sealed ({{@SealedOptions(mode = 
> SealedMode.EMULATE)}}) still needs a {{default}} arm. Dynamic Groovy still 
> throws {{IllegalStateException}} for an unmatched selector. Null stays 
> outside this check, matching enum exhaustiveness.
> h2. Touch points
> * {{org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor}} — 
> {{coversAllPermittedSubclasses}}, {{nativeSealedClosed}}, 
> {{permittedSubclassLabelKey}}
> * {{src/test/groovy/groovy/transform/stc/SwitchExpressionSTCTest.groovy}}
> h2. Scope
> This issue covers STC exhaustiveness of native sealed type-case labels, and 
> duplicate detection of those labels. GEP-19 pattern matching, flow typing of 
> the matched arm (GROOVY-12300), and bytecode dispatch for type cases are 
> separate work.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to