Skip to content

Support [UnsafeAccessor] constructors of library types - #494

Merged
jonathanperis merged 5 commits into
mainfrom
fix/unsafe-accessor-library-ctor
Nov 25, 2026
Merged

jonathanperis merged 5 commits into
mainfrom
fix/unsafe-accessor-library-ctor

Conversation

@jonathanperis

@jonathanperis jonathanperis commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Fixes #99 (its last item, except NFloat, which is in filipnavara's #296).

What changed

  • Library types: an [UnsafeAccessor(UnsafeAccessorKind.Constructor)] for a type that isn't the program's own (DateTime, TimeSpan, StringBuilder, Version, Guid) used to be the explicit diagnostic "[UnsafeAccessor] constructor of 'System.DateTime'". It is now created exactly as new T(...) would be, through an IlObjectCreationOperation, so the type's own representation, corelib shadow or intrinsic constructor applies.
  • DateTime(ulong dateData): the private constructor eM Client's assemblies call. The reference assemblies don't declare it, and csharpc's DateTime already is that _dateData value (corelib/DateTimeOps.cs), so the result is the argument itself.
  • Still a diagnostic: any other constructor csharpc can't find or implement.
  • Generic types: not in scope. .NET itself rejects a constructor accessor of List<int>: the JIT crashes, and NativeAOT fails to compile the method.

Tests

On merge, #99 gets a report listing the status of each item.


Note

Medium Risk
Changes compiler lowering for unsafe constructor accessors and a special-case DateTime representation; incorrect lowering could miscompile code that relies on these accessors.

Overview
Constructor [UnsafeAccessor] no longer rejects non-user types with a hard compile error. Library types (DateTime, TimeSpan, StringBuilder, etc.) are now emitted like ordinary new T(...), via EmitObjectCreation, so corelib shadows and intrinsic constructors apply.

DateTime(ulong dateData) is handled separately: because csharpc’s DateTime is the raw _dateData value and that ctor isn’t in reference assemblies, the accessor returns the single ulong argument unchanged.

User-defined types still use the existing in-place struct or allocate-and-call paths. A new sample test locks expected output against .NET for several BCL constructors, including private DateTime raw data and invalid-ticks validation.

Reviewed by Cursor Bugbot for commit d622e4f. Bugbot is set up for automated code reviews on this repo. Configure here.

A constructor accessor whose type is not the program's (DateTime, TimeSpan,
StringBuilder, Version, Guid) was an explicit diagnostic. It is created as
new T(...) would be, so the type's own representation, shadow or intrinsic
constructor applies. DateTime's private DateTime(ulong dateData), which
eM Client's assemblies call and the reference assemblies don't declare, is the
raw value: csharpc's DateTime is that _dateData. A constructor csharpc has no
implementation for stays an explicit diagnostic.

Sample 1558, recorded from .NET (NativeAOT agrees). .NET's runtime rejects a
constructor accessor of a generic type such as List<int>, so the sample has
none.
@jonathanperis jonathanperis added this to the Drop-in for large apps milestone Nov 24, 2026
@jonathanperis jonathanperis added enhancement New feature or request area: codegen Lowering and code generation labels Nov 24, 2026
@jonathanperis jonathanperis self-assigned this Nov 24, 2026
@jonathanperis
jonathanperis merged commit f2e7c4c into main Nov 25, 2026
35 checks passed
@jonathanperis
jonathanperis deleted the fix/unsafe-accessor-library-ctor branch November 25, 2026 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: codegen Lowering and code generation enhancement New feature or request

Projects

None yet

1 participant