Skip to content

Implementation for UnsafeAccessorTypeAttribute #114881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

AaronRobinsonMSFT
Copy link
Member

@AaronRobinsonMSFT AaronRobinsonMSFT commented Apr 21, 2025

Fixes #90081

Example:

Assembly A

// Note class is private
class Class1
{
    Class1() { }
    List<Class2> GetListOfClass2() => new List<Class2>();
}

// Note class is private
class Class2 { }

Assembly B - no metadata reference to assembly A

object class1 = CreateClass();
object listClass2 = CallGetListOfClass2(class1);
Assert.Equal("System.Collections.Generic.List`1[[Class2, A, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]", listClass2.GetType().FullName);

[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
[return: UnsafeAccessorType("Class1, A")]
extern static object CreateClass();

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "GetListOfClass2")]
[return: UnsafeAccessorType("System.Collections.Generic.List`1[[Class2, A]]")]
extern static object CallGetListOfClass2([UnsafeAccessorType("Class1, A")] object a);

@ghost
Copy link

ghost commented Apr 21, 2025

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

1 similar comment
@ghost
Copy link

ghost commented Apr 21, 2025

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@AaronRobinsonMSFT
Copy link
Member Author

/cc @AndriySvyryd

@AaronRobinsonMSFT
Copy link
Member Author

@jkoritzinsky @sbomer Looks like there is an issue with the new linker test.

Type `System.Runtime.CompilerServices.UnsafeAccessorTypeAttribute' should have been kept

at Mono.Linker.Tests.TestCasesRunner.AssemblyChecker.Verify() in /_/src/coreclr/tools/aot/ILCompiler.Trimming.Tests/TestCasesRunner/AssemblyChecker.cs:line 89
   at Mono.Linker.Tests.TestCasesRunner.ResultChecker.Check(TrimmedTestCaseResult testResult) in /_/src/coreclr/tools/aot/ILCompiler.Trimming.Tests/TestCasesRunner/ResultChecker.cs:line 66
   at Mono.Linker.Tests.TestCases.All.Run(String testName) in /_/src/coreclr/tools/aot/ILCompiler.Trimming.Tests/TestCases/TestSuites.cs:line 154
   at Mono.Linker.Tests.TestCases.All.Reflection(String t) in /_/src/coreclr/tools/aot/ILCompiler.Trimming.Tests/TestCases/TestSuites.cs:line 79
   at InvokeStub_All.Reflection(Object, Span`1)
   at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

@jkoritzinsky
Copy link
Member

I ran the tests for ILLink but not ILC. I'll fix that.

@AaronRobinsonMSFT
Copy link
Member Author

I consider this PR complete, just updated the API documentation. Additional suggestions on what to add to the doc are welcome.

@jkoritzinsky
Copy link
Member

/ba-g deadletter

@AaronRobinsonMSFT AaronRobinsonMSFT merged commit e2634eb into dotnet:main May 13, 2025
151 of 155 checks passed
@AaronRobinsonMSFT AaronRobinsonMSFT deleted the unsafeaccessortype_impl branch May 13, 2025 23:48
@github-actions github-actions bot locked and limited conversation to collaborators Jun 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: UnsafeAccessorTypeAttribute for static or private type access
6 participants