Bug 51166

Summary: mcs converts new T[0] into Array.Empty<T>() which is not conform C# spec
Product: [Mono] Compilers Reporter: Robert van der Boon <rjvdboon>
Component: C#Assignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+mono, neale
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: All   
Tags: 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!