From: Utkarsh Bahuguna <[email protected]>

The guard only popped the resolver for functions, so every impl block
resolved on demand left its lifetime resolver on the stack and later
lookups hit the wrong one. This is the unresolved lifetime core runs
into in str/pattern.rs.

Fixes Rust-GCC/gccrs#4980

Assisted-by: Claude Opus (Anthropic)

gcc/rust/ChangeLog:

        * typecheck/rust-hir-type-check.h
        (TypeCheckContext::LifetimeResolverGuard::~LifetimeResolverGuard):
        Also pop impl block resolvers.

gcc/testsuite/ChangeLog:

        * rust/compile/issues/issue-4980.rs: New test.

Signed-off-by: Utkarsh Bahuguna <[email protected]>
---
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/c415c9906b202a4ec1d051208c0658d432a9781b

The commit has been mentioned in the following issue(s):
 - Rust-GCC/gccrs#4980: https://github.com/Rust-GCC/gccrs/issues/4980

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/5019

 gcc/rust/typecheck/rust-hir-type-check.h      |  2 +-
 .../rust/compile/issues/issue-4980.rs         | 65 +++++++++++++++++++
 2 files changed, 66 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/rust/compile/issues/issue-4980.rs

diff --git a/gcc/rust/typecheck/rust-hir-type-check.h 
b/gcc/rust/typecheck/rust-hir-type-check.h
index 41e02bb00..b94b737b1 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.h
+++ b/gcc/rust/typecheck/rust-hir-type-check.h
@@ -552,7 +552,7 @@ public:
       rust_assert (!ctx.lifetime_resolver_stack.empty ());
       if (!ctx.lifetime_resolver_stack.top ().binder_empty ())
        ctx.lifetime_resolver_stack.top ().pop_binder ();
-      if (kind == RESOLVER)
+      if (kind == RESOLVER || kind == IMPL_BLOCK_RESOLVER)
        {
          ctx.lifetime_resolver_stack.pop ();
        }
diff --git a/gcc/testsuite/rust/compile/issues/issue-4980.rs 
b/gcc/testsuite/rust/compile/issues/issue-4980.rs
new file mode 100644
index 000000000..93378d31c
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issues/issue-4980.rs
@@ -0,0 +1,65 @@
+// { dg-additional-options "-w" }
+#![feature(no_core, lang_items)]
+#![no_core]
+
+// Rust-GCC/gccrs#4980: a bound check in H::g probed the derived Clone impl of
+// CharSliceSearcher before the struct itself was visited. Resolving that impl
+// pushed a lifetime resolver that was never popped, so the struct's own 'a
+// and 'b could not be found once its field was resolved.
+
+#[lang = "sized"]
+pub trait Sized {}
+
+pub mod clone {
+    #[lang = "clone"]
+    pub trait Clone: crate::Sized {
+        fn clone(&self) -> Self;
+    }
+}
+
+impl clone::Clone for u32 {
+    fn clone(&self) -> Self {
+        *self
+    }
+}
+
+fn need<X: clone::Clone>(_x: X) {}
+
+pub struct H;
+
+impl H {
+    pub fn g(x: u32) {
+        need(x);
+    }
+}
+
+pub trait Pattern<'a> {
+    type Searcher;
+}
+
+pub trait MultiCharEq {}
+
+struct MultiCharEqPattern<C: MultiCharEq>(C);
+
+struct MultiCharEqSearcher<'a, C: MultiCharEq> {
+    marker: &'a (),
+    c: *const C,
+}
+
+impl<'a, C: MultiCharEq> Pattern<'a> for MultiCharEqPattern<C> {
+    type Searcher = MultiCharEqSearcher<'a, C>;
+}
+
+impl<'x> MultiCharEq for &'x u8 {}
+
+impl<'a, C: MultiCharEq> clone::Clone for MultiCharEqSearcher<'a, C> {
+    fn clone(&self) -> Self {
+        MultiCharEqSearcher {
+            marker: self.marker,
+            c: self.c,
+        }
+    }
+}
+
+#[derive(Clone)]
+pub struct CharSliceSearcher<'a, 'b>(<MultiCharEqPattern<&'b u8> as 
Pattern<'a>>::Searcher);

base-commit: b635f58370c2e130ad52f1b724ab38cdfc95825d
-- 
2.56.1

Reply via email to