Skip to content

Preserve erased flags on synthetic context-function parameters#25751

Merged
tanishiking merged 1 commit intoscala:mainfrom
tanishiking:erased-context-invalid
Apr 10, 2026
Merged

Preserve erased flags on synthetic context-function parameters#25751
tanishiking merged 1 commit intoscala:mainfrom
tanishiking:erased-context-invalid

Conversation

@tanishiking
Copy link
Copy Markdown
Member

@tanishiking tanishiking commented Apr 9, 2026

fix #25725

problem
For normal parameters, erasure removes erased parameters from both the method type and the erased tree:

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:

def mixed: (erased Proof) ?=> Int ?=> String =
  s"value: ${summon[Int]}"

// after typer: desugared
def mixed:
    PolyFunction {
      def apply(using erased x$1: Proof): (Int) ?=> String
    }
   = (using x$1: Proof) => (using contextual$1: 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 ValDefs 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 ValDefs 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.

How much have you relied on LLM-based tools in this contribution?

moderately use Codex gpt-5.4

How was the solution tested?

testCompilation i25725

Additional notes

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.
@tanishiking
Copy link
Copy Markdown
Member Author

Requesting review to @natsukagami based on the commit history #16507 🙏

Copy link
Copy Markdown
Contributor

@natsukagami natsukagami left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tanishiking
Copy link
Copy Markdown
Member Author

Thanks!

@tanishiking tanishiking merged commit 20fc4c7 into scala:main Apr 10, 2026
64 checks passed
@tanishiking tanishiking deleted the erased-context-invalid branch April 10, 2026 09:40
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.

VerifyError: erased context function parameter before non-erased produces invalid bytecode

2 participants