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

[XamlC] The compiled binding IL is invalid when the path points to a static property #20768

Closed
simonrozsival opened this issue Feb 22, 2024 · 2 comments
Assignees
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-9.0.0-preview.2.10293

Comments

@simonrozsival
Copy link
Member

Consider the following example:

<Label x:Name="TitleLabel" Text="{Binding Title, x:DataType=local:ViewModel}" />
public class ViewModel
{
    public static string Title => "Title";
}

When compiled with ILC, it produces this warning:

ILC: Method '[MyMauiApp]MyMauiApp.MainPage.<InitializeComponent>typedBindingsM__0(ViewModel)' will always throw because: Invalid IL or CLR metadata

The IL of the method in question is:

.method private hidebysig static 
	valuetype [System.Private.CoreLib]System.ValueTuple`2<string, bool> '<InitializeComponent>typedBindingsM__0' (
		class MyMauiApp.ViewModel ''
	) cil managed 
{
	.custom instance void [System.Private.CoreLib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
		01 00 00 00
	)
	// Method begins at RVA 0x11ac8
	// Header size: 12
	// Code size: 33 (0x21)
	.maxstack 3
	.locals init (
		[0] valuetype [System.Private.CoreLib]System.ValueTuple`2<string, bool>
	)

	// if (P_0 != null)
	IL_0000: ldarg.0
	IL_0001: dup
	IL_0002: brfalse IL_0013

	// return ("Title", true);
	IL_0007: ldstr "Title"
	IL_000c: ldc.i4.1
	IL_000d: newobj instance void valuetype [System.Private.CoreLib]System.ValueTuple`2<string, bool>::.ctor(!0, !1)
	IL_0012: ret

	// (no C# code)
	IL_0013: pop
	// return default((string, bool));
	IL_0014: ldloca.s 0
	IL_0016: initobj valuetype [System.Private.CoreLib]System.ValueTuple`2<string, bool>
	IL_001c: ldloc 0
	IL_0020: ret
} // end of method MainPage::'<InitializeComponent>typedBindingsM__0'

It seems to me that the dup and pop instructions are used incorrectly. We never call pop if we reach IL_0012 and we're returning with two items on the stack. We shouldn't need either when the returned value is static.

Should XamlC even allow this type of binding? Should it report an error? On the other hand, this is something that apparently works with classic bindings, so it should be supported.

I found this issue when I tried building the eShop sample app with NativeAOT (see view and the view mode).

/cc @StephaneDelcroix @jamesmontemagno

@simonrozsival simonrozsival added the area-xaml XAML, CSS, Triggers, Behaviors label Feb 22, 2024
@simonrozsival simonrozsival self-assigned this Feb 22, 2024
@simonrozsival
Copy link
Member Author

Should XamlC even allow this type of binding? Should it report an error? On the other hand, this is something that apparently works with classic bindings, so it should be supported.

So, after further testing, I think this should be reported as invalid XAML. When the same binding isn't compiled, it doesn't resolve the static Title value either and the label is blank.

@rmarinho
Copy link
Member

Fixed by #20769

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-9.0.0-preview.2.10293
Projects
None yet
Development

No branches or pull requests

4 participants