Merge: Missing a unboxing when compiling a call to `new NativeArray`
authorJean Privat <jean@pryen.org>
Sat, 23 May 2015 01:01:18 +0000 (21:01 -0400)
committerJean Privat <jean@pryen.org>
Sat, 23 May 2015 01:01:18 +0000 (21:01 -0400)
In the following code,

~~~nit
var i
i = 4
var na = new NativeArray[Object](i)
~~~

the declaration type of `i` is `nullable Object`, so the C-type of `i` is `val*` (and not `int`).
Therefore, in the `new NativeArray`, it should be unboxed because the C signature expect a `int`.

Pull-Request: #1365
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>

src/compiler/global_compiler.nit
src/compiler/separate_compiler.nit
tests/sav/nitg-e/test_new_native.res
tests/sav/nitg-e/test_new_native_alt1.res
tests/sav/test_new_native.res
tests/test_new_native.nit

index ede1beb..b2cf479 100644 (file)
@@ -404,6 +404,7 @@ class GlobalCompilerVisitor
        do
                var ret_type = mmodule.native_array_type(elttype)
                ret_type = anchor(ret_type).as(MClassType)
+               length = autobox(length, compiler.mainmodule.int_type)
                return self.new_expr("NEW_{ret_type.c_name}({length})", ret_type)
        end
 
index 3e7fbcb..085a36d 100644 (file)
@@ -2032,6 +2032,7 @@ class SeparateCompilerVisitor
                self.require_declaration("NEW_{mtype.mclass.c_name}")
                assert mtype isa MGenericType
                var compiler = self.compiler
+               length = autobox(length, compiler.mainmodule.int_type)
                if mtype.need_anchor then
                        hardening_live_open_type(mtype)
                        link_unresolved_type(self.frame.mpropdef.mclassdef, mtype)
index 4fd1ad6..060a759 100644 (file)
@@ -5,3 +5,5 @@ NativeArray[Int]
 3
 1
 1,10,100
+1
+1
index 49ab177..c1ecc49 100644 (file)
@@ -31,3 +31,10 @@ print a.length
 print a[0]
 print a.to_a.join(",")
 
+var i
+i = 3
+a = new NativeArray[Int](i)
+i = 1
+a[i] = i
+print a[i]
+print a[1]