]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: fix ice when function is outside of context
authorPhilip Herron <herron.philip@googlemail.com>
Sat, 29 Jul 2023 23:19:15 +0000 (00:19 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:56:03 +0000 (18:56 +0100)
Fixes #2477

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
We need to check if a function context exists

gcc/testsuite/ChangeLog:

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

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

index b7538abe5db0f719f03f11995d5aa51403e19258..74c4217e578674702799e780f92ac43a9b8b63f3 100644 (file)
@@ -150,6 +150,14 @@ TypeCheckExpr::visit (HIR::TupleExpr &expr)
 void
 TypeCheckExpr::visit (HIR::ReturnExpr &expr)
 {
+  if (!context->have_function_context ())
+    {
+      rust_error_at (expr.get_locus (),
+                    "return statement outside of function body");
+      infered = new TyTy::ErrorType (expr.get_mappings ().get_hirid ());
+      return;
+    }
+
   auto fn_return_tyty = context->peek_return_type ();
   location_t expr_locus = expr.has_return_expr ()
                            ? expr.get_expr ()->get_locus ()
diff --git a/gcc/testsuite/rust/compile/issue-2477.rs b/gcc/testsuite/rust/compile/issue-2477.rs
new file mode 100644 (file)
index 0000000..26831a6
--- /dev/null
@@ -0,0 +1,3 @@
+const FOO: u32 = return 0;
+// { dg-error "return statement outside of function body" "" { target *-*-* } .-1 }
+// { dg-error "expected .u32. got" "" { target *-*-* } .-2 }