[ 
https://issues.apache.org/jira/browse/GROOVY-12562?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18135271#comment-18135271
 ] 

ASF GitHub Bot commented on GROOVY-12562:
-----------------------------------------

testlens-app[bot] commented on PR #3091:
URL: https://github.com/apache/groovy/pull/3091#issuecomment-6527114507

   ## 🚨 All tests passed but jobs failed 🚨
   
   ### Failed Jobs without Test Failures
   
   ❌ [Build and test / lts \(17, windows-latest, 
1\)](https://github.com/apache/groovy/actions/runs/41083173821/job/124157345955?pr=3091)
   
   🏷️ Commit: 1b856c3b9b10caf84028885e695e9cb05ad48c92
   ▶️ Tests:  3062 executed
   🟡 Checks: 3/24 completed
   
   ### Rerun Controls
   
   Click the checkbox to trigger a rerun:
   
   🔲 **Rerun jobs**
   
   ---
   _Learn more about TestLens at 
[testlens.app/docs](https://testlens.app/docs/features/pr-comment/)._
   




> STC: instanceof narrows a variable where the check does not guard the code
> --------------------------------------------------------------------------
>
>                 Key: GROOVY-12562
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12562
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 4.0.33, 5.0.8, 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> The type checker narrows a variable after any {{instanceof}} check on it, for 
> the rest of the expression or statement, wherever the check is. Under 
> {{@CompileStatic}} the program then fails with a ClassCastException:
> {code:groovy}
> @groovy.transform.CompileStatic
> String f(CharSequence cs) { 'x' + (cs instanceof String) + cs.length() }
> f(new StringBuilder('ab')) // ClassCastException: StringBuilder cannot be 
> cast to String (5.0.8, 6.0.0)
> {code}
> The same holds for {{foo(cs instanceof String, cs.length())}}, {{[cs 
> instanceof String, cs]}} and {{(cs instanceof String) == cs.isEmpty()}}. A 
> comparison with {{false}} narrows the wrong branch, and the condition of a 
> loop narrows after the loop:
> {code:groovy}
> @groovy.transform.CompileStatic
> int h(Object x) { if ((x instanceof String) == false) { return x.length() }; 
> 0 }
> h(42) // ClassCastException: Integer cannot be cast to String
> @groovy.transform.CompileStatic
> int g(Object x) { while (x instanceof String) { break }; x.length() }
> g(42) // ClassCastException
> {code}
> The last two fail in 4.0.33, 5.0.8 and 6.0.0, the first in 5.0.8 and 6.0.0.
> Expected: a check narrows only where its outcome guards what follows (the 
> condition of an if, while, for, assert or conditional expression, and 
> operands of &&, || and ! there); {{(x instanceof T) == true}} narrows like 
> the check and {{== false}} like its negation; a loop condition narrows inside 
> the loop only.
> Behaviour change under @TypeChecked and @CompileStatic: an instanceof check 
> now narrows the variable only where its outcome guards the code (conditions, 
> operands of &&, || and !, comparisons with a boolean literal, boolean casts, 
> assignments in a condition), and the condition of a loop narrows inside the 
> loop only. Code that relied on the old narrowing compiled before and is now a 
> compile error, for example
> {code:groovy}
> @CompileStatic
> def f(Object x) { '' + (x instanceof String) + x.length() }
> // before: compiled; f(new StringBuilder('ab')) threw ClassCastException
> // now: [Static type checking] - Cannot find matching method 
> java.lang.Object#length()
> {code}
> and likewise a call of {{x.length()}} after a {{while (x instanceof String)}} 
> loop. A check that is a statement on its own did not narrow later statements 
> before and does not now; switch/case and catch are unaffected. The PR adds a 
> COMPATIBILITY.md entry.
> PR: https://github.com/apache/groovy/pull/3091



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to