AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
26.03k stars 2.25k forks source link

Error AVLN:0004 Avalonia: Internal compiler error while transforming node, System.FormatException: The input string '0 0.000001' was not in a correct format. #17455

Open wieslawsoltes opened 2 weeks ago

wieslawsoltes commented 2 weeks ago

Describe the bug

0>MainWindow.axaml(42,10): Error AVLN:0004 Avalonia: Internal compiler error while transforming node XamlX.Ast.XamlAstXamlPropertyValueNode:
System.FormatException: The input string '0 0.000001' was not in a correct format.
   at System.Number.ThrowFormatException[TChar](ReadOnlySpan`1 value)
   at System.Single.Parse(String s, IFormatProvider provider)
   at XamlX.TypeSystem.TypeSystemHelpers.<ParseConstantIfTypeAllows>g__Parse|7_0(<>c__DisplayClass7_0&) in /_/src/Markup/Avalonia.Markup.Xaml.Loader/xamlil.github/src/XamlX/TypeSystem/TypeSystemHelpers.cs:line 137
   at XamlX.TypeSystem.TypeSystemHelpers.ParseConstantIfTypeAllows(String s, IXamlType type, IXamlLineInfo info, XamlConstantNode& rv) in /_/src/Markup/Avalonia.Markup.Xaml.Loader/xamlil.github/src/XamlX/TypeSystem/TypeSystemHelpers.cs:line 145
   at XamlX.Transform.XamlTransformHelpers.TryConvertValue(AstTransformationContext context, IXamlAstValueNode node, IXamlType type, XamlAstClrProperty propertyContext, IXamlAstValueNode& rv) in /_/src/Markup/Avalonia.Markup.Xaml.Loader/xamlil.github/src/XamlX/Transform/XamlTransformHelpers.cs:line 250
   at XamlX.Transform.XamlTransformHelpers.TryGetCorrectlyTypedValue(AstTransformationContext context, IXamlAstValueNode node, IXamlType type, IXamlAstValueNode& rv) in /_/src/Markup/Avalonia.Markup.Xaml.Loader/xamlil.github/src/XamlX/Transform/XamlTransformHelpers.cs:line 162
   at Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.AvaloniaXamlIlLanguageParseIntrinsics.TryConvert(AstTransformationContext context, IXamlAstValueNode node, String text, IXamlType type, AvaloniaXamlIlWellKnownTypes types, IXamlAstValueNode& result) in /_/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlLanguageParseIntrinsics.cs:line 378
   at Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.AvaloniaXamlIlLanguage.CustomValueConverter(AstTransformationContext context, IXamlAstValueNode node, IXamlType type, IXamlAstValueNode& result) in /_/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlLanguage.cs:line 205
   at XamlX.Transform.XamlTransformHelpers.TryConvertValue(AstTransformationContext context, IXamlAstValueNode node, IXamlType type, XamlAstClrProperty propertyContext, IXamlAstValueNode& rv) in /_/src/Markup/Avalonia.Markup.Xaml.Loader/xamlil.github/src/XamlX/Transform/XamlTransformHelpers.cs:line 231
   at XamlX.Transform.Transformers.ConvertPropertyValuesToAssignmentsTransformer.<>c__DisplayClass0_3.<Transform>g__CreateAssignment|1(<>c__DisplayClass0_0&, <>c__DisplayClass0_1&, <>c__DisplayClass0_2&) in /_/src/Markup/Avalonia.Markup.Xaml.Loader/xamlil.github/src/XamlX/Transform/Transformers/ConvertPropertyValuesToAssignmentsTransformer.cs:line 107
   at XamlX.Transform.Transformers.ConvertPropertyValuesToAssignmentsTransformer.Transform(AstTransformationContext context, IXamlAstNode node) in /_/src/Markup/Avalonia.Markup.Xaml.Loader/xamlil.github/src/XamlX/Transform/Transformers/ConvertPropertyValuesToAssignmentsTransformer.cs:line 129
   at XamlX.Transform.AstTransformationContext.Visitor.Visit(IXamlAstNode node) in /_/src/Markup/Avalonia.Markup.Xaml.Loader/xamlil.github/src/XamlX/Transform/AstTransformationContext.cs:line 58 Line 42, position 10.

To Reproduce

Set property value to: baseFrequency="0 0.000001"


 public static readonly StyledProperty<SvgNumberCollection?> baseFrequencyProperty = AvaloniaProperty.Register<feTurbulence, SvgNumberCollection>(nameof (baseFrequency));

    public SvgNumberCollection? baseFrequency
    {
      get => this.GetValue<SvgNumberCollection>(feTurbulence.baseFrequencyProperty);
      set => this.SetValue<SvgNumberCollection>(feTurbulence.baseFrequencyProperty, value);
    }

    [TypeConverter(typeof(SvgNumberCollectionConverter))]
    public class SvgNumberCollection : List<float>, ICloneable
    {
        public object Clone()
        {
            var numbers = new SvgNumberCollection();
            foreach (var point in this)
                numbers.Add(point);
            return numbers;
        }

        public override string ToString()
        {
            return string.Join(" ", this.Select(v => v.ToSvgString()).ToArray());
        }
    }

Expected behavior

Compiler does not throw System.FormatException and uses provided [TypeConverter(typeof(SvgNumberCollectionConverter))] instead trying to parse property value by itself.

Avalonia version

11.0.11, 11.1.4, 11.2.0

OS

macOS

Additional context

No response

wieslawsoltes commented 2 weeks ago

It seems compiler has problem when list has generic type float, when you change to string type it works.

Change from: public class numbers : AvaloniaList<float> to: public class numbers : AvaloniaList<string>


[TypeConverter(typeof(numbersTypeConverter))]
public class numbers : AvaloniaList<float>
{
}

public class numbersTypeConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
    {
        return destinationType == typeof(numbers);
    }

    public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
    {
        return sourceType == typeof(string);
    }

    public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
    {
        var numbers = (numbers)value;

        return string.Concat(numbers.OfType<float>().Select(x => x));
    }

    public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
    {
        return new numbers();
    }
}