-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Closed
Description
Championed issue: dotnet/csharplang#2691
Speclet: https://github.com/dotnet/csharplang/blob/main/proposals/csharp-12.0/primary-constructors.md
Feature branch: https://github.com/dotnet/roslyn/tree/features/PrimaryConstructors
Compiler
- language version
- syntax changes:
-
type Name;forclass,struct -
type Name(...) ...forclass,struct -
type Name(...) : Base(...) ...forclass - error for
type Name(...) : Interface(...) ... - error for
type Name : Base(...) ...
-
-
publicprimary constructor emitted- (Add Primary Constructor tests for some scenarios from Test Plan #70026) explicit parameterless constructor for
class C0();orstruct S0() { }
- (Add Primary Constructor tests for some scenarios from Test Plan #70026) explicit parameterless constructor for
- declared constructors must call primary constructor
- error for declared constructor with matching signature
- error for primary constructor on multiple
partialdeclarations - parameters:
- (Add Primary Constructor tests for some scenarios from Test Plan #70026) generic types
- (Add Primary Constructor tests for some scenarios from Test Plan #70026) pointer types with/without
unsafe - default parameter values
-
paramsmodifier -
thismodifier (error)
- attributes on parameters:
- with no target modifier; with
param:target; with other target (disallowed) -
[CallerMemberName] - synthesized attributes:
NativeIntegerAttribute,DynamicAttribute,TupleElementNames(seeParameterCapturing_*_SynthesizedAttributes)
- with no target modifier; with
- parameter capturing in
class,[readonly] struct,[readonly] ref struct(seeParameterCapturing_*) - parameter referenced in:
- instance methods and property or event accessors (capturing)
- instance field and property initializers (not capturing)
-
staticmembers (error) -
constfield (error) - other constructor (error)
-
this(x)orbase(x)initializer (error) -
: Base(x)initializer (not capturing) - lambdas (capturing based on containing context)
- local function (capturing based on containing member)
-
staticlambdas and local functions (error if captured) -
partialmethod
- parameter referenced in:
-
is x -
x() -
@x(seeParameterCapturing_011_EscapedIdentifier) -
nameof(x) -
lock (x)(seeParameterCapturing_098_Lock) -
using (x)(seeParameterCapturing_099_Using) -
x switch { ... }(seeParameterCapturing_099_MultiplePathsToNode_SwitchDispatch_02) - deconstruction (see
ParameterCapturing_103_Deconstruction) -
&x(see*AddressOf*) -
x.Ffixed size field (seeParameterCapturing_*FixedSizeBuffer) -
Color Color(seeParameterCapturing_*_ColorColor_*)
-
- error capturing
ref,in,out, orref structparameters (seeIllegalCapturingDueToRefness_*) - name shadowing (see
ParameterScope) from:- field, property, event, method names
- nested type
- local function name
- (Add Primary Constructor tests for some scenarios from Test Plan #70026) type parameter name
- (Add Primary Constructor tests for some scenarios from Test Plan #70026) parameter in instance member, constructor, lambda, or local function
- (Add Primary Constructor tests for some scenarios from Test Plan #70026) local in instance member, lambda, or local function
- local in
: Base(...): error (seeBaseArguments_17) - (Add Primary Constructor tests for some scenarios from Test Plan #70026) attribute
- LINQ expression identifier (see
ParameterCapturing_010_ShadowingInMethodBody,ParameterCapturing_016_Query)
- definite assignment (see
DefiniteAssignment_*)-
outparameters must be assigned - use of
outparameters before assignment - warning for parameters that are not read
-
-
dynamicdispatch not allowed for: Base(@dynamic)initializer (seeBaseArguments_10) - (Add Primary Constructor tests for some scenarios from Test Plan #70056) order of evaluation: field initializers,
: Base() - Semantic model:
- primary constructor symbol available from containing type
-
GetDeclaredSymbol() -
GetSymbolInfo() -
GetSpeculativeSymbolInfo(),GetSpeculativeTypeInfo()
-
LookupSymbolsandLookupNamesfor parameters- in initializers
- in
: Base(x, y) - in attributes
- in members (see
ParameterScope)
- analyzer events:
- syntax events for primary constructor parameters
- syntax events for
: Base(x, y)arguments - symbol events for primary constructor parameters
-
IOperationevents for synthesized constructor bodies, parameters, default values
- XML doc comments:
-
paramon containing type and members -
paramrefon containing type and members - across
partialdeclarations
-
- capturing managed parameter makes
structmanaged (seeParameterCapturing_056_CapturingOfAManagedParameterMakesStructManaged) -
structcycles from capture (seeParameterCapturing_*_Cycle) - synthesized constructor not allowed on
[ComImport]types - parameters (captured or not) referenced in lambda (see
*ParameterUsedInLambda_InPrimaryConstructor) - warning for captured parameter passed to base constructor (see
ParameterCapturing_*CapturedAndPassedToBase*) - nullable analysis (see
ParameterCapturing_*NullableAnalysis*) - parameters are
readonlyinreadonly struct(seeParameterCapturing_*_ReadonlyContext) - parameter assignment in
readonly structorreadonlymethod (seeParameterCapturing_*_ReadonlyContext) -
refsafety analysis of parameter references (seeParameterCapturing_145_ReturnByRef, etc.) - (Add "Primary Constructors" to Compiler Test Plan.md #70050) add feature to Compiler Test Plan.md
- (Add Primary Constructor tests for some scenarios from Test Plan #70056) order of emitted fields with/without explicit fields
- (Add Primary Constructor tests for some scenarios from Test Plan #70056) error for primary constructor on
static class - report unused parameters
-
SyntaxNormalizer(SyntaxNormalizerTests.TestSpacingOnPrimaryConstructor/TestSemicolonBody) - (Add Primary Constructor tests for some scenarios from Test Plan #70056) structs fulfilling the
unmanagedconstraint requires binding all members first
Public API
- API review (New APIs for "Primary Constructors" #66914)
IDE test pass
- Language version upgrade
- Type and paste new constructs
- Completion
- Type completion for parameters
- Parameter name completion
- In base initializer
- In field initializer
- In member body
- Dotting off of parameter name
- In base initializer
- In field initializer
- In member body
- Identifier highlighting
- Prefix matching
- Full identifier matching
- Formatting
- Format command
- Automatic Brace Completion
- Parameter list
- Argument list
- Indentation (typing Enter in an unfinished statement indents the next line)
- Parameter list
- Argument list
- Go To definition
- For parameter
- For constructor
- Find All References
- For parameters
- For constructor
- Colorization
- Types in parameter list
- Parameter names
- Error Squiggles
- Rename of parameter
- Change signature (for constructor)
- Extract method/local function for code referencing parameter
- Debugging
- Stepping
- Non-captured parameters
- Show up in Locals window
- Can be modified
- Captured parameters
- In initializers
- Capture fields are hidden
- Show up in Locals window, the values reflect the actual captured state. Modifications go to the the capture field. (Implement support for captured Primary Constructor parameters in EE when portable PDB is used. #67266)
- Same in Watch window. (Implement support for captured Primary Constructor parameters in EE when portable PDB is used. #67266)
- Inside instance methods
- Capture fields are hidden
- Show up in Locals window, the values reflect the actual captured state. Modifications go to the the capture field. (Implement support for captured Primary Constructor parameters in EE when portable PDB is used. #67266)
- Same in Watch window. (Implement support for captured Primary Constructor parameters in EE when portable PDB is used. #67266)
- In initializers
- Edit and Continue (EnC)
Filed issues:
- Formatting is not triggered after ending class declaration with a semicolon #67011
- Unexpected "IDE0060 Remove unused parameter" suggestion for a Primary Constructor parameter #67013
- Feature "Diagnostic analyzer runner" is currently unavailable due to an internal error (due to an assert) #67015
- Change signature refactoring doesn't update argument list in
class_base#67016 - Extract method refactoring is not offered in the base class argument #67017
- No completion for primary constructor parameters in Watch window in initializers #67021
Reactions are currently unavailable