-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Open
Description
Championed proposal: dotnet/csharplang#8635
Spec: proposals/field-keyword.md
public string PropertyConstraint
{
get;
set => field = value ?? throw new ArgumentNullException();
} = "";Compiler:
- language version
-
fieldkeyword- based on language version
- as primary_expression only
- escaped with
@ -
this.field,base.field,Qualifier.field - in simple property:
object P => expr; - in property accessors: expression body; block body
- in indexer accessors
- in event accessors
- in nested lambdas and local functions
- in property initializer
- in attributes on property, accessor, nested function
- in nested function signature as default value
- in
nameof() - in call to
[Conditional]
- warning when previously binding to other symbol
- based on language version
- member of containing type, base type
- local, in same or nested function
- parameter in nested function
- type parameter, in containing type or nested function
- mixed auto- and manually-implemented accessors (see
FieldKeywordTests.ImplicitAccessorBody_*)-
get,set,init - in
interface
-
- field use
- static and instance properties on
class,struct,ref struct,interface,record* -
get-only,set-only,init-only (seeFieldKeywordTests.AutoPropertyMustHaveGetAccessor_*) -
fieldreferences in multiple properties: each get private backing field - auto-implemented properties (including
field) must override all accessors (seeFieldKeywordTests.Override_*,New_*) - in nested function, including
staticfield reference instaticfunction (seeFieldKeywordTests.Lambda_*,LocalFunction_*) - in attribute arguments (see
FieldKeywordTests.Attribute_*) -
refreturning property (seeFieldKeywordTests.RefReturning_*) - properties with restricted types (see
FieldKeywordTests.RestrictedTypes) - properties with by-ref-like types (see
FieldKeywordTests.ByRefLikeType_*) -
@fieldwith and without backing field -
Color Color - passing by reference: (see
FieldKeywordTests.PassByReference_*)struct S { readonly object P { set { M1(ref field); M2(in field); } } static void M1(ref object o) { } static void M2(in object o) { } }
- static and instance properties on
- synthesized backing field
- backing field emitted when any auto-implemented accessor or
fieldreference - backing field marked
[CompilerGenerated](seeFieldKeywordTests.Attribute_03) - emitted when used in
[Conditional]code only (seeFieldKeywordTests.Conditional) (see open LDM question)object P { set { Debug.Assert(field is null); } }
- emitted to ref assembly
- backing field emitted when any auto-implemented accessor or
-
[field:]attributes- emitted on synthesized backing field (see
FieldKeywordTests.Attribute_03) - not emitted on property (see
FieldKeywordTests.Attribute_03) - error if no auto-implemented accessors and no
fieldreferences (seeFieldKeywordTests.PartialProperty_Attribute_03) -
[field: Obsolete](seeFieldKeywordTests.ObsoleteAttribute)
- emitted on synthesized backing field (see
-
nameof(field)-
nameof(field)as invocation
-
- property initializer (see
FieldKeywordTests.Initializer_*)- allowed when any auto-implemented accessor or
fieldreference - static and instance properties on
class,struct,ref struct,interface - written to field directly
- allowed when any auto-implemented accessor or
- constructor assignment (see
FieldKeywordTests.ConstructorAssignment_*)- assigns to setter if available, otherwise getter
- static and instance properties on
class,struct,ref struct,interface - assigned in other initializer
- assigned in nested function
- flow analysis
- warning CS0414: The field is assigned but its value is never used
- warning CS0649: Field is never assigned to, and will always have its default value
- auto-default initialization of
structbacking fields and warnings when property is accessed in constructor (seeFieldKeywordTests.DefaultInitialization_*)- expect no auto-default inserted when a field initializer is used
- expect an auto-default when assigning in constructor
-
readonlyproperties (seeFieldKeywordTests.ReadOnly_*)- backing field is
initonlywhen type, property, or accessor isreadonly - error reporting when modifying
fieldinget,setaccessors
- backing field is
-
setaccessor inreadonlycontext for property usingfield(see open LDM question) (seeFieldKeywordTests.ReadOnly_01) - partial properties (see
FieldKeywordTests.PartialProperty_*)- mix of auto- and manually-implemented accessors
- static and instance properties on
class,struct,ref struct,interface,record* -
fieldin accessors - initializer on definition or implementation
- initializer on definition and implementation with same or different values
- initializer with no
fieldor auto-implementation -
fieldreferences on definition or implementation -
fieldreferences on definition and implementation -
fieldreferences onreadonlyproperty or accessor with same or different modifiers on parts -
[field:]attributes concatenated across parts- error when the attributes are the same and
AllowMultiple=false
- error when the attributes are the same and
- partial properties where the implementation uses
fieldand one or both parts havefield:targeted attribute lists. (See alsoPartialPropertiesTests.Attributes_Property_BackingField. This issue referenced in source.) - Check warning
WRN_SequentialOnPartialClassis produced when needed (see also comment) - comment from
partialproperties - comment from
partialproperties
- nullability (initial implementation) (see
FieldKeywordTests.Nullability_*) Implement nullable analysis forfieldkeyword #75244-
fieldhas annotations of containing type -
[field:MaybeNull]etc. affectfield -
NullableWalker.Scan().getFieldSymbolToBeInitialized(): Is checking IsAutoProperty sufficient or should we check the appropriate accessors are missing or auto-implemented (see comment)? -
NullableWalker.TryGetReceiverAndMember(): callBinder.AccessingAutoPropertyFromConstructorto matchDefiniteAssignmentimplementation (see comment)?- (wontfix) It feels like the churn of reversing this, just to be consistent with definite assignment, is not worth it right now.
-
- SemanticModel
-
GetSymbolInfo(fieldExpr)returns backing field - add
fieldreference in the speculative semantic model Field-backed properties: supportfieldreferences in speculativeSemanticModel#75398 -
IOperationfor expression referencingfield
-
-
structfulfillsunmanagedconstraint and managed type rules (see pointer types) depending on synthesized fields - ref safety:
struct S { [UnscopedRef] Span<int> Prop1 => M(ref field); // should not error // Should error Span<int> Prop2 => M(ref field); // should error static Span<int> M(ref Span<int> span) => default; }
- public API review: [API proposal] Syntax changes for
fieldkeyword #74937 - update compiler test plan
Productivity:
- Fixer for compat break
- QuickInfo
- Completion
- Colorization
- Conversion?
- F1 help on
field - EnC: EnC: implement support for field keyword in property accessors #77951
Reactions are currently unavailable