Repository navigation
binding: require a public get accessor for properties and indexers - #34
Merged
Merged
Conversation
Expressions read a property or an indexer through its get accessor
regardless of its visibility, so a private, protected, or internal
getter behind a public setter was still readable: for a property
declared as 'public string Secret { private get; set; }', the
expression Secret returned its value. A property with only a set
accessor was accepted as well: Parse returned an expression that
could not be compiled, and Evaluate threw 'Expression must be
readable'.
As in C#, report that the property or indexer cannot be used because
its get accessor is missing or inaccessible. The property is still
found by name, so ContextAwareBinder does not fall back to static
members or types with the same name. An indexer whose get accessor is
not public still takes part in overload resolution, so x[1] is
rejected if this[int] has a private getter, even when this[long] is
public.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Expressions read a property or an indexer through its get accessor regardless of its visibility, so a
private,protected, orinternalgetter behind a public setter was still readable: for a property declared aspublic string Secret { private get; set; }, the expressionSecretreturned its value. A property with only a set accessor was accepted as well:Parsereturned an expression that could not be compiled, andEvaluatethrewExpression must be readable.This PR reports, as C# does, that the property or indexer cannot be used because its get accessor is missing or inaccessible:
The property or indexer 'T.Secret' cannot be used in this context because the get accessor is inaccessible.The property or indexer 'T.WriteOnly' cannot be used in this context because it lacks the get accessor.The check applies to every read of a property: a context member, a member access, and a delegate property invoked as a method, including properties returned by a custom binder. The property is still found by name, so
ContextAwareBinderdoes not fall back to static members or types with the same name. An indexer whose get accessor is not public still takes part in overload resolution, sox[1]is rejected ifthis[int]has a private getter, even whenthis[long]is public.Expressions that read a property or an indexer through a non-public get accessor no longer parse. The error for an indexer with only a set accessor is unchanged:
Cannot apply indexing with [] to an expression of type 'T'.For a
privateorinternalgetter declared in another assembly, the C# compiler reports that the property lacks the get accessor (CS0154), since it does not see such accessors. This PR reports that the get accessor is inaccessible, as C# does for aprotectedgetter (CS0271), because the getter exists.