diff --git a/eng/Versions.props b/eng/Versions.props index 81f31523412c5..2e453f902d8d8 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -31,21 +31,21 @@ 3.11.0 4.0.1 - + - 4.3.0-2.final + 4.4.0-1.22369.1 3.3.3 4.3.0-2.final - 4.3.0-2.final - 4.3.0-2.final + 4.4.0-1.final + 4.4.0-1.final 7.0.0-preview1.22403.2 - 4.3.0-2.final + 4.4.0-1.final diff --git a/src/libraries/System.Private.CoreLib/gen/EventSourceGenerator.Parser.cs b/src/libraries/System.Private.CoreLib/gen/EventSourceGenerator.Parser.cs index 43a498b589af8..b438859ebbf60 100644 --- a/src/libraries/System.Private.CoreLib/gen/EventSourceGenerator.Parser.cs +++ b/src/libraries/System.Private.CoreLib/gen/EventSourceGenerator.Parser.cs @@ -9,7 +9,7 @@ using System.Threading; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.DotnetRuntime.Extensions; +using GeneratorAttributeSyntaxContext = Microsoft.CodeAnalysis.DotnetRuntime.Extensions.GeneratorAttributeSyntaxContext; namespace Generators { diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSImportGenerator.csproj b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSImportGenerator.csproj index 70eda110a1b22..90bdaf34e53dd 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSImportGenerator.csproj +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSImportGenerator.csproj @@ -23,7 +23,7 @@ - + diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj index ab8172b697907..c3cf1a6370f1e 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj @@ -22,7 +22,7 @@ - + @@ -45,9 +45,9 @@ - + - + diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/ManagedTypeInfo.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/ManagedTypeInfo.cs index 21a361486e873..c85f6bdec05e0 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/ManagedTypeInfo.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/ManagedTypeInfo.cs @@ -15,7 +15,17 @@ namespace Microsoft.Interop /// public abstract record ManagedTypeInfo(string FullTypeName, string DiagnosticFormattedName) { - public TypeSyntax Syntax { get; } = SyntaxFactory.ParseTypeName(FullTypeName); + private TypeSyntax? _syntax; + public TypeSyntax Syntax => _syntax ??= SyntaxFactory.ParseTypeName(FullTypeName); + + protected ManagedTypeInfo(ManagedTypeInfo original) + { + FullTypeName = original.FullTypeName; + DiagnosticFormattedName = original.DiagnosticFormattedName; + // Explicitly don't initialize _syntax here. We want Syntax to be recalculated + // from the results of a with-expression, which assigns the new property values + // to the result of this constructor. + } public static ManagedTypeInfo CreateTypeInfoForTypeSymbol(ITypeSymbol type) { diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs index 76a4fc3d01b1b..3325e92cbce81 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs @@ -235,7 +235,7 @@ private IMarshallingGenerator CreateCustomNativeTypeMarshaller(TypePositionInfo ICustomTypeMarshallingStrategy marshallingStrategy; if (marshallerData.HasState) { - marshallingStrategy = new StatefulValueMarshalling(marshallerData.MarshallerType.Syntax, marshallerData.NativeType.Syntax, marshallerData.Shape); + marshallingStrategy = new StatefulValueMarshalling(marshallerData.MarshallerType, marshallerData.NativeType.Syntax, marshallerData.Shape); if (marshallerData.Shape.HasFlag(MarshallerShape.CallerAllocatedBuffer)) marshallingStrategy = new StatefulCallerAllocatedBufferMarshalling(marshallingStrategy, marshallerData.MarshallerType.Syntax, marshallerData.BufferElementType.Syntax); } @@ -283,8 +283,13 @@ private IMarshallingGenerator CreateNativeCollectionMarshaller( // Insert the unmanaged element type into the marshaller type TypeSyntax unmanagedElementType = elementMarshaller.AsNativeType(elementInfo).GetCompatibleGenericTypeParameterSyntax(); - TypeSyntax marshallerTypeSyntax = marshallerData.MarshallerType.Syntax; - marshallerTypeSyntax = ReplacePlaceholderSyntaxWithUnmanagedTypeSyntax(marshallerTypeSyntax, marshalInfo, unmanagedElementType); + ManagedTypeInfo marshallerType = marshallerData.MarshallerType; + TypeSyntax marshallerTypeSyntax = ReplacePlaceholderSyntaxWithUnmanagedTypeSyntax(marshallerType.Syntax, marshalInfo, unmanagedElementType); + marshallerType = marshallerType with + { + FullTypeName = marshallerTypeSyntax.ToString(), + DiagnosticFormattedName = marshallerTypeSyntax.ToString(), + }; TypeSyntax nativeTypeSyntax = ReplacePlaceholderSyntaxWithUnmanagedTypeSyntax(marshallerData.NativeType.Syntax, marshalInfo, unmanagedElementType); ICustomTypeMarshallingStrategy marshallingStrategy; @@ -292,7 +297,7 @@ private IMarshallingGenerator CreateNativeCollectionMarshaller( if (marshallerData.HasState) { - marshallingStrategy = new StatefulValueMarshalling(marshallerTypeSyntax, nativeTypeSyntax, marshallerData.Shape); + marshallingStrategy = new StatefulValueMarshalling(marshallerType, nativeTypeSyntax, marshallerData.Shape); if (marshallerData.Shape.HasFlag(MarshallerShape.CallerAllocatedBuffer)) { // Check if the buffer element type is actually the unmanaged element type diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/StatefulMarshallingStrategy.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/StatefulMarshallingStrategy.cs index ad487bf6d4015..16a54a14070c3 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/StatefulMarshallingStrategy.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/StatefulMarshallingStrategy.cs @@ -14,13 +14,13 @@ namespace Microsoft.Interop internal sealed class StatefulValueMarshalling : ICustomTypeMarshallingStrategy { internal const string MarshallerIdentifier = "marshaller"; - private readonly TypeSyntax _marshallerTypeSyntax; + private readonly ManagedTypeInfo _marshallerType; private readonly TypeSyntax _nativeTypeSyntax; private readonly MarshallerShape _shape; - public StatefulValueMarshalling(TypeSyntax marshallerTypeSyntax, TypeSyntax nativeTypeSyntax, MarshallerShape shape) + public StatefulValueMarshalling(ManagedTypeInfo marshallerType, TypeSyntax nativeTypeSyntax, MarshallerShape shape) { - _marshallerTypeSyntax = marshallerTypeSyntax; + _marshallerType = marshallerType; _nativeTypeSyntax = nativeTypeSyntax; _shape = shape; } @@ -140,10 +140,23 @@ public IEnumerable GenerateUnmarshalCaptureStatements(TypePosit public IEnumerable GenerateSetupStatements(TypePositionInfo info, StubCodeContext context) { // = new(); - yield return MarshallerHelpers.Declare( - _marshallerTypeSyntax, + LocalDeclarationStatementSyntax declaration = MarshallerHelpers.Declare( + _marshallerType.Syntax, context.GetAdditionalIdentifier(info, MarshallerIdentifier), ImplicitObjectCreationExpression(ArgumentList(), initializer: null)); + + // For byref-like marshaller types, we'll mark them as scoped. + // Byref-like types can capture references, so by default the compiler has to worry that + // they could enable those references to escape the current stack frame. + // In particular, this can interact poorly with the caller-allocated-buffer marshalling + // support and make the simple `marshaller.FromManaged(managed, stackalloc X[i])` expression + // illegal. Mark the marshaller type as scoped so the compiler knows that it won't escape. + if (_marshallerType is ValueTypeInfo { IsByRefLike: true }) + { + declaration = declaration.AddModifiers(Token(SyntaxKind.ScopedKeyword)); + } + + yield return declaration; } public IEnumerable GeneratePinStatements(TypePositionInfo info, StubCodeContext context) @@ -218,28 +231,9 @@ public IEnumerable GenerateMarshalStatements(TypePositionInfo i IEnumerable GenerateCallerAllocatedBufferMarshalStatements() { - // TODO: Update once we can consume the scoped keword. We should be able to simplify this once we get that API. - string stackPtrIdentifier = context.GetAdditionalIdentifier(info, "stackptr"); - // * __stackptr = stackalloc [<_bufferSize>]; - yield return LocalDeclarationStatement( - VariableDeclaration( - PointerType(_bufferElementType), - SingletonSeparatedList( - VariableDeclarator(stackPtrIdentifier) - .WithInitializer(EqualsValueClause( - StackAllocArrayCreationExpression( - ArrayType( - _bufferElementType, - SingletonList(ArrayRankSpecifier(SingletonSeparatedList( - MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - _marshallerType, - IdentifierName(ShapeMemberNames.BufferSize)) - )))))))))); - - (string managedIdentifier, _) = context.GetIdentifiers(info); - // .FromManaged(, new Span(, .BufferSize)); + // .FromManaged(, stackalloc [.BufferSize]); yield return ExpressionStatement( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, @@ -249,19 +243,13 @@ IEnumerable GenerateCallerAllocatedBufferMarshalStatements() new[] { Argument(IdentifierName(managedIdentifier)), - Argument( - ObjectCreationExpression( - GenericName(Identifier(TypeNames.System_Span), - TypeArgumentList(SingletonSeparatedList( - _bufferElementType)))) - .WithArgumentList( - ArgumentList(SeparatedList(new ArgumentSyntax[] - { - Argument(IdentifierName(stackPtrIdentifier)), - Argument(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, + Argument(StackAllocArrayCreationExpression( + ArrayType( + _bufferElementType, + SingletonList(ArrayRankSpecifier(SingletonSeparatedList( + MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, _marshallerType, - IdentifierName(ShapeMemberNames.BufferSize))) - })))) + IdentifierName(ShapeMemberNames.BufferSize)))))))) })))); } } diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Microsoft.Interop.SourceGeneration.csproj b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Microsoft.Interop.SourceGeneration.csproj index d2dd79ffb36dc..f9304841f0b34 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Microsoft.Interop.SourceGeneration.csproj +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Microsoft.Interop.SourceGeneration.csproj @@ -14,7 +14,7 @@ - +