Preserve erased flags on synthetic context-function parameters#25751
Merged
tanishiking merged 1 commit intoscala:mainfrom Apr 10, 2026
Merged
Preserve erased flags on synthetic context-function parameters#25751tanishiking merged 1 commit intoscala:mainfrom
tanishiking merged 1 commit intoscala:mainfrom
Conversation
fix 25725
**problem**
For normal parameters, erasure removes erased parameters from both the method type and the erased tree:
```scala
def direct(using erased p: Proof, x: Int): String =
"value: " + x
// [[syntax trees at end of erasure]]
def direct(using x: Int): String =
"value: " + Int.box(x)
```
However, parameters coming from a context-function result were not treated the same way. For example:
```scala
def mixed: (erased Proof) ?=> Int ?=> String =
s"value: ${summon[Int]}"
// erased to a tree that still kept the erased parameter:
def mixed(using x$1: Proof, contextual$1: Int): String =
"value: " + Int.box(contextual$1)
```
This produced a mismatch between the symbol info and the erased tree.
On the type side, `TypeErasure` correctly drops erased parameters from the `MethodType`, so `mixed.symbol.info` becomes `(using Int)String`, and the backend emits the JVM descriptor `(I)Ljava/lang/String;`.
On the tree side, the erased `DefDef` still contains `x$1`, so the backend assigns local slots as if both parameters were present:
0 = this / 1 = x$1 / 2 = contextual$1
As a result, the body loads `contextual$1` with `iload_2`, even though the descriptor contains only one argument. That mismatch results in verifier error.
**Root cause**
The root cause is that erased flagfor synthetic context-function parameters was stored only in `FunctionWithMods.erasedParams`, but was not stored onto the synthetic parameter `ValDef`s themselves.
`makeContextualFunction` created parameters with `Given | Param`, but not `Erased`. Later, `Erasure.skipContextClosures` decides whether to keep flattened context-result parameters by checking `param.symbol.is(Erased)`, so these parameters were incorrectly seen as non-erased and left in the tree.
**fix**
This commit fix by keep function-parameter metadata (erasedness) on synthetic parameters consistently.
We introduce a shared helper in `Desugar` and use it from both `makeContextualFunction` so that parameter-level flags such as `given`/`implicit` and `erased` are applied to the generated `ValDef`s before they are turned into symbols.
With that change, synthetic context-function parameters carry `Erased` to the Erasure phase, the erased tree matches the erased method type, and the backend assigns locals consistently.
Member
Author
|
Requesting review to @natsukagami based on the commit history #16507 🙏 |
Member
Author
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix #25725
problem
For normal parameters, erasure removes erased parameters from both the method type and the erased tree:
However, parameters coming from a context-function result were not treated the same way. For example:
This produced a mismatch between the symbol info and the erased tree.
On the type side,
TypeErasurecorrectly drops erased parameters from theMethodType, somixed.symbol.infobecomes(using Int)String, and the backend emits the JVM descriptor(I)Ljava/lang/String;.On the tree side, the erased
DefDefstill containsx$1, so the backend assigns local slots as if both parameters were present: 0 = this / 1 = x$1 / 2 = contextual$1As a result, the body loads
contextual$1withiload_2, even though the descriptor contains only one argument. That mismatch results in verifier error.Root cause
The root cause is that erased flagfor synthetic context-function parameters was stored only in
FunctionWithMods.erasedParams, but was not stored onto the synthetic parameterValDefs themselves.makeContextualFunctioncreated parameters withGiven | Param, but notErased. Later,Erasure.skipContextClosuresdecides whether to keep flattened context-result parameters by checkingparam.symbol.is(Erased), so these parameters were incorrectly seen as non-erased and left in the tree.fix
This commit fix by keep function-parameter metadata (erasedness) on synthetic parameters consistently. We introduce a shared helper in
Desugarand use it from bothmakeContextualFunctionso that parameter-level flags such asgiven/implicitanderasedare applied to the generatedValDefs before they are turned into symbols.With that change, synthetic context-function parameters carry
Erasedto the Erasure phase, the erased tree matches the erased method type, and the backend assigns locals consistently.How much have you relied on LLM-based tools in this contribution?
moderately use Codex gpt-5.4
How was the solution tested?
testCompilation i25725Additional notes