Registry Mirror
Skip to content

Conversation

@siegfriedpammer

Copy link
Copy Markdown
Member

Fixes #3704.

A dynamic call site that names a type passes typeof(T) as its target. When a later argument contains control flow, the compiler stores that typeof in a temporary, and ILInlining does not undo it: it inlines a store only into the immediately following instruction, so an intervening statement leaves the typeof behind. ExpressionBuilder then printed the temporary instead of the type, producing output that does not compile:

Type typeFromHandle = typeof(CompleteIntegration);
byte[] item = tuple.Value.Item4;
typeFromHandle.WriteToRichTextBox((dynamic)viewModel, $"...{((item != null) ? item.Length : 0)}...", Colors.Green);

Substituting the typeof back into the target slot is not an option: a call there has side effects, so the inliner can no longer move the remaining arguments across it and results of dynamic operations stop collapsing into their consumer (18 DynamicTests configurations regress that way).

The type is stored on the instruction instead. DynamicInvokeConstructorInstruction already resolved it that way but kept the field private, so ExpressionBuilder re-derived it from the target argument and degraded to an error expression on the spilled form. DynamicInvokeMemberInstruction gets the same field. Both drop the target from Arguments, which lets the existing dead-argument bookkeeping retire the store; ArgumentInfo keeps its entry for the target, because the invocation symbol is built from it.

Those two are the only binder method kinds that ever see CSharpArgumentInfoFlags.IsStaticType - every other way of naming a type as the receiver binds statically and leaves only a conversion of the dynamic operand behind.

Tests: DynamicTests covers a discarded result, a used result, explicit type arguments and a dynamic object creation, each with the typeof stranded behind a surviving temporary; DynamicAwait covers the state-machine spill across an await.

Full ICSharpCode.Decompiler.Tests run: 3460 passed, 0 failed, 45 skipped.

🤖 Generated with Claude Code

siegfriedpammer added a commit that referenced this pull request Aug 29, 2026
NullPropagationTransform only rewrites "x != null ? x.Chain : fallback"
into "x?.Chain ?? fallback" when the chain's inferred type is a
non-nullable value type, and InferType had no case for ldlen. Array length
therefore came back as UnknownType, so "arr?.Length ?? 0" was left as a
ternary.

The inferred type mirrors ExpressionBuilder.VisitLdLen, which decides
between Array.Length and Array.LongLength from the result type alone.

Found while investigating #3704, where the surviving ternary also keeps the
tested array in a stack slot and strands the typeof of a dynamic call's
static target. That issue is fixed separately in #4072.

Assisted-by: Claude:claude-opus-5[1m]:Claude Code
@siegfriedpammer
siegfriedpammer force-pushed the fix/3704-static-type-target branch from af75e7c to 1c9d2f2 Compare August 29, 2026 18:09
@siegfriedpammer
siegfriedpammer force-pushed the fix/3704-static-type-target branch from 1c9d2f2 to adddb04 Compare August 29, 2026 20:38
A dynamic call site that names a type passes typeof(T) as its target. When
a later argument contains control flow, the compiler stores that typeof in
a temporary. ILInlining does not undo this: it inlines a store only into
the immediately following instruction, so an intervening statement leaves
the typeof behind, and the expression builder printed the temporary instead
of the type. Substituting the typeof back into the target slot is not an
option either, because a call there has side effects and blocks inlining of
the remaining arguments.

The type is therefore stored on the instruction. Object creation already
did this, but kept the field private, so the expression builder re-derived
the type from the target argument and failed on the spilled form. Member
invocation gets the same field. Both drop the target from Arguments, so the
dead-argument handling removes the store; ArgumentInfo keeps its entry for
the target, because the invocation symbol is built from it.

The type of an object creation is not nullable: the transform returns
before constructing the instruction when it cannot match the typeof, and
substitutes the unknown type otherwise. An unresolvable type therefore
reaches the expression builder as the unresolved type it is, and prints as
`?` like any other, rather than being indistinguishable from a missing one.

Those two are also the only binder method kinds that ever see
CSharpArgumentInfoFlags.IsStaticType. Every other way of naming a type as
the receiver binds statically and leaves only a conversion of the dynamic
operand behind.

Assisted-by: Claude:claude-opus-5[1m]:Claude Code
@siegfriedpammer
siegfriedpammer force-pushed the fix/3704-static-type-target branch from adddb04 to 8ab3f93 Compare August 29, 2026 20:41
{
Expression targetExpr;
var target = TranslateDynamicTarget(inst.Arguments[0], inst.ArgumentInfo[0]);
var target = inst.StaticTargetType is IType staticTargetType

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is another weird null check.

Reviewed with Stampeded!

@siegfriedpammer
siegfriedpammer merged commit c29b0e8 into master Aug 29, 2026
15 checks passed
@siegfriedpammer
siegfriedpammer deleted the fix/3704-static-type-target branch August 29, 2026 21:09
siegfriedpammer added a commit that referenced this pull request Aug 29, 2026
NullPropagationTransform only rewrites "x != null ? x.Chain : fallback"
into "x?.Chain ?? fallback" when the chain's inferred type is a
non-nullable value type, and InferType had no case for ldlen. Array length
therefore came back as UnknownType, so "arr?.Length ?? 0" was left as a
ternary.

The inferred type mirrors ExpressionBuilder.VisitLdLen, which decides
between Array.Length and Array.LongLength from the result type alone.

Found while investigating #3704, where the surviving ternary also keeps the
tested array in a stack slot and strands the typeof of a dynamic call's
static target. That issue is fixed separately in #4072, whose DynamicTests
cases pinned the ternary as expected output; those blocks round-trip
exactly now, so they are gone.

Also carries a review follow-up that missed #4072: the static-target test
in VisitDynamicInvokeMemberInstruction is a plain null check, the way
DynamicInvokeMemberInstruction itself tests the field, rather than a
pattern match binding a name it does not need.

Assisted-by: Claude:claude-opus-5[1m]:Claude Code
siegfriedpammer added a commit that referenced this pull request Aug 30, 2026
NullPropagationTransform only rewrites "x != null ? x.Chain : fallback"
into "x?.Chain ?? fallback" when the chain's inferred type is a
non-nullable value type, and InferType had no case for ldlen. Array length
therefore came back as UnknownType, so "arr?.Length ?? 0" was left as a
ternary.

The inferred type mirrors ExpressionBuilder.VisitLdLen, which decides
between Array.Length and Array.LongLength from the result type alone.

Found while investigating #3704, where the surviving ternary also keeps the
tested array in a stack slot and strands the typeof of a dynamic call's
static target. That issue is fixed separately in #4072, whose DynamicTests
cases pinned the ternary as expected output; those blocks round-trip
exactly now, so they are gone.

Also carries a review follow-up that missed #4072: the static-target test
in VisitDynamicInvokeMemberInstruction is a plain null check, the way
DynamicInvokeMemberInstruction itself tests the field, rather than a
pattern match binding a name it does not need.

Assisted-by: Claude:claude-opus-5[1m]:Claude Code
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.

dynamic issue

2 participants