Skip to content

binding: require a public get accessor for properties and indexers - #34

Merged
rameel merged 1 commit into
mainfrom
binding/require-public-getters
Nov 18, 2026
Merged

rameel merged 1 commit into
mainfrom
binding/require-public-getters

Conversation

@rameel

@rameel rameel commented Nov 18, 2026

Copy link
Copy Markdown
Owner

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.

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 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.

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 private or internal getter 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 a protected getter (CS0271), because the getter exists.

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.
@rameel
rameel merged commit 9f8f1cc into main Nov 18, 2026
1 check passed
@rameel
rameel deleted the binding/require-public-getters branch November 18, 2026 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant