-
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
Open
333fred
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
333fred:record-unsafe-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+343
−103
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,3 +156,30 @@ struct S | |
public static void M([UnscopedRef] ref int x) { } | ||
} | ||
``` | ||
|
||
## `record` and `record struct` types cannot define pointer type members, even when providing their own Equals implementations | ||
|
||
PROTOTYPE: Figure what version this is going into | ||
|
||
***Introduced in Visual Studio 2022 version 17.14*** | ||
|
||
The specification for `record class` and `record struct` types indicated that any unsafe types are disallowed. However, this was not | ||
enforced correctly in 2 scenarios: | ||
|
||
1. When the `record class` or `record struct` type defined its own `Equals` implementation. | ||
2. When the field type only used the unsafe type in a nested context, such as `int*[]`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
The compiler now correctly forbids both scenarios. | ||
|
||
```cs | ||
unsafe record struct R1( | ||
int* P // Previously fine, now CS8908 | ||
) | ||
{ | ||
public bool Equals(R other) => true; | ||
} | ||
|
||
unsafe record struct R2( | ||
int*[] P // Previously fine, now CS8908 | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,15 +57,20 @@ protected void TypeChecks(TypeSymbol type, BindingDiagnosticBag diagnostics) | |
} | ||
else if (type.IsVoidType()) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_FieldCantHaveVoidType, TypeSyntax?.Location ?? this.GetFirstLocation()); | ||
diagnostics.Add(ErrorCode.ERR_FieldCantHaveVoidType, getTypeErrorLocation()); | ||
} | ||
else if (type.IsRestrictedType(ignoreSpanLikeTypes: true)) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_FieldCantBeRefAny, TypeSyntax?.Location ?? this.GetFirstLocation(), type); | ||
diagnostics.Add(ErrorCode.ERR_FieldCantBeRefAny, getTypeErrorLocation(), type); | ||
} | ||
else if (type.IsRefLikeOrAllowsRefLikeType() && (this.IsStatic || !containingType.IsRefLikeType)) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_FieldAutoPropCantBeByRefLike, TypeSyntax?.Location ?? this.GetFirstLocation(), type); | ||
diagnostics.Add(ErrorCode.ERR_FieldAutoPropCantBeByRefLike, getTypeErrorLocation(), type); | ||
} | ||
else if (!this.IsStatic && type.ContainsPointerOrFunctionPointer() && (ContainingType.IsRecord || ContainingType.IsRecordStruct)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
// The type '{0}' may not be used for a field of a record. | ||
diagnostics.Add(ErrorCode.ERR_BadFieldTypeInRecord, getTypeErrorLocation(), type); | ||
} | ||
else if (IsConst && !type.CanBeConst()) | ||
{ | ||
|
@@ -96,6 +101,11 @@ protected void TypeChecks(TypeSymbol type, BindingDiagnosticBag diagnostics) | |
} | ||
|
||
diagnostics.Add(this.ErrorLocation, useSiteInfo); | ||
|
||
Location getTypeErrorLocation() | ||
{ | ||
return TypeSyntax?.Location ?? this.GetFirstLocation(); | ||
} | ||
} | ||
|
||
public abstract bool HasInitializer { get; } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1839,9 +1839,18 @@ protected virtual void ValidatePropertyType(BindingDiagnosticBag diagnostics) | |
{ | ||
diagnostics.Add(ErrorCode.ERR_FieldCantBeRefAny, TypeLocation, type); | ||
} | ||
else if (this.IsAutoPropertyOrUsesFieldKeyword && type.IsRefLikeOrAllowsRefLikeType() && (this.IsStatic || !this.ContainingType.IsRefLikeType)) | ||
|
||
if (this.IsAutoPropertyOrUsesFieldKeyword) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_FieldAutoPropCantBeByRefLike, TypeLocation, type); | ||
if (!this.IsStatic && type.ContainsPointerOrFunctionPointer() && (ContainingType.IsRecord || ContainingType.IsRecordStruct)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
// The type '{0}' may not be used for a field of a record. | ||
diagnostics.Add(ErrorCode.ERR_BadFieldTypeInRecord, TypeLocation, type); | ||
} | ||
else if (type.IsRefLikeOrAllowsRefLikeType() && (this.IsStatic || !this.ContainingType.IsRefLikeType)) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_FieldAutoPropCantBeByRefLike, TypeLocation, type); | ||
} | ||
} | ||
|
||
if (type.IsStatic) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This sounds too broad. The spec says: