Skip to content

Commit 03c2146

Browse files
authored
Use the scoped keyword in LibraryImportGenerator (#73479)
1 parent 85cadd9 commit 03c2146

File tree

8 files changed

+57
-54
lines changed

8 files changed

+57
-54
lines changed

eng/Versions.props

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@
3131
<MicrosoftCodeAnalysisVersion_3_11>3.11.0</MicrosoftCodeAnalysisVersion_3_11>
3232
<!-- Compatibility with VS 17.0/.NET SDK 6.0.1xx -->
3333
<MicrosoftCodeAnalysisVersion_4_0>4.0.1</MicrosoftCodeAnalysisVersion_4_0>
34-
<!-- Compatibility with VS 17.X/.NET SDK 7.0.1xx -->
34+
<!-- Compatibility with VS 17.4/.NET SDK 7.0.1xx -->
3535
<!--
36-
This version is a moving target until we ship.
36+
The exact version is a moving target until we ship.
3737
It should never go ahead of the Roslyn version included in the SDK version in dotnet/arcade's global.json to avoid causing breaks in product construction.
3838
-->
39-
<MicrosoftCodeAnalysisVersion_4_X>4.3.0-2.final</MicrosoftCodeAnalysisVersion_4_X>
39+
<MicrosoftCodeAnalysisVersion_4_4>4.4.0-1.22369.1</MicrosoftCodeAnalysisVersion_4_4>
4040
</PropertyGroup>
4141
<PropertyGroup>
4242
<!-- Code analysis dependencies -->
4343
<MicrosoftCodeAnalysisAnalyzersVersion>3.3.3</MicrosoftCodeAnalysisAnalyzersVersion>
4444
<MicrosoftCodeAnalysisCSharpCodeStyleVersion>4.3.0-2.final</MicrosoftCodeAnalysisCSharpCodeStyleVersion>
45-
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>4.3.0-2.final</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
46-
<MicrosoftCodeAnalysisCSharpVersion>4.3.0-2.final</MicrosoftCodeAnalysisCSharpVersion>
45+
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>4.4.0-1.final</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
46+
<MicrosoftCodeAnalysisCSharpVersion>4.4.0-1.final</MicrosoftCodeAnalysisCSharpVersion>
4747
<MicrosoftCodeAnalysisNetAnalyzersVersion>7.0.0-preview1.22403.2</MicrosoftCodeAnalysisNetAnalyzersVersion>
48-
<MicrosoftCodeAnalysisVersion>4.3.0-2.final</MicrosoftCodeAnalysisVersion>
48+
<MicrosoftCodeAnalysisVersion>4.4.0-1.final</MicrosoftCodeAnalysisVersion>
4949
<!--
5050
TODO: Remove pinned version once arcade supplies a compiler that enables the repo to compile.
5151
-->

src/libraries/System.Private.CoreLib/gen/EventSourceGenerator.Parser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using System.Threading;
1010
using Microsoft.CodeAnalysis;
1111
using Microsoft.CodeAnalysis.CSharp.Syntax;
12-
using Microsoft.CodeAnalysis.DotnetRuntime.Extensions;
12+
using GeneratorAttributeSyntaxContext = Microsoft.CodeAnalysis.DotnetRuntime.Extensions.GeneratorAttributeSyntaxContext;
1313

1414
namespace Generators
1515
{

src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSImportGenerator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</PropertyGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion_4_X)" PrivateAssets="all" />
26+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion_4_4)" PrivateAssets="all" />
2727
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
2828
</ItemGroup>
2929

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</PropertyGroup>
2323

2424
<ItemGroup>
25-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion_4_X)" PrivateAssets="all" />
25+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion_4_4)" PrivateAssets="all" />
2626
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
2727
</ItemGroup>
2828

@@ -45,9 +45,9 @@
4545
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider.ImmutableArrayValueComparer.cs" Link="Common\Roslyn\SyntaxValueProvider.ImmutableArrayValueComparer.cs" />
4646
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider_ForAttributeWithMetadataName.cs" Link="Common\Roslyn\SyntaxValueProvider_ForAttributeWithMetadataName.cs" />
4747
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider_ForAttributeWithSimpleName.cs" Link="Common\Roslyn\SyntaxValueProvider_ForAttributeWithSimpleName.cs" />
48-
48+
4949
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.cs" Link="Production\ValueListBuilder.cs" />
5050
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.Pop.cs" Link="Production\ValueListBuilder.Pop.cs" />
5151
</ItemGroup>
52-
52+
5353
</Project>

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ namespace Microsoft.Interop
1515
/// </summary>
1616
public abstract record ManagedTypeInfo(string FullTypeName, string DiagnosticFormattedName)
1717
{
18-
public TypeSyntax Syntax { get; } = SyntaxFactory.ParseTypeName(FullTypeName);
18+
private TypeSyntax? _syntax;
19+
public TypeSyntax Syntax => _syntax ??= SyntaxFactory.ParseTypeName(FullTypeName);
20+
21+
protected ManagedTypeInfo(ManagedTypeInfo original)
22+
{
23+
FullTypeName = original.FullTypeName;
24+
DiagnosticFormattedName = original.DiagnosticFormattedName;
25+
// Explicitly don't initialize _syntax here. We want Syntax to be recalculated
26+
// from the results of a with-expression, which assigns the new property values
27+
// to the result of this constructor.
28+
}
1929

2030
public static ManagedTypeInfo CreateTypeInfoForTypeSymbol(ITypeSymbol type)
2131
{

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ private IMarshallingGenerator CreateCustomNativeTypeMarshaller(TypePositionInfo
235235
ICustomTypeMarshallingStrategy marshallingStrategy;
236236
if (marshallerData.HasState)
237237
{
238-
marshallingStrategy = new StatefulValueMarshalling(marshallerData.MarshallerType.Syntax, marshallerData.NativeType.Syntax, marshallerData.Shape);
238+
marshallingStrategy = new StatefulValueMarshalling(marshallerData.MarshallerType, marshallerData.NativeType.Syntax, marshallerData.Shape);
239239
if (marshallerData.Shape.HasFlag(MarshallerShape.CallerAllocatedBuffer))
240240
marshallingStrategy = new StatefulCallerAllocatedBufferMarshalling(marshallingStrategy, marshallerData.MarshallerType.Syntax, marshallerData.BufferElementType.Syntax);
241241
}
@@ -283,16 +283,21 @@ private IMarshallingGenerator CreateNativeCollectionMarshaller(
283283

284284
// Insert the unmanaged element type into the marshaller type
285285
TypeSyntax unmanagedElementType = elementMarshaller.AsNativeType(elementInfo).GetCompatibleGenericTypeParameterSyntax();
286-
TypeSyntax marshallerTypeSyntax = marshallerData.MarshallerType.Syntax;
287-
marshallerTypeSyntax = ReplacePlaceholderSyntaxWithUnmanagedTypeSyntax(marshallerTypeSyntax, marshalInfo, unmanagedElementType);
286+
ManagedTypeInfo marshallerType = marshallerData.MarshallerType;
287+
TypeSyntax marshallerTypeSyntax = ReplacePlaceholderSyntaxWithUnmanagedTypeSyntax(marshallerType.Syntax, marshalInfo, unmanagedElementType);
288+
marshallerType = marshallerType with
289+
{
290+
FullTypeName = marshallerTypeSyntax.ToString(),
291+
DiagnosticFormattedName = marshallerTypeSyntax.ToString(),
292+
};
288293
TypeSyntax nativeTypeSyntax = ReplacePlaceholderSyntaxWithUnmanagedTypeSyntax(marshallerData.NativeType.Syntax, marshalInfo, unmanagedElementType);
289294

290295
ICustomTypeMarshallingStrategy marshallingStrategy;
291296
bool elementIsBlittable = elementMarshaller is BlittableMarshaller;
292297

293298
if (marshallerData.HasState)
294299
{
295-
marshallingStrategy = new StatefulValueMarshalling(marshallerTypeSyntax, nativeTypeSyntax, marshallerData.Shape);
300+
marshallingStrategy = new StatefulValueMarshalling(marshallerType, nativeTypeSyntax, marshallerData.Shape);
296301
if (marshallerData.Shape.HasFlag(MarshallerShape.CallerAllocatedBuffer))
297302
{
298303
// Check if the buffer element type is actually the unmanaged element type

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

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ namespace Microsoft.Interop
1414
internal sealed class StatefulValueMarshalling : ICustomTypeMarshallingStrategy
1515
{
1616
internal const string MarshallerIdentifier = "marshaller";
17-
private readonly TypeSyntax _marshallerTypeSyntax;
17+
private readonly ManagedTypeInfo _marshallerType;
1818
private readonly TypeSyntax _nativeTypeSyntax;
1919
private readonly MarshallerShape _shape;
2020

21-
public StatefulValueMarshalling(TypeSyntax marshallerTypeSyntax, TypeSyntax nativeTypeSyntax, MarshallerShape shape)
21+
public StatefulValueMarshalling(ManagedTypeInfo marshallerType, TypeSyntax nativeTypeSyntax, MarshallerShape shape)
2222
{
23-
_marshallerTypeSyntax = marshallerTypeSyntax;
23+
_marshallerType = marshallerType;
2424
_nativeTypeSyntax = nativeTypeSyntax;
2525
_shape = shape;
2626
}
@@ -140,10 +140,23 @@ public IEnumerable<StatementSyntax> GenerateUnmarshalCaptureStatements(TypePosit
140140
public IEnumerable<StatementSyntax> GenerateSetupStatements(TypePositionInfo info, StubCodeContext context)
141141
{
142142
// <marshaller> = new();
143-
yield return MarshallerHelpers.Declare(
144-
_marshallerTypeSyntax,
143+
LocalDeclarationStatementSyntax declaration = MarshallerHelpers.Declare(
144+
_marshallerType.Syntax,
145145
context.GetAdditionalIdentifier(info, MarshallerIdentifier),
146146
ImplicitObjectCreationExpression(ArgumentList(), initializer: null));
147+
148+
// For byref-like marshaller types, we'll mark them as scoped.
149+
// Byref-like types can capture references, so by default the compiler has to worry that
150+
// they could enable those references to escape the current stack frame.
151+
// In particular, this can interact poorly with the caller-allocated-buffer marshalling
152+
// support and make the simple `marshaller.FromManaged(managed, stackalloc X[i])` expression
153+
// illegal. Mark the marshaller type as scoped so the compiler knows that it won't escape.
154+
if (_marshallerType is ValueTypeInfo { IsByRefLike: true })
155+
{
156+
declaration = declaration.AddModifiers(Token(SyntaxKind.ScopedKeyword));
157+
}
158+
159+
yield return declaration;
147160
}
148161

149162
public IEnumerable<StatementSyntax> GeneratePinStatements(TypePositionInfo info, StubCodeContext context)
@@ -218,28 +231,9 @@ public IEnumerable<StatementSyntax> GenerateMarshalStatements(TypePositionInfo i
218231

219232
IEnumerable<StatementSyntax> GenerateCallerAllocatedBufferMarshalStatements()
220233
{
221-
// TODO: Update once we can consume the scoped keword. We should be able to simplify this once we get that API.
222-
string stackPtrIdentifier = context.GetAdditionalIdentifier(info, "stackptr");
223-
// <bufferElementType>* <managedIdentifier>__stackptr = stackalloc <bufferElementType>[<_bufferSize>];
224-
yield return LocalDeclarationStatement(
225-
VariableDeclaration(
226-
PointerType(_bufferElementType),
227-
SingletonSeparatedList(
228-
VariableDeclarator(stackPtrIdentifier)
229-
.WithInitializer(EqualsValueClause(
230-
StackAllocArrayCreationExpression(
231-
ArrayType(
232-
_bufferElementType,
233-
SingletonList(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(
234-
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
235-
_marshallerType,
236-
IdentifierName(ShapeMemberNames.BufferSize))
237-
))))))))));
238-
239-
240234
(string managedIdentifier, _) = context.GetIdentifiers(info);
241235

242-
// <marshaller>.FromManaged(<managedIdentifier>, new Span<bufferElementType>(<stackPtrIdentifier>, <marshallerType>.BufferSize));
236+
// <marshaller>.FromManaged(<managedIdentifier>, stackalloc <bufferElementType>[<marshallerType>.BufferSize]);
243237
yield return ExpressionStatement(
244238
InvocationExpression(
245239
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
@@ -249,19 +243,13 @@ IEnumerable<StatementSyntax> GenerateCallerAllocatedBufferMarshalStatements()
249243
new[]
250244
{
251245
Argument(IdentifierName(managedIdentifier)),
252-
Argument(
253-
ObjectCreationExpression(
254-
GenericName(Identifier(TypeNames.System_Span),
255-
TypeArgumentList(SingletonSeparatedList(
256-
_bufferElementType))))
257-
.WithArgumentList(
258-
ArgumentList(SeparatedList(new ArgumentSyntax[]
259-
{
260-
Argument(IdentifierName(stackPtrIdentifier)),
261-
Argument(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
246+
Argument(StackAllocArrayCreationExpression(
247+
ArrayType(
248+
_bufferElementType,
249+
SingletonList(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(
250+
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
262251
_marshallerType,
263-
IdentifierName(ShapeMemberNames.BufferSize)))
264-
}))))
252+
IdentifierName(ShapeMemberNames.BufferSize))))))))
265253
}))));
266254
}
267255
}

src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Microsoft.Interop.SourceGeneration.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion_4_X)" PrivateAssets="all" />
17+
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion_4_4)" PrivateAssets="all" />
1818
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
1919
</ItemGroup>
2020

0 commit comments

Comments
 (0)