Skip to content

MVVM Source Generators: ObservablePropertyAttribute on member var named value #4216

Closed
@Rosuavio

Description

@Rosuavio

Describe the bug

If you put the ObservablePropertyAttribute on a member var named value it successfully generates a source code for a property that does reference the member variable named value in the generated code as intended. Instead the member var named value gets shadowed by the property setter key word value and the value from the setter gets compared with its self and the member var does not get updated.

  • Is this bug a regression in the toolkit? If so, what toolkit version did you last see it work:

Steps to Reproduce

  • Can this be reproduced in the Sample App? (Either in a sample as-is or with new XAML pasted in the editor.) If so, please provide custom XAML or steps to reproduce. If not, let us know why it can't be reproduced (e.g. more complex setup, environment, dependencies, etc...)

Steps to reproduce the behavior:

  1. Created a project where you can use the source generators (netstandard)
  2. Add the Mvvm package.
  3. Write this class
[ObservableObject]
    public partial class Class1
    {
        [ObservableProperty]
        private int value;
    }
  1. See this generated code
[global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "7.1.0.0")]
        [global::System.Diagnostics.DebuggerNonUserCode]
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        public string Value
        {
            get => value;
            set
            {
                if (!global::System.Collections.Generic.EqualityComparer<string>.Default.Equals(value, value))
                {
                    OnPropertyChanging(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.ValuePropertyChangingEventArgs);
                    value = value;
                    OnPropertyChanged(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.ValuePropertyChangedEventArgs);
                }
            }
        }

Expected behavior

The member var value and any other keywords in the context of the generated code that could be names of vars are distinguished from keywords in the contexts of the the generated code, or something warns or errors out before such code can be compiled and/or ran, so that users of the generators are not caught off guard with a sneaky error.

Screenshots

Environment

NuGet Package(s):

Package Version(s):

Windows 10 Build Number:

  • Fall Creators Update (16299)
  • April 2018 Update (17134)
  • October 2018 Update (17763)
  • May 2019 Update (18362)
  • May 2020 Update (19041)
  • Insider Build ({build_number})

App min and target version:

  • Fall Creators Update (16299)
  • April 2018 Update (17134)
  • October 2018 Update (17763)
  • May 2019 Update (18362)
  • May 2020 Update (19041)
  • Insider Build ({build_number})

Device form factor:

  • Desktop
  • Xbox
  • Surface Hub
  • IoT

Visual Studio version:

  • 2017 (15.{minor_version})
  • 2019 (16.{minor_version})
  • 2022 (17.{minor_version})

Additional context

Metadata

Metadata

Assignees

Labels

Completed 🔥bug 🐛An unexpected issue that highlights incorrect behaviormvvm-toolkit 🧰Issues/PRs for the Microsoft.Toolkit.Mvvm package

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions