Skip to content

Do not expose ClassInfo in memberType in reflect API - #24922

Merged
hamzaremmal merged 1 commit into
scala:mainfrom
dotty-staging:fix-classinfo-i22395
Feb 25, 2026
Merged

Do not expose ClassInfo in memberType in reflect API#24922
hamzaremmal merged 1 commit into
scala:mainfrom
dotty-staging:fix-classinfo-i22395

Conversation

@jchyb

@jchyb jchyb commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

fixes #22395
fixes #13319
fixes #15159

The asSeenFrom method, used in TypeRepr.memberType, sometimes can return ClassInfo instances, which we do not expose in the reflect API - meaning that any user interaction with them will cause a crash. This is either caused by nonsensical calls (calls with symbols unrelated to the type), or as in case of issue #22395, correct calls on nested classes.

Since ClassInfo gives us precise type prefix and symbol, we transform that ClassInfo into a TypeRef (which is how types pointing to classes are usually represented in the reflect API).

Previously (in #15161) there were attempts to completely limit calling memberType to the direct members of the type (to avoid the nonsensical calls), however that causes major regressions in both the dotty compilation tests, and (in my opinion) functionality (as explained in the comment). So instead, we check if any owner of the passed symbol is a member of the TypeRepr (basically, we allow obtaining the types of nested members). With the above fix, this check is not strictly necessary, but I think it might help avoid confusion about how memberType is supposed to be used.

@jchyb

jchyb commented Jan 8, 2026

Copy link
Copy Markdown
Contributor Author

If this PR is familiar, it's because it's refreshed and brought back from the last year: #22402

If ClassInfo is returned, it is now transformed into a TypeRef.
Additionally, we throw an assertion error if the TypeRepr and the symbol
are completely unrelated.
@jchyb
jchyb force-pushed the fix-classinfo-i22395 branch from eca64a8 to 46ca491 Compare January 9, 2026 01:29
@jchyb
jchyb requested a review from hamzaremmal January 9, 2026 10:29
@hamzaremmal
hamzaremmal merged commit 0a2f32d into scala:main Feb 25, 2026
56 checks passed
@hamzaremmal
hamzaremmal deleted the fix-classinfo-i22395 branch February 25, 2026 11:08
@WojciechMazur WojciechMazur added this to the 3.8.4 milestone Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants