Skip to content

[NativeAOT] don't generate __GetFieldHelper if 'boxed' struct overrides object Equals #119321

@NinoFloris

Description

@NinoFloris

Tangential follow up on #116301

If we cannot prevent constructs like typeof from marking structs as boxed we should make it possible to limit what a boxed struct implies.

Given the following code I would expect there to be no record of __GetFieldHelper in the mstat:

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

_ = TypeOf<Foo>();

[MethodImpl(MethodImplOptions.NoInlining)]
static Type TypeOf<T>() => typeof(T);

struct Foo
{
    public override int GetHashCode() => throw new NotSupportedException();
    public override bool Equals([NotNullWhen(true)] object? obj) => throw new NotSupportedException();
}

Once fixed it would be *a* mitigation for large __GetFieldHelpers being emitted.

Ideally though there would be an attribute like OmitObjectMethodImpls (or something more narrow like OmitObjectEquality) which could be applied to a type (or module even?).

Something that would make runtimes do the cheapest thing to produce a NotSupportedException in object's virtual methods for the affected types.

For NativeAOT that could be a bit flag in the type info or some __GetFieldHelper sentinel return value.

Having an intrinsic attribute would also be preferable to needing to override two or more methods (though worst case this could be handled by a source generator).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions