Bug 51166 - mcs converts new T[0] into Array.Empty<T>() which is not conform C# spec
Summary: mcs converts new T[0] into Array.Empty<T>() which is not conform C# spec
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC All
: --- normal
Target Milestone: ---
Assignee: Marek Safar
: 51163 ()
Depends on:
Reported: 2017-01-02 12:05 UTC by Robert van der Boon
Modified: 2017-01-02 20:52 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Robert van der Boon 2017-01-02 12:05:23 UTC
When compiling C# sources with mcs it transforms new int[0] into Array.Empty<int>(). This causes problems in code that rightly expects that new-ing a class will return a unique instance.
From the C# spec:
  Instances of classes are created using the new operator,
  which allocates memory for a new instance,
  invokes a constructor to initialize the instance, and
  returns a reference to the instance.

As an array is a class it must conform to this spec, and I don't see anything in the spec file that allows the current transformation.
I also haven't seen any version of csc.exe apply this conversion either (including the Roslyn compiler included in mono trunk).
Comment 1 Robert van der Boon 2017-01-02 12:11:48 UTC
Additionally: I'm currently relying on the fact that doing "new something" returns references for which object.ReferenceEquals() returns false. This transformation invalidates that assumption.
Comment 2 Marek Safar 2017-01-02 18:12:17 UTC
*** Bug 51163 has been marked as a duplicate of this bug. ***
Comment 3 Marek Safar 2017-01-02 18:22:43 UTC
Fixed in master and Mono 4.8
Comment 4 Robert van der Boon 2017-01-02 20:52:09 UTC
That was quick! Thanks!