Skip to content

Commit

Permalink
Merge pull request #7374 from Youssef1313/issues/7372
Browse files Browse the repository at this point in the history
fix: Don't use nullable reference types in generated code
  • Loading branch information
jeromelaban authored Oct 25, 2021
2 parents ae9729b + b167107 commit 20deb7a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3880,7 +3880,7 @@ string buildBindBack()
{
if (!string.IsNullOrWhiteSpace(rawBindBack))
{
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0], GetType(dataType));
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0], GetType(dataType)).ToDisplayString(NullableFlowState.None);
return $"(___ctx, __value) => {{ if(___ctx is {dataType} ___tctx) {{ ___tctx.{rawBindBack}(({propertyType})__value); }} }}";
}
else
Expand All @@ -3892,8 +3892,8 @@ string buildBindBack()
{
if (propertyPaths.properties.Length == 1)
{
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0], GetType(dataType));
return $"(___ctx, __value) => {{ if(___ctx is {dataType} ___tctx) {{ {contextFunction} = ({targetPropertyType})global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof({targetPropertyType.ToDisplayString(NullableFlowState.None)}), __value); }} }}";
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0], GetType(dataType)).ToDisplayString(NullableFlowState.None);
return $"(___ctx, __value) => {{ if(___ctx is {dataType} ___tctx) {{ {contextFunction} = ({targetPropertyType})global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof({targetPropertyType}), __value); }} }}";
}
else
{
Expand Down Expand Up @@ -3933,10 +3933,10 @@ string buildBindBack()
{
if (propertyPaths.properties.Length == 1)
{
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0]);
var targetPropertyType = GetXBindPropertyPathType(propertyPaths.properties[0]).ToDisplayString(NullableFlowState.None);
return $"(___ctx, __value) => {{ " +
$"if(___ctx is global::{_className.ns + "." + _className.className} ___tctx) " +
$"{rawFunction} = ({targetPropertyType})global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof({targetPropertyType.ToDisplayString(NullableFlowState.None)}), __value);" +
$"{rawFunction} = ({targetPropertyType})global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof({targetPropertyType}), __value);" +
$" }}";
}
else
Expand Down Expand Up @@ -4110,7 +4110,7 @@ private string GetCustomMarkupExtensionValue(XamlMemberDefinition member)
{
// The target property implements IConvertible, therefore
// cast ProvideValue() using Convert.ChangeType
var targetTypeDisplay = propertyType.ToDisplayString();
var targetTypeDisplay = propertyType.ToDisplayString(NullableFlowState.None);
var targetType = $"typeof({targetTypeDisplay})";

// It's important to cast to string before performing the conversion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<ResourceDictionary
x:Class="UnoNullableContextGeneratedBug.TestTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UnoNullableContextGeneratedBug">

<DataTemplate x:Key="TestDataTemplate" x:DataType="local:TestViewModel">
<StackPanel>
<!--This works-->
<TextBlock Text="{x:Bind ObjectInstance, Mode=OneWay}"/>

<!-- This generates code with a nullable error at compile time -->
<TextBlock Text="{x:Bind ObjectInstance, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>

</ResourceDictionary>
24 changes: 24 additions & 0 deletions src/SourceGenerators/XamlGenerationTests/TestNullable_ViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;

namespace UnoNullableContextGeneratedBug
{
public class TestViewModel
{
public TestViewModel()
{
ObjectInstance = new ThrowawayClass();
}

public ThrowawayClass? ObjectInstance { get; set; } = new ThrowawayClass();
}

public class ThrowawayClass
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<PropertyGroup>
<TargetFrameworks>MonoAndroid11.0;xamarinios10;netstandard2.0</TargetFrameworks>
<TargetFrameworksCI>MonoAndroid11.0;MonoAndroid10.0;xamarinios10;netstandard2.0</TargetFrameworksCI>

<LangVersion>8.0</LangVersion>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit 20deb7a

Please sign in to comment.