Skip to content

Prevent opaque types leaking from transparent inline methods #23792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jchyb
Copy link
Contributor

@jchyb jchyb commented Aug 21, 2025

Fixes #13461
Before this PR, every transparent inline call returning an opaque type would actually be typed with an intersection type DECLARED & ACTUAL, where DECLARED was the declared return type of the transparent method, and ACTUAL was the type actually returned in the expansion, with every opaque type alias then dealiased. There was no way to guard against this dealiasing. With the changes in this PR, users are now able to manually ensure that they receive the types they want, although they might have to manually annotate the returned type inside of the transparent inline method body (as described in the added documentation section).

The previous dealiasing was caused by the proxy mechanism in inlining, which would effectively deals every opaque type, that is transparent from the perspective of the original method declaration. Now, we try to map the results of the transparent inline back to the original (opaque) types.

However all of this is only true for the outermost transparent inline method calls. Nested calls will not be affected by this change. This is because the type checker in the original context of the method will see the opaque type as transparent (so it will type the rest of the method according to that), and that typing must still hold after inlining the method e.g.:

object Time:
  opaque type Time = String
  transparent inline makeTime(): Time = "1h"
  transparent inline listTime(): List[Time] = List[String](makeTime())
    // mapping the results of makeTime() back into opaque types outside
    // of the scope of Time will cause an inlining compilation error
    // (which we are generally trying to avoid, and which would be
    // considered a bug in the compiler).

This might cause the aliased type to still leak in a manner that may feel unexpected. In the above example, even if the List does not have an explicit type parameter, the type inference will still decide on String, causing any call to listTime to leak that type. This is also touched upon in the added docs.

This PR might cause some source/library incompatibilities connected to the changed returned types (but I doubt it’s many, considering the additional required effort of ignoring type inference if we want the outputted type to be different).

Before this PR, every transparent inline call returning an opaque type
would actually be typed with an intersection type `DECLARED & ACTUAL`,
where `DECLARED` was the declared return type of the transparent method,
and `ACTUAL` was the type actually returned in the expansion, with every
opaque type alias then dealiased. There was no way to guard against this
dealiasing. With the changes in this PR, users are now able to manually
ensure that they receive the types they want, although they might have
to manually annotate the returned type inside of the transparent inline
method body (as described in the added documentation section).

The previous dealiasing was caused by the proxy mechanism in inlining,
which would effectively deals every opaque type, that is transparent
from the perspective of the original method declaration. Now, we try to
map the results of the transparent inline back to the original (opaque)
types.

However all of this is only true for the outermost transparent inline
method calls. Nested calls will not be affected by this change. This is
because the type checker in the original context of the method will see
the opaque type as transparent (so it will type the rest of the method
according to that), and that typing must still hold after inlining the
method e.g.:
```
object Time:
  opaque type Time = String
  transparent inline makeTime(): Time = "1h"
  transparent inline listTime(): List[Time] = List[String](makeTime())
    // mapping the results of makeTime() back into opaque types outside
    // of the scope of Time will cause an inlining compilation error
    // (which we are generally trying to avoid, and which would be
    // considered a bug in the compiler).
```
This might cause the aliased type to still leak in a manner that may
feel unexpected. In the above example, even if the List does not have
an explicit type parameter, the type inference will still decide on
`String`, causing any call to listTime to leak that type. This is also
touched upon in the added docs.

This PR might cause some source/library incompatibilities connected to
the changed returned types (but I doubt it’s many, considering the
additional required effort of ignoring type inference if we want the
outputted type to be different).
@soronpo
Copy link
Contributor

soronpo commented Aug 22, 2025

Did you check the original issue example of #13461 ? I don't see it in the tests.

@jchyb
Copy link
Contributor Author

jchyb commented Aug 22, 2025

Oh no, it's a macro :( . Sorry, for some reason multiple very different issues got merged into that one issue report, and I didn't notice how different the initial minimisation was. It doesn't work currently. I'll close this, but I want to see how open-community-build fares first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

transparent inline leak underlying opaque types
2 participants