]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: report unsupported Drop ref patterns
authorlishin <lishin1008@gmail.com>
Tue, 2 Jun 2026 09:52:39 +0000 (09:52 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 25 Jun 2026 17:21:37 +0000 (19:21 +0200)
Add an unsupported error for ref patterns and subpatterns.
Add an unsupported ref-pattern test case,
and extend the existing block-exit test case.

gcc/rust/ChangeLog:

* backend/rust-compile-pattern.cc (CompilePatternLet::visit): Check ref
pattern base types and emit a sorry for unsupported Drop ref/subpatterns.

gcc/testsuite/ChangeLog:

* rust/execute/drop-block-scope.rs: Extend test coverage for simple
immutable and mutable bindings.
* rust/compile/drop-ref-pattern.rs: New test.

Signed-off-by: lishin <lishin1008@gmail.com>
gcc/rust/backend/rust-compile-pattern.cc
gcc/testsuite/rust/compile/drop-ref-pattern.rs [new file with mode: 0644]
gcc/testsuite/rust/execute/drop-block-scope.rs

index eb1541c8ab2d3fb7e61e456f28b3656a5ec51178..021e438b66c0f3dc22065fa483ef9c739030b92b 100644 (file)
@@ -1369,10 +1369,23 @@ CompilePatternLet::visit (HIR::IdentifierPattern &pattern)
       ctx->add_statement (s);
     }
 
-  if (!pattern.has_subpattern () && !pattern.get_is_ref ()
-      && type_has_drop_impl (ctx, ty))
+  TyTy::BaseType *drop_ty = ty;
+  if (pattern.get_is_ref ())
+    {
+      auto ref_ty = ty->try_as<TyTy::ReferenceType> ();
+      rust_assert (ref_ty != nullptr);
+      drop_ty = ref_ty->get_base ();
+    }
+
+  if (!type_has_drop_impl (ctx, drop_ty))
+    return;
+
+  if (!pattern.has_subpattern () && !pattern.get_is_ref ())
     ctx->note_simple_drop_candidate (pattern.get_mappings ().get_hirid (),
                                     pattern.get_locus ());
+  else
+    rust_sorry_at (pattern.get_locus (),
+                  "drop trait not supported for subpatterns and ref patterns");
 }
 
 void
diff --git a/gcc/testsuite/rust/compile/drop-ref-pattern.rs b/gcc/testsuite/rust/compile/drop-ref-pattern.rs
new file mode 100644 (file)
index 0000000..f0053dc
--- /dev/null
@@ -0,0 +1,23 @@
+#![feature(no_core)]
+#![feature(lang_items)]
+#![no_core]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+#[lang = "drop"]
+pub trait Drop {
+    fn drop (&mut self);
+}
+
+struct Droppable;
+
+impl Drop for Droppable {
+    fn drop(&mut self) {}
+}
+
+fn main() {
+    {
+        let ref _x = Droppable; // { dg-message "sorry, unimplemented: drop trait not supported for subpatterns and ref patterns" }
+    }
+}
\ No newline at end of file
index 678ad63d75297fb95ad90bb8f6ccbaf5cac6ff9c..943cf5ee1e57439990065dcbb2ef7b9e135413b9 100644 (file)
@@ -1,4 +1,5 @@
-// { dg-output "d\r*\n" }
+// { dg-output "d\r*\nd\r*\nd\r*\n" }
+// { dg-additional-options "-w" }
 #![feature(no_core)]
 #![feature(lang_items)]
 #![no_core]
@@ -30,5 +31,11 @@ fn main() -> i32 {
     {
         let _x = Droppable;
     }
+    {
+        let x = Droppable;
+    }
+    {
+        let mut x = Droppable;
+    }
     0
 }