]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix cast site to not miss type-unifications
authorPhilip Herron <herron.philip@googlemail.com>
Tue, 30 May 2023 16:16:30 +0000 (17:16 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:37:22 +0000 (18:37 +0100)
When attempting casts we can try a type coercion first, this is a
TryCoercion which will return a result. In the event this is ok we need to
perform a true coercion so that we don't leave missing infeence variable's
ununified.

Fixes #2195

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::check): do coercion
(TypeCastRules::emit_cast_error): clang-format

gcc/testsuite/ChangeLog:

* rust/compile/issue-2195.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-casts.cc
gcc/testsuite/rust/compile/issue-2195.rs [new file with mode: 0644]

index 8e5e65eb22c77704aa9145526a4a0bbf5bbe7397..c9a25a03355129bb1dd39ea82252ee4e9182eebe 100644 (file)
@@ -42,7 +42,12 @@ TypeCastRules::check ()
     = TypeCoercionRules::TryCoerce (from.get_ty (), to.get_ty (), locus,
                                    true /*allow-autoderef*/);
   if (!possible_coercion.is_error ())
-    return possible_coercion;
+    {
+      // given the attempt was ok we need to ensure we perform it so that any
+      // inference variables are unified correctly
+      return TypeCoercionRules::Coerce (from.get_ty (), to.get_ty (), locus,
+                                       true /*allow-autoderef*/);
+    }
 
   // try the simple cast rules
   auto simple_cast = cast_rules ();
@@ -303,4 +308,4 @@ TypeCastRules::emit_cast_error () const
 }
 
 } // namespace Resolver
-} // namespace Rust
\ No newline at end of file
+} // namespace Rust
diff --git a/gcc/testsuite/rust/compile/issue-2195.rs b/gcc/testsuite/rust/compile/issue-2195.rs
new file mode 100644 (file)
index 0000000..c955df4
--- /dev/null
@@ -0,0 +1,8 @@
+struct A<T> {
+    // { dg-warning "struct is never constructed" "" { target *-*-* } .-1 }
+    f: *const T,
+}
+
+pub fn cast<T>(a: A<T>) {
+    let z = a.f as *const ();
+}