Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
23 changes: 19 additions & 4 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ src/
│ │ └── ToPropertyInvocationGenerator.cs # Observable-backed read-only property
│ ├── Helpers/ # Extraction and validation helpers
│ │ ├── ViewRegistrationExtractor.cs # IViewFor<T> → ViewRegistrationInfo extraction
│ │ ├── PathSyntax.cs # Walks a selector's path through !, parentheses and ?. (linked to Analyzer)
│ │ ├── SourceGeneratorsMemberExtractor.cs # ReactiveUI.SourceGenerators' naming and typing rules
│ │ ├── XamlPageReader.cs # Named elements of MAUI and Avalonia XAML pages
│ │ ├── XamlMemberResolver.cs # Those elements' types, as fields of the code-behind class
Expand All @@ -404,7 +405,7 @@ src/
│
├── ReactiveUI.Binding.Analyzer/ # Roslyn analyzer (netstandard2.0)
│ ├── Analyzers/
│ │ ├── BindingInvocationAnalyzer.cs # RXUIBIND001, 003, 004, 005, 006, 007, 008
│ │ ├── BindingInvocationAnalyzer.cs # RXUIBIND001, 003, 004, 005, 006, 007, 008, 010, 022
│ │ ├── DispatchReachAnalyzer.cs # RXUIBIND009
│ │ ├── ServiceLocatorViewAnalyzer.cs # RXUIBIND020
│ │ ├── TypeAnalyzer.cs # RXUIBIND002
Expand Down Expand Up @@ -522,12 +523,25 @@ two ways.
A call site the generator cannot read has to use the `Unsafe` overload. That overload finds properties by
reflection.

**Every property path is a delegate.** The stubs, the generated overloads and the interceptors take a path as
`Func<TSource, TValue>`. An expression tree parameter makes the compiler build the tree at the call site on every
call, by reflection, even though generated code never reads it. Only the `Unsafe` overloads take
`Expression<Func<TSource, TValue>>`, because they read the tree. Three things follow:

- A delegate type alone does not mark a path out from a result selector or a converter, so
`ExtractorValidation.IsPropertySelector` decides by the stub's parameter name (`property1`, `obs1`,
`sourceProperty`, `controlName` and the rest). A new stub's path parameters have to pass that test.
- A delegate accepts any lambda body. `PathSyntax` is the one reader of a path for the generator and the
analyzers: `!`, parentheses and `?.` name the same members. RXUIBIND022 reports a statement body, a computed
value, a member of anything but the parameter, and `?.` ahead of a plain value, which makes the result nullable.
- A selector lambda is real code to the other analyzers, so the examples, tests and benchmarks mark it `static`.

```csharp
// User writes:
var obs = vm.WhenChanged(x => x.Name);

// The runtime stub, which throws when nothing replaces the call:
public static IObservable<TReturn> WhenChanged<TObj, TReturn>(this TObj obj, Expression<Func<TObj, TReturn>> property, ...)
public static IObservable<TReturn> WhenChanged<TObj, TReturn>(this TObj obj, Func<TObj, TReturn> property, ...)

// The generator writes one method per call site:
private static IObservable<string> __WhenChanged_7FFFD2E8D6FC818E(MyViewModel obj)
Expand All @@ -537,11 +551,11 @@ private static IObservable<string> __WhenChanged_7FFFD2E8D6FC818E(MyViewModel ob

// Roslyn 4.13 and newer: an interceptor claims the call site.
[InterceptsLocation(1, "...")]
internal static IObservable<string> __Intercept_WhenChanged_7FFFD2E8D6FC818E(this MyViewModel obj, Expression<Func<MyViewModel, string>> property, ...)
internal static IObservable<string> __Intercept_WhenChanged_7FFFD2E8D6FC818E(this MyViewModel obj, Func<MyViewModel, string> property, ...)
=> __WhenChanged_7FFFD2E8D6FC818E(obj);

// Roslyn 4.8 to 4.12: a concrete overload wins lookup and matches the call site.
public static IObservable<string> WhenChanged(this MyViewModel obj, Expression<Func<MyViewModel, string>> property, [CallerArgumentExpression("property")] string propertyExpression = "", ...)
public static IObservable<string> WhenChanged(this MyViewModel obj, Func<MyViewModel, string> property, [CallerArgumentExpression("property")] string propertyExpression = "", ...)
{
if (propertyExpression == "x => x.Name")
{
Expand Down Expand Up @@ -1099,6 +1113,7 @@ Not all platforms support before-change notifications (WPF DP, WinUI DP, WinForm
| RXUIBIND019 | Warning | ObservableAsProperty method takes parameters |
| RXUIBIND020 | Info | View is registered only in the service locator |
| RXUIBIND021 | Warning | Binding call has no generated binding |
| RXUIBIND022 | Error | Selector is not a property path |

## Code Style & Quality Requirements

Expand Down
30 changes: 27 additions & 3 deletions docs/ReactiveUI.Binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,24 @@ Two build properties change this.
Some call sites cannot be read when you build. Examples are a lambda stored in a variable, an expression built
at run time, and a call on a generic type parameter `T`. None of them name a path the generator can see.

The plain methods take each property path as a delegate, `Func<TSource, TValue>`. The generator reads the members
the lambda names and writes code that observes them. The lambda itself is never run, and the compiler builds no
expression tree for it, so a call costs no reflection and no allocation for its selectors. Mark the lambda `static`
to make sure it captures nothing.

A path is one expression that reads properties or instance fields from the lambda's parameter:

```csharp
vm.WhenChanged(static x => x.Name);
vm.WhenChanged(static x => x.Address!.City); // ! and parentheses name the same members
vm.WhenChanged(static x => x.Address?.City); // so does ?. ahead of a reference or a nullable value
```

A delegate accepts more than a path, so the analyzer reports what the generator cannot read. RXUIBIND022 reports a
statement body, a computed value such as `x => x.First + x.Last`, a member of anything but the parameter, and `?.`
ahead of a plain value such as `x => x.Address?.Floor`. That last form makes the result `int?`, which the member
is not; end the path at a reference or a nullable member, or write `!.`.

For those call sites, call the `Unsafe` overload. It uses reflection. Reflection looks up each property by name
while the app runs. A trimmer cannot see those lookups, so an `Unsafe` overload may break after trimming or
ahead-of-time publishing. The other overloads keep working.
Expand All @@ -310,9 +328,10 @@ When nothing was generated for a call site, the plain method throws. The error m
overload to use:

```csharp
Func<MyViewModel, string> stored = x => x.Name;
Expression<Func<MyViewModel, string>> selector = x => x.Name;

vm.WhenChanged(selector); // throws, and names WhenChangedUnsafe
vm.WhenChanged(stored); // RXUIBIND001: throws, and names WhenChangedUnsafe
vm.WhenChangedUnsafe(selector); // walks the path by reflection
```

Expand All @@ -337,8 +356,12 @@ Fourteen methods have an `Unsafe` twin.

Every `Unsafe` overload carries `[RequiresUnreferencedCode]`. The plain overloads carry none. So a
`PublishTrimmed` or `PublishAot` build warns about each call that uses reflection, and about nothing else.
RXUIBIND021 reports every plain call that nothing was generated for, whatever the reason. RXUIBIND001, RXUIBIND006
and RXUIBIND009 name some of the reasons.
RXUIBIND021 reports every plain call that nothing was generated for, whatever the reason. RXUIBIND001, RXUIBIND006,
RXUIBIND009 and RXUIBIND022 name some of the reasons.

The `Unsafe` overloads take the path as an expression tree, `Expression<Func<TSource, TValue>>`, because reflection
needs the tree to find the members. An expression tree does not convert to a delegate, so a stored expression only
compiles against an `Unsafe` overload.

The scheduler overloads split the same way. Both kinds live on `ReactiveSchedulerExtensions`.

Expand Down Expand Up @@ -949,6 +972,7 @@ The analyzer ships inside the runtime packages. It reports these diagnostics.
| RXUIBIND019 | Warning | A method marked `[ObservableAsProperty]` takes parameters, so it cannot supply a property's values. |
| RXUIBIND020 | Info | A view is registered as `IViewFor<T>` in the service locator, and this project has no generated view and no `Map` for `T`. `ResolveView` with a view model held as an `object` does not find it. Add it with `Map`, or call `ResolveViewUnsafe`. |
| RXUIBIND021 | Warning | Nothing was generated for the call, so it throws when it runs. A lambda the generator cannot read, or a member another source generator adds, are common causes. Name each member directly, declare a generated member as a partial property, or call the `Unsafe` overload. |
| RXUIBIND022 | Error | The selector is not a property path: it has a statement body, computes a value, reads something other than its own parameter, or puts `?.` ahead of a plain value. Write one expression that reads properties from the parameter. |

The package's build targets report one error of their own.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<Compile Include="..\ReactiveUI.Binding.SourceGenerators\Models\InterceptorLocation.cs" Link="Models\InterceptorLocation.cs"/>
<Compile Include="..\ReactiveUI.Binding.SourceGenerators\Helpers\InterceptableLocationReader.cs" Link="Helpers\InterceptableLocationReader.cs"/>
<Compile Include="..\ReactiveUI.Binding.SourceGenerators\Helpers\ExtractorValidation.cs" Link="Helpers\ExtractorValidation.cs"/>
<Compile Include="..\ReactiveUI.Binding.SourceGenerators\Helpers\PathSyntax.cs" Link="Helpers\PathSyntax.cs"/>
<Compile Include="..\ReactiveUI.Binding.SourceGenerators\Helpers\NativeCommandMembers.cs" Link="Helpers\NativeCommandMembers.cs"/>
<Compile Include="..\ReactiveUI.Binding.SourceGenerators\Helpers\NativeTypeIdentity.cs" Link="Helpers\NativeTypeIdentity.cs"/>
<Compile Include="..\ReactiveUI.Binding.SourceGenerators\Plugins\CommandBinding\AppKitCommandSymbols.cs" Link="Helpers\AppKitCommandSymbols.cs"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
RXUIBIND019 | Usage | Warning | ObservableAsProperty method takes parameters
RXUIBIND020 | Usage | Info | View is registered only in the service locator
RXUIBIND021 | Usage | Error | Binding call has no generated binding
RXUIBIND022 | Usage | Error | Selector is not a property path
Loading
Loading