-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BOOL
/BOOLEAN
templates are incorrect
#624
Comments
BOOL
template is incorrectBOOL
/BOOLEAN
templates are incorrect
Wow, that's a good caveat to know. Is that just with the default value, or even if you then assign |
Also, comparisons in different contexts don't operate the same on |
With the current implementation, even if a .NET |
This documentation comment is in the winforms code base. Does that make this implicit operator incorrect as well? CsWin32/src/Microsoft.Windows.CsWin32/templates/BOOL.cs Lines 4 to 8 in ab63c9a
|
@elachlan Good lord, I didn't even pay attention to that. Yes, that is very suspect. The Windows |
@AaronRobinsonMSFT , do we just go with this? public static unsafe implicit operator bool(BOOL value)
{
return this.value != 0 ? true : false;
} |
Sure or, even simpler - |
@AaronRobinsonMSFT the CsWin32/src/Microsoft.Windows.CsWin32/templates/BOOLEAN.cs Lines 3 to 8 in ab63c9a
The tests seem strange: It looks like CsWin32/test/GenerationSandbox.Tests/BooleanTests.cs Lines 41 to 48 in ab63c9a
But then, when comparing a native CsWin32/test/GenerationSandbox.Tests/BooleanTests.cs Lines 19 to 39 in ab63c9a
So the changes to |
I think the same tests exist for |
The documentation on the datatypes doesn't say anything about preserving the underlying values. @AArnott have you got any guidance? |
CC: @RussKie |
That is an odd test. All Win32 boolean based types normalize to a "true" or "false" state. As long as we are in managed code the actual value does matter beyond 0 and non-zero. All non-zero are equal from a boolean type's perspective. |
I think its because some Win32APIs return Thank git and its version history for me being able to track that down. |
This is news to me - thanks for tracking it down. That code/tests likely warrant a comment as it isn't intuitive and is likely limited to a narrow set of APIs. This doesn't change the marshal to unmanaged logic though. Basically, going from C# |
@AArnott, can you explain why |
Because of what you found in #624 (comment). We allow implicit conversions across the bool types, and implicit conversions should not be lossy. So either we make BOOL and BOOLEAN convert to It sounds like we don't have a bug at this point because we in fact don't yet set |
Instead of lossy implicit conversions to |
Aren't these types injected into the user's project as source? |
Yes. Are you calling out that
That's a very interesting idea. I didn't know about I haven't deeply studied the problem yet, but in passing it seems like the problem is a risk that we read more bytes than are guaranteed to be initialized. Isn't that solvable without changing the fundamental idea of preserving the underlying value (the part of it that's guaranteed to be initialized)? |
Yes. I can accept that data coming from an unmanaged context may not want to be normalized but going in the other direction the code should really just be |
What value is there in not being lossy in one conversion but being lossy in the other? Since C# makes extracting the non-binary value from a |
I don't see how that would work in practice. It would mean the |
@stephentoub, sorry to snipe you like this. Could you please chime in on how we should be handling interop |
The runtime cannot change the memory layout of |
Do we just initialize the bytes ourselves when doing the conversion? |
@AArnott this checked conversion is a hard blocker for WinForms. We're getting back It's way too risky for us to have checked operations with |
This means it has to be a lossy conversion, since `BOOL` is 4 bytes and `bool` is 1 byte. Generally .NET recommends avoiding lossy implicity operators (though explicit is ok). But for a type like this, it seems like super-high value for folks to be able to author `if (!SomeBOOL())` and have that work without casts. Fixes #624
This means it has to be a lossy conversion, since `BOOL` is 4 bytes and `bool` is 1 byte. Generally .NET recommends avoiding lossy implicity operators (though explicit is ok). But for a type like this, it seems like super-high value for folks to be able to author `if (!SomeBOOL())` and have that work without casts. Fixes #624
This means it has to be a lossy conversion, since `BOOL` is 4 bytes and `bool` is 1 byte. Generally .NET recommends avoiding lossy implicity operators (though explicit is ok). But for a type like this, it seems like super-high value for folks to be able to author `if (!SomeBOOL())` and have that work without casts. Fixes #624
The
BOOL
andBOOLEAN
template constructors taking a C#bool
are invalid.CsWin32/src/Microsoft.Windows.CsWin32/templates/BOOL.cs
Line 3 in ab63c9a
CsWin32/src/Microsoft.Windows.CsWin32/templates/BOOLEAN.cs
Line 3 in ab63c9a
The
BOOL
template should be, theBOOLEAN
is similar.The notion of simply blitting the lowest bits of a
bool
instance is undefined. It also makes this usage troublesome if a user definesSkipLocalsInitAttribute
, which with the current implementation leave the upper bits ofValue
in an undefined state thus making a .NETfalse
possibly be non-zero.The text was updated successfully, but these errors were encountered: