Skip to content

Clarify rules on safety and sizeof#1721

Merged
BillWagner merged 2 commits into
dotnet:draft-v8from
BillWagner:unmanaged-sizeof
Jul 2, 2026
Merged

Clarify rules on safety and sizeof#1721
BillWagner merged 2 commits into
dotnet:draft-v8from
BillWagner:unmanaged-sizeof

Conversation

@BillWagner

Copy link
Copy Markdown
Member

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.

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.
@BillWagner BillWagner added the meeting: discuss This issue should be discussed at the next TC49-TG2 meeting label Jun 30, 2026

@Nigel-Ecma Nigel-Ecma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

§24.6.9 is as undefined as ever, but that is by design.

Comment thread standard/expressions.md Outdated
@jskeet

jskeet commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Adding as a comment here so we can discuss it... 24.6.9 starts with:

For certain predefined types (§12.8.19), 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.

It's not clear from that sentence that sizeof yields a constant int value for enums. Should we update that as well? (I'm happy to create a PR for that.)

Separately, 24.6.9 doesn't have a restriction on sizeof only being used for unmanaged types, so there's nothing to stop sizeof(MyClassType) being valid in an unsafe context - it's implementation-specific. Is that the intention?

@jskeet

jskeet commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Bill will remove "predefined" from the first sentence in 24.6.9 and add the line break as suggested, then we can merge.

We made a few smaller tweaks to the fix here.
@BillWagner BillWagner merged commit 06f177a into dotnet:draft-v8 Jul 2, 2026
6 checks passed
@BillWagner BillWagner deleted the unmanaged-sizeof branch July 2, 2026 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

meeting: discuss This issue should be discussed at the next TC49-TG2 meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants