From 3734a25644d2f7ea652303f4268de9e52e4a5a64 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Tue, 29 Sep 2015 22:27:53 +0000 Subject: [PATCH] compiler: Accept untyped integral values as string/array indices. When determining the type of an index for a string/array indexing expression, the gofrontend would disallow floating-point and complex values even if they were integral and throw an internal error. This patch changes gofrontend to use an integral type context when determining the types of a string/array index. Fixes golang/go#11545. Reviewed-on: https://go-review.googlesource.com/13796 From-SVN: r228270 --- gcc/go/gofrontend/MERGE | 2 +- gcc/go/gofrontend/expressions.cc | 35 +++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/gcc/go/gofrontend/MERGE b/gcc/go/gofrontend/MERGE index 448e5e82bad..eaa24dcbfef 100644 --- a/gcc/go/gofrontend/MERGE +++ b/gcc/go/gofrontend/MERGE @@ -1,4 +1,4 @@ -66c113f1af300ce27b99f18f792901d7327d6699 +f187e13b712824b08f2a8833033840cd52a3b95a The first line of this file holds the git revision number of the last merge done from the gofrontend repository. diff --git a/gcc/go/gofrontend/expressions.cc b/gcc/go/gofrontend/expressions.cc index 7ad271f109b..56bbf619bd8 100644 --- a/gcc/go/gofrontend/expressions.cc +++ b/gcc/go/gofrontend/expressions.cc @@ -9870,11 +9870,26 @@ void Array_index_expression::do_determine_type(const Type_context*) { this->array_->determine_type_no_context(); - this->start_->determine_type_no_context(); + + Type_context index_context(Type::lookup_integer_type("int"), false); + if (this->start_->is_constant()) + this->start_->determine_type(&index_context); + else + this->start_->determine_type_no_context(); if (this->end_ != NULL) - this->end_->determine_type_no_context(); + { + if (this->end_->is_constant()) + this->end_->determine_type(&index_context); + else + this->end_->determine_type_no_context(); + } if (this->cap_ != NULL) - this->cap_->determine_type_no_context(); + { + if (this->cap_->is_constant()) + this->cap_->determine_type(&index_context); + else + this->cap_->determine_type_no_context(); + } } // Check types of an array index. @@ -10415,9 +10430,19 @@ void String_index_expression::do_determine_type(const Type_context*) { this->string_->determine_type_no_context(); - this->start_->determine_type_no_context(); + + Type_context index_context(Type::lookup_integer_type("int"), false); + if (this->start_->is_constant()) + this->start_->determine_type(&index_context); + else + this->start_->determine_type_no_context(); if (this->end_ != NULL) - this->end_->determine_type_no_context(); + { + if (this->end_->is_constant()) + this->end_->determine_type(&index_context); + else + this->end_->determine_type_no_context(); + } } // Check types of a string index. -- 2.30.2