After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 701227 - Type checking of generic parameters
Type checking of generic parameters
Status: RESOLVED DUPLICATE of bug 700142
Product: vala
Classification: Core
Component: general
0.20.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2013-05-29 21:39 UTC by Eric Gregory
Modified: 2013-05-29 21:42 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Full sample code (566 bytes, text/x-vala)
2013-05-29 21:39 UTC, Eric Gregory
Details

Description Eric Gregory 2013-05-29 21:39:57 UTC
Created attachment 245596 [details]
Full sample code

We've been hitting a strange bug recently where generic type parameters aren't always checked, which can lead to all kinds of nasty runtime issues.

Let's say we have two classes:

> class TypeA {
>     public string foo = "foo";
> }
> 
> class TypeB {
> }

and the following method:
> void print_collection(Gee.Collection<TypeA> list) {
>     foreach(TypeA a in list)
>         stdout.printf("%s\n", a.foo);
> }

We set up a list of TypeBs:

> Gee.List<TypeB> list = new Gee.ArrayList<TypeB>();
> list.add(new TypeB());

Now we print them:

> print_collection(list);

This, of course, creates undefined behavior at runtime.  On my machine at least, I get the following output:

> (null)

I should mention that if I change both classes to use GObject, the runtime output is garbage characters.

Still with me?  Okay, now's the interesting part.

> void print_list(Gee.List<TypeA> list) {
>    foreach(TypeA a in list)
>        stdout.printf("%s\n", a.foo);
> }
> /* snip */
> print_list(list);

This generates the following error:
> error: Argument 1: Cannot convert from `Gee.List<TypeB>?' to `Gee.List<TypeA>'

Aside from the fact that the "list" variable isn't nullable, this error is more or less what I'd expect here.  It's worth noting that List and Collection are both interfaces, so it seems strange that they'd be treated differently here.

I've confirmed that his happens with both Gee 0.6 and 0.8, and with Vala 0.18 and 0.20.
Comment 1 Maciej (Matthew) Piechotka 2013-05-29 21:42:06 UTC
Thanks for the bug report. This particular bug has already been reported into our bug tracking system, but please feel free to report any further bugs you find.

*** This bug has been marked as a duplicate of bug 700142 ***