gccrs: fix ice when function is outside of context

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>
This commit is contained in:
Philip Herron 2023-07-30 00:19:15 +01:00 committed by Arthur Cohen
parent 6164ec07c6
commit 0610584d0d
2 changed files with 11 additions and 0 deletions

View 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 ()

View file

@ -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 }