From 37fad08e3930a34d12027ae5a8bba957fdd41bad Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 30 Jun 2026 15:39:24 -0400 Subject: [PATCH 1/2] Clarify rules on safety and sizeof This is a small focused change, but fixes what I (at least) misinterpreted at least twice. In version 7.3 we correctly updated the rules for in 8.8 for Unmanaged types to include struct types where the fields are all unmanaged types. This includes generic types where the type parameter has the `unmanaged` constraint. However, we didn't restate the restrictions for `sizeof` in 12.8.19 to prevent extending the allowed types in safe context to include those types. I updated the language in 12.8.19 to make those restrictions clear. --- standard/expressions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/standard/expressions.md b/standard/expressions.md index 2287f2233..53030382c 100644 --- a/standard/expressions.md +++ b/standard/expressions.md @@ -3201,7 +3201,7 @@ The `typeof` operator can be used on a type parameter. It is a compile time erro ### 12.8.19 The sizeof operator -The `sizeof` operator returns the number of 8-bit bytes occupied by a variable of a given type. The type specified as an operand to sizeof shall be an *unmanaged_type* ([§8.8](types.md#88-unmanaged-types)). +The `sizeof` operator returns the number of 8-bit bytes occupied by a variable of a given type. ```ANTLR sizeof_expression @@ -3227,7 +3227,7 @@ For certain predefined types the `sizeof` operator yields a constant `int` value |`sizeof(bool)` | 1 | |`sizeof(decimal)` | 16 | -For an enum type `T`, the result of the expression `sizeof(T)` is a constant value equal to the size of its underlying type, as given above. For all other operand types, the `sizeof` operator is specified in [§24.6.9](unsafe-code.md#2469-the-sizeof-operator). +For an enum type `T`, the result of the expression `sizeof(T)` is a constant value equal to the size of its underlying type, as given above. For all other operand types, the `sizeof` operator is specified in [§24.6.9](unsafe-code.md#2469-the-sizeof-operator), and is allowed only in an unsafe context. ### 12.8.20 The checked and unchecked operators From 0d8e3d43f9816f83e5414bed67bb5ed7ff208028 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 1 Jul 2026 16:03:41 -0400 Subject: [PATCH 2/2] Respond to comments in meeting We made a few smaller tweaks to the fix here. --- standard/expressions.md | 6 ++++-- standard/unsafe-code.md | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/standard/expressions.md b/standard/expressions.md index 53030382c..e0f6dd3aa 100644 --- a/standard/expressions.md +++ b/standard/expressions.md @@ -3209,7 +3209,7 @@ sizeof_expression ; ``` -For certain predefined types the `sizeof` operator yields a constant `int` value as shown in the table below: +For certain types the `sizeof` operator yields a constant `int` value as shown in the table below: |**Expression** | **Result** | |----------------- | ---------- | @@ -3227,7 +3227,9 @@ For certain predefined types the `sizeof` operator yields a constant `int` value |`sizeof(bool)` | 1 | |`sizeof(decimal)` | 16 | -For an enum type `T`, the result of the expression `sizeof(T)` is a constant value equal to the size of its underlying type, as given above. For all other operand types, the `sizeof` operator is specified in [§24.6.9](unsafe-code.md#2469-the-sizeof-operator), and is allowed only in an unsafe context. +For an enum type `T`, the result of the expression `sizeof(T)` is a constant value equal to the size of its underlying type, as given above. + +For all other operand types, the `sizeof` operator is specified in [§24.6.9](unsafe-code.md#2469-the-sizeof-operator), and is allowed only in an unsafe context. ### 12.8.20 The checked and unchecked operators diff --git a/standard/unsafe-code.md b/standard/unsafe-code.md index 605026d18..3eb811ed5 100644 --- a/standard/unsafe-code.md +++ b/standard/unsafe-code.md @@ -668,7 +668,7 @@ Because an implicit conversion exists from any pointer type to the `void*` type, ### 24.6.9 The sizeof operator -For certain predefined types ([§12.8.19](expressions.md#12819-the-sizeof-operator)), the `sizeof` operator yields a constant `int` value. For all other types, the result of the `sizeof` operator is implementation-defined and is classified as a value, not a constant. +For certain types ([§12.8.19](expressions.md#12819-the-sizeof-operator)), the `sizeof` operator yields a constant `int` value. For all other types, the result of the `sizeof` operator is implementation-defined and is classified as a value, not a constant. The order in which members are packed into a struct is unspecified.