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] Support generics in check for valid types in arithmetic expressions
Date: Fri, 15 Jul 2022 10:58:21 +0000 (GMT)	[thread overview]
Message-ID: <20220715105821.345EC385735C@sourceware.org> (raw)

https://gcc.gnu.org/g:95aa351e65723ce1ff9aa568523b1d5173acacf4

commit 95aa351e65723ce1ff9aa568523b1d5173acacf4
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Fri Jul 15 10:21:11 2022 +0100

    Support generics in check for valid types in arithmetic expressions
    
    When we check for valid types we need to be sure to call destructure which
    will extract out any generics so that we are checking the actual concrete
    type that is being used here.
    
    Fixes #1383

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-expr.h | 14 +++++++++-----
 gcc/testsuite/rust/compile/issue-1383.rs      |  8 ++++++++
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h
index a5f0a849b91..df07cb3e920 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h
@@ -1060,9 +1060,11 @@ private:
 			 Location expr_locus);
 
   bool
-  validate_arithmetic_type (TyTy::BaseType *type,
+  validate_arithmetic_type (const TyTy::BaseType *tyty,
 			    HIR::ArithmeticOrLogicalExpr::ExprType expr_type)
   {
+    const TyTy::BaseType *type = tyty->destructure ();
+
     // https://doc.rust-lang.org/reference/expressions/operator-expr.html#arithmetic-and-logical-binary-operators
     // this will change later when traits are added
     switch (expr_type)
@@ -1078,10 +1080,10 @@ private:
 	       || (type->get_kind () == TyTy::TypeKind::USIZE)
 	       || (type->get_kind () == TyTy::TypeKind::ISIZE)
 	       || (type->get_kind () == TyTy::TypeKind::INFER
-		   && (((TyTy::InferType *) type)->get_infer_kind ()
+		   && (((const TyTy::InferType *) type)->get_infer_kind ()
 		       == TyTy::InferType::INTEGRAL))
 	       || (type->get_kind () == TyTy::TypeKind::INFER
-		   && (((TyTy::InferType *) type)->get_infer_kind ()
+		   && (((const TyTy::InferType *) type)->get_infer_kind ()
 		       == TyTy::InferType::FLOAT));
 
 	// integers or bools
@@ -1094,7 +1096,7 @@ private:
 	       || (type->get_kind () == TyTy::TypeKind::ISIZE)
 	       || (type->get_kind () == TyTy::TypeKind::BOOL)
 	       || (type->get_kind () == TyTy::TypeKind::INFER
-		   && (((TyTy::InferType *) type)->get_infer_kind ()
+		   && (((const TyTy::InferType *) type)->get_infer_kind ()
 		       == TyTy::InferType::INTEGRAL));
 
 	// integers only
@@ -1105,10 +1107,12 @@ private:
 	       || (type->get_kind () == TyTy::TypeKind::USIZE)
 	       || (type->get_kind () == TyTy::TypeKind::ISIZE)
 	       || (type->get_kind () == TyTy::TypeKind::INFER
-		   && (((TyTy::InferType *) type)->get_infer_kind ()
+		   && (((const TyTy::InferType *) type)->get_infer_kind ()
 		       == TyTy::InferType::INTEGRAL));
       }
+
     gcc_unreachable ();
+    return false;
   }
 
   /* The return value of TypeCheckExpr::Resolve */
diff --git a/gcc/testsuite/rust/compile/issue-1383.rs b/gcc/testsuite/rust/compile/issue-1383.rs
new file mode 100644
index 00000000000..cca12e8fc71
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-1383.rs
@@ -0,0 +1,8 @@
+pub fn generic_function<X>(a: X) -> X {
+    a
+}
+
+fn main() -> i32 {
+    let a = generic_function(123);
+    a - 123
+}


                 reply	other threads:[~2022-07-15 10:58 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=20220715105821.345EC385735C@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).