]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
compiler: Permit converting between string and named []byte/[]rune.
authorIan Lance Taylor <ian@gcc.gnu.org>
Tue, 13 Dec 2011 18:09:56 +0000 (18:09 +0000)
committerIan Lance Taylor <ian@gcc.gnu.org>
Tue, 13 Dec 2011 18:09:56 +0000 (18:09 +0000)
From-SVN: r182291

gcc/go/gofrontend/expressions.cc
gcc/go/gofrontend/types.cc
gcc/testsuite/go.test/test/convlit.go
gcc/testsuite/go.test/test/named1.go

index 18858d519a08ee0b112a8ffdd92b425bfff0f001..ab6f4feeaf3da45cddecad3aed17c2e72c90c016 100644 (file)
@@ -3322,7 +3322,7 @@ Type_conversion_expression::do_lower(Gogo*, Named_object*,
       mpfr_clear(imag);
     }
 
-  if (type->is_slice_type() && type->named_type() == NULL)
+  if (type->is_slice_type())
     {
       Type* element_type = type->array_type()->element_type()->forwarded();
       bool is_byte = element_type == Type::lookup_integer_type("uint8");
@@ -3621,20 +3621,11 @@ Type_conversion_expression::do_get_tree(Translate_context* context)
                               integer_type_node,
                               fold_convert(integer_type_node, expr_tree));
     }
-  else if (type->is_string_type()
-          && (expr_type->array_type() != NULL
-              || (expr_type->points_to() != NULL
-                  && expr_type->points_to()->array_type() != NULL)))
+  else if (type->is_string_type() && expr_type->is_slice_type())
     {
-      Type* t = expr_type;
-      if (t->points_to() != NULL)
-       {
-         t = t->points_to();
-         expr_tree = build_fold_indirect_ref(expr_tree);
-       }
       if (!DECL_P(expr_tree))
        expr_tree = save_expr(expr_tree);
-      Array_type* a = t->array_type();
+      Array_type* a = expr_type->array_type();
       Type* e = a->element_type()->forwarded();
       go_assert(e->integer_type() != NULL);
       tree valptr = fold_convert(const_ptr_type_node,
index fed83b31b9a7dd15d6279a747df748cd5052a3b0..432a647221764d63f5fbbcc726faf97c7b644ac5 100644 (file)
@@ -662,7 +662,7 @@ Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
     {
       if (rhs->integer_type() != NULL)
        return true;
-      if (rhs->is_slice_type() && rhs->named_type() == NULL)
+      if (rhs->is_slice_type())
        {
          const Type* e = rhs->array_type()->element_type()->forwarded();
          if (e->integer_type() != NULL
@@ -673,9 +673,7 @@ Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
     }
 
   // A string may be converted to []byte or []int.
-  if (rhs->is_string_type()
-      && lhs->is_slice_type()
-      && lhs->named_type() == NULL)
+  if (rhs->is_string_type() && lhs->is_slice_type())
     {
       const Type* e = lhs->array_type()->element_type()->forwarded();
       if (e->integer_type() != NULL
index 90ac5490c84d2531fd8c97b78f2fe59f72f99128..1e82d1f2f561ebc60ac903066a4fb87795df8381 100644 (file)
@@ -36,7 +36,7 @@ var good3 int = 1e9
 var good4 float64 = 1e20
 
 // explicit conversion of string is okay
-var _ = []int("abc")
+var _ = []rune("abc")
 var _ = []byte("abc")
 
 // implicit is not
@@ -47,20 +47,20 @@ var _ []byte = "abc" // ERROR "cannot use|incompatible|invalid"
 type Tstring string
 
 var ss Tstring = "abc"
-var _ = []int(ss)
+var _ = []rune(ss)
 var _ = []byte(ss)
 
 // implicit is still not
-var _ []int = ss  // ERROR "cannot use|incompatible|invalid"
+var _ []rune = ss // ERROR "cannot use|incompatible|invalid"
 var _ []byte = ss // ERROR "cannot use|incompatible|invalid"
 
-// named slice is not
-type Tint []int
+// named slice is now ok
+type Trune []rune
 type Tbyte []byte
 
-var _ = Tint("abc")  // ERROR "convert|incompatible|invalid"
-var _ = Tbyte("abc") // ERROR "convert|incompatible|invalid"
+var _ = Trune("abc") // ok
+var _ = Tbyte("abc") // ok
 
 // implicit is still not
-var _ Tint = "abc"  // ERROR "cannot use|incompatible|invalid"
+var _ Trune = "abc" // ERROR "cannot use|incompatible|invalid"
 var _ Tbyte = "abc" // ERROR "cannot use|incompatible|invalid"
index 7e7aab9c1d815dd7b2f0a47a6e8bb8e487ac4ac3..499b77b9615e5fe92bed7bcd8509fb8a252df9fb 100644 (file)
@@ -41,7 +41,6 @@ func main() {
        asBool(i < j)  // ERROR "cannot use.*type bool.*as type Bool"
 
        _, b = m[2] // ERROR "cannot .* bool.*type Bool"
-       m[2] = 1, b // ERROR "cannot use.*type Bool.*as type bool"
 
        var inter interface{}
        _, b = inter.(Map) // ERROR "cannot .* bool.*type Bool"
@@ -55,8 +54,8 @@ func main() {
 
        _, bb := <-c
        asBool(bb) // ERROR "cannot use.*type bool.*as type Bool"
-       _, b = <-c     // ERROR "cannot .* bool.*type Bool"
+       _, b = <-c // ERROR "cannot .* bool.*type Bool"
        _ = b
 
-       asString(String(slice)) // ERROR "cannot .*type Slice.*type String"
+       asString(String(slice)) // ok
 }