Improper Generics type checking in class inheritance
@ban
Submitted by Colomban Wendling Link to original bug (#707635)
Description
If a class inherits from a generic class and specifies its type, that type isn't properly validated:
abstract class TestA`<T>`
{
/* C signature will be:
* gpointer stuff(GError** error) */
protected abstract T stuff() throws Error;
public T actually_do_stuff() throws Error
{
/* calls:
* CLASS(this)->stuff(&error)
* segfaults with ClassB that expects 2 args */
return stuff();
}
}
class TestB : TestA<int[]>
{
/* C signature will be:
* int* stuff(int* length, GError** error) */
protected override int[] stuff() throws Error
{
return new int[42];
}
}
int main()
{
var b = new TestB();
b.actually_do_stuff();
return 0;
}
Interestingly changing class TestB : TestA<int[]>
to class TestB : TestA<Object[]>
do not trigger any compile-type error either -- although TestB's stuff()
implementation still has type int[]
.
However, generic class instantiation would generate a compiler error:
class TestA`<T>`
{
public T stuff(T x)
{
return x;
}
}
int main ()
{
var a = new TestA<int[]>();
int[] x = {1, 2, 3};
a.stuff(x);
return 0;
}
Which generates the errors (duplicated not particularly helpful since one can't add any ?
to make this work, but at least it fails):
/tmp/bug.vala:12.23-12.27: error: `int[]' is not a supported generic type argument, use `?' to box value types
var a = new TestA<int[]>();
^^^^^
/tmp/bug.vala:12.23-12.27: error: `int[]' is not a supported generic type argument, use `?' to box value types
var a = new TestA<int[]>();
^^^^^
Compilation failed: 2 error(s), 0 warning(s)
Tested with 0.20.1 and current Git (0.21.1.37-49b8).
Version: 0.21.x