Scott Murphy Heiberg created GROOVY-12523:
---------------------------------------------

             Summary: STC: x.@name is accepted for a private field of a 
superclass when the type of x is a subclass
                 Key: GROOVY-12523
                 URL: https://issues.apache.org/jira/browse/GROOVY-12523
             Project: Groovy
          Issue Type: Bug
          Components: Static Type Checker
    Affects Versions: 6.0.0
            Reporter: Scott Murphy Heiberg


In a class nested in {{Outer}}, the type checker accepts {{sub.@f}}, where 
{{f}} is a private field of {{Outer}} and {{sub}} is typed as a subclass of 
{{Outer}}:

{code:groovy}
@groovy.transform.TypeChecked
class Outer {
    private String f = 'outer'
    static class Sub extends Outer {
        String read(Sub sub) { sub.@f }
    }
}
new Outer.Sub().read(new Outer.Sub()) // MissingFieldException: No such field: 
f for class: Outer$Sub
{code}

Dynamic Groovy and {{@TypeChecked}} throw {{MissingFieldException}}, because 
the meta-class of a subclass offers no private field of its superclass. 
{{@CompileStatic}} reads the field. javac rejects {{sub.f}} ("f has private 
access in Outer"), since a private field is not a member of a subclass (JLS 
8.2). With {{f}} declared in a nested class {{Box}} and {{Sub extends Box}}, or 
with a top-level subclass, the type checker already rejects {{sub.@f}} with 
"Cannot access field". Seen in 5.0.8, 6.0.0 and master; 4.0.33 failed at run 
time in every mode.

Expected: "Cannot access field: f of class: Outer", also for writes, closures, 
lambdas and {{new Sub().@f}}. {{this.@f}}, {{super.@f}}, {{((Outer) sub).@f}} 
and {{sub.@f}} inside {{Outer}} keep compiling.



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

Reply via email to