* Go patch committed: Don't initialize zero-sized fields in constructors
@ 2015-01-06 18:57 Ian Lance Taylor
0 siblings, 0 replies; only message in thread
From: Ian Lance Taylor @ 2015-01-06 18:57 UTC (permalink / raw)
To: gcc-patches, gofrontend-dev
[-- Attachment #1: Type: text/plain, Size: 574 bytes --]
This patch by Chris Manghane skips initializing zero-sized fields in
constructor expressions. Initializing them tends to lead into GIMPLE
errors when using map composite literals. Note that zero-sized fields
are useful in maps, but of course the compiler should not crash. This
is http://golang.org/issue/9406 . Bootstrapped and ran Go testsuite
on x86_64-unknown-linux-gnu. Committed to mainline.
Ian
2015-01-06 Chris Manghane <cmang@google.com>
* go-gcc.cc (constructor_expression): Don't initialize zero-sized
fields, just evaluate the values for side effects.
[-- Attachment #2: foo.txt --]
[-- Type: text/plain, Size: 1466 bytes --]
Index: go-gcc.cc
===================================================================
--- go-gcc.cc (revision 219261)
+++ go-gcc.cc (working copy)
@@ -1656,6 +1656,7 @@ Gcc_backend::constructor_expression(Btyp
vec<constructor_elt, va_gc> *init;
vec_alloc(init, vals.size());
+ tree sink = NULL_TREE;
bool is_constant = true;
tree field = TYPE_FIELDS(type_tree);
for (std::vector<Bexpression*>::const_iterator p = vals.begin();
@@ -1669,6 +1670,17 @@ Gcc_backend::constructor_expression(Btyp
|| TREE_TYPE(val) == error_mark_node)
return this->error_expression();
+ if (int_size_in_bytes(TREE_TYPE(field)) == 0)
+ {
+ // GIMPLE cannot represent indices of zero-sized types so
+ // trying to construct a map with zero-sized keys might lead
+ // to errors. Instead, we evaluate each expression that
+ // would have been added as a map element for its
+ // side-effects and construct an empty map.
+ append_to_statement_list(val, &sink);
+ continue;
+ }
+
constructor_elt empty = {NULL, NULL};
constructor_elt* elt = init->quick_push(empty);
elt->index = field;
@@ -1681,7 +1693,9 @@ Gcc_backend::constructor_expression(Btyp
tree ret = build_constructor(type_tree, init);
if (is_constant)
TREE_CONSTANT(ret) = 1;
-
+ if (sink != NULL_TREE)
+ ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
+ type_tree, sink, ret);
return this->make_expression(ret);
}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2015-01-06 18:57 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-06 18:57 Go patch committed: Don't initialize zero-sized fields in constructors Ian Lance Taylor
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).