-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add proposal for unsigned sizeof #9634
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
Open
jnm2
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
jnm2:unsigned_sizeof
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Unsigned `sizeof` | ||
|
||
Champion issue: <https://github.com/dotnet/csharplang/issues/9633> | ||
|
||
## Summary | ||
|
||
This proposal enables `sizeof` expressions without a constant value to implicitly convert to unsigned integer types the same size as `uint` or larger: | ||
|
||
```cs | ||
uint size = sizeof(SomeStruct); | ||
``` | ||
|
||
## Motivation | ||
|
||
This is a minute language change which alleviates a paper cut within native interop domains. | ||
|
||
A common pattern for native Windows APIs involves assigning the size of a struct to one of its own fields, or passing the struct's size as an additional argument to a function along with a pointer to the struct. The language provides the required value easily using `sizeof(NativeStruct)`. This is only permitted in unsafe code, and it is safe so long as the struct is authored to be blittable—to have a memory layout identical to what the native API expects. | ||
|
||
These same native APIs make heavy use of unsigned integer types. The C# struct or method representing the native API will often use `uint` or other unsigned types in order to faithfully preserve the native API's distinction between signed and unsigned values. For example, `Microsoft.Windows.CsWin32` is a source generator which autogenerates such structs and methods based on Windows header files. | ||
|
||
A size is never negative. Thus, invariably, the native API is defined to take an unsigned integer, and the automated C# projection of that API requires the language user to produce a `uint` value for the size. Since the C# `sizeof` operator produces a non-constant `int` value for a user-defined struct, this results in the user having to insert explicit `uint` casts most of the time that `sizeof` is used with a struct. | ||
|
||
For example: | ||
|
||
```cs | ||
var buffer = default(PROCESS_BASIC_INFORMATION); | ||
|
||
PInvoke.NtQueryInformationProcess( | ||
processHandle, | ||
PROCESSINFOCLASS.ProcessBasicInformation, | ||
&buffer, | ||
(uint)sizeof(PROCESS_BASIC_INFORMATION), | ||
null); | ||
``` | ||
|
||
Or: | ||
|
||
```cs | ||
var header = default(BITMAPINFOHEADER); | ||
header.biSize = (uint)sizeof(BITMAPINFOHEADER); | ||
|
||
// Generated from native headers: | ||
struct BITMAPINFOHEADER | ||
{ | ||
public uint biSize; | ||
public int biWidth; | ||
// ... | ||
} | ||
``` | ||
|
||
The frequent insertion of explicit uint casts is ironic: the compiler is emitting the `sizeof` CIL instruction which itself natively produces an `unsigned int32` as defined by ECMA-335, 6th edition, §III.2.45. | ||
|
||
## Detailed design | ||
|
||
A new implicit conversion is defined, an _unsigned sizeof conversion_, from a `sizeof` expression to `uint`, `nuint`, or `ulong`. | ||
|
||
This conversion is added to the list of standard implicit conversions so that a `sizeof` expression may implicitly convert to a user-defined type which has an implicit conversion from an unsigned integer type. | ||
|
||
## Specification | ||
|
||
The [sizeof operator](https://github.com/dotnet/csharpstandard/blob/draft-v9/standard/expressions.md#12819-the-sizeof-operator) section is adjusted as follows (additions in **bold**): | ||
|
||
> ### 12.8.19 The sizeof operator | ||
> | ||
> The `sizeof` operator returns the number of 8-bit bytes occupied by a variable of a given type **as an `int` value**. | ||
|
||
Then, a new conversion is added to the [Implicit conversions](https://github.com/dotnet/csharpstandard/blob/draft-v9/standard/conversions.md#102-implicit-conversions) section: | ||
|
||
> ### Unsigned sizeof conversions | ||
> | ||
> An implicit conversion exists from a _sizeof_expression_ ([§12.8.19](https://github.com/dotnet/csharpstandard/blob/draft-v9/standard/expressions.md#12819-the-sizeof-operator)) to `uint`. | ||
|
||
The new conversion is added to the [Standard implicit conversions](https://github.com/dotnet/csharpstandard/blob/draft-v9/standard/conversions.md#1042-standard-implicit-conversions) section: | ||
|
||
> The following implicit conversions are classified as standard implicit conversions: | ||
> | ||
> - Identity conversions ([§10.2.2](conversions.md#1022-identity-conversion)) | ||
> - Implicit numeric conversions ([§10.2.3](conversions.md#1023-implicit-numeric-conversions)) | ||
> - Implicit nullable conversions ([§10.2.6](conversions.md#1026-implicit-nullable-conversions)) | ||
> - Null literal conversions ([§10.2.7](conversions.md#1027-null-literal-conversions)) | ||
> - Implicit reference conversions ([§10.2.8](conversions.md#1028-implicit-reference-conversions)) | ||
> - Boxing conversions ([§10.2.9](conversions.md#1029-boxing-conversions)) | ||
> - Implicit constant expression conversions ([§10.2.11](conversions.md#10211-implicit-constant-expression-conversions)) | ||
> - Implicit conversions involving type parameters ([§10.2.12](conversions.md#10212-implicit-conversions-involving-type-parameters)) | ||
> - **Unsigned sizeof conversions** | ||
|
||
## Drawbacks | ||
|
||
No drawbacks are anticipated. | ||
|
||
## Answered questions | ||
|
||
## Open questions | ||
|
||
1. Is a betterness rule needed in order to prevent this change from affecting overload resolution? For example: | ||
|
||
```cs | ||
M(sizeof(SomeStruct)); | ||
void M(long s) { } // Selected in C# 14 | ||
void M(uint s) { } | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.