Skip to content

Commit 1deac17

Browse files
authored
Strip trivia from tokens. (#88856)
* Strip trivia from tokens. Fixes #88798 * Move the trivia stripping into ContainingSyntax record constructor * Make ContainingSyntax a regular struct with a primary constructor instead of a record struct. * Fix #88867 * Suppress compiler diagnostics based on the linked issue.
1 parent 45db21d commit 1deac17

File tree

7 files changed

+36
-47
lines changed

7 files changed

+36
-47
lines changed

src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSExportGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
206206

207207
var containingTypeContext = new ContainingSyntaxContext(originalSyntax);
208208

209-
var methodSyntaxTemplate = new ContainingSyntax(originalSyntax.Modifiers.StripTriviaFromTokens(), SyntaxKind.MethodDeclaration, originalSyntax.Identifier, originalSyntax.TypeParameterList);
209+
var methodSyntaxTemplate = new ContainingSyntax(originalSyntax.Modifiers, SyntaxKind.MethodDeclaration, originalSyntax.Identifier, originalSyntax.TypeParameterList);
210210

211211
return new IncrementalStubGenerationContext(
212212
signatureContext,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
194194

195195
var containingTypeContext = new ContainingSyntaxContext(originalSyntax);
196196

197-
var methodSyntaxTemplate = new ContainingSyntax(originalSyntax.Modifiers.StripTriviaFromTokens(), SyntaxKind.MethodDeclaration, originalSyntax.Identifier, originalSyntax.TypeParameterList);
197+
var methodSyntaxTemplate = new ContainingSyntax(originalSyntax.Modifiers, SyntaxKind.MethodDeclaration, originalSyntax.Identifier, originalSyntax.TypeParameterList);
198198
return new IncrementalStubGenerationContext(
199199
signatureContext,
200200
containingTypeContext,

src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ private static IncrementalMethodStubGenerationContext CalculateStubInformation(M
307307

308308
var containingSyntaxContext = new ContainingSyntaxContext(syntax);
309309

310-
var methodSyntaxTemplate = new ContainingSyntax(syntax.Modifiers.StripAccessibilityModifiers().StripTriviaFromTokens(), SyntaxKind.MethodDeclaration, syntax.Identifier, syntax.TypeParameterList);
310+
var methodSyntaxTemplate = new ContainingSyntax(syntax.Modifiers.StripAccessibilityModifiers(), SyntaxKind.MethodDeclaration, syntax.Identifier, syntax.TypeParameterList);
311311

312312
ImmutableArray<FunctionPointerUnmanagedCallingConventionSyntax> callConv = VirtualMethodPointerStubGenerator.GenerateCallConvSyntaxFromAttributes(
313313
suppressGCTransitionAttribute,

src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VtableIndexStubGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ private static IncrementalMethodStubGenerationContext CalculateStubInformation(M
278278

279279
var containingSyntaxContext = new ContainingSyntaxContext(syntax);
280280

281-
var methodSyntaxTemplate = new ContainingSyntax(syntax.Modifiers.StripAccessibilityModifiers().StripTriviaFromTokens(), SyntaxKind.MethodDeclaration, syntax.Identifier, syntax.TypeParameterList);
281+
var methodSyntaxTemplate = new ContainingSyntax(syntax.Modifiers.StripAccessibilityModifiers(), SyntaxKind.MethodDeclaration, syntax.Identifier, syntax.TypeParameterList);
282282

283283
ImmutableArray<FunctionPointerUnmanagedCallingConventionSyntax> callConv = VirtualMethodPointerStubGenerator.GenerateCallConvSyntaxFromAttributes(suppressGCTransitionAttribute, unmanagedCallConvAttribute, defaultCallingConventions: ImmutableArray<FunctionPointerUnmanagedCallingConventionSyntax>.Empty);
284284

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
288288

289289
var containingTypeContext = new ContainingSyntaxContext(originalSyntax);
290290

291-
var methodSyntaxTemplate = new ContainingSyntax(originalSyntax.Modifiers.StripTriviaFromTokens(), SyntaxKind.MethodDeclaration, originalSyntax.Identifier, originalSyntax.TypeParameterList);
291+
var methodSyntaxTemplate = new ContainingSyntax(originalSyntax.Modifiers, SyntaxKind.MethodDeclaration, originalSyntax.Identifier, originalSyntax.TypeParameterList);
292292

293293
List<AttributeSyntax> additionalAttributes = GenerateSyntaxForForwardedAttributes(suppressGCTransitionAttribute, unmanagedCallConvAttribute, defaultDllImportSearchPathsAttribute);
294294
return new IncrementalStubGenerationContext(

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@
1212

1313
namespace Microsoft.Interop
1414
{
15-
public readonly record struct ContainingSyntax(SyntaxTokenList Modifiers, SyntaxKind TypeKind, SyntaxToken Identifier, TypeParameterListSyntax? TypeParameters)
15+
public readonly struct ContainingSyntax(SyntaxTokenList modifiers, SyntaxKind typeKind, SyntaxToken identifier, TypeParameterListSyntax? typeParameters) : IEquatable<ContainingSyntax>
1616
{
17+
public SyntaxTokenList Modifiers { get; init; } = modifiers.StripTriviaFromTokens();
18+
19+
public SyntaxToken Identifier { get; init; } = identifier.WithoutTrivia();
20+
21+
public SyntaxKind TypeKind { get; init; } = typeKind;
22+
23+
public TypeParameterListSyntax? TypeParameters { get; init; } = typeParameters;
24+
25+
public override bool Equals(object obj) => obj is ContainingSyntax other && Equals(other);
26+
1727
public bool Equals(ContainingSyntax other)
1828
{
1929
return Modifiers.SequenceEqual(other.Modifiers, SyntaxEquivalentComparer.Instance)
@@ -42,8 +52,12 @@ private static ImmutableArray<ContainingSyntax> GetContainingTypes(MemberDeclara
4252
ImmutableArray<ContainingSyntax>.Builder containingTypeInfoBuilder = ImmutableArray.CreateBuilder<ContainingSyntax>();
4353
for (SyntaxNode? parent = memberDeclaration.Parent; parent is TypeDeclarationSyntax typeDeclaration; parent = parent.Parent)
4454
{
45-
containingTypeInfoBuilder.Add(new ContainingSyntax(typeDeclaration.Modifiers.StripTriviaFromTokens(), typeDeclaration.Kind(), typeDeclaration.Identifier.WithoutTrivia(),
46-
typeDeclaration.TypeParameterList));
55+
containingTypeInfoBuilder.Add(
56+
new ContainingSyntax(
57+
typeDeclaration.Modifiers,
58+
typeDeclaration.Kind(),
59+
typeDeclaration.Identifier,
60+
typeDeclaration.TypeParameterList));
4761
}
4862

4963
return containingTypeInfoBuilder.ToImmutable();

src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CompileFails.cs

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -558,69 +558,45 @@ public static IEnumerable<object[]> ByValueMarshalAttributeOnValueTypes()
558558

559559
// [In] is default for all non-pinned marshalled types
560560
yield return new object[] { ID(), codeSnippets.ByValueMarshallingOfType(inAttribute, "int", paramNameWithLocation), new DiagnosticResult[] {
561-
inAttributeIsDefaultDiagnostic,
562-
//https://github.com/dotnet/runtime/issues/88540
563561
inAttributeIsDefaultDiagnostic } };
564562
yield return new object[] { ID(), codeSnippets.ByValueMarshallingOfType(inAttribute, "byte", paramNameWithLocation), new DiagnosticResult[] {
565-
inAttributeIsDefaultDiagnostic,
566-
//https://github.com/dotnet/runtime/issues/88540
567563
inAttributeIsDefaultDiagnostic } };
568564
yield return new object[] { ID(), codeSnippets.ByValueMarshallingOfType(inAttribute + "[MarshalAs(UnmanagedType.U4)]", "bool", paramNameWithLocation), new DiagnosticResult[] {
569-
inAttributeIsDefaultDiagnostic,
570-
//https://github.com/dotnet/runtime/issues/88540
571565
inAttributeIsDefaultDiagnostic } };
572566
yield return new object[] { ID(), codeSnippets.ByValueMarshallingOfType(inAttribute + "[MarshalAs(UnmanagedType.U2)]", "char", paramNameWithLocation), new DiagnosticResult[] {
573-
inAttributeIsDefaultDiagnostic,
574-
//https://github.com/dotnet/runtime/issues/88540
575567
inAttributeIsDefaultDiagnostic } };
576568

577569
// [Out] is not allowed on value types passed by value - there is no indirection for the callee to make visible modifications.
578570
var outAttributeNotSupportedOnValueParameters = new DiagnosticResult(GeneratorDiagnostics.ParameterTypeNotSupportedWithDetails)
579571
.WithLocation(0)
580572
.WithArguments(SR.OutAttributeNotSupportedOnByValueParameters, paramName);
581573
yield return new object[] { ID(), codeSnippets.ByValueMarshallingOfType(outAttribute, "int", paramNameWithLocation), new DiagnosticResult[] {
582-
outAttributeNotSupportedOnValueParameters,
583-
//https://github.com/dotnet/runtime/issues/88540
584574
outAttributeNotSupportedOnValueParameters } };
585575
yield return new object[] {
586576
ID(),
587577
codeSnippets.ByValueMarshallingOfType(outAttribute, "IntStruct", paramNameWithLocation) + CodeSnippets.IntStructAndMarshaller,
588578
new DiagnosticResult[] {
589-
outAttributeNotSupportedOnValueParameters,
590-
//https://github.com/dotnet/runtime/issues/88540
591-
outAttributeNotSupportedOnValueParameters,
579+
outAttributeNotSupportedOnValueParameters
592580
} };
593581
yield return new object[] { ID(), codeSnippets.ByValueMarshallingOfType(outAttribute + "[MarshalAs(UnmanagedType.U4)]", "bool", paramNameWithLocation), new DiagnosticResult[] {
594-
outAttributeNotSupportedOnValueParameters,
595-
//https://github.com/dotnet/runtime/issues/88540
596582
outAttributeNotSupportedOnValueParameters
597583
} };
598584
yield return new object[] { ID(), codeSnippets.ByValueMarshallingOfType(outAttribute, "[MarshalAs(UnmanagedType.U2)] char", paramNameWithLocation), new DiagnosticResult[] {
599-
outAttributeNotSupportedOnValueParameters,
600-
//https://github.com/dotnet/runtime/issues/88540
601585
outAttributeNotSupportedOnValueParameters
602586
} };
603587
// [In,Out] should only warn for Out attribute
604588
yield return new object[] { ID(), codeSnippets.ByValueMarshallingOfType(inAttribute+outAttribute, "int", paramNameWithLocation), new DiagnosticResult[] {
605-
outAttributeNotSupportedOnValueParameters,
606-
//https://github.com/dotnet/runtime/issues/88540
607589
outAttributeNotSupportedOnValueParameters } };
608590
yield return new object[] {
609591
ID(),
610592
codeSnippets.ByValueMarshallingOfType(inAttribute+outAttribute, "IntStruct", paramNameWithLocation) + CodeSnippets.IntStructAndMarshaller,
611593
new DiagnosticResult[] {
612-
outAttributeNotSupportedOnValueParameters,
613-
//https://github.com/dotnet/runtime/issues/88540
614-
outAttributeNotSupportedOnValueParameters,
594+
outAttributeNotSupportedOnValueParameters
615595
} };
616596
yield return new object[] { ID(), codeSnippets.ByValueMarshallingOfType(inAttribute + outAttribute + "[MarshalAs(UnmanagedType.U4)]", "bool", paramNameWithLocation), new DiagnosticResult[] {
617-
outAttributeNotSupportedOnValueParameters,
618-
//https://github.com/dotnet/runtime/issues/88540
619597
outAttributeNotSupportedOnValueParameters
620598
} };
621599
yield return new object[] { ID(), codeSnippets.ByValueMarshallingOfType(inAttribute + outAttribute, "[MarshalAs(UnmanagedType.U2)] char", paramNameWithLocation), new DiagnosticResult[] {
622-
outAttributeNotSupportedOnValueParameters,
623-
//https://github.com/dotnet/runtime/issues/88540
624600
outAttributeNotSupportedOnValueParameters
625601
} };
626602

@@ -688,12 +664,12 @@ public static IEnumerable<object[]> ByValueMarshalAttributeOnReferenceTypes()
688664
yield return new object[] {
689665
ID(),
690666
codeSnippets.ByValueMarshallingOfType(inAttribute, "string", paramNameWithLocation, (StringMarshalling.Utf8, null)),
691-
new DiagnosticResult[] { inAttributeIsDefaultDiagnostic, inAttributeIsDefaultDiagnostic }
667+
new DiagnosticResult[] { inAttributeIsDefaultDiagnostic }
692668
};
693669
yield return new object[] {
694670
ID(),
695671
codeSnippets.ByValueMarshallingOfType(inAttribute, "IntClass", paramNameWithLocation) + CodeSnippets.IntClassAndMarshaller,
696-
new DiagnosticResult[] { inAttributeIsDefaultDiagnostic, inAttributeIsDefaultDiagnostic }
672+
new DiagnosticResult[] { inAttributeIsDefaultDiagnostic }
697673
};
698674

699675
var outNotAllowedOnRefTypes = new DiagnosticResult(GeneratorDiagnostics.ParameterTypeNotSupportedWithDetails)
@@ -704,21 +680,21 @@ public static IEnumerable<object[]> ByValueMarshalAttributeOnReferenceTypes()
704680
yield return new object[] {
705681
ID(),
706682
codeSnippets.ByValueMarshallingOfType(outAttribute, "string", paramNameWithLocation, (StringMarshalling.Utf8, null)),
707-
new DiagnosticResult[] { outNotAllowedOnRefTypes, outNotAllowedOnRefTypes }
683+
new DiagnosticResult[] { outNotAllowedOnRefTypes }
708684
};
709685

710686
// [Out] warns on by value reference types
711687
yield return new object[] {
712688
ID(),
713689
codeSnippets.ByValueMarshallingOfType(outAttribute, "IntClass", paramNameWithLocation) + CodeSnippets.IntClassAndMarshaller,
714-
new DiagnosticResult[] { outNotAllowedOnRefTypes, outNotAllowedOnRefTypes }
690+
new DiagnosticResult[] { outNotAllowedOnRefTypes }
715691
};
716692

717693
// [In,Out] is fine on classes
718694
yield return new object[] {
719695
ID(),
720696
codeSnippets.ByValueMarshallingOfType(inAttribute + outAttribute, "IntClass", paramNameWithLocation) + CodeSnippets.IntClassAndMarshaller,
721-
new DiagnosticResult[] { outNotAllowedOnRefTypes, outNotAllowedOnRefTypes }
697+
new DiagnosticResult[] { outNotAllowedOnRefTypes }
722698
};
723699

724700
// All refkinds are okay on classes and strings
@@ -774,11 +750,10 @@ public static IEnumerable<object[]> ByValueMarshalAttributeOnPinnedMarshalledTyp
774750
.WithLocation(0)
775751
.WithArguments(SR.InAttributeOnlyNotSupportedOnPinnedParameters, paramName);
776752
yield return new object[] { ID(), codeSnippets.ByValueMarshallingOfType(inAttribute + constElementCount, "int[]", paramNameWithLocation), new DiagnosticResult[] {
777-
inAttributeNotSupportedOnPinnedParameter,
778-
//https://github.com/dotnet/runtime/issues/88540
779753
inAttributeNotSupportedOnPinnedParameter
780754
}};
781-
// new issue before merge: char generated code doesn't seem to work well with [In, Out]
755+
// blittable arrays don't support [In] only. Different diagnostics are issued because we can pin in one direction (managed->unmanaged)
756+
// but not the other direction.
782757
yield return new object[] {
783758
ID(),
784759
codeSnippets.ByValueMarshallingOfType(inAttribute + constElementCount, "char[]", paramNameWithLocation, (StringMarshalling.Utf16, null)),
@@ -793,13 +768,13 @@ public static IEnumerable<object[]> ByValueMarshalAttributeOnPinnedMarshalledTyp
793768
"bool[]",
794769
paramNameWithLocation,
795770
(StringMarshalling.Utf16, null)),
796-
new DiagnosticResult[] { inAttributeIsDefaultDiagnostic, inAttributeIsDefaultDiagnostic}
771+
new DiagnosticResult[] { inAttributeIsDefaultDiagnostic }
797772
};
798773
// Overriding marshalling with a custom marshaller makes it not pinned
799774
yield return new object[] {
800775
ID(),
801776
codeSnippets.ByValueMarshallingOfType(inAttribute, "[MarshalUsing(typeof(IntMarshaller), ElementIndirectionDepth = 1), MarshalUsing(ConstantElementCount = 10)]int[]", paramNameWithLocation) + CodeSnippets.IntMarshaller,
802-
new DiagnosticResult[] { inAttributeIsDefaultDiagnostic, inAttributeIsDefaultDiagnostic}
777+
new DiagnosticResult[] { inAttributeIsDefaultDiagnostic }
803778
};
804779

805780
// [In, Out] is default
@@ -811,12 +786,12 @@ public static IEnumerable<object[]> ByValueMarshalAttributeOnPinnedMarshalledTyp
811786
yield return new object[] {
812787
ID(),
813788
codeSnippets.ByValueMarshallingOfType(inAttribute + outAttribute + constElementCount, "int[]", paramNameWithLocation),
814-
new DiagnosticResult[] { inOutAttributeIsDefaultDiagnostic, inOutAttributeIsDefaultDiagnostic}
789+
new DiagnosticResult[] { inOutAttributeIsDefaultDiagnostic }
815790
};
816791
yield return new object[] {
817792
ID(),
818793
codeSnippets.ByValueMarshallingOfType(inAttribute + outAttribute + constElementCount, "char[]", paramNameWithLocation, (StringMarshalling.Utf16, null)),
819-
//https://github.com/dotnet/runtime/issues/88540
794+
//https://github.com/dotnet/runtime/issues/88708
820795
new DiagnosticResult[] { inOutAttributeIsDefaultDiagnostic }
821796
};
822797

@@ -847,7 +822,7 @@ public async Task VerifyByValueMarshallingAttributeUsage(string id, string sourc
847822
{
848823
TestCode = source,
849824
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck,
850-
// Our fallback mechanism for invalid code for unmanaged->managed stubs sometimes generates invalid code.
825+
// https://github.com/dotnet/runtime/issues/88708
851826
CompilerDiagnostics = diagnostics.Length != 0 ? CompilerDiagnostics.None : CompilerDiagnostics.Errors,
852827
};
853828
test.ExpectedDiagnostics.AddRange(diagnostics);

0 commit comments

Comments
 (0)