From patchwork Wed Jul 8 10:02:50 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: 138723 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 6E05C4BA2E3A for ; Wed, 8 Jul 2026 10:03:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6E05C4BA2E3A 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=aOOLdhLl X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by sourceware.org (Postfix) with ESMTPS id B39354BA2E0F for ; Wed, 8 Jul 2026 10:02:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B39354BA2E0F 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 B39354BA2E0F Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1035 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783504973; cv=none; b=Zl1ApELsE0UkMDNezZvonnQ8EL3F+IrTErsBJOp88gFXj4OW5g+5meYZqPHUW7W37+eqCV1vxuMPo/0o7jY8kVjF83ZKrstfGrngLdSQ1GtxMt9f5pLuhs6opjrhJSr511QtLgi0eZKHXeCl9gbS/v0qd8wR/YxJVWUzsov+aRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783504973; c=relaxed/simple; bh=2U4dI6e026GDHegVkWvsP7EL6q1SPXlgqSRbrGFrsL4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=avvU1mnCKV9GHP/A/o7U2VaoxH7rQ4Vh9wrUienMrd9LNfH6YGxy15Jq2YeDRtPqDfLWfSlo36LToTBvc1Q8p4IQkLlhkKG0kFoNaYSSyVpV9/4k6bD68wUUaMvyuEWPqYTwb02tQqz3kdMFRiXGr62nWyu1M4eBoffhVthelUQ= 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=aOOLdhLl DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B39354BA2E0F Received: by mail-pj1-x1035.google.com with SMTP id 98e67ed59e1d1-3804e42ad5fso488310a91.2 for ; Wed, 08 Jul 2026 03:02:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783504973; x=1784109773; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=+eu197QnIv/sQdAdBwEh/m2k1O8BGkYl4NCDVFIQIGw=; b=aOOLdhLlkG+/jlspkEXnzmdhljRaItbOgUmOV5vFW9S8wGLRKNlQjXRebyvD04Ed76 u8u8/q+DBV59+DBQL3u6vhra9B6fsFyoQPe+i9mMCOu7sbiJeN3T7XYuGUeT3U1bdbHn PabCO1Vfssmo22t6tXjAr7GTLuQLZAFE1ZH/zjIy9ZlAImpz1ylUBUXjx/f/Unzhal15 +1W5slMocVzCATUGFVMrx4PGA7OfwZOTu7Dw25YVAod0ziNMS8OGr5koB/q9bufF2hsj IvAlHsEvvVeRux/KlYfnXzC61QjWz4T+dyHEOnsRNmRmDkoylln3+C9BRg8Gjpt5SngW IWhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783504973; x=1784109773; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+eu197QnIv/sQdAdBwEh/m2k1O8BGkYl4NCDVFIQIGw=; b=Ai/Ib0P+LuMSzA9E9ok05+/hWcDSZ9vWti20q8Rn/fXSuoYe5+k+rEW9/U2HyWveXP w+aqRrm3vajz6QF+y+FmbhMSgs8l01Bn+x3ObxNqqM2yY0DOYIcXh8NsSNJ59b6hB7o8 P5GA4GQufOzJnuYZbgAJAgxP/aFppamKYJB/lYSF7ZFesHsVmyTxTq90AePdPs16BKgz As/wbD4L9dgpUrlTAQemi07FOHJVfODpCzShP3y6muI0ChOQxuifUA3/NGDZmjilc2i5 AlYyf5ouyNGxRdINTPmm8VOihJzP5BXgQX+eIJMo8bvQ1XBHzvp0x/5S5cGyzEnk9kmd qjEw== X-Gm-Message-State: AOJu0Yy8l9czvi1R5rGsbYgQh6lhUGMpqG17CRmp4pmR24pYBnCh6dBu 9xIwL/mDBZl5s0kMn2Yyxy5sKLtY323tIr6sGyefJ4NFQ6xA3A6/+AXjQCRzaA== X-Gm-Gg: AfdE7cku0I3VIkKbb63IQHBmsNggt1BV2twquFjB7YaWLLzbpgGRa/o3tBfBKmwpAiq wSI+Ba/gs4zddif5NVttfZCUyraqKNZdZxYIe+vJ1921nr7XyhWI4U6w8lT80fKdwm0nAN59hyB wZ//pPd/+8scX1reuygNElpZywX2rjEY8M8wcQWdFmygzD5W3Go+Js5kCBHqaS/oub8uCHz1T3n KEncM5Xq3Ydu5Cod0o8g2Ce/68CuJ1WPYEqo2Jm+Ydqpf13iscylLb5qvvzG8akrw9eIb7HDHL0 E4oIdPnt9C4jh6X8sdB6aIUi7wJa4m8XB0B0ufpmv2fvz8HnxlILSj6PfzKUCvjaIjVKse7hHH2 SPqLWYiWhk7E2YvdLrPPNI6G4imsVa48nTi8kb1Plfbjsc9v+J4fBb2gpt1EUFZFMSvrnR5a2mb ZmP+lw65jox5RnCsp/7gOB7JTDLCEayp0jKQsVoJSL4oP3cOFYUjmI5gR98nZGSnrBu5kqDAMZp NLGHj2YU0Irqtk0tHWh X-Received: by 2002:a17:90b:2d05:b0:381:29f2:481b with SMTP id 98e67ed59e1d1-38940150106mr1898722a91.11.1783504972485; Wed, 08 Jul 2026 03:02:52 -0700 (PDT) Received: from runnervmkkn4f.hvvnqjvkf3sudeo4wt2ywfc0tg.yx.internal.cloudapp.net ([57.151.130.0]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-31174a56848sm19185197eec.16.2026.07.08.03.02.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jul 2026 03:02:51 -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] gccrs: Evaluate non-unit tail expressions before drops Date: Wed, 8 Jul 2026 10:02:50 +0000 Message-ID: <20260708100250.3079-1-gerris.rs@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 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, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham 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 A non-unit tail call could run after local drops. Store the tail expression result before emitting drops, then return the stored value. gcc/rust/ChangeLog: * backend/rust-compile-base.cc (HIRCompileBase::compile_function_body): Store non-unit tail expression results before emitting current scope drops. gcc/testsuite/ChangeLog: * rust/execute/drop-function-scope-non-unit-tail.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/99edb212c204385d994c72b85e8098be6f464226 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/4602 gcc/rust/backend/rust-compile-base.cc | 22 ++++++++- .../drop-function-scope-non-unit-tail.rs | 46 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/rust/execute/drop-function-scope-non-unit-tail.rs base-commit: 25ab7ba2877cec8bb831ddfeea9ecb72ef23c9d5 diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 1a6ce99f4..e049e18a4 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -708,10 +708,30 @@ HIRCompileBase::compile_function_body (tree fndecl, return_value = coercion_site (id, return_value, actual, expected, lvalue_locus, rvalue_locus); + /* Save the non-unit tail expression result before emitting scope + drops, so a tail call like foo() is evaluated before locals are + dropped. Conceptually, this changes lowering from: + + drop (_x); + return foo (); + + to: + + ret_slot = foo (); + drop (_x); + return ret_slot; */ + fncontext fnctx = ctx->peek_fn (); + tree result_reference + = Backend::var_expression (fnctx.ret_addr, lvalue_locus); + tree assignment = Backend::assignment_statement (result_reference, + return_value, locus); + ctx->add_statement (assignment); + CompileDrop (ctx).emit_current_scope_drop_calls (); + result_reference = Backend::var_expression (fnctx.ret_addr, locus); tree return_stmt - = Backend::return_statement (fndecl, return_value, locus); + = Backend::return_statement (fndecl, result_reference, locus); ctx->add_statement (return_stmt); } else diff --git a/gcc/testsuite/rust/execute/drop-function-scope-non-unit-tail.rs b/gcc/testsuite/rust/execute/drop-function-scope-non-unit-tail.rs new file mode 100644 index 000000000..71df1615c --- /dev/null +++ b/gcc/testsuite/rust/execute/drop-function-scope-non-unit-tail.rs @@ -0,0 +1,46 @@ +// { dg-output "f\r*\nd\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 Droppable; + +impl Drop for Droppable { + fn drop(&mut self) { + let msg = "d\n\0" as *const str as *const i8; + unsafe { + printf(msg); + } + } +} + +fn foo() -> i32 { + let msg = "f\n\0" as *const str as *const i8; + unsafe { + printf(msg); + } + + 0 +} + +fn f() -> i32 { + let _x = Droppable; + foo() +} + +fn main() -> i32 { + f() +}