Skip to content

Commit 5ad0feb

Browse files
committed
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 <[email protected]>
1 parent 5785d32 commit 5ad0feb

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

gcc/rust/typecheck/rust-hir-type-check-expr.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ TypeCheckExpr::visit (HIR::TupleExpr &expr)
150150
void
151151
TypeCheckExpr::visit (HIR::ReturnExpr &expr)
152152
{
153+
if (!context->have_function_context ())
154+
{
155+
rust_error_at (expr.get_locus (),
156+
"return statement outside of function body");
157+
infered = new TyTy::ErrorType (expr.get_mappings ().get_hirid ());
158+
return;
159+
}
160+
153161
auto fn_return_tyty = context->peek_return_type ();
154162
location_t expr_locus = expr.has_return_expr ()
155163
? expr.get_expr ()->get_locus ()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const FOO: u32 = return 0;
2+
// { dg-error "return statement outside of function body" "" { target *-*-* } .-1 }
3+
// { dg-error "expected .u32. got" "" { target *-*-* } .-2 }

0 commit comments

Comments
 (0)