Skip to content

Commit 6d6fbe0

Browse files
authored
Allow unnecessary [In, Out] attributes on blittable arrays and report them as unnecessary (#87774)
* Change our '[In, Out]' attribute handling to have a concept of 'unnecessary but not invalid' to enable us in the future to report a non-fatal diagnostic. * WIP enable unnecessary diagnostic in LibraryImportGenerator' * Hook up diagnostics and add tests for LibraryImportGenerator * Hook up diagnostics for ComInterfaceGenerator as well * Update XLF * Don't reference the live LibraryImportGenerator when building .NET 7 assets. Use the inbox instead. * PR feedback * Fix merge
1 parent bc70fec commit 6d6fbe0

File tree

70 files changed

+823
-69
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+823
-69
lines changed

eng/generators.targets

+2-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<EnabledGenerators Include="LibraryImportGenerator" Condition="'$(EnableLibraryImportGenerator)' == 'true'" />
1414
<!-- If the current project is not System.Private.CoreLib, we enable the LibraryImportGenerator source generator
1515
when the project is a C# source project that:
16-
- doesn't target the latest TFM or
16+
- doesn't target the a TFM that includes LibraryImportGenerator or
1717
- doesn't reference the live targeting pack (i.e. when inbox) and
1818
- references System.Private.CoreLib, or
1919
- references System.Runtime.InteropServices -->
@@ -22,7 +22,7 @@
2222
'$(IsSourceProject)' == 'true' and
2323
'$(MSBuildProjectExtension)' == '.csproj' and
2424
(
25-
'$(TargetFrameworkMoniker)' != '$(NetCoreAppCurrentTargetFrameworkMoniker)' or
25+
!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0')) or
2626
(
2727
'$(DisableImplicitFrameworkReferences)' == 'true' and
2828
(
@@ -33,17 +33,13 @@
3333
)" />
3434
<!-- We enable the ComInterfaceGenerator source generator
3535
when the project is a C# source project that:
36-
- doesn't target the latest TFM or
3736
- references System.Runtime.InteropServices directly and not through the live targeting pack (i.e. when inbox) -->
3837
<EnabledGenerators Include="ComInterfaceGenerator"
3938
Condition="'$(IsSourceProject)' == 'true' and
4039
'$(MSBuildProjectExtension)' == '.csproj' and
4140
(
42-
'$(TargetFrameworkMoniker)' != '$(NetCoreAppCurrentTargetFrameworkMoniker)' or
43-
(
4441
'$(DisableImplicitFrameworkReferences)' == 'true' and
4542
'@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices'))' == 'true'
46-
)
4743
)" />
4844
</ItemGroup>
4945

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected BaseJSGenerator(MarshalerType marshalerType, IMarshallingGenerator inn
2626
public virtual bool UsesNativeIdentifier(TypePositionInfo info, StubCodeContext context) => _inner.UsesNativeIdentifier(info, context);
2727
public SignatureBehavior GetNativeSignatureBehavior(TypePositionInfo info) => _inner.GetNativeSignatureBehavior(info);
2828
public ValueBoundaryBehavior GetValueBoundaryBehavior(TypePositionInfo info, StubCodeContext context) => _inner.GetValueBoundaryBehavior(info, context);
29-
public bool SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => _inner.SupportsByValueMarshalKind(marshalKind, context);
29+
public ByValueMarshalKindSupport SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => _inner.SupportsByValueMarshalKind(marshalKind, context);
3030

3131
public virtual IEnumerable<ExpressionSyntax> GenerateBind(TypePositionInfo info, StubCodeContext context)
3232
{

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal sealed class EmptyJSGenerator : IJSMarshallingGenerator
1515
public SignatureBehavior GetNativeSignatureBehavior(TypePositionInfo info) => SignatureBehavior.ManagedTypeAndAttributes;
1616
public ValueBoundaryBehavior GetValueBoundaryBehavior(TypePositionInfo info, StubCodeContext context) => ValueBoundaryBehavior.ManagedIdentifier;
1717
public bool IsSupported(TargetFramework target, Version version) => false;
18-
public bool SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => false;
18+
public ByValueMarshalKindSupport SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => ByValueMarshalKindSupport.NotSupported;
1919
public bool UsesNativeIdentifier(TypePositionInfo info, StubCodeContext context) => false;
2020
}
2121
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ internal sealed class DiagnosticDescriptorProvider : IDiagnosticDescriptorProvid
2424
GeneratorDiagnostic.NotSupported { NotSupportedDetails: null, TypePositionInfo: { IsManagedReturnPosition: false, MarshallingAttributeInfo: MarshalAsInfo } } => GeneratorDiagnostics.MarshalAsParameterConfigurationNotSupported,
2525
GeneratorDiagnostic.NotSupported { NotSupportedDetails: not null, TypePositionInfo.IsManagedReturnPosition: true } => GeneratorDiagnostics.ReturnTypeNotSupportedWithDetails,
2626
GeneratorDiagnostic.NotSupported { NotSupportedDetails: not null, TypePositionInfo.IsManagedReturnPosition: false } => GeneratorDiagnostics.ParameterTypeNotSupportedWithDetails,
27+
GeneratorDiagnostic.UnnecessaryData { TypePositionInfo.IsManagedReturnPosition: false } => GeneratorDiagnostics.UnnecessaryParameterMarshallingInfo,
28+
GeneratorDiagnostic.UnnecessaryData { TypePositionInfo.IsManagedReturnPosition: true } => GeneratorDiagnostics.UnnecessaryReturnMarshallingInfo,
2729
{ IsFatal: false } => null,
2830
{ TypePositionInfo.IsManagedReturnPosition: true } => GeneratorDiagnostics.ReturnTypeNotSupported,
2931
{ TypePositionInfo.IsManagedReturnPosition: false } => GeneratorDiagnostics.ParameterTypeNotSupported,

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

+29
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class Ids
2121
public const string TypeNotSupported = Prefix + "1051";
2222
public const string ConfigurationNotSupported = Prefix + "1052";
2323
public const string RequiresAllowUnsafeBlocks = Prefix + "1062";
24+
public const string UnnecessaryMarshallingInfo = Prefix + "1063";
2425
public const string InvalidGeneratedComInterfaceAttributeUsage = Prefix + "1090";
2526
public const string MemberWillNotBeSourceGenerated = Prefix + "1091";
2627
public const string MultipleComInterfaceBaseTypes = Prefix + "1092";
@@ -415,6 +416,34 @@ public class Ids
415416
isEnabledByDefault: true,
416417
description: GetResourceString(nameof(SR.ClassDoesNotImplementAnyGeneratedComInterfacesDescription)));
417418

419+
public static readonly DiagnosticDescriptor UnnecessaryParameterMarshallingInfo =
420+
new DiagnosticDescriptor(
421+
Ids.UnnecessaryMarshallingInfo,
422+
GetResourceString(nameof(SR.UnnecessaryMarshallingInfoTitle)),
423+
GetResourceString(nameof(SR.UnnecessaryParameterMarshallingInfoMessage)),
424+
Category,
425+
DiagnosticSeverity.Info,
426+
isEnabledByDefault: true,
427+
description: GetResourceString(nameof(SR.UnnecessaryMarshallingInfoDescription)),
428+
customTags: new[]
429+
{
430+
WellKnownDiagnosticTags.Unnecessary
431+
});
432+
433+
public static readonly DiagnosticDescriptor UnnecessaryReturnMarshallingInfo =
434+
new DiagnosticDescriptor(
435+
Ids.UnnecessaryMarshallingInfo,
436+
GetResourceString(nameof(SR.UnnecessaryMarshallingInfoTitle)),
437+
GetResourceString(nameof(SR.UnnecessaryReturnMarshallingInfoMessage)),
438+
Category,
439+
DiagnosticSeverity.Info,
440+
isEnabledByDefault: true,
441+
description: GetResourceString(nameof(SR.UnnecessaryMarshallingInfoDescription)),
442+
customTags: new[]
443+
{
444+
WellKnownDiagnosticTags.Unnecessary
445+
});
446+
418447
/// <summary>
419448
/// Report diagnostic for invalid configuration for string marshalling.
420449
/// </summary>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
6161
public ValueBoundaryBehavior GetValueBoundaryBehavior(TypePositionInfo info, StubCodeContext context) => ValueBoundaryBehavior.NativeIdentifier;
6262
public bool IsSupported(TargetFramework target, Version version)
6363
=> target == TargetFramework.Net && version >= new Version(5, 0);
64-
public bool SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => false;
64+
public ByValueMarshalKindSupport SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => ByValueMarshalKindSupport.NotSupported;
6565
public bool UsesNativeIdentifier(TypePositionInfo info, StubCodeContext context) => true;
6666
}
6767
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
7171
public SignatureBehavior GetNativeSignatureBehavior(TypePositionInfo info) => SignatureBehavior.NativeType;
7272
public ValueBoundaryBehavior GetValueBoundaryBehavior(TypePositionInfo info, StubCodeContext context) => ValueBoundaryBehavior.ManagedIdentifier;
7373
public bool IsSupported(TargetFramework target, Version version) => true;
74-
public bool SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => false;
74+
public ByValueMarshalKindSupport SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => ByValueMarshalKindSupport.NotSupported;
7575
public bool UsesNativeIdentifier(TypePositionInfo info, StubCodeContext context) => false;
7676
}
7777

@@ -108,7 +108,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
108108
public SignatureBehavior GetNativeSignatureBehavior(TypePositionInfo info) => SignatureBehavior.NativeType;
109109
public ValueBoundaryBehavior GetValueBoundaryBehavior(TypePositionInfo info, StubCodeContext context) => ValueBoundaryBehavior.ManagedIdentifier;
110110
public bool IsSupported(TargetFramework target, Version version) => true;
111-
public bool SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => false;
111+
public ByValueMarshalKindSupport SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => ByValueMarshalKindSupport.NotSupported;
112112
public bool UsesNativeIdentifier(TypePositionInfo info, StubCodeContext context) => false;
113113
}
114114
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
6161
public SignatureBehavior GetNativeSignatureBehavior(TypePositionInfo info) => SignatureBehavior.NativeType;
6262
public ValueBoundaryBehavior GetValueBoundaryBehavior(TypePositionInfo info, StubCodeContext context) => ValueBoundaryBehavior.NativeIdentifier;
6363
public bool IsSupported(TargetFramework target, Version version) => true;
64-
public bool SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => false;
64+
public ByValueMarshalKindSupport SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => ByValueMarshalKindSupport.NotSupported;
6565
public bool UsesNativeIdentifier(TypePositionInfo info, StubCodeContext context) => true;
6666
}
6767
}

src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/Strings.resx

+12
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,16 @@
395395
<data name="MarshalAsConfigurationNotSupportedMessageReturn" xml:space="preserve">
396396
<value>The specified 'MarshalAsAttribute' configuration for the return value of method '{1}' is not supported by source-generated COM. If the specified configuration is required, use `ComImport` instead.</value>
397397
</data>
398+
<data name="UnnecessaryMarshallingInfoDescription" xml:space="preserve">
399+
<value>Unnecesssary marshalling info was provided. This marshalling information can be removed without any change in behavior to the application.</value>
400+
</data>
401+
<data name="UnnecessaryMarshallingInfoTitle" xml:space="preserve">
402+
<value>Unnecessary marshalling info was provided and can be removed.</value>
403+
</data>
404+
<data name="UnnecessaryParameterMarshallingInfoMessage" xml:space="preserve">
405+
<value>Unnecessary marshalling info '{0}' was provided for parameter '{1}'</value>
406+
</data>
407+
<data name="UnnecessaryReturnMarshallingInfoMessage" xml:space="preserve">
408+
<value>Unnecessary marshalling info '{0}' was provided for the return type of method '{1}'</value>
409+
</data>
398410
</root>

src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.cs.xlf

+20
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,26 @@
454454
<target state="translated">Zadaný typ není podporován modelem COM generovaným zdrojem.</target>
455455
<note />
456456
</trans-unit>
457+
<trans-unit id="UnnecessaryMarshallingInfoDescription">
458+
<source>Unnecesssary marshalling info was provided. This marshalling information can be removed without any change in behavior to the application.</source>
459+
<target state="new">Unnecesssary marshalling info was provided. This marshalling information can be removed without any change in behavior to the application.</target>
460+
<note />
461+
</trans-unit>
462+
<trans-unit id="UnnecessaryMarshallingInfoTitle">
463+
<source>Unnecessary marshalling info was provided and can be removed.</source>
464+
<target state="new">Unnecessary marshalling info was provided and can be removed.</target>
465+
<note />
466+
</trans-unit>
467+
<trans-unit id="UnnecessaryParameterMarshallingInfoMessage">
468+
<source>Unnecessary marshalling info '{0}' was provided for parameter '{1}'</source>
469+
<target state="new">Unnecessary marshalling info '{0}' was provided for parameter '{1}'</target>
470+
<note />
471+
</trans-unit>
472+
<trans-unit id="UnnecessaryReturnMarshallingInfoMessage">
473+
<source>Unnecessary marshalling info '{0}' was provided for the return type of method '{1}'</source>
474+
<target state="new">Unnecessary marshalling info '{0}' was provided for the return type of method '{1}'</target>
475+
<note />
476+
</trans-unit>
457477
</body>
458478
</file>
459479
</xliff>

src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.de.xlf

+20
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,26 @@
454454
<target state="translated">Der angegebene Typ wird vom quellgenerierten COM nicht unterstützt.</target>
455455
<note />
456456
</trans-unit>
457+
<trans-unit id="UnnecessaryMarshallingInfoDescription">
458+
<source>Unnecesssary marshalling info was provided. This marshalling information can be removed without any change in behavior to the application.</source>
459+
<target state="new">Unnecesssary marshalling info was provided. This marshalling information can be removed without any change in behavior to the application.</target>
460+
<note />
461+
</trans-unit>
462+
<trans-unit id="UnnecessaryMarshallingInfoTitle">
463+
<source>Unnecessary marshalling info was provided and can be removed.</source>
464+
<target state="new">Unnecessary marshalling info was provided and can be removed.</target>
465+
<note />
466+
</trans-unit>
467+
<trans-unit id="UnnecessaryParameterMarshallingInfoMessage">
468+
<source>Unnecessary marshalling info '{0}' was provided for parameter '{1}'</source>
469+
<target state="new">Unnecessary marshalling info '{0}' was provided for parameter '{1}'</target>
470+
<note />
471+
</trans-unit>
472+
<trans-unit id="UnnecessaryReturnMarshallingInfoMessage">
473+
<source>Unnecessary marshalling info '{0}' was provided for the return type of method '{1}'</source>
474+
<target state="new">Unnecessary marshalling info '{0}' was provided for the return type of method '{1}'</target>
475+
<note />
476+
</trans-unit>
457477
</body>
458478
</file>
459479
</xliff>

src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.es.xlf

+20
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,26 @@
454454
<target state="translated">El tipo especificado no es compatible con COM generado por el origen</target>
455455
<note />
456456
</trans-unit>
457+
<trans-unit id="UnnecessaryMarshallingInfoDescription">
458+
<source>Unnecesssary marshalling info was provided. This marshalling information can be removed without any change in behavior to the application.</source>
459+
<target state="new">Unnecesssary marshalling info was provided. This marshalling information can be removed without any change in behavior to the application.</target>
460+
<note />
461+
</trans-unit>
462+
<trans-unit id="UnnecessaryMarshallingInfoTitle">
463+
<source>Unnecessary marshalling info was provided and can be removed.</source>
464+
<target state="new">Unnecessary marshalling info was provided and can be removed.</target>
465+
<note />
466+
</trans-unit>
467+
<trans-unit id="UnnecessaryParameterMarshallingInfoMessage">
468+
<source>Unnecessary marshalling info '{0}' was provided for parameter '{1}'</source>
469+
<target state="new">Unnecessary marshalling info '{0}' was provided for parameter '{1}'</target>
470+
<note />
471+
</trans-unit>
472+
<trans-unit id="UnnecessaryReturnMarshallingInfoMessage">
473+
<source>Unnecessary marshalling info '{0}' was provided for the return type of method '{1}'</source>
474+
<target state="new">Unnecessary marshalling info '{0}' was provided for the return type of method '{1}'</target>
475+
<note />
476+
</trans-unit>
457477
</body>
458478
</file>
459479
</xliff>

src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.fr.xlf

+20
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,26 @@
454454
<target state="translated">Le type spécifié n’est pas pris en charge par com généré par la source</target>
455455
<note />
456456
</trans-unit>
457+
<trans-unit id="UnnecessaryMarshallingInfoDescription">
458+
<source>Unnecesssary marshalling info was provided. This marshalling information can be removed without any change in behavior to the application.</source>
459+
<target state="new">Unnecesssary marshalling info was provided. This marshalling information can be removed without any change in behavior to the application.</target>
460+
<note />
461+
</trans-unit>
462+
<trans-unit id="UnnecessaryMarshallingInfoTitle">
463+
<source>Unnecessary marshalling info was provided and can be removed.</source>
464+
<target state="new">Unnecessary marshalling info was provided and can be removed.</target>
465+
<note />
466+
</trans-unit>
467+
<trans-unit id="UnnecessaryParameterMarshallingInfoMessage">
468+
<source>Unnecessary marshalling info '{0}' was provided for parameter '{1}'</source>
469+
<target state="new">Unnecessary marshalling info '{0}' was provided for parameter '{1}'</target>
470+
<note />
471+
</trans-unit>
472+
<trans-unit id="UnnecessaryReturnMarshallingInfoMessage">
473+
<source>Unnecessary marshalling info '{0}' was provided for the return type of method '{1}'</source>
474+
<target state="new">Unnecessary marshalling info '{0}' was provided for the return type of method '{1}'</target>
475+
<note />
476+
</trans-unit>
457477
</body>
458478
</file>
459479
</xliff>

0 commit comments

Comments
 (0)