Repository navigation
Support [UnsafeAccessor] constructors of library types - #494
Merged
Merged
Conversation
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.
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.
Fixes #99 (its last item, except
NFloat, which is in filipnavara's #296).What changed
[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 asnew T(...)would be, through anIlObjectCreationOperation, 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'sDateTimealready is that_dateDatavalue (corelib/DateTimeOps.cs), so the result is the argument itself.List<int>: the JIT crashes, and NativeAOT fails to compile the method.Tests
tests/samples/1558_unsafe_accessor_library_ctor, recorded from .NET; the NativeAOT build prints the same.size.shis within every budget;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 ordinarynew T(...), viaEmitObjectCreation, so corelib shadows and intrinsic constructors apply.DateTime(ulong dateData)is handled separately: because csharpc’sDateTimeis the raw_dateDatavalue and that ctor isn’t in reference assemblies, the accessor returns the singleulongargument 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
DateTimeraw 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.