Skip to content

Commit b167107

Browse files
committed
fix: Don't use nullable reference types in generated code
1 parent 9e059e2 commit b167107

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3875,7 +3875,7 @@ string buildBindBack()
38753875
{
38763876
if (!string.IsNullOrWhiteSpace(rawBindBack))
38773877
{
3878-
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0], GetType(dataType));
3878+
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0], GetType(dataType)).ToDisplayString(NullableFlowState.None);
38793879
return $"(___ctx, __value) => {{ if(___ctx is {dataType} ___tctx) {{ ___tctx.{rawBindBack}(({propertyType})__value); }} }}";
38803880
}
38813881
else
@@ -3887,8 +3887,8 @@ string buildBindBack()
38873887
{
38883888
if (propertyPaths.properties.Length == 1)
38893889
{
3890-
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0], GetType(dataType));
3891-
return $"(___ctx, __value) => {{ if(___ctx is {dataType} ___tctx) {{ {contextFunction} = ({targetPropertyType})global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof({targetPropertyType.ToDisplayString(NullableFlowState.None)}), __value); }} }}";
3890+
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0], GetType(dataType)).ToDisplayString(NullableFlowState.None);
3891+
return $"(___ctx, __value) => {{ if(___ctx is {dataType} ___tctx) {{ {contextFunction} = ({targetPropertyType})global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof({targetPropertyType}), __value); }} }}";
38923892
}
38933893
else
38943894
{
@@ -3928,10 +3928,10 @@ string buildBindBack()
39283928
{
39293929
if (propertyPaths.properties.Length == 1)
39303930
{
3931-
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0]);
3931+
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0]).ToDisplayString(NullableFlowState.None);
39323932
return $"(___ctx, __value) => {{ " +
39333933
$"if(___ctx is global::{_className.ns + "." + _className.className} ___tctx) " +
3934-
$"{rawFunction} = ({targetPropertyType})global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof({targetPropertyType.ToDisplayString(NullableFlowState.None)}), __value);" +
3934+
$"{rawFunction} = ({targetPropertyType})global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof({targetPropertyType}), __value);" +
39353935
$" }}";
39363936
}
39373937
else
@@ -4105,7 +4105,7 @@ private string GetCustomMarkupExtensionValue(XamlMemberDefinition member)
41054105
{
41064106
// The target property implements IConvertible, therefore
41074107
// cast ProvideValue() using Convert.ChangeType
4108-
var targetTypeDisplay = propertyType.ToDisplayString();
4108+
var targetTypeDisplay = propertyType.ToDisplayString(NullableFlowState.None);
41094109
var targetType = $"typeof({targetTypeDisplay})";
41104110

41114111
// It's important to cast to string before performing the conversion
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<ResourceDictionary
2+
x:Class="UnoNullableContextGeneratedBug.TestTemplate"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:local="using:UnoNullableContextGeneratedBug">
6+
7+
<DataTemplate x:Key="TestDataTemplate" x:DataType="local:TestViewModel">
8+
<StackPanel>
9+
<!--This works-->
10+
<TextBlock Text="{x:Bind ObjectInstance, Mode=OneWay}"/>
11+
12+
<!-- This generates code with a nullable error at compile time -->
13+
<TextBlock Text="{x:Bind ObjectInstance, Mode=TwoWay}"/>
14+
</StackPanel>
15+
</DataTemplate>
16+
17+
</ResourceDictionary>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#nullable enable
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Collections.ObjectModel;
6+
using System.ComponentModel;
7+
using System.Text;
8+
9+
namespace UnoNullableContextGeneratedBug
10+
{
11+
public class TestViewModel
12+
{
13+
public TestViewModel()
14+
{
15+
ObjectInstance = new ThrowawayClass();
16+
}
17+
18+
public ThrowawayClass? ObjectInstance { get; set; } = new ThrowawayClass();
19+
}
20+
21+
public class ThrowawayClass
22+
{
23+
}
24+
}

src/SourceGenerators/XamlGenerationTests/XamlGenerationTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<PropertyGroup>
33
<TargetFrameworks>MonoAndroid11.0;xamarinios10;netstandard2.0</TargetFrameworks>
44
<TargetFrameworksCI>MonoAndroid11.0;MonoAndroid10.0;xamarinios10;netstandard2.0</TargetFrameworksCI>
5+
6+
<LangVersion>8.0</LangVersion>
7+
<WarningsAsErrors>nullable</WarningsAsErrors>
58
</PropertyGroup>
69

710
<PropertyGroup>

0 commit comments

Comments
 (0)