From cf311c5324c745e0edde5cd9ed526aea018a169f Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Wed, 29 Mar 2023 16:28:11 +0100 Subject: gccrs: handle bare function types with no specified return type 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 --- gcc/rust/typecheck/rust-hir-type-check-type.cc | 16 ++++++++++++---- gcc/testsuite/rust/compile/issue-2042.rs | 6 ++++++ 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/rust/compile/issue-2042.rs (limited to 'gcc') diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc b/gcc/rust/typecheck/rust-hir-type-check-type.cc index e5564ab..981786c 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc @@ -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 params; for (auto ¶m : 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 index 0000000..9fee13d --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-2042.rs @@ -0,0 +1,6 @@ +fn f<'r>(p: &'r mut fn(p: &mut ())) { + (*p)(()) + // { dg-error "expected .&mut ()." "" { target *-*-* } .-1 } +} + +fn main() {} -- cgit v1.1