Skip to content

Unsafe evolution: require unsafe for some pointer operations#81240

Merged
jjonescz merged 18 commits into
dotnet:features/UnsafeEvolutionfrom
jjonescz:Unsafe-01-Pointers
Nov 24, 2025
Merged

Unsafe evolution: require unsafe for some pointer operations#81240
jjonescz merged 18 commits into
dotnet:features/UnsafeEvolutionfrom
jjonescz:Unsafe-01-Pointers

Conversation

@jjonescz

Copy link
Copy Markdown
Member

Test plan: #81207

Require unsafe for pointer indirection, member access, and element access when the new memory safety rules are enabled.
Unsafe is not required elsewhere (like for pointer types) under the new rules, but more restrictions will be added in follow up PRs, like to also require unsafe for function pointer invocation, etc.

Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Unsafe.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Unsafe.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Errors/ErrorCode.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs Outdated
Comment thread src/Compilers/CSharp/Portable/CSharpCompilationOptions.cs Outdated
Comment thread src/Compilers/CSharp/Portable/CSharpCompilationOptions.cs Outdated
Comment thread src/Compilers/CSharp/Test/Emit3/Semantics/UnsafeEvolutionTests.cs Outdated
@jjonescz

Copy link
Copy Markdown
Member Author

@jcouv for the second review, thanks

Comment thread src/Compilers/CSharp/Portable/CSharpCompilationOptions.cs Outdated

internal CSharpCompilationOptions WithEvolvedMemorySafetyRules() => WithMemorySafetyRules(EvolvedMemorySafetyRulesVersion);

internal bool HasEvolvedMemorySafetyRules => MemorySafetyRules >= EvolvedMemorySafetyRulesVersion;

@jcouv jcouv Nov 21, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HasEvolvedMemorySafetyRules

nit: consider aligning name with existing UseUpdatedEscapeRules. Maybe "UseUpdatedSafetyRules" or "UseUpdatedMemorySafetyRules" #Closed

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's good to keep the terminology in line with the speclet (and later the standard). But it seems there is no terminology for this thing in the existing design docs, right, @333fred? So I will update "evolved" to "updated", thanks.

Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs
Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs Outdated
Comment thread src/Compilers/CSharp/Test/Emit3/Semantics/UnsafeEvolutionTests.cs
// (1,1): error CS8652: The feature 'updated memory safety rules' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// int* x = null;
Diagnostic(ErrorCode.ERR_FeatureInPreview, "int*").WithArguments("updated memory safety rules").WithLocation(1, 1));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: consider including a test for fixed (no updated unsafe check). Could be in a later PR

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm planning to test fixed along with other remaining unsafe constructs in the next PR.

@jcouv jcouv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks (commit 18)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants