public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7866] gccrs: fix ice when function is outside of context
@ 2024-01-16 18:05 Arthur Cohen
  0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-16 18:05 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:0610584d0d03dc44ce7403dad7028bdc1acceb7e

commit r14-7866-g0610584d0d03dc44ce7403dad7028bdc1acceb7e
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Sun Jul 30 00:19:15 2023 +0100

    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>

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-expr.cc | 8 ++++++++
 gcc/testsuite/rust/compile/issue-2477.rs       | 3 +++
 2 files changed, 11 insertions(+)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
index b7538abe5db..74c4217e578 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -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
index 00000000000..26831a6625d
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-2477.rs
@@ -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 }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-16 18:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 18:05 [gcc r14-7866] gccrs: fix ice when function is outside of context Arthur Cohen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).