diff options
author | Philip Herron <herron.philip@googlemail.com> | 2023-05-25 17:43:31 +0100 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-01-16 18:37:21 +0100 |
commit | 74db136a7cf7505691d409e01f757a5b86f119c9 (patch) | |
tree | 909496ad8e7f109e55e3c21a2f264d9a9480ee99 /gcc/testsuite/rust/execute | |
parent | 6ed987d5bc6a11735a3244d6f69d8f3e8879fbc0 (diff) | |
download | gcc-74db136a7cf7505691d409e01f757a5b86f119c9.zip gcc-74db136a7cf7505691d409e01f757a5b86f119c9.tar.gz gcc-74db136a7cf7505691d409e01f757a5b86f119c9.tar.bz2 |
gccrs: Fix bounds checking to check both sides
We were missing bounds checking for both lhs and rhs directions this is
important as we might fail checking for all specified bounds properly.
This is why for #2236 we need the Type parameter T to realise that it
_cannot_ coerce to the i32 max function directly without any adjustments
because T has the specified bound of Deref but i32 does not implement
Deref. This indrectly forces the autoderef cycle to try a deref in order
to get an i32 which _will_ match the i32 max function in the case we
pass an &32 as the type parameter T.
Fixes #2236
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments): stop if error
* typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::assemble_sized_builtin): fix sized options
* typecheck/rust-tyty.cc (BaseType::satisfies_bound): its ok if its an ?T
(BaseType::bounds_compatible): likewise
* typecheck/rust-tyty.h: update prototype
* typecheck/rust-unify.cc (UnifyRules::go): check both sides bounds
gcc/testsuite/ChangeLog:
* rust/execute/torture/issue-2236.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Diffstat (limited to 'gcc/testsuite/rust/execute')
-rw-r--r-- | gcc/testsuite/rust/execute/torture/issue-2236.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/gcc/testsuite/rust/execute/torture/issue-2236.rs b/gcc/testsuite/rust/execute/torture/issue-2236.rs new file mode 100644 index 0000000..1edc5a5 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/issue-2236.rs @@ -0,0 +1,37 @@ +// { dg-options "-w" } +mod core { + mod ops { + #[lang = "deref"] + trait Deref { + type Target; + fn deref(&self) -> &Self::Target; + } + + impl<T> Deref for &T { + type Target = T; + + fn deref(&self) -> &T { + *self + } + } + } +} + +impl i32 { + fn max(self, other: i32) -> i32 { + if self > other { + self + } else { + other + } + } +} + +fn foo<T: core::ops::Deref<Target = i32>>(t: T) -> i32 { + t.max(2) +} + +fn main() -> i32 { + let a: i32 = 1; + foo(&a) - 2 +} |