Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[E0571] break with argument in non-loop loop #2455

Merged
merged 1 commit into from
Jul 28, 2023

Conversation

MahadMuhammad
Copy link
Contributor

break with argument in non-loop loop - E0571

  • A break statement with an argument appeared in a non-loop loop.
  • Refactored error message similiar to rustc
    & called error function.

Code Tested from E0571

// 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 ".break. with value from a .while. loop" }
        }
        i += 1;
    };
}

Output:

/home/mahad/Desktop/mahad/gccrs/gcc/testsuite/rust/compile/break_with_value_inside_loop.rs:10:13: error: 'break' with value from a 'while' loop [E0571]
compiler exited with status 1
PASS: rust/compile/break_with_value_inside_loop.rs  (test for errors, line 10)
PASS: rust/compile/break_with_value_inside_loop.rs (test for excess errors)

Running testcases:

/home/mahad/Desktop/mahad/gccrs/gcc/testsuite/rust/compile/break2.rs:8:13: error: 'break' with value from a 'while' loop [E0571]
compiler exited with status 1
PASS: rust/compile/break2.rs  (test for errors, line 8)
PASS: rust/compile/break2.rs (test for excess errors)

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.

@MahadMuhammad
Copy link
Contributor Author

  • This error code works, if we use only while loop. We need to improve this PR to also give this same error for for loop.
  • You can view the same on godbolt

  • Like if we try to use for loop, in the code tested from E0571.
  • We expect to give error code like rustc i.e.,
error[E0571]: `break` with value from a `for` loop

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 <mahadtxt@gmail.com>
rust_error_at (expr.get_locus (),
"can only break with a value inside %<loop%>");
rust_error_at (
expr.get_locus (), ErrorCode::E0571,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated ErrorCode according to ErrorCode enum class.

Copy link
Member

@philberty philberty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@philberty philberty added this pull request to the merge queue Jul 28, 2023
Merged via the queue into Rust-GCC:master with commit f201ef9 Jul 28, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
diagnostic diagnostic static analysis enhancement
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants