Add public IsInitOnly API and use it to fix code generation of 'init' #44077
Add public IsInitOnly API and use it to fix code generation of 'init' #44077jcouv merged 10 commits intodotnet:features/recordsfrom
Conversation
41f4f81 to
8c9ac79
Compare
|
|
|
Tagging @jaredpar since any decision we make for public API should preferably align with the language spec
Yes, the runtime has the concept of FWIW, I also considered Suggestions welcome. |
| } | ||
|
|
||
| [Fact] | ||
| public void ConstructorAndDestructorAreNotInitOnly() |
There was a problem hiding this comment.
ConstructorAndDestructorAreNotInitOnly [](start = 20, length = 38)
Could also cover operators #Closed
| var sourceModule = compilation.SourceModule; | ||
| var sourceAssembly = (SourceAssemblySymbol)sourceModule.ContainingAssembly; | ||
|
|
||
| var retargetingAssembly = new RetargetingAssemblySymbol(sourceAssembly, isLinked: false); |
There was a problem hiding this comment.
var retargetingAssembly = new RetargetingAssemblySymbol(sourceAssembly, isLinked: false); [](start = 12, length = 89)
Please do not use this legacy way of testing retargeting. Create a scenario when the system itself creates one, a real scenario and test that. #Closed
There was a problem hiding this comment.
I've looked at more recent retargeting tests (Fred's recent PRs) but could not figure out the key to triggering retargeting logic to kick in. I tried tracing it back (seems to be triggered in ReferenceManager?).
What's a simple/modern test you'd recommend as a model for retargeting?
In reply to: 422534751 [](ancestors = 422534751)
There was a problem hiding this comment.
What's a simple/modern test you'd recommend as a model for retargeting?
For example, look at CodeGenTests.CustomFields_01. The 7th compilation, the commented out, is supposed to use retargeting symbols for comp1. The change in the target framework does the trick.
In reply to: 422651853 [](ancestors = 422651853,422534751)
There was a problem hiding this comment.
Thanks a lot. Changed the retargeting test to use that model. #Resolved
| } | ||
|
|
||
| [Fact, CompilerTrait(CompilerFeature.InitOnlySetters)] | ||
| public void RetargetProperties_WithInitOnlySetter() |
There was a problem hiding this comment.
RetargetProperties_WithInitOnlySetter [](start = 20, length = 37)
I suggest that we keep this test next to other InitOnly tests. #Closed
|
Done with review pass (iteration 3) #Closed |
| modifiers:=New DeclarationModifiers(isOverride:=True), | ||
| returnType:=compilation.GetSpecialType(SpecialType.System_Void), | ||
| refKind:=RefKind.None, | ||
| isInitOnly:=False, |
There was a problem hiding this comment.
this feels weird. how do we do other accessors? isn't this just a certain type of accessor, not a method? #Resolved
| modifiers: new DeclarationModifiers(isStatic, isAsync: declaredSymbol.IsAsync), | ||
| returnType: declaredSymbol.ReturnType, | ||
| refKind: default, | ||
| isInitOnly: false, |
There was a problem hiding this comment.
not really getting how methods are init-only. #Resolved
There was a problem hiding this comment.
Currently, only accessors can be init-only. But accessors are methods.
We have a discussion queued to discuss init-only methods more generally, but that should not affect this PR (assume that we don't have generalized init-only methods).
I can make the parameter optional, so it's not in your face at most places where it could not legitimately be set to true. Would that resolve your concern?
In reply to: 422535858 [](ancestors = 422535858)
There was a problem hiding this comment.
Isn't init a different type of accessor entirely. I.e. a property would have three potential accessors? GetMethod/SetMethod/InitMethod? #Resolved
There was a problem hiding this comment.
No, it's best thought of as a modifier on set. But we shortened initonly set to init.
https://github.com/dotnet/csharplang/blob/master/proposals/init.md #Resolved
There was a problem hiding this comment.
got it 👍 can you make it a default value? thanks! #Resolved
|
done with IDE pass. very large question about the design here. #Resolved |
| using Roslyn.Test.Utilities; | ||
| using Xunit; | ||
| using Utils = Microsoft.CodeAnalysis.CSharp.UnitTests.CompilationUtils; | ||
| using Microsoft.CodeAnalysis.Test.Utilities; |
There was a problem hiding this comment.
This looks like an unnecessary change, consider reverting the file. #Closed
|
Done with review pass (iteration 4) #Closed |
|
I've added a follow-up item to the test plan to resolve the naming question in API and spec. |
| bool IsReadOnly { get; } | ||
|
|
||
| /// <summary> | ||
| /// 'init' set accessors can only be invoked during construction or in the initialization phase |
There was a problem hiding this comment.
set [](start = 19, length = 3)
It isn't clear if this is intended to apply only to set accessors, or if other methods may be so flagged.
|
FYI @AlekseyTs I made minor change to retargeting test to reflect different verification behavior on desktop versus Core |
|
@CyrusNajmabadi Please take another look when you can. Thanks |
Question on compiler API:
static int Property { get; init; }haveIsInitOnlytrue or false? Currently, it is false, to be consistent with behavior when loading such property from PE.IDE scenarios supported by this API: generate override, generate implementation, view metadata-as-source.
Relates to #40726 (test plan for records/init-only)