Skip to content

Commit 5eb62da

Browse files
committed
Don't emit the assingment to the Value property in the Unmarshal stage when using [Out]
1 parent 5104216 commit 5eb62da

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ICustomNativeTypeMarshallingStrategy.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ public IEnumerable<StatementSyntax> GenerateUnmarshalStatements(TypePositionInfo
235235
{
236236
var subContext = new CustomNativeTypeWithValuePropertyStubContext(context);
237237

238-
yield return GenerateValuePropertyAssignment(info, context, subContext);
238+
if (!info.IsByRef && info.ByValueContentsMarshalKind.HasFlag(ByValueContentsMarshalKind.Out))
239+
{
240+
yield return GenerateValuePropertyAssignment(info, context, subContext);
241+
}
239242

240243
foreach (StatementSyntax statement in _innerMarshaller.GenerateUnmarshalStatements(info, subContext))
241244
{
@@ -450,7 +453,7 @@ public bool UsesNativeIdentifier(TypePositionInfo info, StubCodeContext context)
450453
}
451454

452455
/// <summary>
453-
/// Marshaller that enables support for a GetPinnableReference method on a native type, with a Value property fallback.
456+
/// Marshaller that calls the GetPinnableReference method on the marshaller value and enables support for the Value property.
454457
/// </summary>
455458
internal sealed class PinnableMarshallerTypeMarshalling : ICustomNativeTypeMarshallingStrategy
456459
{
@@ -482,8 +485,11 @@ public IEnumerable<StatementSyntax> GenerateCleanupStatements(TypePositionInfo i
482485
{
483486
var subContext = new CustomNativeTypeWithValuePropertyStubContext(context);
484487

485-
// <marshalerIdentifier>.Value = <nativeIdentifier>;
486-
yield return GenerateValuePropertyAssignment(info, context, subContext);
488+
if (!context.AdditionalTemporaryStateLivesAcrossStages)
489+
{
490+
// <marshalerIdentifier>.Value = <nativeIdentifier>;
491+
yield return GenerateValuePropertyAssignment(info, context, subContext);
492+
}
487493

488494
foreach (StatementSyntax statement in _innerMarshaller.GenerateCleanupStatements(info, subContext))
489495
{
@@ -562,8 +568,11 @@ public IEnumerable<StatementSyntax> GenerateUnmarshalStatements(TypePositionInfo
562568
{
563569
var subContext = new CustomNativeTypeWithValuePropertyStubContext(context);
564570

565-
// <marshalerIdentifier>.Value = <nativeIdentifier>;
566-
yield return GenerateValuePropertyAssignment(info, context, subContext);
571+
if (!info.IsByRef && info.ByValueContentsMarshalKind.HasFlag(ByValueContentsMarshalKind.Out))
572+
{
573+
// <marshalerIdentifier>.Value = <nativeIdentifier>;
574+
yield return GenerateValuePropertyAssignment(info, context, subContext);
575+
}
567576

568577
foreach (StatementSyntax statement in _innerMarshaller.GenerateUnmarshalStatements(info, subContext))
569578
{

0 commit comments

Comments
 (0)