[committed,66/88] gccrs: Add variadic argument type checking

Message ID 20230405140411.3016563-67-arthur.cohen@embecosm.com
State Accepted
Headers
Series [committed,01/88] gccrs: fatal_error_flag: Fix typo in error message |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Arthur Cohen April 5, 2023, 2:03 p.m. UTC
  From: Owen Avery <powerboat9.gamer@gmail.com>

gcc/rust/ChangeLog:

	* typecheck/rust-tyty-call.cc
	(TypeCheckCallExpr::visit): Add variadic argument type checking.
	(TypeCheckCallExpr::visit): Fix comment spelling ("varadic").

gcc/testsuite/ChangeLog:

	* rust/execute/torture/overflow1.rs: Fix test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
---
 gcc/rust/typecheck/rust-tyty-call.cc          | 56 ++++++++++++++++++-
 .../rust/execute/torture/overflow1.rs         |  2 +-
 2 files changed, 56 insertions(+), 2 deletions(-)
  

Patch

diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc
index 67e2866cbce..f0846ae25ec 100644
--- a/gcc/rust/typecheck/rust-tyty-call.cc
+++ b/gcc/rust/typecheck/rust-tyty-call.cc
@@ -122,7 +122,7 @@  TypeCheckCallExpr::visit (FnType &type)
 	  return;
 	}
 
-      // it might be a varadic function
+      // it might be a variadic function
       if (i < type.num_params ())
 	{
 	  auto fnparam = type.param_at (i);
@@ -143,6 +143,60 @@  TypeCheckCallExpr::visit (FnType &type)
 	      return;
 	    }
 	}
+      else
+	{
+	  switch (argument_expr_tyty->get_kind ())
+	    {
+	    case TyTy::TypeKind::ERROR:
+	      return;
+	      case TyTy::TypeKind::INT: {
+		auto &int_ty
+		  = static_cast<TyTy::IntType &> (*argument_expr_tyty);
+		if ((int_ty.get_int_kind () == TyTy::IntType::IntKind::I8)
+		    || (int_ty.get_int_kind () == TyTy::IntType::IntKind::I16))
+		  {
+		    rust_error_at (arg_locus,
+				   "expected %<c_int%> variadic argument");
+		    return;
+		  }
+		break;
+	      }
+	      case TyTy::TypeKind::UINT: {
+		auto &uint_ty
+		  = static_cast<TyTy::UintType &> (*argument_expr_tyty);
+		if ((uint_ty.get_uint_kind () == TyTy::UintType::UintKind::U8)
+		    || (uint_ty.get_uint_kind ()
+			== TyTy::UintType::UintKind::U16))
+		  {
+		    rust_error_at (arg_locus,
+				   "expected %<c_uint%> variadic argument");
+		    return;
+		  }
+		break;
+	      }
+	      case TyTy::TypeKind::FLOAT: {
+		if (static_cast<TyTy::FloatType &> (*argument_expr_tyty)
+		      .get_float_kind ()
+		    == TyTy::FloatType::FloatKind::F32)
+		  {
+		    rust_error_at (arg_locus,
+				   "expected %<c_double%> variadic argument");
+		    return;
+		  }
+		break;
+	      }
+	    case TyTy::TypeKind::BOOL:
+	      rust_error_at (arg_locus, "expected %<c_int%> variadic argument");
+	      return;
+	    case TyTy::TypeKind::FNDEF:
+	      rust_error_at (arg_locus,
+			     "unexpected function definition type as variadic "
+			     "argument - cast to function pointer");
+	      return;
+	    default:
+	      break;
+	    }
+	}
 
       i++;
     }
diff --git a/gcc/testsuite/rust/execute/torture/overflow1.rs b/gcc/testsuite/rust/execute/torture/overflow1.rs
index 57a0824ce0a..c4ccab1a133 100644
--- a/gcc/testsuite/rust/execute/torture/overflow1.rs
+++ b/gcc/testsuite/rust/execute/torture/overflow1.rs
@@ -16,5 +16,5 @@  fn main() {
     // { dg-final { scan-tree-dump ADD_OVERFLOW original } }
     let c = a + b;
 
-    unsafe { printf("%d\n\0" as *const str as *const i8, c) }
+    unsafe { printf("%d\n\0" as *const str as *const i8, c as i32) }
 }