From patchwork Wed Jul 8 10:06:17 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: gerris.rs@gmail.com X-Patchwork-Id: 138724 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E1F194BA2E24 for ; Wed, 8 Jul 2026 10:07:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1F194BA2E24 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=ZEVko/rX X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by sourceware.org (Postfix) with ESMTPS id 6B2604BA2E2D for ; Wed, 8 Jul 2026 10:06:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6B2604BA2E2D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6B2604BA2E2D Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::52f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783505183; cv=none; b=cNiKEM7p4G8Dqx+MtaYM14SF+N931XVNenqYXonuJpfKSzwGWOVL69DvN7gse4AOw4AsEVmSu6fwazT4BaUkKcnUmj5Fi+K3aDWBrM0F1gQssLI/wxUoDPRKuHNBI83Ha4jyNjmn0nR8A7tUFFFAvFJTfsbQTG0Cnf/+Bq8fIjQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783505183; c=relaxed/simple; bh=9+QkKIFAOjK1wCfXleTfh2aWm05IcV5hr199aF4P8WI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=B5S6W2K5On1rl7nbE3vAhO/K4KCE21rX17JgnuV64Amp5aAEOnC9sHhYtEwRtolS6dG6WuMaffN3ta/VnN46Zn7dLHMDe2/B6DxtxC/T2vJ39C79Bw37LSdfYzaPqFj4aHZ56BUBsnyo+a4pDiCzXLRieiTd8RU19pOAt1LpTOI= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=ZEVko/rX DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6B2604BA2E2D Received: by mail-pg1-x52f.google.com with SMTP id 41be03b00d2f7-ca00f126b7eso268569a12.2 for ; Wed, 08 Jul 2026 03:06:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783505182; x=1784109982; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WhE1HOCkGMhnIXKTNqL4dRcXQWxLVFMFlSpyPCnun/c=; b=ZEVko/rXhv1gpv2JPDOYkz7sfwwvCmlZBcq/ym2hWxArvb7QMZAN4d+Tqkc8h9eCDD sAn9V8vUMagbHJLaK0D0no9JNdkUIm9VVmOwVxYsWXvzxbfmoPXcB9vkV0p7roi/uU9n nH4Pgx8zcNVB1XeRIonj5U6sDfLwboI6xNm1jklbmb4qU0dcwBycCs1SMbWfbaj7/kWW yUCiLeIbtvGIj5IbLqy7zBS/cIUYydlNWTACgGPG8pNjTAyI9QbwwlNRf4RVIBRCKWFc 5XzGldoXWVHTPoNyjor7pyOsBHVglS7Hg5HvrmbtklUc6s717Oy3zGlAh7W+nsv5X7nB yW0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783505182; x=1784109982; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=WhE1HOCkGMhnIXKTNqL4dRcXQWxLVFMFlSpyPCnun/c=; b=YJJ/R9ewlP2YflkuB0tSPC1TqqUqJSf/avxf7cGF0EEEjwEX/weDro9OM24W8LcxaP N2/YwSDwdZ6BX3yH6b7tdcDUl1N00O306WSoLsBtzQ64UNveB4KTWmfsZOx3xRKTp72+ f2dQOyyCLga0bTOGYgoCFavPanK05IQ9KtRN5B2DfKj89e3T8Pwx1Udbs0ag0bf1j6B2 ZXmLFOCjGqSIyt/37MUGWh9Z7mb/PXLYDwdiNYGajxOjL3X7V/PEMmCl7MyDVNE9dScK Sg6yzwLwcKfyErTDGYhqyI6vSl+z1HlIDXssO/xBejdzuaqBWokjps5ks5wNlDCAzS4R Wr/w== X-Gm-Message-State: AOJu0YyvvTe3yZLg+V0lUUXCbAshak0Y9mEe4/ydQTlzh1eIcv5Es2DS +3RmHwwZFhI0dJfdq1ZEbreBQCpuxwpz/hdG/rxdJFrojYZP5n2TM5GhTAGrcQ== X-Gm-Gg: AfdE7cmzfKt34Z0QjR64zSwSha8YDYt9A8a50Ar4FpqJa4Hpb7h8zZbsv34dIoBGpmI JDsxJmjmVh7SxAVdmg+BeCWTK7DLamCXudoePCJ2iFnT3Qb33yczQ8c6Q2Tg0tsjwFy9PYAmNQN tSDIJaWW9//ZD1Yt67em82niTPLtv2k+KjHB6/lPnk9R2eKbQfovDyfFfnqPOqtxrUAneGzoER+ dICMKKyRW+XjRVepdhszBZwnQR7ThewoR9bAdd1b7bypygyCox+tkLdTM0TLaa3+mFRcN4rMFwL T5LNE5u+PTDHObc92bHOx+3wEhmbYMyktw5N0pFfiHGYvR+RNCvn1JC6ZD9Xl4/nBcz7CksGg6u rP+SA07UuwtOil6aM0lVMLKcGSGXU1VYrsCji61Hc11aVzL5v5d8V1DMmt4q0wRhs6bXwOR77A9 GaI7V21/7Wbu/jGhPx/xdRZCz4jd2PXKfaYdRVfH2fVJOBYKA4s580pDWEZEIfB5Ii1O48lOcLw xn7ZAL3Er8= X-Received: by 2002:a05:6a20:6f04:b0:3b7:9c18:c681 with SMTP id adf61e73a8af0-3c0bce21d5bmr2457265637.9.1783505182178; Wed, 08 Jul 2026 03:06:22 -0700 (PDT) Received: from runnervm5mmn9.3kssg3t0abtefpjlvgkicfjm3d.dx.internal.cloudapp.net ([20.189.179.85]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-311950eb930sm3853100eec.8.2026.07.08.03.06.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jul 2026 03:06:21 -0700 (PDT) From: gerris.rs@gmail.com To: gcc-patches@gcc.gnu.org, gcc-rust@gcc.gnu.org Cc: pierre-emmanuel.patry@embecosm.com, arthur.cohen@embecosm.com, dkm+gccrs@kataplop.net, Lishin Subject: [gccrs COMMIT 2/2] gccrs: Emit drops for function parameters Date: Wed, 8 Jul 2026 10:06:17 +0000 Message-ID: <20260708100618.3141-2-gerris.rs@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260708100618.3141-1-gerris.rs@gmail.com> References: <20260708100618.3141-1-gerris.rs@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org From: Lishin Add function parameters to the function scope drop list after the function body block is created. This makes parameters drop after local variables on normal function exit. gcc/rust/ChangeLog: * backend/rust-compile-base.cc (HIRCompileBase::compile_function): Track droppable function parameters in the function scope. gcc/testsuite/ChangeLog: * rust/execute/drop-function-params.rs: New test. Signed-off-by: Lishin --- This change was merged into the gccrs repository and is posted here for upstream visibility and potential drive-by review, as requested by GCC release managers. Each commit email contains a link to its details on github from where you can find the Pull-Request and associated discussions. Commit on github: https://github.com/Rust-GCC/gccrs/commit/76397e896150e3bf04567fc8df45ef780df4e501 The commit has NOT been mentioned in any issue. The commit has been mentioned in the following pull-request(s): - https://github.com/Rust-GCC/gccrs/pull/4591 gcc/rust/backend/rust-compile-base.cc | 13 ++++- .../rust/execute/drop-function-params.rs | 52 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/rust/execute/drop-function-params.rs diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 4e618243b..04696787e 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -21,6 +21,7 @@ #include "rust-compile-stmt.h" #include "rust-compile-expr.h" #include "rust-compile-drop.h" +#include "rust-compile-drop-builder.h" #include "rust-compile-fnparam.h" #include "rust-compile-var-decl.h" #include "rust-compile-type.h" @@ -739,7 +740,7 @@ HIRCompileBase::compile_function_body (tree fndecl, // just add the stmt expression ctx->add_statement (return_value); - CompileDrop::emit_current_scope_drop_calls (ctx); + CompileDrop (ctx).emit_current_scope_drop_calls (); // now just return unit expression tree unit_expr = unit_expression (locus); @@ -845,6 +846,7 @@ HIRCompileBase::compile_function ( // setup the params TyTy::BaseType *tyret = fntype->get_return_type (); std::vector param_vars; + std::vector param_drop_candidates; if (self_param) { rust_assert (fntype->is_method ()); @@ -880,6 +882,11 @@ HIRCompileBase::compile_function ( const HIR::Pattern ¶m_pattern = referenced_param.get_param_name (); ctx->insert_var_decl (param_pattern.get_mappings ().get_hirid (), compiled_param_var); + + if (CompileDrop (ctx).type_has_drop_impl (param_tyty)) + param_drop_candidates.emplace_back ( + param_pattern.get_mappings ().get_hirid (), + param_pattern.get_locus ()); } if (!Backend::function_set_parameters (fndecl, param_vars)) @@ -893,6 +900,10 @@ HIRCompileBase::compile_function ( start_location, end_location); ctx->push_block (code_block); + DropBuilder drop_builder (*ctx); + for (auto &candidate : param_drop_candidates) + drop_builder.note_simple_drop_candidate (candidate.hirid, candidate.locus); + Bvariable *return_address = nullptr; tree return_type = TyTyResolveCompile::compile (ctx, tyret); diff --git a/gcc/testsuite/rust/execute/drop-function-params.rs b/gcc/testsuite/rust/execute/drop-function-params.rs new file mode 100644 index 000000000..c538922f5 --- /dev/null +++ b/gcc/testsuite/rust/execute/drop-function-params.rs @@ -0,0 +1,52 @@ +// { dg-output "l\r*\np\r*\nl\r*\np\r*\n" } +// { dg-additional-options "-w" } +#![feature(no_core)] +#![feature(lang_items)] +#![no_core] + +extern "C" { + fn printf(s: *const i8, ...); +} + +#[lang = "sized"] +pub trait Sized {} + +#[lang = "drop"] +pub trait Drop { + fn drop(&mut self); +} + +struct ParamDroppable; +struct LocalDroppable; + +impl Drop for ParamDroppable { + fn drop(&mut self) { + let msg = "p\n\0" as *const str as *const i8; + unsafe { + printf(msg); + } + } +} + +impl Drop for LocalDroppable { + fn drop(&mut self) { + let msg = "l\n\0" as *const str as *const i8; + unsafe { + printf(msg); + } + } +} + +fn named_param(_p: ParamDroppable) { + let _l = LocalDroppable; +} + +fn wildcard_param(_: ParamDroppable) { + let _l = LocalDroppable; +} + +fn main() -> i32 { + named_param(ParamDroppable); + wildcard_param(ParamDroppable); + 0 +} \ No newline at end of file