Four await shapes decompile to uncompilable C#

Aperta
#4,015 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Tranquilla
Stack tecnologico
csharp

Direzione di ricerca

Inizia con i quattro membri che falliscono in ICSharpCode.Decompiler.Tests/TestCases/Pretty/AsyncAwaitPatternsBugs.cs sul branch tests/async-await-patterns. Esegui i test async-await-patterns, poi esamina VisitAwait e la gestione di LdObjIfRef in ExpressionBuilder insieme ai controlli dynamic-call e record-with. Il lavoro è completato quando tutti e quattro i casi vengono decompilati in C# compilabile e i test del fixture passano.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Four await shapes decompile to C# that does not compile. All four reproduce on
master @ 40e6f2cf2 and still reproduce after 257c96711 ("Avoid allowImplicitConversion: true
for await expressions"), which fixed a different set of await defects.

Each of them is covered by a member of ICSharpCode.Decompiler.Tests/TestCases/Pretty/AsyncAwaitPatternsBugs.cs
on the tests/async-await-patterns branch, where the fixture is written as the C# that should
come out; the test is red for exactly these four.

1. Extension GetAwaiter taking its receiver by in
public struct ByRefReceiver
{
    public long A;
    public long B;
    public TaskAwaiter Self() => default(TaskAwaiter);
}

public static class ByRefExtensions
{
    public static TaskAwaiter GetAwaiter(this in ByRefReceiver receiver) => receiver.Self();
}

public async Task InReceiverExtensionAwaiter(ByRefReceiver value)
{
    await value;
}

decompiles to:

public unsafe async Task InReceiverExtensionAwaiter(ByRefReceiver value)
{
    await (ref *(ByRefReceiver*)value);
}

error CS1525: Invalid expression term 'ref'. The method also acquires an unsafe modifier it
did not have. The in parameter makes the awaited expression's expected type a
ByReferenceType; VisitAwait strips the DirectionExpression off the operand before
converting it.

2. await on a type parameter constrained to an awaitable interface
public interface IAwaitable
{
    TaskAwaiter GetAwaiter();
}

public async Task TypeParameterWithInterfaceConstraint<T>(T value) where T : IAwaitable
{
    await value;
}

decompiles to:

await (IAwaitable)/*OpCode not supported: LdObjIfRef*/;

error CS0119: 'IAwaitable' is a type, which is not valid in the given context. The operand is
not merely wrong, it is dropped: the constrained. callvirt lowers to an LdObjIfRef that
ExpressionBuilder has no case for.

3. dynamic await inside the argument list of a static call
public async Task DynamicAwaitInStaticCall(dynamic value)
{
    Console.WriteLine("x" + await value);
}

decompiles to:

Type typeFromHandle = typeof(Console);
typeFromHandle.WriteLine("x" + await value);

error CS1061: 'Type' does not contain a definition for 'WriteLine'. The call site's
typeof(TargetType) marker argument becomes the receiver. Isolated against two controls: the
same call without the await, and an instance dynamic call with an await, both decompile
correctly.

4. await inside a record with initializer
public record Record(int X);

public async Task<Record> WithExpressionContainingAwait(Record value)
{
    return value with { X = await Get() };
}

decompiles to:

Record record = value._003CClone_003E_0024();
Record record2 = record;
record2.X = await Get();
return record;

which does not compile. Isolated against two controls: value with { X = 1 } and
value with { X = Compute() } both round-trip as with expressions, so the await is what
defeats the transform.


Posted by an AI agent (Claude) on Siegfried's behalf.

Lingua principale
C#
Stelle
27.1k
Fork
3.8k
Merge medio
1g 23h
PR unite (30g)
44

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di icsharpcode/ILSpy

Tutte le issue di icsharpcode/ILSpy

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.