Skip to content

Commit e521123

Browse files
cherrymuiianlancetaylor
authored andcommitted
compiler: handle int-to-string conversion with large integer constant
Currently, Type_conversion_expression::do_is_constant thinks the int-to-string conversion is constant if the integer operand is constant, but Type_conversion_expression::do_get_backend actually generates a call to runtime.intstring if the integer does not fit in a "ushort", which makes it not suitable in constant context, such as static initializer. This CL makes it handle all constant integer input as constant, generating constant string. Fixes golang/go#32347. Change-Id: I64502e2fec034d5081be395ee7636791e5601215 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/179777 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 8402f6a commit e521123

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

go/expressions.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3842,11 +3842,20 @@ Type_conversion_expression::do_get_backend(Translate_context* context)
38423842
mpz_t intval;
38433843
Numeric_constant nc;
38443844
if (this->expr_->numeric_constant_value(&nc)
3845-
&& nc.to_int(&intval)
3846-
&& mpz_fits_ushort_p(intval))
3845+
&& nc.to_int(&intval))
38473846
{
38483847
std::string s;
3849-
Lex::append_char(mpz_get_ui(intval), true, &s, loc);
3848+
unsigned int x;
3849+
if (mpz_fits_uint_p(intval))
3850+
x = mpz_get_ui(intval);
3851+
else
3852+
{
3853+
char* s = mpz_get_str(NULL, 16, intval);
3854+
go_warning_at(loc, 0,
3855+
"unicode code point 0x%s out of range in string", s);
3856+
x = 0xfffd;
3857+
}
3858+
Lex::append_char(x, true, &s, loc);
38503859
mpz_clear(intval);
38513860
Expression* se = Expression::make_string(s, loc);
38523861
return se->get_backend(context);

0 commit comments

Comments
 (0)