Skip to content

Commit 06f177a

Browse files
authored
Clarify rules on safety and sizeof (#1721)
* 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. * Respond to comments in meeting We made a few smaller tweaks to the fix here.
1 parent 8977a22 commit 06f177a

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

standard/expressions.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3201,15 +3201,15 @@ The `typeof` operator can be used on a type parameter. It is a compile time erro
32013201
32023202
### 12.8.19 The sizeof operator
32033203
3204-
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)).
3204+
The `sizeof` operator returns the number of 8-bit bytes occupied by a variable of a given type.
32053205
32063206
```ANTLR
32073207
sizeof_expression
32083208
: 'sizeof' '(' unmanaged_type ')'
32093209
;
32103210
```
32113211
3212-
For certain predefined types the `sizeof` operator yields a constant `int` value as shown in the table below:
3212+
For certain types the `sizeof` operator yields a constant `int` value as shown in the table below:
32133213

32143214
|**Expression** | **Result** |
32153215
|----------------- | ---------- |
@@ -3227,7 +3227,9 @@ For certain predefined types the `sizeof` operator yields a constant `int` value
32273227
|`sizeof(bool)` | 1 |
32283228
|`sizeof(decimal)` | 16 |
32293229

3230-
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).
3230+
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.
3231+
3232+
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.
32313233

32323234
### 12.8.20 The checked and unchecked operators
32333235

standard/unsafe-code.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ Because an implicit conversion exists from any pointer type to the `void*` type,
668668

669669
### 24.6.9 The sizeof operator
670670

671-
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.
671+
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.
672672

673673
The order in which members are packed into a struct is unspecified.
674674

0 commit comments

Comments
 (0)