You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Object expression in struct generates byref field in a class #19068
Using an object expression in a struct that depends on this or primary constructor parameters in the arguments to the object's constructor results in a class being emitted with a byref field.
Struct should be compiled into a valid type that does what the code expresses.
Actual behavior
The type of the anonymous class created in Test has a byref<Struct>, resulting in a TypeLoadException at runtime.
It seems that when the "closure" type that is generated here depends on the containing type, it is always generated with a byref _ field that stores a reference to the original struct, even if that reference is never used.
Normally, situations like this result in FS0406 (The byref-typed variable '_' is used in an invalid way.), however here the instance is not referenced in any of the members of the anonymous class.
Even the constructor itself is kinda strange:
public Test@5(refStruct_){Unsafe.As<Struct,refStruct>(refthis._)=ref_;base..ctor(((Struct)Unsafe.As<Struct,refStruct>(refthis._)).test);}
No idea what the intention here was, but calling base(_.test) instead would have worked quite fine.
Known workarounds
If the constructor's arguments are first bound to variables, everything is fine.
Activity
Add release notes for PR #19070
Add CodeGen regression test infrastructure and document first 10 issues
Sprint 5: Final verification complete - 100% DoD satisfied
Fix #19068: Object expressions in struct types no longer generate inv…
Extract capturedTypeForFreeVar helper for byref free variable handling
Restore missing doc comment on GenGetFreeVarForClosure (#19068)
Fix struct/closure byref capture and let-rec initialization order
Fix struct/closure byref capture and let-rec initialization order
Fix struct/closure byref capture and let-rec initialization order
Fix struct/closure byref capture and let-rec initialization order
Fix struct/closure byref capture and let-rec initialization order
Fix struct/closure byref capture and let-rec initialization order