-
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
Add public IsInitOnly API and use it to fix code generation of 'init' #44077
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot. Changed the retargeting test to use that model. #Resolved
@@ -120,6 +121,39 @@ class C | |||
RetargetingSymbolChecker.CheckSymbols(sourceNamespace.GetMember<NamedTypeSymbol>("C"), retargetingNamespace.GetMember<NamedTypeSymbol>("C")); | |||
} | |||
|
|||
[Fact, CompilerTrait(CompilerFeature.InitOnlySetters)] | |||
public void RetargetProperties_WithInitOnlySetter() |
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.
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 |
@@ -48,6 +48,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.NavigationBar | |||
modifiers:=New DeclarationModifiers(isOverride:=True), | |||
returnType:=compilation.GetSpecialType(SpecialType.System_Void), | |||
refKind:=RefKind.None, | |||
isInitOnly:=False, |
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 feels weird. how do we do other accessors? isn't this just a certain type of accessor, not a method? #Resolved
@@ -114,6 +114,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte | |||
modifiers: new DeclarationModifiers(isStatic, isAsync: declaredSymbol.IsAsync), | |||
returnType: declaredSymbol.ReturnType, | |||
refKind: default, | |||
isInitOnly: false, |
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.
not really getting how methods are init-only. #Resolved
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it 👍 can you make it a default value? thanks! #Resolved
done with IDE pass. very large question about the design here. #Resolved |
@@ -13,6 +13,7 @@ | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like an unnecessary change, consider reverting the file. #Closed
Done with review pass (iteration 4) #Closed |
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 (iteration 5).
I've added a follow-up item to the test plan to resolve the naming question in API and spec. |
@@ -140,6 +140,12 @@ public interface IMethodSymbol : ISymbol | |||
/// </summary> | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
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 |
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.
IDE side lgtm.
Question on compiler API:
static int Property { get; init; }
haveIsInitOnly
true 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)