Add ExtendedLayoutAttribute support for F# compiler#19194
Draft
Add ExtendedLayoutAttribute support for F# compiler#19194
Conversation
Copilot
AI
changed the title
[WIP] Add ExtendedLayoutAttribute support for F# compiler
Add ExtendedLayoutAttribute support for F# compiler
Jan 6, 2026
T-Gro
reviewed
Jan 6, 2026
jkoritzinsky
reviewed
Jan 6, 2026
Comment on lines
+4
to
+13
| // Mock ExtendedLayoutAttribute and ExtendedLayoutKind for testing | ||
| // (will be replaced by real runtime types when available) | ||
| type ExtendedLayoutKind = | ||
| | CStruct = 0 | ||
| | CUnion = 1 | ||
|
|
||
| [<System.AttributeUsage(System.AttributeTargets.Struct, AllowMultiple = false)>] | ||
| type ExtendedLayoutAttribute(kind: ExtendedLayoutKind) = | ||
| inherit System.Attribute() | ||
| member _.Kind = kind |
Member
There was a problem hiding this comment.
To match the C# behavior. F# should require the ExtendedLayoutAttribute and ExtendedLayoutKind to come from the core assembly (if F# does this for other types provided by the core library).
Member
There was a problem hiding this comment.
The current FSharp implementation does search sys dll, in which version is the attribute going live to have it available for testing?
Member
There was a problem hiding this comment.
It will be in .NET 11. Should be in any alpha/preview version (as we've already merged the PR in runtime months ago).
Member
|
Note to self: This waits for NET11 alpha to have the attribute available. |
Contributor
❗ Release notes required
|
ed686c0 to
11730fc
Compare
…alidation Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
… IL test Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
- Resolve merge conflicts in TcGlobals, CheckDeclarations, IlxGen - Adapt to new WellKnownEntityAttributes pattern (HasFSharpAttributeOpt removed) - Remove mock-based ExtendedLayout tests (need .NET 11 runtime) - Fix E_StructLayout_Extended test line/col expectations - Update surface area baseline for ILTypeDefLayout.Extended - Add release notes entry Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Error codes 3879-3882 were already allocated on main. Renumber ExtendedLayout diagnostics to 3885-3888 and move them to end of FSComp.txt for proper sorting. - Move release notes from 9.0.300.md to 11.0.100.md (correct file). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- il.fs: Replace incorrect 'ECMA-335 spec' reference with runtime issue link (0x18 Extended layout is a .NET runtime extension, not in ECMA-335) - ilread.fs: Add comment explaining why hasLayout only matches Explicit - CheckDeclarations.fs: Document unreachable None branch in runtime check Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…, and other type kinds - Add noExtendedLayoutAttributeCheck() to hiddenReprChecks, Union, Record, Enum, LibraryOnlyILAssembly, and TyconCoreAbbrevThatIsReallyAUnion paths - Without this, ExtendedLayoutAttribute on non-struct types (when .NET 11 ships the attribute) would be silently accepted instead of raising FS3887 - Remove dead code: unreachable None branch in extendedLayoutAttributeCheck (hasExtendedLayoutAttr is false when attrib_ExtendedLayoutAttribute_opt is None, making the inner match redundant) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
11730fc to
798f2ee
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
ExtendedLayoutAttribute Support Implementation - COMPLETE ✅
Phase 1: IL Layer Changes ✅
Extendedcase toILTypeDefLayouttype inil.fsiandil.fsconvertLayoutfunction inil.fsto handleExtended→TypeAttributes0x18typeLayoutOfFlagsinilread.fsto read 0x18 flagsGenTypeDefPass3inilwrite.fsto handleExtendedlayouttypeAttributesOfTypeLayoutinilreflect.fsforExtendedsplitTypeLayoutinilprint.fsforExtendedPhase 2: Compiler Infrastructure ✅
attrib_ExtendedLayoutAttribute_opttoTcGlobals.fsandTcGlobals.fsiFSComp.txt(errors 3879-3882) for new validationsPhase 3: Type Checking & Validation ✅
CheckDeclarations.fsPhase 4: IL Generation ✅
IlxGen.fsto detect ExtendedLayoutAttribute and setILTypeDefLayout.ExtendedPhase 5: Testing ✅
Quality Assurance ✅
Implementation Complete
All tasks have been successfully completed. The F# compiler now supports ExtendedLayoutAttribute with:
Original prompt
ExtendedLayoutAttribute Support for F# Compiler
Summary
Add support for
System.Runtime.InteropServices.ExtendedLayoutAttribute, a new .NET runtime feature that allows specifying extended type layouts (like C structs/unions) for interop scenarios. When a user applies[<ExtendedLayout(ExtendedLayoutKind.CStruct)>]to a struct, the compiler must:TypeAttributes.ExtendedLayoutbit (0x18) in metadataStructLayoutAttribute, noFieldOffseton fields, runtime support check)The compiler intentionally does not validate the specific
ExtendedLayoutKindvalues or field types - those semantics are owned by the runtime and may evolve.Reference: #19190
Architecture Overview
The layout information flows through the system as follows:
Implementation Tasks
Task 1: Add Attribute Reference to TcGlobals
Files:
src/Compiler/TypedTree/TcGlobals.fssrc/Compiler/TypedTree/TcGlobals.fsiLocation in TcGlobals.fs: Near line 1489, where
attrib_StructLayoutAttributeis defined.Add:
Location in TcGlobals.fsi: Near line 477, where
attrib_StructLayoutAttributeis declared.Add:
Task 2: Add
ILTypeDefLayout.ExtendedCaseFiles:
src/Compiler/AbstractIL/il.fssrc/Compiler/AbstractIL/il.fsiLocation in il.fsi: Around line 1456, the
ILTypeDefLayouttype definition.Change from:
To:
Location in il.fs: Around line 2517, the
ILTypeDefLayouttype definition. Same change.Location in il.fs: Around line 2630, the
convertLayoutfunction.Change from:
To:
Task 3: Update IL Reading
File:
src/Compiler/AbstractIL/ilread.fsLocation: Around line 2040, the
typeLayoutOfFlagsfunction.Change from:
To:
Task 4: Update IL Writing
File:
src/Compiler/AbstractIL/ilwrite.fsLocation: Around line 2884, in
GenTypeDefPass3, the ClassLayout handling.Change from:
To: