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

Avoid compiler error when using init properties with BindingSourceGenerator #27655

Merged
merged 1 commit into from
Feb 10, 2025
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ BinaryExpressionSyntax binary when binary.Kind() == SyntaxKind.AsExpression => b
static bool IsWritable(ISymbol? symbol)
=> symbol switch
{
IPropertySymbol { OriginalDefinition.SetMethod.IsInitOnly: true } => false,
IPropertySymbol propertySymbol => propertySymbol.SetMethod != null,
IFieldSymbol fieldSymbol => !fieldSymbol.IsReadOnly,
_ => true,
Expand Down
110 changes: 110 additions & 0 deletions src/Controls/tests/BindingSourceGen.UnitTests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1885,4 +1885,114 @@ internal static partial class GeneratedBindingInterceptors
""",
result.GeneratedFiles["Path-To-Program.cs-GeneratedBindingInterceptors-17-23.g.cs"]);
}

[Fact]
public void GenerateSimpleBinding_WithInitSetter()
{
const string source =
"""
using Microsoft.Maui.Controls;
using MyNamespace;

var label = new Label();
label.SetBinding(Label.TextProperty, static (MyClass c) => c.Text);

namespace MyNamespace
{
public class MyClass
{
public required string Text { get; init; }
}
}
""";

var result = SourceGenHelpers.Run(source);
var actual = result.GeneratedFiles["Path-To-Program.cs-GeneratedBindingInterceptors-5-7.g.cs"];
AssertExtensions.AssertNoDiagnostics(result);

var id = Math.Abs(result.Binding!.SimpleLocation.GetHashCode());

AssertExtensions.CodeIsEqual(
$$"""
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a .NET MAUI source generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable

namespace System.Runtime.CompilerServices
{
using System;
using System.CodeDom.Compiler;

{{BindingCodeWriter.GeneratedCodeAttribute}}
[global::System.Diagnostics.Conditional("DEBUG")]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
file sealed class InterceptsLocationAttribute : Attribute
{
public InterceptsLocationAttribute(int version, string data)
{
_ = version;
_ = data;
}
}
}

namespace Microsoft.Maui.Controls.Generated
{
using System;
using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Controls.Internals;

internal static partial class GeneratedBindingInterceptors
{

{{BindingCodeWriter.GeneratedCodeAttribute}}
[InterceptsLocationAttribute({{result.Binding.InterceptableLocation.Version}}, @"{{result.Binding.InterceptableLocation.Data}}")]
public static void SetBinding{{id}}(
this BindableObject bindableObject,
BindableProperty bindableProperty,
Func<global::MyNamespace.MyClass, string> getter,
BindingMode mode = BindingMode.Default,
IValueConverter? converter = null,
object? converterParameter = null,
string? stringFormat = null,
object? source = null,
object? fallbackValue = null,
object? targetNullValue = null)
{
Action<global::MyNamespace.MyClass, string>? setter = null;
if (ShouldUseSetter(mode, bindableProperty))
{
throw new InvalidOperationException("Cannot set value on the source object.");
}

var binding = new TypedBinding<global::MyNamespace.MyClass, string>(
getter: source => (getter(source), true),
setter,
handlers: new Tuple<Func<global::MyNamespace.MyClass, object?>, string>[]
{
new(static source => source, "Text"),
})
{
Mode = mode,
Converter = converter,
ConverterParameter = converterParameter,
StringFormat = stringFormat,
Source = source,
FallbackValue = fallbackValue,
TargetNullValue = targetNullValue
};

bindableObject.SetBinding(bindableProperty, binding);
}
}
}
""", actual);
}
}
Loading