Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 368357

Summary: Changes needed for assignment compatibility concerning array types and array literals
Product: z_Archived Reporter: Paul Harmon <pharmon>
Component: EDTAssignee: Paul Harmon <pharmon>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: mheitz, svihovec
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Paul Harmon CLA 2012-01-11 10:09:27 EST
When assigning an array literal or array type variable(or passing to a function parm) to an array type, the following rules should be used:

1) If the type of the target and source are value types, the types need to match exactly. This is because the assignment would really require conversion to happen for each element of the array, however, assignments with array types are a simple "pointer" assignement.

2) If the type of the target and source are reference types, the assignment is valid if the element type of the source extends the target type (or matches exactly).

3) if the type of the target element is reference and the type of the source element is value, the arrays are not compatible

4) if the type of the target element is value and the type of the source element is reference, the arrays are not compatible.

5) literal arrays that contain strings ( ["abc"] ) and decimal ( [1.2, 3.23] ) have an element type that is reference.

6) special processing is needed for annotation fields to allow for a more relaxed validation. For example, an annotation field defined as int[] should allow [1,2,3] to be assigned to it.


NOTE: Validation rules should be the same if the source is an array literal or a variable. Currently, the rules are different.


Following is a test case:

externaltype type1 type javaobject
end

externaltype type2 extends type1 type javaobject
end 


library lib1
   function f1()

        i int[];
        si smallint[];
        f float[];
        d decimal[];
        n number[];
        t1 type1[];
        t2 type2[];


	i = [1,2,3];  //1 invalid, source is smallint[]
        si = [1,2,3]; //0 valid
	f = [1,2,3];  //1 not compatible
        f = i;  //1 not compatible
        n number = [1,2,3];  //1 not compatible
        n = i;   //1 not compatible
        i = n;   //1 not compatible
        n = d;  //0 compatible       
	
	t1 = [new type2];  //0
        t1 = t2; //0
   end
end
Comment 1 Paul Harmon CLA 2012-01-11 10:10:41 EST
The same validation rules need to apply to the use of the AS operator for literal types. For example, you should not be able to say 

[1,2,3] as int[]
Comment 2 Paul Harmon CLA 2012-02-07 09:11:14 EST
I have released some changes for this already. Rules 1-4 are now complete.

I have not tightned up validation for rule 5 yet. This will be done with validation is rewritten to be based on IRs
Comment 3 Brian Svihovec CLA 2012-03-09 15:50:33 EST
Additional Scenario to consider:

customer Customer{};
var1 Customer?[] = [cust]; // Invalid

Issues - 6653 - Customer?[] and Customer[] are not compatible types

The following is valid:

var1 Customer?[] = new Customer?[]{cust};
Comment 4 Matt Heitz CLA 2013-01-03 15:15:39 EST
Paul, please look at this one.  If the work is all done, close it.  If the work isn't done, either do it for 082 Final or change the target milestone to Future.
Comment 5 Paul Harmon CLA 2013-01-07 10:57:10 EST
The array assignment compatibility rules have been reworked because the strict rules defined by this bug proved to be too user unfriendly (caused breakages in many of our samples).

Because of this, we now allow assignment between 2 arrays as long as their element types are assignment compatible. If an assignment is done between 2 arrays whose element types do not exactly match, or whose element nullability flags do not match, a copy operation is performed.