Skip to content

Commit fbadca0

Browse files
compiler: error for duplicate bool map keys
For golang/go#35945 Fixes golang/go#28104 Change-Id: I6d0f3188e99d99e92dabab68949d0d0d24841388 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/403954 Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Than McIntosh <[email protected]>
1 parent 70ca85f commit fbadca0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

go/expressions.cc

+16
Original file line numberDiff line numberDiff line change
@@ -17266,6 +17266,8 @@ Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
1726617266
Location location = this->location();
1726717267
Unordered_map(unsigned int, std::vector<Expression*>) st;
1726817268
Unordered_map(unsigned int, std::vector<Expression*>) nt;
17269+
bool saw_false = false;
17270+
bool saw_true = false;
1726917271
if (this->vals_ != NULL)
1727017272
{
1727117273
if (!this->has_keys_)
@@ -17300,6 +17302,7 @@ Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
1730017302
continue;
1730117303
std::string sval;
1730217304
Numeric_constant nval;
17305+
bool bval;
1730317306
if ((*p)->string_constant_value(&sval)) // Check string keys.
1730417307
{
1730517308
unsigned int h = Gogo::hash_string(sval, 0);
@@ -17373,6 +17376,19 @@ Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
1737317376
mit->second.push_back(*p);
1737417377
}
1737517378
}
17379+
else if ((*p)->boolean_constant_value(&bval))
17380+
{
17381+
if ((bval && saw_true) || (!bval && saw_false))
17382+
{
17383+
go_error_at((*p)->location(),
17384+
"duplicate key in map literal");
17385+
return Expression::make_error(location);
17386+
}
17387+
if (bval)
17388+
saw_true = true;
17389+
else
17390+
saw_false = true;
17391+
}
1737617392
}
1737717393
}
1737817394

0 commit comments

Comments
 (0)