public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] Allow cast of integers to pointers
Date: Wed,  8 Jun 2022 12:46:00 +0000 (GMT)	[thread overview]
Message-ID: <20220608124600.8C422381D483@sourceware.org> (raw)

https://gcc.gnu.org/g:2e65c14bef940fa2f7dff4f407178af21488b76e

commit 2e65c14bef940fa2f7dff4f407178af21488b76e
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Tue May 10 17:51:05 2022 +0100

    Allow cast of integers to pointers
    
    This adds the cast rules of integer types and integer inference variables
    to pointers. The code-generation needed to remove the bad assertion that
    all integer literals were always going to be of type integer. This also
    needed a tweak to a bad port from the cp/constexpr.cc code which assumed
    that all integer_cst of pointer types would be a zero pointer which was
    used to detect cases of bad method pointers in CPP which we does not apply
    here.
    
    see gcc/cp/constexpr.cc:6564-6488
    
    Fixes #1226

Diff:
---
 gcc/rust/backend/rust-compile-expr.cc    |  1 -
 gcc/rust/backend/rust-constexpr.cc       | 12 +-----------
 gcc/rust/typecheck/rust-tyty-cast.h      | 11 +++++++++--
 gcc/testsuite/rust/compile/issue-1226.rs |  6 ++++++
 4 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index 1cf4e3d5499..0307df10f1f 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -903,7 +903,6 @@ CompileExpr::compile_integer_literal (const HIR::LiteralExpr &expr,
   const auto literal_value = expr.get_literal ();
 
   tree type = TyTyResolveCompile::compile (ctx, tyty);
-  rust_assert (TREE_CODE (type) == INTEGER_TYPE);
 
   mpz_t ival;
   if (mpz_init_set_str (ival, literal_value.as_string ().c_str (), 10) != 0)
diff --git a/gcc/rust/backend/rust-constexpr.cc b/gcc/rust/backend/rust-constexpr.cc
index 1b0515e959c..aee41e43c0a 100644
--- a/gcc/rust/backend/rust-constexpr.cc
+++ b/gcc/rust/backend/rust-constexpr.cc
@@ -59,17 +59,7 @@ ConstCtx::constexpr_expression (tree t)
     {
       if (TREE_OVERFLOW (t))
 	{
-	  rust_error_at (Location (loc), "overflow in constant expression");
-	  return t;
-	}
-
-      if (TREE_CODE (t) == INTEGER_CST && TYPE_PTR_P (TREE_TYPE (t))
-	  && !integer_zerop (t))
-	{
-	  // FIXME check does this actually work to print out tree types
-	  rust_error_at (Location (loc),
-			 "value %qE of type %qT is not a constant expression",
-			 t, TREE_TYPE (t));
+	  error_at (loc, "overflow in constant expression");
 	  return t;
 	}
 
diff --git a/gcc/rust/typecheck/rust-tyty-cast.h b/gcc/rust/typecheck/rust-tyty-cast.h
index 0e0e7b0a65b..5ca68d07aa2 100644
--- a/gcc/rust/typecheck/rust-tyty-cast.h
+++ b/gcc/rust/typecheck/rust-tyty-cast.h
@@ -588,8 +588,11 @@ public:
 
   void visit (PointerType &type) override
   {
-    bool is_valid
-      = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
+    bool is_general_infer_var
+      = base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
+    bool is_integral_infer_var
+      = base->get_infer_kind () == TyTy::InferType::InferTypeKind::INTEGRAL;
+    bool is_valid = is_general_infer_var || is_integral_infer_var;
     if (is_valid)
       {
 	resolved = type.clone ();
@@ -939,6 +942,8 @@ public:
 
   void visit (ISizeType &type) override { resolved = type.clone (); }
 
+  void visit (PointerType &type) override { resolved = type.clone (); }
+
 private:
   BaseType *get_base () override { return base; }
 
@@ -975,6 +980,8 @@ public:
 
   void visit (ISizeType &type) override { resolved = type.clone (); }
 
+  void visit (PointerType &type) override { resolved = type.clone (); }
+
   void visit (CharType &type) override
   {
     // error[E0604]: only `u8` can be cast as `char`, not `i32`
diff --git a/gcc/testsuite/rust/compile/issue-1226.rs b/gcc/testsuite/rust/compile/issue-1226.rs
new file mode 100644
index 00000000000..f5f9e5ff08d
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-1226.rs
@@ -0,0 +1,6 @@
+// { dg-additional-options "-w" }
+const TEST: *mut u8 = 123 as *mut u8;
+
+fn test() {
+    let a = TEST;
+}


                 reply	other threads:[~2022-06-08 12:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220608124600.8C422381D483@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).