Skip to content

[Bug]: Source Generator emits code for an un-settable property if the property name is "Value" #192

@dwcullop

Description

@dwcullop

Describe the bug 🐞

If you use the Source Generator to create a property named "Value", the emitted code won't allow the property to be actually updated because this.RaiseAndSetIfChanged(ref value, value) actually uses the keyword value twice instead of the field named value.

Step to reproduce

If you write this:

[Reactive]
private string value = string.Empty;

The Source Generator emits (roughly) this:

    public string Value
    {
        get => value;
        set => this.RaiseAndSetIfChanged(ref value, value);
    }

In this case ref value is refers to the value keyword and essentially tries to assign it back to itself and leaves the field value unaffected. Since value is already equal to itself, then the whole set operation is a no-op.

Reproduction repository

https://github.com/reactiveui/ReactiveUI

Expected behavior

Source Generator should always emit functional code.

If this code were to be generated instead, it would work as expected:

    public string Value
    {
        get => value;
        set => this.RaiseAndSetIfChanged(ref this.value, value);
    }

Adding the "this." ensures that it always refers to the field and not the keyword.

Screenshots 🖼️

No response

IDE

No response

Operating system

No response

Version

No response

Device

No response

ReactiveUI Version

No response

Additional information ℹ️

A workaround would be to use another field name, but that isn't always an option for those trying to port existing code to use ReactiveUI and the Source Generator. Other keywords may also be impacted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions