Wiki

Ticket #354 (closed defect: fixed)

Opened 11 years ago

Last modified 11 years ago

Shared protected var is not visible to subclass if base class is defined in reference

Reported by: nerdzero Owned by:
Priority: major Milestone:
Component: Cobra Compiler Version: 0.9.6
Keywords: reference, inheritance Cc:

Description

Cobra svn:3097 (post 0.9.6) / 2014-01-13 on Mono 3.2.7 CLR v4.0.30319 on Ubuntu 13.10

The following snippet compiles without issue.

@args -t:lib

class Foo
    shared
        var _bar = 1

class Bar inherits Foo
    def testing
        print _bar

This next one fails with the following message: error: For "print" arg 1: Cannot find "_bar".

@ref "Foo.dll"

class Program inherits Foo

    def main
        print _bar

Shared variables with protected accessibility are not visible to subclasses if the base class is defined in a referenced assembly.

Change History

Changed 11 years ago by nerdzero

Does this combination of modifiers make the variable internal to the assembly? Clearly this doesn't happen often as I haven't needed it until now.

Changed 11 years ago by hopscc

  • status changed from new to closed
  • resolution set to invalid

This is as intended.
Theres a naming and access modifier shortcut in cobra (making a shorthand out of a common convention)
vars with a (single) underscore prefix default to 'protected' rather than 'public'
vars with a double underscore prefix default to 'private' rather than 'public'

Override with an explicit is-names clause (watch out for tkt:34 biting) or drop the '_' prefix.

Changed 11 years ago by nerdzero

Wait, why does the behavior change if the base class is defined in an assembly?

Changed 11 years ago by nerdzero

  • status changed from closed to reopened
  • resolution invalid deleted

Changed 11 years ago by nerdzero

Maybe it wasn't obvious but both the "Bar" and "Program" classes in the sample code are inheriting from the "Foo" classes. Protected members should be accessible.

Changed 11 years ago by nerdzero

the "Foo" class*

Changed 11 years ago by hopscc

Good point - missed that.

Looks like the lookup for Field members in scanClrType.cobra (_scanClrFields) isnt specifying binding flags for private and static members.

Its using

(BindingFlags(Instance, Static, DeclaredOnly, Public)

which wont get nonPublic or public and static members up the hierarchy...
(strange set of distinctions to have)
should probably be

(BindingFlags(Instance, Static, DeclaredOnly, Public, NonPublic, FlattenHierarchy)

instead which will get protected static members in ancestor classes...

(tried that and it then made your example work....
probably indicates some of the other lookups might also need to include those flags to also find ancestor protected members (properties, methods)...

downside is that it may pick upo a whole lot of other members we're not interested in (and its possibly slower searching up the hierarchy..)

Changed 11 years ago by Charles

  • status changed from reopened to closed
  • resolution set to fixed

Fixed in changeset:3098

Changed 11 years ago by nerdzero

Awesome. Thanks, guys.

Note: See TracTickets for help on using tickets.