Open kant2002 opened 1 year ago
I think this is by-design.
In frameworks that don't have the RuntimeFeature.NumericIntPtr
feature flag, UIntPtr
and nuint
are not considered identical.
You have two possible solutions:
UIntPtr
and UInt32
instead of corresponding built-in type syntax (nuint
and uint
): public static explicit operator UIntPtr(UInt32 value)
RuntimeFeature.NumericIntPtr
feature flag (after that release we do consider the two types to be strictly equivalent)The test below shows that both of those solutions work. Tagging @cston (who worked on native integers) in case anything to add.
[Fact]
public void TODO2()
{
var src = """
namespace System
{
public class Object
{
}
public readonly struct UIntPtr
{
// error CS0556: User-defined conversion must convert to or from the enclosing type
public static explicit operator nuint(uint value) => checked((nuint)value);
}
public class Attribute { }
public class String { }
public abstract class ValueType { }
public struct Void { }
public struct UInt32 { }
namespace Runtime.Versioning
{
public class TargetFrameworkAttribute : Attribute
{
public TargetFrameworkAttribute(string name)
{
}
public string FrameworkDisplayName { get; set; }
}
}
namespace Reflection
{
class Dummmy { }
}
}
""";
var comp = CreateEmptyCompilation(src);
comp.VerifyDiagnostics(
// (11,41): error CS0556: User-defined conversion must convert to or from the enclosing type
// public static explicit operator nuint(uint value) => checked((nuint)value);
Diagnostic(ErrorCode.ERR_ConversionNotInvolvingContainedType, "nuint").WithLocation(11, 41)
);
var runtimeFeatures = """
namespace System.Runtime.CompilerServices
{
public static class RuntimeFeature
{
public const string NumericIntPtr = "NumericIntPtr";
}
}
""";
comp = CreateEmptyCompilation(src + runtimeFeatures);
comp.VerifyDiagnostics();
var src2 = """
namespace System
{
public class Object
{
}
public readonly struct UIntPtr
{
// error CS0556: User-defined conversion must convert to or from the enclosing type
public static explicit operator UIntPtr(UInt32 value) => checked((nuint)value);
}
public class Attribute { }
public class String { }
public abstract class ValueType { }
public struct Void { }
public struct UInt32 { }
namespace Runtime.Versioning
{
public class TargetFrameworkAttribute : Attribute
{
public TargetFrameworkAttribute(string name)
{
}
public string FrameworkDisplayName { get; set; }
}
}
namespace Reflection
{
class Dummmy { }
}
}
""";
comp = CreateEmptyCompilation(src2);
comp.VerifyDiagnostics(
);
}
Thanks a lot, that unblocks me and I really should be looking on RuntimeFeature
class more in the future.
@jcouv same setup with RuntimeFeature.NumericIntPtr
added.
If I add this line
public static bool operator ==(nint value1, nint value2) => value1 == value2;
I receive error CS0563: One of the parameters of a binary operator must be the containing type
which at this point also strange
You'll need to provide the complete snippet to get any useful advice. The operator you provided can't work in the code snippet in OP (which doesn't define IntPtr
).
Version Used:
Steps to Reproduce:
Create Class1.cs with following content
Create nuintissue.csproj with following content
error CS0556: User-defined conversion must convert to or from the enclosing type