Skip to content

Commit

Permalink
compiler: handle int-to-string conversion with large integer constant
Browse files Browse the repository at this point in the history
    
    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.
    
    Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/179777


git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@271821 138bc75d-0d04-0410-961f-82ee72b054a4
  • Loading branch information
ian committed May 31, 2019
1 parent 6e3f3e4 commit 9ee9df2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gcc/go/gofrontend/MERGE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
8402f6ac021ba20163ab4fcdb10ab7bb642de6dc
e521123b23494148d534755e2f3d7806b42c96ad

The first line of this file holds the git revision number of the last
merge done from the gofrontend repository.
15 changes: 12 additions & 3 deletions gcc/go/gofrontend/expressions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3842,11 +3842,20 @@ Type_conversion_expression::do_get_backend(Translate_context* context)
mpz_t intval;
Numeric_constant nc;
if (this->expr_->numeric_constant_value(&nc)
&& nc.to_int(&intval)
&& mpz_fits_ushort_p(intval))
&& nc.to_int(&intval))
{
std::string s;
Lex::append_char(mpz_get_ui(intval), true, &s, loc);
unsigned int x;
if (mpz_fits_uint_p(intval))
x = mpz_get_ui(intval);
else
{
char* s = mpz_get_str(NULL, 16, intval);
go_warning_at(loc, 0,
"unicode code point 0x%s out of range in string", s);
x = 0xfffd;
}
Lex::append_char(x, true, &s, loc);
mpz_clear(intval);
Expression* se = Expression::make_string(s, loc);
return se->get_backend(context);
Expand Down

0 comments on commit 9ee9df2

Please sign in to comment.