60

For some reason, even so much as typing this into a C# file in Visual Studio is enough to cause it to instantly crash. Why?

unsafe struct node {
    node*[] child;
}

It seems to occur when the IDE would start coloring keywords and the like.

Trying it in TIO spits out csc.exe exited with code 1 which isn't very helpful.

While array declarations in C# are different than in C/C++, the above seems like it should be perfectly valid. Why isn't it, and why does it crash Visual Studio?

My Visual Studio version is 14.0.23107.

Orion
  • 1,157
  • 9
  • 18
  • Confirmed, I pasted that code into VS2015 and it immediately froze, then crashed 20 seconds later. If you remove the `[]`, it can be safely copied into a VS code window. However, the moment you type the `[]`, VS crashes. –  Aug 07 '18 at 17:51
  • 1
    VS 2012 produces a compile error: `Cannot take the address of, get the size of, or declare a pointer to a managed type` – LarsTech Aug 07 '18 at 17:54
  • 3
    VS2017 instantly, immediately closes – Camilo Terevinto Aug 07 '18 at 17:55
  • This seems to crash the latest version of Roslyn, too (including the master branch). I would file an issue at https://github.com/dotnet/roslyn/issues. – vcsjones Aug 07 '18 at 17:55
  • Looks like a bug to me. Even totally erroneous code shouldn't cause a crash. It also crashes VS 2017 :( – Cetin Basoz Aug 07 '18 at 17:56
  • 2
    Ditto LINQPad. StackoverflowException. – Crowcoder Aug 07 '18 at 18:00
  • 8
    I found an existing bug: https://github.com/dotnet/roslyn/issues/24978 –  Aug 07 '18 at 18:22
  • Because of bugs? Not other answer possible - head over to github for roslyn and file a bug report. – TomTom Aug 07 '18 at 17:59
  • @Amy you can make it a good answer if you provide a workaround. – Jean-François Fabre Aug 07 '18 at 19:40
  • @Jean-FrançoisFabre I don't know of a workaround aside from "don't do that for now". I have a half-written answer just saying its a bug, citing the bug, and providing a few specifics from the bug page, but no workaround. –  Aug 07 '18 at 19:42
  • yeah, workaround is "use notepad++" for such constructs. I know other IDEs where I had to do this. So this is a valid workaround after all :) sometimes the parsing is okay when the code is complete, it crashes when typing it – Jean-François Fabre Aug 07 '18 at 19:44
  • 1
    FWIW your code is *not valid*, as you can't take a pointer to a struct that contains a managed object in C#. And an array is a managed object. So even if VS didn't crash, that code wouldn't compile. – Lucas Trzesniewski Aug 08 '18 at 10:20
  • Just opened another bug, completely unrelated to this, where Visual Studio "instantly, immediately" closes :) https://developercommunity.visualstudio.com/content/problem/374230/bad-using-statement-and-extern-alias-crashes-visua.html – csrowell Nov 02 '18 at 13:46

1 Answers1

50

This is a known bug in Roslyn. This bug will affect any version of Visual Studio that uses Roslyn.

If I am interpreting VersionOf.net correctly, the first version of Visual Studio with Roslyn built-in is 2015. Before then, I think it was available only as an extension. So, Visual Studio 2013 and prior should be unaffected.

It's due to be fixed in the milestone 16 release. At this time, that release is not scheduled.

Because this is a bug in Roslyn, you can "get around" it by editing and compiling the code containing the unsafe struct in an older version of Visual Studio, one that predates Roslyn. Visual Studio 2012 should work. You can then use the resultant .DLL in your current software.

An unverified fix is available if you build Roslyn yourself from this branch. The fix was made in this commit.

  • 4
    according to this, you're qualifying for step number 3 or 4: https://meta.stackexchange.com/a/132704/382678 between awesome and legendary. – Jean-François Fabre Aug 07 '18 at 19:49
  • nice move answering this properly: the question is probably in the 10k tools best questions of today. So here comes the 20k ;) – Jean-François Fabre Aug 07 '18 at 19:50
  • @Jean-FrançoisFabre What is "10k tools best questions of today"? –  Aug 07 '18 at 21:05
  • https://stackoverflow.com/tools. This question is indeed #1. seems that I was right :) – Jean-François Fabre Aug 07 '18 at 21:06
  • As is evident from comments (to the question, not here) by `LarsTech` and `Lucas Trzesniewski`, the code as it stands is not valid, because if the struct contains a reference type you cannot take a pointer to that struct, and an array is a reference type. _So the part of the answer about compiling with an older compiler and porting the DLL may be irrelevant._ Unless somebody knows of a case that is affected by this bug but will actually compile with Visual Studio 2012 (or another non-Roslyn compiler)? By the way, I _think_ the Mono compiler has other issues (allows too much with pointers). – Jeppe Stig Nielsen Jul 10 '19 at 15:21