-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Fix support for externally declared records #9104
Changes from all commits
6357654
9e64309
7b48cc2
050c1c9
e919b73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,10 +145,10 @@ public CompilationUnitSyntax GenerateCode(CancellationToken cancellationToken) | |
if (symbol.IsRecord) | ||
{ | ||
// If there is a primary constructor then that will be declared before the copy constructor | ||
// A record always generates a copy constructor and marks it as implicitly declared | ||
// A record always generates a copy constructor and marks it as compiler generated | ||
// todo: find an alternative to this magic | ||
var potentialPrimaryConstructor = symbol.Constructors[0]; | ||
if (!potentialPrimaryConstructor.IsImplicitlyDeclared) | ||
if (!potentialPrimaryConstructor.IsImplicitlyDeclared && !potentialPrimaryConstructor.IsCompilerGenerated()) | ||
{ | ||
constructorParameters = potentialPrimaryConstructor.Parameters; | ||
} | ||
|
@@ -160,6 +160,23 @@ public CompilationUnitSyntax GenerateCode(CancellationToken cancellationToken) | |
{ | ||
constructorParameters = annotatedConstructors[0].Parameters; | ||
} | ||
else | ||
{ | ||
// record structs from referenced assemblies do not return IsRecord=true | ||
// above. See https://github.com/dotnet/roslyn/issues/69326 | ||
// So we implement the same heuristics from ShouldIncludePrimaryConstructorParameters | ||
// to detect a primary constructor. | ||
var properties = symbol.GetMembers().OfType<IPropertySymbol>().ToImmutableArray(); | ||
var primaryConstructor = symbol.GetMembers() | ||
.OfType<IMethodSymbol>() | ||
.Where(m => m.MethodKind == MethodKind.Constructor && m.Parameters.Length > 0) | ||
// Check for a ctor where all parameters have a corresponding compiler-generated prop. | ||
.FirstOrDefault(ctor => ctor.Parameters.All(prm => | ||
properties.Any(prop => prop.Name.Equals(prm.Name, StringComparison.Ordinal) && prop.IsCompilerGenerated()))); | ||
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. How reliable is this? Could some language/compiler change in the future break it and cause a wire compatibility issue for users? 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. I couldn't find anything more reliable than that :( 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. As sharplab shows, there's nothing else we can depend on, unless the runtime adds something: https://sharplab.io/#v2:EYLgtghglgdgNAExAagD4AEDMACATgUwGMB7XBbAMWOIAp0BGABmwCEJcBKAbiA=. The property itself isn't 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. FWIW, I think the proposed heuristics is as specific and detailed as possible to detect the situation given the current compiler output. |
||
|
||
if (primaryConstructor != null) | ||
constructorParameters = primaryConstructor.Parameters; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -273,8 +290,18 @@ bool ShouldIncludePrimaryConstructorParameters(INamedTypeSymbol t) | |
} | ||
} | ||
|
||
// Default to true for records, false otherwise. | ||
return t.IsRecord; | ||
// Default to true for records. | ||
if (t.IsRecord) | ||
return true; | ||
|
||
var properties = t.GetMembers().OfType<IPropertySymbol>().ToImmutableArray(); | ||
|
||
return t.GetMembers() | ||
.OfType<IMethodSymbol>() | ||
.Where(m => m.MethodKind == MethodKind.Constructor && m.Parameters.Length > 0) | ||
// Check for a ctor where all parameters have a corresponding compiler-generated prop. | ||
.Any(ctor => ctor.Parameters.All(prm => | ||
properties.Any(prop => prop.Name.Equals(prm.Name, StringComparison.Ordinal) && prop.IsCompilerGenerated()))); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"profiles": { | ||
"Roslyn": { | ||
"commandName": "DebugRoslynComponent", | ||
"targetProject": "..\\..\\test\\Orleans.Serialization.UnitTests\\Orleans.Serialization.UnitTests.csproj" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace System.Runtime.CompilerServices; | ||
|
||
// required for record serialization support for downlevel | ||
internal static class IsExternalInit {} |
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.
I find this variation easier to read, and possibly a little faster.