From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id E8BE7385783E; Tue, 16 Jan 2024 18:14:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E8BE7385783E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705428846; bh=di/FMmasj76MomXkN6z9eV0SJjAqoQfxZNm65gVk/qQ=; h=From:To:Subject:Date:From; b=F7iDAjapx/ht9Wfl60ItPpgF2cg5Nw3ZOaweKXhjtm8ZftiDPw7HpyK1DnBQM2uxr msKQ9mmsVSesxWjnCQHs4zPMUz/GQHfxxmt2OJO7DSOizL2xeiM408n3Cd8oVdO4Uv uPDSoBrmi2p7yEAFqPfxYXHlRj74X2Km/CoUZL7c= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-8020] gccrs: [E0617] attempt for invalid type variable in variadic function X-Act-Checkin: gcc X-Git-Author: Muhammad Mahad X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 95703bb75efc3ca0300b78ab9d3f4d544faad4b6 X-Git-Newrev: 718b65a93d194a97a88f96780b97023ce9ed9378 Message-Id: <20240116181406.E8BE7385783E@sourceware.org> Date: Tue, 16 Jan 2024 18:14:06 +0000 (GMT) List-Id: https://gcc.gnu.org/g:718b65a93d194a97a88f96780b97023ce9ed9378 commit r14-8020-g718b65a93d194a97a88f96780b97023ce9ed9378 Author: Muhammad Mahad Date: Sun Sep 17 16:58:51 2023 +0500 gccrs: [E0617] attempt for invalid type variable in variadic function There are some certain rust types must be cast before passing them to a variadic function, because of arcane ABI rules dictated by the C standard. To fix the error, cast the value to the type specified by the error message. gcc/rust/ChangeLog: * typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): Added ErrorCode & more fixit hints. gcc/testsuite/ChangeLog: * rust/compile/variadic.rs: Added new checks. Signed-off-by: Muhammad Mahad Diff: --- gcc/rust/typecheck/rust-tyty-call.cc | 35 ++++++++++++++++++++++++---------- gcc/testsuite/rust/compile/variadic.rs | 8 +++++++- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc index 667d54c6d9e..31afe046a13 100644 --- a/gcc/rust/typecheck/rust-tyty-call.cc +++ b/gcc/rust/typecheck/rust-tyty-call.cc @@ -184,7 +184,10 @@ TypeCheckCallExpr::visit (FnType &type) if ((int_ty.get_int_kind () == TyTy::IntType::IntKind::I8) || (int_ty.get_int_kind () == TyTy::IntType::IntKind::I16)) { - rust_error_at (arg_locus, + rich_location richloc (line_table, arg_locus); + richloc.add_fixit_replace ( + "cast the value to c_int: as c_int"); + rust_error_at (richloc, ErrorCode::E0617, "expected % variadic argument"); return; } @@ -197,7 +200,10 @@ TypeCheckCallExpr::visit (FnType &type) || (uint_ty.get_uint_kind () == TyTy::UintType::UintKind::U16)) { - rust_error_at (arg_locus, + rich_location richloc (line_table, arg_locus); + richloc.add_fixit_replace ( + "cast the value to c_uint: as c_uint"); + rust_error_at (richloc, ErrorCode::E0617, "expected % variadic argument"); return; } @@ -208,19 +214,28 @@ TypeCheckCallExpr::visit (FnType &type) .get_float_kind () == TyTy::FloatType::FloatKind::F32) { - rust_error_at (arg_locus, + rich_location richloc (line_table, arg_locus); + richloc.add_fixit_replace ( + "cast the value to c_double: as c_double"); + rust_error_at (richloc, ErrorCode::E0617, "expected % variadic argument"); return; } break; } - case TyTy::TypeKind::BOOL: - rust_error_at (arg_locus, "expected % variadic argument"); - return; - case TyTy::TypeKind::FNDEF: - rust_error_at (arg_locus, - "unexpected function definition type as variadic " - "argument - cast to function pointer"); + case TyTy::TypeKind::BOOL: { + rich_location richloc (line_table, arg_locus); + richloc.add_fixit_replace ("cast the value to c_int: as c_int"); + rust_error_at (arg_locus, ErrorCode::E0617, + "expected % variadic argument"); + return; + } + case TyTy::TypeKind::FNDEF: { + rust_error_at ( + arg_locus, ErrorCode::E0617, + "unexpected function definition type as variadic " + "argument - cast to function pointer"); + } return; default: break; diff --git a/gcc/testsuite/rust/compile/variadic.rs b/gcc/testsuite/rust/compile/variadic.rs index 886341b088c..e970cd16a19 100644 --- a/gcc/testsuite/rust/compile/variadic.rs +++ b/gcc/testsuite/rust/compile/variadic.rs @@ -4,5 +4,11 @@ extern "C" { fn main() { // { dg-error "expected .c_int. variadic argument" "" { target *-*-* } .+1 } - printf("%d\n" as *const str as *const i8, 1i8); + printf("%d\n" as *const str as *const i8, 1i8); + + // { dg-error "expected .c_uint. variadic argument" "" { target *-*-* } .+1 } + printf("%d\n" as *const str as *const i8, 1u8); + + // { dg-error "expected .c_double. variadic argument" "" { target *-*-* } .+1 } + printf("%d\n" as *const str as *const i8, 1f32); }