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# (show other bugs)
Version: unspecified
Hardware: PC All
: --- normal
Target Milestone: ---
Assignee: Marek Safar
: 51163 (view as bug list)
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)

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


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!

Note You need to log in before you can comment on or make changes to this bug.