https://gcc.gnu.org/g:17216afdd239f104a71f511de1fb2834c791f6db
commit r17-4343-g17216afdd239f104a71f511de1fb2834c791f6db Author: Philip Herron <[email protected]> Date: Tue Sep 8 10:34:08 2026 +0100 gccrs: Track lifetimes per trait item Fixes Rust-GCC/gccrs#4861 gcc/rust/ChangeLog: * typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn): grab lifetimes gcc/testsuite/ChangeLog: * rust/compile/issue-4861.rs: New test. Signed-off-by: Philip Herron <[email protected]> Diff: --- gcc/rust/typecheck/rust-hir-type-check.cc | 11 +++++++++++ gcc/testsuite/rust/compile/issue-4861.rs | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc b/gcc/rust/typecheck/rust-hir-type-check.cc index e0f819ea1760..d111207b7294 100644 --- a/gcc/rust/typecheck/rust-hir-type-check.cc +++ b/gcc/rust/typecheck/rust-hir-type-check.cc @@ -155,6 +155,17 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const { auto binder_pin = context->push_clean_lifetime_resolver (); + auto &mappings = Analysis::Mappings::get (); + auto *trait + = mappings.lookup_trait_item_mapping (get_mappings ().get_hirid ()); + rust_assert (trait != nullptr); + for (auto ¶m : trait->get_generic_params ()) + if (param->get_kind () == HIR::GenericParam::GenericKind::LIFETIME) + { + auto &lifetime_param = static_cast<HIR::LifetimeParam &> (*param); + context->intern_and_insert_lifetime (lifetime_param.get_lifetime ()); + } + std::vector<TyTy::SubstitutionParamMapping> substitutions = inherited_substitutions; diff --git a/gcc/testsuite/rust/compile/issue-4861.rs b/gcc/testsuite/rust/compile/issue-4861.rs new file mode 100644 index 000000000000..513f09f6f2dc --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-4861.rs @@ -0,0 +1,17 @@ +#![feature(no_core, lang_items)] +#![no_core] + +#[lang = "sized"] +pub trait Sized {} + +trait Searcher<'a> { + fn haystack(&self) -> &'a str; + fn with_lifetime<'b>(&self, value: &'b str) -> &'a str; + fn borrow(&'a self) -> &'a str; +} + +trait Other<'a, 'b> { + fn first(&self) -> &'a str; + fn second(&self) -> &'b str; + fn method<'c>(&self, value: &'c str) -> &'c str; +}
