Skip to content

Commit

Permalink
Merge pull request #44 from Cysharp/hadashiA/fix-net8
Browse files Browse the repository at this point in the history
Fix compilation errors in .net8 with explicit static members
  • Loading branch information
neuecc committed Feb 3, 2024
2 parents a40a091 + fa0c19d commit 3ac1cc8
Show file tree
Hide file tree
Showing 11 changed files with 322 additions and 98 deletions.
2 changes: 1 addition & 1 deletion sandbox/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion sandbox/FileGenerate/FileGenerate.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(ProjectDir)..\Generated</CompilerGeneratedFilesOutputPath>
Expand Down
9 changes: 7 additions & 2 deletions sandbox/FileGenerate/SimplePrimitive.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnitGenerator;
using System;
using UnitGenerator;

namespace FileGenerate
{
Expand All @@ -11,12 +12,16 @@ namespace FileGenerate
public readonly partial struct B
{
}

[UnitOf(typeof(int), UnitGenerateOptions.Comparable | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.ParseMethod)]
public readonly partial struct C
{
}

[UnitOf(typeof(Guid), UnitGenerateOptions.Comparable | UnitGenerateOptions.ParseMethod)]
public readonly partial struct D
{
}

[UnitOf<int>]
public readonly partial struct Aa
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace FileGenerate
#endif
#if NET8_0_OR_GREATER
, IEqualityOperators<A, A, bool>
, IUtf8SpanFormattable
#endif
{
readonly int value;
Expand Down Expand Up @@ -83,6 +84,11 @@ public override int GetHashCode()
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
((ISpanFormattable)value).TryFormat(destination, out charsWritten, format, provider);
#endif
#if NET8_0_OR_GREATER
public bool TryFormat (Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
((IUtf8SpanFormattable)value).TryFormat(utf8Destination, out bytesWritten, format, provider);
#endif

// Default

private class ATypeConverter : System.ComponentModel.TypeConverter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ namespace FileGenerate
[System.ComponentModel.TypeConverter(typeof(AaTypeConverter))]
readonly partial struct Aa
: IEquatable<Aa>
#if NET7_0_OR_GREATER
, IEqualityOperators<Aa, Aa, bool>
#endif
, IFormattable
#if NET6_0_OR_GREATER
, ISpanFormattable
#endif
#if NET8_0_OR_GREATER
, IEqualityOperators<Aa, Aa, bool>
, IUtf8SpanFormattable
#endif
{
readonly int value;

Expand Down Expand Up @@ -76,6 +80,15 @@ public override int GetHashCode()

public string ToString(string? format, IFormatProvider? formatProvider) => value.ToString(format, formatProvider);

#if NET6_0_OR_GREATER
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
((ISpanFormattable)value).TryFormat(destination, out charsWritten, format, provider);
#endif
#if NET8_0_OR_GREATER
public bool TryFormat (Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
((IUtf8SpanFormattable)value).TryFormat(utf8Destination, out bytesWritten, format, provider);
#endif

// Default

private class AaTypeConverter : System.ComponentModel.TypeConverter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace FileGenerate
[System.ComponentModel.TypeConverter(typeof(BbTypeConverter))]
readonly partial struct Bb
: IEquatable<Bb>
#if NET7_0_OR_GREATER
#if NET8_0_OR_GREATER
, IEqualityOperators<Bb, Bb, bool>
#endif
#endif
{
readonly string value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ namespace FileGenerate
#endif
#if NET7_0_OR_GREATER
, IComparisonOperators<C, C, bool>
, IParsable<C>
, ISpanParsable<C>
, IAdditionOperators<C, C, C>
, ISubtractionOperators<C, C, C>
, IMultiplyOperators<C, C, C>
Expand All @@ -32,7 +30,7 @@ namespace FileGenerate
#endif
#if NET8_0_OR_GREATER
, IEqualityOperators<C, C, bool>
, IUtf8SpanParsable<C>
, IUtf8SpanFormattable
#endif
{
readonly int value;
Expand Down Expand Up @@ -98,6 +96,11 @@ public override int GetHashCode()
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
((ISpanFormattable)value).TryFormat(destination, out charsWritten, format, provider);
#endif
#if NET8_0_OR_GREATER
public bool TryFormat (Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
((IUtf8SpanFormattable)value).TryFormat(utf8Destination, out bytesWritten, format, provider);
#endif

// UnitGenerateOptions.ParseMethod

public static C Parse(string s)
Expand All @@ -119,69 +122,6 @@ public static bool TryParse(string s, out C result)
}
}

#if NET7_0_OR_GREATER
public static C Parse(string s, IFormatProvider? provider)
{
return new C(int.Parse(s, provider));
}

public static bool TryParse(string s, IFormatProvider? provider, out C result)
{
if (int.TryParse(s, provider, out var r))
{
result = new C(r);
return true;
}
else
{
result = default(C);
return false;
}
}
#endif

#if NET7_0_OR_GREATER
public static C Parse(ReadOnlySpan<char> s, IFormatProvider? provider)
{
return new C(int.Parse(s, provider));
}

public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out C result)
{
if (int.TryParse(s, provider, out var r))
{
result = new C(r);
return true;
}
else
{
result = default(C);
return false;
}
}
#endif

#if NET8_0_OR_GREATER
public static C Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider)
{
return new C(int.Parse(utf8Text, provider));
}

public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out C result)
{
if (int.TryParse(utf8Text, provider, out var r))
{
result = new C(r);
return true;
}
else
{
result = default(C);
return false;
}
}
#endif

// UnitGenerateOptions.ArithmeticOperator

public static C operator +(C x, C y)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ namespace FileGenerate
[System.ComponentModel.TypeConverter(typeof(CcTypeConverter))]
readonly partial struct Cc
: IEquatable<Cc>
#if NET7_0_OR_GREATER
, IEqualityOperators<Cc, Cc, bool>
#endif
, IComparable<Cc>
#if NET7_0_OR_GREATER
, IComparisonOperators<Cc, Cc, bool>
#endif
, IFormattable
#if NET6_0_OR_GREATER
, ISpanFormattable
#endif
#if NET7_0_OR_GREATER
, IComparisonOperators<Cc, Cc, bool>
, IAdditionOperators<Cc, Cc, Cc>
, ISubtractionOperators<Cc, Cc, Cc>
, IMultiplyOperators<Cc, Cc, Cc>
Expand All @@ -29,6 +27,10 @@ namespace FileGenerate
, IUnaryNegationOperators<Cc, Cc>
, IIncrementOperators<Cc>
, IDecrementOperators<Cc>
#endif
#if NET8_0_OR_GREATER
, IEqualityOperators<Cc, Cc, bool>
, IUtf8SpanFormattable
#endif
{
readonly int value;
Expand Down Expand Up @@ -90,6 +92,15 @@ public override int GetHashCode()

public string ToString(string? format, IFormatProvider? formatProvider) => value.ToString(format, formatProvider);

#if NET6_0_OR_GREATER
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
((ISpanFormattable)value).TryFormat(destination, out charsWritten, format, provider);
#endif
#if NET8_0_OR_GREATER
public bool TryFormat (Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
((IUtf8SpanFormattable)value).TryFormat(utf8Destination, out bytesWritten, format, provider);
#endif

// UnitGenerateOptions.ArithmeticOperator

public static Cc operator +(Cc x, Cc y)
Expand Down

0 comments on commit 3ac1cc8

Please sign in to comment.