Skip to content

Commit

Permalink
gccrs: fix ice when function is outside of context
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
philberty committed Jul 30, 2023
1 parent 5785d32 commit 5ad0feb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
3 changes: 3 additions & 0 deletions gcc/testsuite/rust/compile/issue-2477.rs
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit 5ad0feb

Please sign in to comment.