Skip to content

Deprecate and remove Xamarin types which conflict with predefined .NET types #10508

Closed

Description

@marek-safar

As part of .NET 6 migration existing types that are not compatible or serve for exactly the same purpose should be removed or at least deprecated to allow Xamarin SDK to work with all .NET libraries.

nint/nuint types

These types in Xamarin have a bit different semantic than in C# and I think the best approach would be to remove them from public API. I didn't try what happens when C# compiler see them defined as types but they should certainly not be prefered types over C# built-in version.

CLong/CULong/NFloat types

The existing types which serve exactly the same needs should be deprecated to allow easier sharing for .NET libraries.


Time estimate: depends on what we decide to do. Worst case is probably ~4 weeks to remove the Xamarin types and deal with the fallout, best case is no time at all if we decide to do nothing.

Activity

  1. AaronRobinsonMSFT commented on Jan 24, 2021

    @AaronRobinsonMSFT
    Member

    I believe an issue with removing these types and not handling this in CoreCLR is that legacy Xamarin assemblies wouldn't be supported. Please let me know if I am missing something.

    /cc @Redth @rolfbjarne @jkotas

  2. Redth commented on Jan 24, 2021

    @Redth
    Member
  3. rolfbjarne commented on Jan 25, 2021

    @rolfbjarne
    Member

    These types in Xamarin have a bit different semantic than in C# and I think the best approach would be to remove them from public API.

    This means that pretty much every existing Xamarin.iOS assembly (including NuGets) will not be compatible with .NET 6.

    Also people will probably have to update their source code to make it compile.

    I didn't try what happens when C# compiler see them defined as types but they should certainly not be prefered types over C# built-in version.

    The spec says:

    "The identifiers are only treated as keywords when name lookup does not find a viable result at that program location."

  4. added
    iOS
    macOS
    on Jan 25, 2021
  5. added this to the .NET 6 milestone on Jan 25, 2021
  6. added
    request-for-comments
    on Jan 25, 2021
  7. marek-safar commented on Jan 25, 2021

    @marek-safar
    ContributorAuthor

    This means that pretty much every existing Xamarin.iOS assembly (including NuGets) will not be compatible with .NET 6.

    It's not a new problem in .NET6, a similar problem exists today. It's not as visible because we didn't upgrade Mono bundled C# to version 9 but it's the same problem. .NET library targetting multiple platforms cannot use these types in the same way.

    Also people will probably have to update their source code to make it compile.

    I think the migration tool should try to cover this as much as possible

  8. added
    breaking-change
    on Jan 25, 2021
  9. added
    dotnet-pri0
    dotnet
    on Feb 3, 2021
  10. rolfbjarne commented on Aug 17, 2021

    @rolfbjarne
    Member

    I haven't heard about a single customer (internal or external) running into any problem with the existing situation (where we have and use our own version of these types), so I don't think we need to do anything here.

    Everything just works as-is (and I'm quite reluctant to rock that boat).

    One point to have in mind is that our types are Apple-specific, which means they only show up in our bindings, which also means they will never be a part of platform-neutral code.

  11. Perksey commented on Aug 18, 2021

    @Perksey
    Member

    Just attempted to add support for .NET 6 in Silk.NET and it failed because we use C# 9 nint and nuint. This creates massive complications in upgrade paths, particularly in more native-oriented applications which took advantage of C# 9's new types (such as bindings libraries).

    The most important scenario not considered here is source generators - they will expect using System to just work and may also generate public APIs using these new language features - massively blocking!

  12. tannergooding commented on Aug 18, 2021

    @tannergooding
    Member

    Everything just works as-is

    I don't think this is strictly true. There are a number of things that don't or won't work with the System.nint and System.nuint types including constants, default parameter values, interop (for certain scenarios, possibly not applicable to iOS), checked arithmetic, and others.

    I'd expect its simply that customers haven't hit them yet and will in due time as codebases which take/use/expose C# nint/nuint start interacting more with Xamarin apps using System.nint and System.nuint.

    Edit: I'm not saying this does or does not warrant a break. Just calling out that there will be downstream repercussions in either scenario and that will expose themselves over time, particularly when non-preview bits start getting picked up and applications are ported. The BCL itself is going to continue using and exposing C# nint/nuint in APIs, including core performance APIs such as System.Numerics.BitOperations, System.Math, and the SIMD/HWIntrinsic APIs (many of which were already extended to support C# nint/nuint for .NET 6 and which have additional approved surface area for .NET 7).

  13. 9 remaining items

  14. HurricanKai commented on Sep 17, 2021

    @HurricanKai
    Member

    Given this is now in the .NET 7 milestone, can we get some guidance on what library authors should do right now?
    In a library I want to use the new C# definitions, but it appears there is no real way for .NET 6 users to use the library given they are targeting iOS.
    I effectively have to pass this headache down to all users or refrain from using a new language feature.

  15. rolfbjarne commented on Sep 17, 2021

    @rolfbjarne
    Member

    In a library I want to use the new C# definitions, but it appears there is no real way for .NET 6 users to use the library given they are targeting iOS.

    As far as I know, it's not impossible, just quite clunky.

    The workaround (for any consumer of your library that wants to use your library with the C# nint types and our System.nint type in their own code) is to (for every source file where they want to use both types):

    • Avoid using System; (because this brings in our nint type, and there's no way to get the C# nint type).
      • The System namespace is imported by default in new projects in .NET 6; IIRC this can be turned off by adding something like this to the project file:
      <ItemGroup>
          <Using Remove="System" />
      </ItemGroup>
    • Explicitly write System.nint every time they want to use that type.
  16. HurricanKai commented on Sep 17, 2021

    @HurricanKai
    Member

    imo this isn't a real solution, it passes a huge headache down to all users targeting iOS, and they probably end up having to somehow move usage of my library out of the main code, as the System namespace isn't really something you can just avoid...

  17. hamarb123 commented on Sep 17, 2021

    @hamarb123

    I agree that this is not a good solution. Perhaps the types could be moved somewhere else eg. System.Runtime.InteropServices.XamarinMac (namespace) and then people who need to have their code maintained across 2 versions for a while can do conditional usings (all that would be necessary for these users is to import the additional namespace with a #if - this could even be done with a global import and it would essentially be a non-issue at that point), because in the future this is going to be a big issue. I also think it's a shame this isn't going to be fixed for .NET 6, because that will just increase the amount of conditional statements that are required from essentially 'am I using .NET Core' to 'am I using .NET core that is at least 7.0' which will probably require the programmer to make conditional constants in their csproj themselves. But I can see how this will cause major issues with using old Xamarin.Mac libraries.
    Note, I specifically suggest System.Runtime.InteropServices.XamarinMac rather than System.Runtime.InteropServices because it's not uncommon to have a use for nints if you're using interopsevices. I also think nfloat should be moved there because I wouldn't be surprised if future .NET has an nfloat type (but I also wouldn't be surprised if it didn't).

  18. tannergooding commented on Sep 18, 2021

    @tannergooding
    Member

    Note, I specifically suggest System.Runtime.InteropServices.XamarinMac rather than System.Runtime.InteropServices because it's not uncommon to have a use for nints if you're using interopsevices. I also think nfloat should be moved there because I wouldn't be surprised if future .NET has an nfloat type (but I also wouldn't be surprised if it didn't).

    .NET 6 actually has this already as NFloat (native sized floating-point): https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.nfloat?view=net-6.0. Alongside CLong (C/C++ long) and CULong (C/C++ unsigned long) these types are explicitly for interop purposes and are specially handled by the marshaller to treat them as "ABI primitives" rather than as "User-defined structs" (because those two types behave differently in some scenarios).

    The major difference is that the general .NET 6 types follow the standard naming convention (PascalCase) and don't support operators as of today (they could in the future if enough demand existed).

  19. removed
    dotnet-pri0
    on Oct 5, 2021
  20. NogginBops commented on Oct 10, 2021

    @NogginBops

    If this ever gets tackled I think that is the perfect time to finally solve the OpenTK issue that has been present since 2014 and continues to be present to this day for users of OpenTK (#5275 #5107 mono/opentk#19).

    People are stuck with the 1.0 version of OpenTK.dll and I can say as a maintainer that the only reason why OpenTK doesn't support android and iOS is because of this issue. And it's also the only reason OpenTK isn't used for android/iOS development.

    This issue has had some heated discussion before and it definitely is a bad look for Xamarin (and now by extension Microsoft) to be continually blocking users from using OpenTK and us from developing OpenTK.

    I understand that this is a complicated issue, but it should really have been solved with .net 5 but wasn't so now we are waiting for .net 7 to become what .net 5 and .net 6 promised to be.

  21. removed
    request-for-comments
    on Oct 22, 2021
  22. modified the milestones: .NET 7, .NET 6 on Oct 22, 2021
  23. rolfbjarne commented on Oct 22, 2021

    @rolfbjarne
    Member

    We've decided that we're going to remove the existing System.nint and System.nuint types in .NET 6, so this will be fixed.

    More information here: #13087

    Note that this is a breaking change, and everything will have to be recompiled for .NET 6 (existing NuGets and assemblies built for legacy Xamarin.iOS or Xamarin.Mac won't work).

  24. Perksey commented on Oct 22, 2021

    @Perksey
    Member

    @rolfbjarne Life saver, thank you so much :)

  25. added a commit that references this issue on Dec 7, 2021
    90b8e89
  26. ghost locked as resolved and limited conversation to collaborators on Apr 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

breaking-changedotnetestimate-4wiOSmacOS

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions