Skip to content

Disambiguate forwarded types when global using directives are used #454

@Flash0ver

Description

@Flash0ver

Is the feature request related to a problem

A potential CS0104 might occur in polyfillings when global using directives are used.

Example:
When consuming both Polyfill (7.33.0+) and either Microsoft.MacCatalyst or Microsoft.iOS:

RequiredMemberAttribute.cs(32,35): Error CS0104 : 'RequiredMemberAttribute' is an ambiguous reference between 'Foundation.RequiredMemberAttribute' and 'System.Runtime.CompilerServices.RequiredMemberAttribute'

Then an ambiguity with Foundation.RequiredMemberAttribute can be caused in the polyfill when the consuming project is also declaring a global using directive:

<ItemGroup>
  <Using Include="Foundation" />
</ItemGroup>

Minimal Repro:

  1. new .NET MacOS or iOS project
  2. add Polyfill (7.33.0+)
  3. add global using directive "Foundation"
    • <Using Include="Foundation" />
    • global using Foundation;
  4. error CS0104

Describe the solution

Disambiguate by avoiding using directives for forwarded types.

Option 1: Fully qualified type names
#else
-using System.Runtime.CompilerServices;
-[assembly: TypeForwardedTo(typeof(RequiredMemberAttribute))]
+[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.CompilerServices.RequiredMemberAttribute))]
#endif

Drawback:
Duplicates the namespace in particular for polyfills in the System.Runtime.CompilerServices namespace,
making the source / dependency a bit larger.

Option 2: Global alias
#else
-using System.Runtime.CompilerServices;
-[assembly: TypeForwardedTo(typeof(RequiredMemberAttribute))]
+[assembly: global::System.Runtime.CompilerServices.TypeForwardedTo(typeof(global::System.Runtime.CompilerServices.RequiredMemberAttribute))]
#endif

Drawback:
Although being the "safest" way to reference types in auto-generated code,
this makes the source / dependency even larger.

Describe alternatives considered

The alternative is to not consider this (and related) scenarios in Polyfill,
and instead let the consuming project resolve the issue by avoiding or removing problematic global using directives.

E.g.:

<ItemGroup>
  <Using Remove="Foundation" />
</ItemGroup>

Additional context

This issue may also occur in other places of Polyfill,
but I only considered forwarded types for the scope of this issue,
as this is what we ran into in https://github.com/getsentry/sentry-dotnet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions