Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observable property attribute with negative enum values gives cast error #731

Closed
1 of 4 tasks
n-coelho-cerinnov opened this issue Jul 7, 2023 · 3 comments · Fixed by #736
Closed
1 of 4 tasks
Assignees
Labels
analyzer 👓 A new analyzer being implemented or updated bug 🐛 An unexpected issue that highlights incorrect behavior mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit

Comments

@n-coelho-cerinnov
Copy link

n-coelho-cerinnov commented Jul 7, 2023

Describe the bug

After updating my project that as an observable property with a negative enum attribute to version 8.2.1, I still have an error when I try to use the enum. As a workaround I've been using a literal value to suppress the error.
Before, in version 8.2.0, the source generators stopped working. In version 8.2.1 it gives the errors:

  • To cast a negative value, you must enclose the value in parentheses.
  • 'NegativeEnum' is a type, which is not valid in the given context.

This bug can be easily reproduced with the code below.

Regression

No response

Steps to reproduce

This bug occurred when I tested in .NET 7 in a WPF project with both Visual Studio 17.6.4 and 17.7.0 Preview 2.0 and the latest CommunityToolkit.Mvvm 8.2.1. It probably would happen with other versions.
Using the following code the bug can be reproduced:

using CommunityToolkit.Mvvm.ComponentModel;
using System.ComponentModel;

namespace MvvmBug;
public partial class MainViewModel : ObservableObject
{
    [ObservableProperty]
    private bool _test1;

    [ObservableProperty]
    [property: DefaultValue(PositiveEnum.Something)]
    private PositiveEnum _test2;

    [ObservableProperty]
    [property: DefaultValue(NegativeEnum.Problem)]
    private NegativeEnum _test3;

    [ObservableProperty]
    private int _test4;

    public MainViewModel()
    {
        Test1 = true;
        Test2 = PositiveEnum.Else;
    }
}

public enum PositiveEnum
{
    Something = 0,
    Else = 1
}

public enum NegativeEnum
{
    Problem = -1,
    OK = 0
}

Changing the default value for _test3 to NegativeEnum.OK will allow the code to compile without errors, changing back to NegativeEnum.Problem and the errors described above occur.
Also note that if using the NegativeEnum.Problem, removing the 'property:' before the DefaultValueAttribute doesn't seem to have the problem. It only occurs for attributes passed to the property generated.

Expected behavior

An enum with a negative value in a property attribute shouldn't give a cast error.

Screenshots

No response

IDE and version

VS 2022, VS 2022 Preview

IDE version

17.6.4, 17.7.0 Preview 2.0

Nuget packages

  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.2.1

Additional context

This is a continuation of the problem reported in issue #681.

Help us help you

No, just wanted to report this

@n-coelho-cerinnov n-coelho-cerinnov added the bug 🐛 An unexpected issue that highlights incorrect behavior label Jul 7, 2023
@Sergio0694 Sergio0694 added mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit analyzer 👓 A new analyzer being implemented or updated labels Jul 11, 2023
@Sergio0694 Sergio0694 self-assigned this Jul 11, 2023
@Sergio0694
Copy link
Member

Oh, good catch! Mmh this is weird that the unit tests didn't spot the error 🤔

I have these test attributes that clearly are using the wrong syntax (missing parentheses):

[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum1)-1073741824)]
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum2)-9223372036854775808)]
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum3)-1234)]
[global::MyApp.DefaultValueAttribute((global::MyApp.NegativeEnum4)-1)]

But then the test runner below isn't producing any diagnostics for it (it's missing those CS0075 errors):

// Run all source generators on the input source code
_ = driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation outputCompilation, out ImmutableArray<Diagnostic> diagnostics);
// Ensure that no diagnostics were generated
CollectionAssert.AreEquivalent(Array.Empty<Diagnostic>(), diagnostics);

@Youssef1313 any ideas why the generator driver might not be reporting all compiler errors here by any chance? 😅

@Youssef1313
Copy link
Contributor

@Sergio0694 I think you should be getting the diagnostics from outputCompilation

@Youssef1313
Copy link
Contributor

I think the diagnostics you're checking against might be the generator exceptions plus the diagnostics produced by the generator itself. So it's not the whole diagnostics of the compilation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer 👓 A new analyzer being implemented or updated bug 🐛 An unexpected issue that highlights incorrect behavior mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants