Skip to content

Improve types for variables generated from stack slots - #4071

Merged
dgrunwald merged 2 commits into
masterfrom
better-types-for-stack-slots
Aug 29, 2026
Merged

dgrunwald merged 2 commits into
masterfrom
better-types-for-stack-slots

Conversation

@dgrunwald

Copy link
Copy Markdown
Member

Stack slots holding value types previously nevertheless often had type object
This was due to StackType.O doing double-duty as object and other. While ExpressionBuilder would often improve the type of such locals, the object nevertheless ended up used in a couple of places, e.g. via the typeHint. This could result in value types being boxed even though the original IL didn't contain any box instruction.

This is an attempt to use better types for stack slot variables created by ILReader. The idea is: there aren't many IL instructions that produce "other" value types, and InferType() already handles pretty much all of them, so we can use that to assign types to our stack slots.

It's a bit more tricky if the stack is pushed to on multiple branches that join together before the value is used: here the variable type must be suitable for both assignments. In this case, we go back to the previously-used stacktype.

Fixes #4068 (comment) -- the issue there was that the stack slot holding the (int, bool) had type object and was passing this as a TypeHint to the switch-expression translation. That caused switch-expression translation to box -- but the uses of the variable didn't unbox, so this was a correctness bug.

…e `object`

This was due to StackType.O doing double-duty as `object` and `other`.
While ExpressionBuilder would often improve the type of such locals, the `object` nevertheless ended up used in a couple of places, e.g. via the `typeHint`. This could result in value types being boxed even though the original IL didn't contain any `box` instruction.

This is an attempt to use better types for stack slot variables created by ILReader. The idea is: there aren't many IL instructions that produce "other" value types, and `InferType()` already handles pretty much all of them, so we can use that to assign types to our stack slots.

It's a bit more tricky if the stack is pushed to on multiple branches that join together before the value is used: here the variable type must be suitable for both assignments. In this case, we go back to the previously-used stacktype.
@dgrunwald
dgrunwald merged commit d410645 into master Aug 29, 2026
15 checks passed
@siegfriedpammer
siegfriedpammer deleted the better-types-for-stack-slots branch August 29, 2026 16:17
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.

1 participant