-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Record structs: add ToString and PrintMembers #52012
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -29,11 +29,11 @@ public SynthesizedRecordPrintMembers( | |
|
||
protected override DeclarationModifiers MakeDeclarationModifiers(DeclarationModifiers allowedModifiers, BindingDiagnosticBag diagnostics) | ||
{ | ||
var result = (ContainingType.BaseTypeNoUseSiteDiagnostics.IsObjectType() && ContainingType.IsSealed) ? | ||
var result = (ContainingType.IsRecordStruct || (ContainingType.BaseTypeNoUseSiteDiagnostics.IsObjectType() && ContainingType.IsSealed)) ? | ||
DeclarationModifiers.Private : | ||
DeclarationModifiers.Protected; | ||
|
||
if (virtualPrintInBase() is object) | ||
if (ContainingType.IsRecord && !ContainingType.BaseTypeNoUseSiteDiagnostics.IsObjectType()) | ||
{ | ||
result |= DeclarationModifiers.Override; | ||
} | ||
|
@@ -48,21 +48,14 @@ protected override DeclarationModifiers MakeDeclarationModifiers(DeclarationModi | |
#endif | ||
return result; | ||
|
||
MethodSymbol? virtualPrintInBase() | ||
#if DEBUG | ||
bool modifiersAreValid(DeclarationModifiers modifiers) | ||
{ | ||
NamedTypeSymbol baseType = ContainingType.BaseTypeNoUseSiteDiagnostics; | ||
|
||
if (!baseType.IsObjectType()) | ||
if (ContainingType.IsRecordStruct) | ||
{ | ||
return FindValidPrintMembersMethod(baseType, ContainingType.DeclaringCompilation); | ||
return modifiers == DeclarationModifiers.Private; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
#if DEBUG | ||
static bool modifiersAreValid(DeclarationModifiers modifiers) | ||
{ | ||
if ((modifiers & DeclarationModifiers.AccessibilityMask) != DeclarationModifiers.Private && | ||
(modifiers & DeclarationModifiers.AccessibilityMask) != DeclarationModifiers.Protected) | ||
{ | ||
|
@@ -88,17 +81,31 @@ protected override (TypeWithAnnotations ReturnType, ImmutableArray<ParameterSymb | |
{ | ||
var compilation = DeclaringCompilation; | ||
var location = ReturnTypeLocation; | ||
var annotation = ContainingType.IsRecordStruct ? NullableAnnotation.Oblivious : NullableAnnotation.NotAnnotated; | ||
return (ReturnType: TypeWithAnnotations.Create(Binder.GetSpecialType(compilation, SpecialType.System_Boolean, location, diagnostics)), | ||
Parameters: ImmutableArray.Create<ParameterSymbol>( | ||
new SourceSimpleParameterSymbol(owner: this, | ||
TypeWithAnnotations.Create(Binder.GetWellKnownType(compilation, WellKnownType.System_Text_StringBuilder, diagnostics, location), NullableAnnotation.NotAnnotated), | ||
TypeWithAnnotations.Create(Binder.GetWellKnownType(compilation, WellKnownType.System_Text_StringBuilder, diagnostics, location), annotation), | ||
ordinal: 0, RefKind.None, "builder", isDiscard: false, Locations)), | ||
IsVararg: false, | ||
DeclaredConstraintsForOverrideOrImplementation: ImmutableArray<TypeParameterConstraintClause>.Empty); | ||
} | ||
|
||
protected override int GetParameterCountFromSyntax() => 1; | ||
|
||
protected override void MethodChecks(BindingDiagnosticBag diagnostics) | ||
{ | ||
base.MethodChecks(diagnostics); | ||
|
||
var overridden = OverriddenMethod; | ||
|
||
if (overridden is object && | ||
!overridden.ContainingType.Equals(ContainingType.BaseTypeNoUseSiteDiagnostics, TypeCompareKind.AllIgnoreOptions)) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_DoesNotOverrideBaseMethod, Locations[0], this, ContainingType.BaseTypeNoUseSiteDiagnostics); | ||
} | ||
} | ||
|
||
internal override void GenerateMethodBody(TypeCompilationState compilationState, BindingDiagnosticBag diagnostics) | ||
{ | ||
var F = new SyntheticBoundNodeFactory(this, ContainingType.GetNonNullSyntaxNode(), compilationState, diagnostics); | ||
|
@@ -113,27 +120,29 @@ internal override void GenerateMethodBody(TypeCompilationState compilationState, | |
return; | ||
} | ||
|
||
ArrayBuilder<BoundStatement>? block = printableMembers.IsEmpty ? null : ArrayBuilder<BoundStatement>.GetInstance(); | ||
ArrayBuilder<BoundStatement> block; | ||
BoundParameter builder = F.Parameter(this.Parameters[0]); | ||
if (ContainingType.BaseTypeNoUseSiteDiagnostics.IsObjectType()) | ||
if (ContainingType.BaseTypeNoUseSiteDiagnostics.IsObjectType() || ContainingType.IsRecordStruct) | ||
{ | ||
if (printableMembers.IsEmpty) | ||
{ | ||
// return false; | ||
F.CloseMethod(F.Return(F.Literal(false))); | ||
return; | ||
} | ||
block = ArrayBuilder<BoundStatement>.GetInstance(); | ||
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.
Consider moving 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. |
||
} | ||
else | ||
{ | ||
MethodSymbol? printMethod = FindValidPrintMembersMethod(ContainingType.BaseTypeNoUseSiteDiagnostics, DeclaringCompilation); | ||
if (printMethod is null) | ||
MethodSymbol? basePrintMethod = OverriddenMethod; | ||
if (basePrintMethod is null || | ||
basePrintMethod.ReturnType.SpecialType != SpecialType.System_Boolean) | ||
{ | ||
F.CloseMethod(F.ThrowNull()); // an error was reported in base checks already | ||
return; | ||
} | ||
|
||
var basePrintCall = F.Call(receiver: F.Base(ContainingType.BaseTypeNoUseSiteDiagnostics), printMethod, builder); | ||
var basePrintCall = F.Call(receiver: F.Base(ContainingType.BaseTypeNoUseSiteDiagnostics), basePrintMethod, builder); | ||
if (printableMembers.IsEmpty) | ||
{ | ||
// return base.PrintMembers(builder); | ||
|
@@ -142,13 +151,14 @@ internal override void GenerateMethodBody(TypeCompilationState compilationState, | |
} | ||
else | ||
{ | ||
block = ArrayBuilder<BoundStatement>.GetInstance(); | ||
// if (base.PrintMembers(builder)) | ||
// builder.Append(", ") | ||
block!.Add(F.If(basePrintCall, makeAppendString(F, builder, ", "))); | ||
block.Add(F.If(basePrintCall, makeAppendString(F, builder, ", "))); | ||
} | ||
} | ||
|
||
Debug.Assert(!printableMembers.IsEmpty && block is object); | ||
Debug.Assert(!printableMembers.IsEmpty); | ||
|
||
for (var i = 0; i < printableMembers.Length; i++) | ||
{ | ||
|
@@ -227,41 +237,6 @@ static bool isPrintable(Symbol m) | |
} | ||
} | ||
|
||
internal static MethodSymbol? FindValidPrintMembersMethod(TypeSymbol containingType, CSharpCompilation compilation) | ||
{ | ||
if (containingType.IsObjectType()) | ||
{ | ||
return null; | ||
} | ||
|
||
MethodSymbol? candidate = null; | ||
var stringBuilder = TypeWithAnnotations.Create(compilation.GetWellKnownType(WellKnownType.System_Text_StringBuilder)); | ||
|
||
foreach (var member in containingType.GetMembers(WellKnownMemberNames.PrintMembersMethodName)) | ||
{ | ||
if (member is MethodSymbol { DeclaredAccessibility: Accessibility.Protected, IsStatic: false, ParameterCount: 1, Arity: 0 } method && | ||
method.ParameterTypesWithAnnotations[0].Equals(stringBuilder, TypeCompareKind.AllIgnoreOptions)) | ||
{ | ||
if (candidate is object) | ||
{ | ||
// An ambiguity case, can come from metadata, treat as an error for simplicity. | ||
return null; | ||
} | ||
|
||
candidate = method; | ||
} | ||
} | ||
|
||
if (candidate is null || | ||
!(containingType.IsSealed || candidate.IsOverride || candidate.IsVirtual) || | ||
candidate.ReturnType.SpecialType != SpecialType.System_Boolean) | ||
{ | ||
return null; | ||
} | ||
|
||
return candidate; | ||
} | ||
|
||
internal static void VerifyOverridesPrintMembersFromBase(MethodSymbol overriding, BindingDiagnosticBag diagnostics) | ||
{ | ||
NamedTypeSymbol baseType = overriding.ContainingType.BaseTypeNoUseSiteDiagnostics; | ||
|
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.
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.
What happens when the surrounding
#if DEBUG
directives are removed from here and the call site? #ResolvedThere 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.
We'd emit non-product code into the shipping product.
In reply to: 603564256 [](ancestors = 603564256)