wlav / cppyy

Other
384 stars 38 forks source link

Enums are not correctly resolved when using an alias within a class/struct #214

Open guitargeek opened 5 months ago

guitargeek commented 5 months ago

When defining aliases for an enum, it is not recognized as the right type by cppyy.

Reproducer:

import cppyy

cppyy.cppdef("enum Enum { a,b,c };")
cppyy.cppdef("struct Track { using Type = Enum; };")

# Works!
print(cppyy.gbl.Enum.a)

# Doesn't
print(cppyy.gbl.Track.Type.a)

The output is:

0
Traceback (most recent call last):
  File "/home/rembserj/spaces/master/root/src/root/tst99.py", line 10, in <module>
    print(cppyy.gbl.Track.Type.a)
          ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: type object 'int' has no attribute 'a'

In case this is a more complicated problem, any advice on how I can contribute to support this is greatly appreciated!

Originally reported in the ROOT issue tracker: https://github.com/root-project/root/issues/9246

wlav commented 5 months ago

The type Track::Type is actually found as an enum, but it's seen as disjoint from the type Enum, not an alias of it. I'd figure it should resolve as an alias, to make Python's is work. That's a failure of ROOT/meta's type resolution.

However, even if not seen as an alias, the lookup should have worked if a if the enum was properly reflected. Looks like it's not:

import cppyy

cppyy.cppdef("enum Enum { a,b,c };")
cppyy.cppdef("struct Track { using Type = Enum; };")

print("is enum:", cppyy.gbl.gInterpreter.ClassInfo_IsEnum("Track::Type"))

c = cppyy.gbl.CppyyLegacy.TClass.GetClass("Track")
e = c.GetListOfEnums(1).FindObject("Type")
print("is valid:", not not e)

which yields:

is enum: True
is valid: False

In general, ROOT/meta lacks support for using and friend.

guitargeek commented 5 months ago

Thank you so much for giving some context @wlav! That helped me to understand where the root cause is, maybe I can fix it at some point.