[gccrs COMMIT] gccrs: Fix return type leaking into other function contexts
gerris.rs@gmail.com
gerris.rs@gmail.com
Tue Sep 8 09:45:19 GMT 2026
From: Philip Herron <herron.philip@googlemail.com>
We use this expected types to help with type unification hints in awkward
cases iirc it was for Try. But then when we have cases of return expressions
which are const position and end up needing to resolve into other blocks
which then bleeds these expected types into those chains.
Fixes Rust-GCC/gccrs#4860
gcc/rust/ChangeLog:
* typecheck/rust-typecheck-context.cc (TypeCheckContext::pop_return_type):
push expected null
gcc/testsuite/ChangeLog:
* rust/compile/issue-4860.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
---
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/4c3e9d122ea17aec02fcad266c36e958ddaa0b12
The commit has been mentioned in the following issue(s):
- Rust-GCC/gccrs#4860: https://github.com/Rust-GCC/gccrs/issues/4860
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/4869
gcc/rust/typecheck/rust-typecheck-context.cc | 4 +++
gcc/testsuite/rust/compile/issue-4860.rs | 29 ++++++++++++++++++++
2 files changed, 33 insertions(+)
create mode 100644 gcc/testsuite/rust/compile/issue-4860.rs
diff --git a/gcc/rust/typecheck/rust-typecheck-context.cc b/gcc/rust/typecheck/rust-typecheck-context.cc
index 404f83443..a1f10ea29 100644
--- a/gcc/rust/typecheck/rust-typecheck-context.cc
+++ b/gcc/rust/typecheck/rust-typecheck-context.cc
@@ -173,12 +173,16 @@ TypeCheckContext::push_return_type (TypeCheckContextItem item,
TyTy::BaseType *return_type)
{
return_type_stack.emplace_back (std::move (item), return_type);
+ // a query can check another function body while an expression in the
+ // caller has an expected type
+ push_expected_type (nullptr);
}
void
TypeCheckContext::pop_return_type ()
{
rust_assert (!return_type_stack.empty ());
+ pop_expected_type ();
return_type_stack.pop_back ();
}
diff --git a/gcc/testsuite/rust/compile/issue-4860.rs b/gcc/testsuite/rust/compile/issue-4860.rs
new file mode 100644
index 000000000..0d054dc78
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4860.rs
@@ -0,0 +1,29 @@
+// { dg-options "-w" }
+#![feature(no_core, lang_items, intrinsics, staged_api)]
+#![no_core]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+pub const FLAG: bool = check();
+
+pub const fn check() -> bool {
+ return 8i8.wrapping_shr(1) == 4;
+}
+
+extern "rust-intrinsic" {
+ #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
+ fn unchecked_shr<T>(x: T, y: T) -> T;
+}
+
+impl i8 {
+ pub const fn wrapping_shr(self, rhs: u32) -> Self {
+ unsafe { unchecked_shr(self, (rhs & 7) as i8) }
+ }
+}
+
+impl i16 {
+ pub const fn wrapping_shr(self, rhs: u32) -> Self {
+ unsafe { unchecked_shr(self, (rhs & 15) as i16) }
+ }
+}
base-commit: 9a7356b6fbb4a32c18a30bb0834b44ef0f62b41d
--
2.55.0
More information about the Gcc-rust
mailing list