jamescourtney / FlatSharp

Fast, idiomatic C# implementation of Flatbuffers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generated code uses nullable annotations

dorny opened this issue · comments

I'm trying to use FlatSharp with netstandard2.0 which don't support nullable annotations.
I've tried to disable them: https://github.com/jamescourtney/FlatSharp/wiki/Compiler#nullable-annotations but seems the FlatSharpNullable property has no effect.

Example

csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Nullable>disable</Nullable>
    <FlatSharpNullable>false</FlatSharpNullable>
  </PropertyGroup>

    <ItemGroup>    
        <PackageReference Include="FlatSharp.Compiler" Version="7.1.0">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>        
    </ItemGroup>  

    <ItemGroup>
      <FlatSharpSchema Include="model.fbs" />
    </ItemGroup>

</Project>

Model:

namespace MyLibrary;

struct Frame
{
    Ts: long;
}

MSBuild output:

dotnet build
Microsoft (R) Build Engine version 17.2.2+038f9bae9 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored C:\Users\m.dorner.ext\Workspace\tmp\MyLibrary\MyLibrary.csproj (in 121 ms).
  dotnet C:\Users\m.dorner.ext\.nuget\packages\flatsharp.compiler\7.1.0\tools\net6.0\FlatSharp.Compiler.dll --nullable-warnings false --normalize-field-names true --gen-poolable false --input "model.fbs" --in
  cludes "" --output obj\Debug\netstandard2.0\
C:\Users\m.dorner.ext\Workspace\tmp\MyLibrary\obj\Debug\netstandard2.0\FlatSharp.generated.cs(19,2): error CS8370: Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.
0 or greater. [C:\Users\m.dorner.ext\Workspace\tmp\MyLibrary\MyLibrary.csproj]
C:\Users\m.dorner.ext\Workspace\tmp\MyLibrary\obj\Debug\netstandard2.0\model.fbs.generated.cs(21,2): error CS8370: Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.
0 or greater. [C:\Users\m.dorner.ext\Workspace\tmp\MyLibrary\MyLibrary.csproj]

Build FAILED.

Generated code:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by the FlatSharp FBS to C# compiler (source hash: 6.3.0.a29+2Dr7Dof07jeXZ5uPAVZt3ri8LR4NbnEwECovq78=)
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using FlatSharp;
using FlatSharp.Attributes;
using FlatSharp.Internal;
#pragma warning disable 0618
#nullable enable annotations
namespace FlatSharp.Compiler.Generated
{
    internal static class CloneHelpers_1e04873070724bc7b012b376cf3370cf
    {
        [global::System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        public static global::MyLibrary.Frame? Clone(global::MyLibrary.Frame? item)
        {
            checked
            {
                return item is null ? null : new MyLibrary.Frame(item);
            }
        }

        [global::System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        public static global::System.Int64 Clone(global::System.Int64 item)
        {
            checked
            {
                return item;
            }
        }
    }

}

namespace MyLibrary
{
    [FlatBufferStruct]
    [System.Runtime.CompilerServices.CompilerGenerated]
    public partial class Frame
    {
#pragma warning disable CS8618
        public Frame()
        {
            checked
            {
                this.OnInitialized(null);
            }
        }

#pragma warning restore CS8618
#pragma warning disable CS8618
        protected Frame(FlatBufferDeserializationContext context)
        {
            checked
            {
            }
        }

#pragma warning restore CS8618
        public Frame(Frame source)
        {
            checked
            {
                this.Ts = FlatSharp.Compiler.Generated.CloneHelpers_1e04873070724bc7b012b376cf3370cf.Clone(source.Ts);
                this.OnInitialized(null);
            }
        }

        partial void OnInitialized(FlatBufferDeserializationContext? context);
        protected void OnFlatSharpDeserialized(FlatBufferDeserializationContext? context) => this.OnInitialized(context);


        [FlatBufferItemAttribute(0)]
        public virtual long Ts { get; set; }
    }

}

Am I missing something or it's a bug?

PS: I've previously used your library on a different project and worked flawlessly. Thanks for your work on this 👍

Thanks for the kind words! They are appreciated.

Can you make sure your <LangVersion> is set to at least 8.0 in the .csproj? 8.0 is the minimum that FlatSharp tests and officially supports at this point. The official docs say that if unspecified, netstandard2.0 will default to version 7.3.

Yeah, seems this what the thing I was missing. I was under impression the C# 8 is not supported when targeting netstandard2.0.
Oh man, you probably just saved my some hours of work of backporting bunch of code to C# 7.3. I'm going to try it now and if all works out I will close the issue. Thanks.