Skip to content

Commit

Permalink
gccrs: [E0571] break with argument in non-loop loop
Browse files Browse the repository at this point in the history
Refactored error message similiar to rustc
& called error function.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
	refactored error message & called error function.

gcc/testsuite/ChangeLog:

	* rust/compile/break2.rs: Modified file to pass test case.
	* rust/compile/break_with_value_inside_loop.rs: New test.

Signed-off-by: Muhammad Mahad <[email protected]>
  • Loading branch information
MahadMuhammad authored and philberty committed Jul 28, 2023
1 parent fe6ad2e commit f201ef9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 3 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1290,8 +1290,9 @@ TypeCheckExpr::visit (HIR::BreakExpr &expr)
TyTy::BaseType *loop_context = context->peek_loop_context ();
if (loop_context->get_kind () == TyTy::TypeKind::ERROR)
{
rust_error_at (expr.get_locus (),
"can only break with a value inside %<loop%>");
rust_error_at (
expr.get_locus (), ErrorCode::E0571,
"can only %<break%> with a value inside a %<loop%> block");
return;
}

Expand Down
3 changes: 2 additions & 1 deletion gcc/testsuite/rust/compile/break2.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// ErrorCode::E0571
fn main() {
let mut a = 1;
let mut b = 1;

let mut c;
while b > 10 {
if (b == 2) {
break b; // { dg-error "can only break with a value inside 'loop'" }
break b; // { dg-error "can only .break. with a value inside a .loop. block" }
}
c = a + b;
a = b;
Expand Down
14 changes: 14 additions & 0 deletions gcc/testsuite/rust/compile/break_with_value_inside_loop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// https://doc.rust-lang.org/error_codes/E0571.html
#![allow(unused)]
fn main() {
let mut i = 1;
fn satisfied(n: usize) -> bool {
n % 23 == 0
}
let result = while true {
if satisfied(i) {
break 2 * i; // { dg-error "can only .break. with a value inside a .loop. block" }
}
i += 1;
};
}

0 comments on commit f201ef9

Please sign in to comment.