]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: handle bare function types with no specified return type
authorPhilip Herron <herron.philip@googlemail.com>
Wed, 29 Mar 2023 15:28:11 +0000 (16:28 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:28:40 +0000 (18:28 +0100)
When we have a function with no return type this defaults to (), but we
need to be sure we generate a new implicit HirId for it otherwise it will
end up in a recursive reference chain.

Fixes #2042

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit):
Add implicit unit type as the return type when not specified

gcc/testsuite/ChangeLog:

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

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

index e5564ab4ee8b09a83800b8c218c19f89189cfd2b..981786c9ad5c981bbe47bb3e146cc2dc284d7dc1 100644 (file)
@@ -70,10 +70,18 @@ TypeCheckType::Resolve (HIR::Type *type)
 void
 TypeCheckType::visit (HIR::BareFunctionType &fntype)
 {
-  TyTy::BaseType *return_type
-    = fntype.has_return_type ()
-       ? TypeCheckType::Resolve (fntype.get_return_type ().get ())
-       : TyTy::TupleType::get_unit_type (fntype.get_mappings ().get_hirid ());
+  TyTy::BaseType *return_type;
+  if (fntype.has_return_type ())
+    {
+      return_type = TypeCheckType::Resolve (fntype.get_return_type ().get ());
+    }
+  else
+    {
+      // needs a new implicit ID
+      HirId ref = mappings->get_next_hir_id ();
+      return_type = TyTy::TupleType::get_unit_type (ref);
+      context->insert_implicit_type (ref, return_type);
+    }
 
   std::vector<TyTy::TyVar> params;
   for (auto &param : fntype.get_function_params ())
diff --git a/gcc/testsuite/rust/compile/issue-2042.rs b/gcc/testsuite/rust/compile/issue-2042.rs
new file mode 100644 (file)
index 0000000..9fee13d
--- /dev/null
@@ -0,0 +1,6 @@
+fn f<'r>(p: &'r mut fn(p: &mut ())) {
+    (*p)(())
+    // { dg-error "expected .&mut ()." "" { target *-*-* } .-1 }
+}
+
+fn main() {}