From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id EBADA393BA5C; Thu, 6 Oct 2022 20:06:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EBADA393BA5C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665086788; bh=V5q2qnywh/qqDMXr0Mh8Og4v+K58EjaZBWQsScJXX8Q=; h=From:To:Subject:Date:From; b=bmksV3TzFrg1FQctvMmTHKrnwi7ZdbssBqm/6higfm7iVZUP/vHgBvZ2mNbDg0Y4r FTxnJ9bVcEY/NNwQY8Ce66QqcmH+OGQHj59vWqUFcQ3IFoUUoVRtc8V5Hj/YeYfB9j NEWfbkglVaoM10tTlcstIFiD7gTqx1TgF11bN3wU= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Support looking up super traits for trait items X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: e033f1705dde104c44d69cec87cb728dba596c6f X-Git-Newrev: a7d2643d9b09af9f5c5c670626becaa0c0fc1481 Message-Id: <20221006200628.EBADA393BA5C@sourceware.org> Date: Thu, 6 Oct 2022 20:06:28 +0000 (GMT) List-Id: https://gcc.gnu.org/g:a7d2643d9b09af9f5c5c670626becaa0c0fc1481 commit a7d2643d9b09af9f5c5c670626becaa0c0fc1481 Author: Philip Herron Date: Wed Oct 5 17:24:42 2022 +0100 Support looking up super traits for trait items When supporting calls to super traits we need to allow lookups based on the super traits as specified on the TraitReferences. Fixes #1555 Diff: --- gcc/rust/typecheck/rust-hir-trait-ref.h | 19 ++++++++++ gcc/testsuite/rust/compile/torture/issue-1555.rs | 48 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/gcc/rust/typecheck/rust-hir-trait-ref.h b/gcc/rust/typecheck/rust-hir-trait-ref.h index 6eec461e8a5..0f4883d6ba3 100644 --- a/gcc/rust/typecheck/rust-hir-trait-ref.h +++ b/gcc/rust/typecheck/rust-hir-trait-ref.h @@ -336,6 +336,15 @@ public: return true; } } + + // lookup super traits + for (const auto &super_trait : super_traits) + { + bool found = super_trait->lookup_trait_item (ident, ref); + if (found) + return true; + } + return false; } @@ -351,6 +360,16 @@ public: if (ident.compare (item.get_identifier ()) == 0) return &item; } + + // lookup super traits + for (const auto &super_trait : super_traits) + { + const TraitItemReference *res + = super_trait->lookup_trait_item (ident, type); + if (!res->is_error ()) + return res; + } + return &TraitItemReference::error_node (); } diff --git a/gcc/testsuite/rust/compile/torture/issue-1555.rs b/gcc/testsuite/rust/compile/torture/issue-1555.rs new file mode 100644 index 00000000000..adb48911648 --- /dev/null +++ b/gcc/testsuite/rust/compile/torture/issue-1555.rs @@ -0,0 +1,48 @@ +extern "C" { + fn printf(s: *const i8, ...); +} + +struct Foo(i32); +trait Bar { + fn baz(&self); +} + +trait Baz: Bar { + fn qux(&self); +} + +impl Bar for Foo { + fn baz(&self) { + unsafe { + let a = "baz %i\n\0"; + let b = a as *const str; + let c = b as *const i8; + + printf(c, self.0); + } + } +} + +impl Baz for Foo { + fn qux(&self) { + unsafe { + let a = "qux %i\n\0"; + let b = a as *const str; + let c = b as *const i8; + + printf(c, self.0); + } + } +} + +fn static_dispatch(t: &T) { + t.baz(); + t.qux(); +} + +pub fn main() { + let a; + a = &Foo(123); + + static_dispatch(a); +}