-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Forbid unsafe types in records #76588
Conversation
[The specification](https://github.com/dotnet/csharplang/blob/main/proposals/csharp-9.0/records.md#members-of-a-record-type) for records forbids the use of unsafe types as an instance field of the type. However, we didn't check that in all cases; we only checked when generating the `Equals` method for the record type. This meant that if the user provided their own definition of `Equals`, they could bypass this restriction. We also did not check for nested unsafe types, so `int*[]` was permitted in any scenario. We have 2 options for fixing this: 1. Treat it as a specification bug, and update the spec to follow the current behavior. We'd therefore fix dotnet#66312 by moving the diagnostic to SourceNamedTypeSymbol.AfterMembersChecks, and only error when the user didn't provide their own implementation of `Equals`. 2. Treat it as a compiler bug, and forbid use of unsafe types in all scenarios. I've opted for path 2 here, but we can discuss whether this is too aggressive and if we should opt for path 1 instead. Fixes dotnet#66312.
We also need to decide what to do with manually-implemented properties that have unsafe types. As pointed out by @hamarb123, even with this change we'll still generate invalid IL for |
src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberFieldSymbol.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertySymbolBase.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Semantic/Semantics/RecordStructTests.cs
Outdated
Show resolved
Hide resolved
It looks like more test base-lines need an adjustment |
Done with review pass (commit 1) |
* upstream/main: (368 commits) Cleanup tests Add test Add test Add test Fix issue parsing regex category Properly simplify pattern when converting to pattern matching update publishdata after VS 17.13 snap Simplify Docs Do not lift type parameters in extract method declared within the selected region Fix ExtractMethod in VB elseif blocks Rework our Helix Process (dotnet#76703) Stash and restore original culture in CultureNormalizer (dotnet#76713) PR comments Adding checks for mutable structs. Add additional tests for string escape sequences CodeGenerator.EmitStackAlloc - Avoid capturing blob array (dotnet#76660) Update comments and exception type for LSP stdio configuration based on review feedback Fix race generating Microsoft.Managed.Core.CurrentVersions.targets (dotnet#76701) Update FileDownloader.cs ...
@AlekseyTs addressed your feedback. The spec part of this change is at dotnet/csharplang#9050, clarifying what "unsafe types" is actually supposed to mean. |
src/Compilers/CSharp/Portable/Symbols/Synthesized/Records/SynthesizedRecordEquals.cs
Outdated
Show resolved
Hide resolved
Done with review pass (commit 3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (commit 4)
@dotnet/roslyn-compiler for a second review |
Did you end up blocking manually implemented pointer type properties, or allowing them (and fixing the |
No change was made to locations without fields. |
@dotnet/roslyn-compiler for a second review |
* upstream/main: (172 commits) Move impl of ILspWorkpace to EditorTestWorkspace/LspTestWorkspace (dotnet#76753) Field-backed properties: report diagnostic for variable named field declared in accessor (dotnet#76671) Add more tests Update 'use nameof instead of typeof' to support generic types Update dependencies from https://github.com/dotnet/arcade build 20250115.2 Add docs invert Update src/Analyzers/CSharp/Tests/CSharpAnalyzers.UnitTests.projitems Update options Fixup tests fixup options Update tests Add test Fix trivia Handle params Support modifiers with simple lambda parameters. (dotnet#75400) Handle params Use helper Add fixes Flesh out ...
The specification for records forbids the use of unsafe types as an instance field of the type. However, we didn't check that in all cases; we only checked when generating the
Equals
method for the record type. This meant that if the user provided their own definition ofEquals
, they could bypass this restriction. We also did not check for nested unsafe types, soint*[]
was permitted in any scenario. We have 2 options for fixing this:Equals
.I've opted for path 2 here, but we can discuss whether this is too aggressive and if we should opt for path 1 instead. Fixes #66312.