Skip to content
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

Compiling against methods that accept params span with only System.Runtime reference produces compiler error #102513

Closed
ericstj opened this issue May 21, 2024 · 1 comment · Fixed by #106360
Assignees
Labels
area-System.Runtime blocking-release in-pr There is an active PR which will close this issue when it is merged regression-from-last-release
Milestone

Comments

@ericstj
Copy link
Member

ericstj commented May 21, 2024

Description

We have API in System.Runtime that accepts params ReadonlySpan. Normally we'd be able to consume any API in System.Runtime with just a reference to System.Runtime - but calling these new APIs demands System.Runtime.InteropServices.MemoryMarshal.CreateReadOnlySpan

I wonder if we should push this type down, perhaps others in System.Memory as well. It's implementation already lives in System.Private.CoreLib

Reproduction Steps

Here's a repro that demonstrates a customer scenario where they do runtime compilation and specify System.Runtime only as a reference. This isn't too farfetched since we recommend that folks use reference assemblies.

compileSR.zip -> derived from https://learn.microsoft.com/en-us/archive/msdn-magazine/2017/may/net-core-cross-platform-code-generation-with-roslyn-and-net-core#executing-code-the-emit-apis sample

using System.Reflection;
using System.Runtime.Loader;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;

const string code = """
        public static class C
        {
            public static string M() => string.Concat("Hello", " ", "World", "!", "!", "!");
        }
        """;
var tree = SyntaxFactory.ParseSyntaxTree(code, new CSharpParseOptions(LanguageVersion.Preview));
string fileName="lib.dll";
// add reference to only System.Runtime
var systemRuntimeRefLocation=Path.Combine(AppContext.BaseDirectory, "refs", "System.Runtime.dll");
var systemRuntimeReference = MetadataReference.CreateFromFile(systemRuntimeRefLocation);
var compilation = CSharpCompilation.Create(fileName)
    .WithOptions(
        new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
    .AddReferences(systemRuntimeReference)
    .AddSyntaxTrees(tree);
string path = Path.Combine(Directory.GetCurrentDirectory(), fileName);

EmitResult compilationResult = compilation.Emit(path);
if(compilationResult.Success)
{
    // Load the assembly
    Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
    object? result = asm!.GetType("C")!.GetMethod("M")!.Invoke(null, []);
    Console.WriteLine(result);
}
else
{
    foreach (Diagnostic codeIssue in compilationResult.Diagnostics)
    {
        string issue = $"ID: {codeIssue.Id}, Message: {codeIssue.GetMessage()}, Location: {codeIssue.Location.GetLineSpan()}, Severity: {codeIssue.Severity}";
        Console.WriteLine(issue);
    }
}

Expected behavior

Comple successful and output "Hello World!!!"

Actual behavior

ID: CS0656, Message: Missing compiler required member 'System.Runtime.InteropServices.MemoryMarshal.CreateReadOnlySpan', Location: : (0,0)-(0,0), Severity: Error

Regression?

Yes, the addition of the params ReadOnlySpan overloads introduced this break.

Known Workarounds

Add a reference to System.Memory

Configuration

No response

Other information

See dotnet/sdk#41014 for some background. CC @333fred @jaredpar @jozkee

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 21, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 21, 2024
@ericstj ericstj added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels May 21, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

@jeffhandley jeffhandley added this to the 9.0.0 milestone Jul 20, 2024
@jeffhandley jeffhandley removed the untriaged New issue has not been triaged by the area owner label Jul 20, 2024
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Aug 13, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime blocking-release in-pr There is an active PR which will close this issue when it is merged regression-from-last-release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants