runtime: [System.Memory] Embedded Reference Types compiler bug
Code that was previously working (on 4.5.0-preview2-25707-02 , which seems to have disappeared!?) breaks on the latest preview: 4.5.0-preview1-26002-01, this is breaking deployment of new CI infrastructure as the working package is not obtainable. I have tried to hunt down the the issue, but it is beyond my understanding.
Repro steps
-
dotnet new console
-
dotnet add package System.Memory -v 4.5.0-preview1-26002-01 -s https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json
-
Edit Program.cs and insert
ReadOnlySpan<char> x = "hello".AsReadOnlySpan();before the print statement -
Run
dotnet build
Project file (repo.csproj)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.0-preview1-26002-01" />
</ItemGroup>
</Project>
Source file (Program.cs)
using System;
namespace repo
{
class Program
{
static void Main(string[] args)
{
ReadOnlySpan<char> x = "hello".AsReadOnlySpan();
Console.WriteLine("Hello World!");
}
}
}
Expected behavior
Expected that the above code works, the usage was taken from the Span doco (https://github.com/dotnet/corefxlab/blob/master/docs/specs/span.md)
Actual behavior
Compiler error: Program.cs(10,12): error CS0619: ‘ReadOnlySpan<char>’ is obsolete: ‘Types with embedded references are not supported in this version of your compiler.’ [/tmp/repo/repo.csproj]
Known workarounds
If you have a cached version of 4.5.0-preview2-25707-02 , you can use this and there will be no errors
Related information
Output of dotnet --info
.NET Command Line Tools (2.0.3)
Product Information:
Version: 2.0.3
Commit SHA-1 hash: eb1d5ee318
Runtime Environment:
OS Name: debian
OS Version: 9
OS Platform: Linux
RID: linux-x64
Base Path: /usr/share/dotnet/sdk/2.0.3/
Microsoft .NET Core Shared Framework Host
Version : 2.0.3
Build : a9190d4a75f4a982ae4b4fa8d1a24526566c69df
Edit: Tagging area owners @KrzysztofCwalina and @ahsonkhan
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 34 (11 by maintainers)
Commits related to this issue
- Bump lang version https://github.com/dotnet/corefx/issues/25669 — committed to SixLabors/ImageSharp.Web by JimBobSquarePants 6 years ago
@ahsonkhan I’ve confirmed this is an issue with ReSharper, as suspending it removes the error message - I’ll open a ticket with JetBrains instead!
dotnet build works correctly in my case, however, vscode shows the error as a tooltip and in the ‘problems’ pane. I presume its not using the correct roslyn version but i can’t figure out how to change that.
@ahsonkhan I’m ok now, apologies for not getting back to you. Turned out it was a Resharper bug.
I’m on VS 2017 15.7.1 and having this same problem with the latest .NET Core 2.1 RC. Strange thing is, if I set
LangVersionto7.2, it goes away - if it’slatestor7.3, the error comes back.I’m experiencing the same issue in Visual Studio 15.6.7 with VB 15.5. I have added
<LangVersion>Latest</LangVersion>in<PropertyGroup>in my .vbproj file. But this error is still showing. I clearly know thatSystem.Span(Of T)can’t be stored on heap, so I don’t want to see the BC30668 error preventing me from compiling my code. I have to downgrade System.Memory to 4.4.0-preview1-25305-02 to suppress this error.Right, excellent @ahsonkhan thank you for solving this one, which was really a problem on my end. In summary, it previously worked (boxing a Span) due to the compiler being less stringent about the correctness of boxing a stack only type (in other words, my code was working but probably broken!)
Hi @ahsonkhan, I am doing so now (sorry slow internet!)
As long as your code isn’t using any of the new language features (like ref readonly/etc) then you don’t need to bump your language version to use System.Memory.
The compiler update is there to ensure correct usage of Span and catch programming errors at compile time instead of the program crashing at runtime.
~What version of the dotnet cli are you using to compile your project?~ Edit: Just noticed the version info in the initial repro. If you use the latest preview cli, your app should compile fine as well without having to explicitly install the compiler package.
You can see the version if you run the
dotnet --infocommandEssentially, with the latest cli, I cannot repro the issue using your initial steps. Let me know if that works for you.
System.Memory should already work with netstandard. We have netstandard libraries today in corefxlab that reference System.Memory and work. Example: https://github.com/dotnet/corefxlab/blob/master/src/System.Buffers.Primitives/System.Buffers.Primitives.csproj#L12
I don’t see any reason why it would. It should still work as expected. Please let me know if you are observing any issues.