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 608250 - Vala should use intermediate variables to initialize static structs
Vala should use intermediate variables to initialize static structs
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Code Generator
0.7.x
Other All
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2010-01-27 13:56 UTC by Sandino Flores-Moreno
Modified: 2010-02-03 19:06 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Use intermediate variables to initialize static structs (1.80 KB, patch)
2010-01-28 00:01 UTC, Marc-Andre Lureau
none Details | Review

Description Sandino Flores-Moreno 2010-01-27 13:56:04 UTC
Consider this test case:

[test.vala]

class TestParser: Object {
    
    static MarkupParser parser = {start, end, text, null, null};

    void start (MarkupParseContext context, string name,
                string[] attr_names, string[] attr_values) throws MarkupError
    { }

    void end (MarkupParseContext context, string name) throws MarkupError
    { }

    void text (MarkupParseContext context,
               string text, size_t text_len) throws MarkupError
    { }
}

[/test.vala]

For the class init, vala is currently generating:

[class_init]
static void test_parser_class_init (TestParserClass * klass) {
  ...
  test_parser_parser = {
    _test_parser_start_gmarkup_parser_start_element_func,
    _test_parser_end_gmarkup_parser_end_element_func,
    _test_parser_text_gmarkup_parser_text_func, NULL, NULL
  };
}
[/class_init]

Which does not compile:
  test.c: In function ‘test_parser_class_init’:
  test.c:104: error: expected expression before ‘{’ token

Instead, vala should generate:

[class_init]
static void test_parser_class_init (TestParserClass * klass) {
  ...
  GMarkupParser tmp0 = {
  _test_parser_start_gmarkup_parser_start_element_func,
  _test_parser_end_gmarkup_parser_end_element_func,
  _test_parser_text_gmarkup_parser_text_func, NULL, NULL
  };
  test_parser_parser = tmp0;
}
[/class_init]

Which compiles fine.

Actually, vala does exactly that to initialize instance variables.
So, the same logic should apply to static variables too.

And that explains why:
  http://live.gnome.org/Vala/MarkupSample
Compiles with:
  const MarkupParser parser
But not with:
  static MarkupParser parser
Comment 1 Marc-Andre Lureau 2010-01-28 00:01:22 UTC
Created attachment 152458 [details] [review]
Use intermediate variables to initialize static structs

Fixes bug 608250.
Comment 2 Sandino Flores-Moreno 2010-01-28 00:37:33 UTC
I verified with current git, and it works fine.

Could you commit it please?
Comment 3 Jürg Billeter 2010-02-03 19:06:03 UTC
commit 0958339872e8510a34f85edbc5a60ee21d46d840
Author: Marc-André Lureau <marcandre.lureau@gmail.com>
Date:   Thu Jan 28 01:01:04 2010 +0100

    Use intermediate variables to initialize static structs
    
    Fixes bug 608250.