Skip to content

Update for new ref safety rules #169

@idg10

Description

@idg10

C# 11.0 changes the ref safety rules. These changes are designed to support new scenarios that would previously have required unsafe code. In C# 11.0, we are able to provide the compiler with a more nuanced description of our intentions, and this enables it to recognize certain usage patterns as safe, which it would previously have had to reject as unsafe because its less detailed understanding of our code necessitated a more conservative approach.

Unfortunately, these new safety rules constitute a breaking change for code that makes use of ref struct. The upshot is that the Ais.NET codebase currently does not compile with the .NET 7.0 era SDK.

To participate in the newer, more flexible ref safety rules, it is necessary to provide the compiler with more information. C# 11.0 has introduced a new keyword, scoped for this purpose.

We should add the scoped keyword where necessary to enable the code to compile.

We also need to determine whether doing so causes problems for consumers of our library using pre-C# 11.0 compilation rules. When you build a component that relies on the new rules, the compiler emits a [module: RefSafetyRules(11)] annotation. It's not currently clear to me whether that renders the component unusable by older consumers. There are two possibilities:

  1. older compilers will be unaware of this annotation, and will apply the older, more conservative rules, and that will be OK
  2. libraries compiled under the newer, more flexible ref safety rules cannot be consumed by older compilers that don't know about those rules.

I'm hoping it's 1. If that's the case, we can just add the scoped annotations and make a new release available. But if it's 2, we will need to add a net7.0 target, and use conditional compilation to use these attributes on that target, but not the netstandard2.0 and netstandard2.1 targets.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions