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

[E0268] break or continue used outside of loop #2453

Merged
merged 1 commit into from
Jul 20, 2023

Conversation

MahadMuhammad
Copy link
Contributor

Loop Keyword used outside of loop - E0268

  • A loop keyword (break or continue) was used outside of a loop.
  • Refactored error description similiar to rustc.

Code tested from E0268

// https://doc.rust-lang.org/error_codes/E0268.html
#![allow(unused)]
fn boo() {
    continue; // { dg-error ".continue. outside of a loop" }
    break; // { dg-error ".break. outside of a loop or labeled block" }
}

fn main() {
    boo()
}

Output:

➜  gccrs-build gcc/crab1 /home/mahad/Desktop/mahad/gccrs/gcc/testsuite/rust/compile/break_continue_outside_loop.rs
/home/mahad/Desktop/mahad/gccrs/gcc/testsuite/rust/compile/break_continue_outside_loop.rs:4:5: error:continue’ outside of a loop [E0268]
    4 |     continue;       // { dg-error ".continue. outside of a loop"}
      |     ^~~~~~~~
/home/mahad/Desktop/mahad/gccrs/gcc/testsuite/rust/compile/break_continue_outside_loop.rs:5:5: error:break’ outside of a loop or labeled block [E0268]
    5 |     break;          // { dg-error ".break. outside of a loop or labeled block"}
      |     ^~~~~

Analyzing compilation unit

Time variable                                   usr           sys          wall           GGC
 phase parsing                      :   0.00 (  0%)   0.01 (100%)   0.00 (  0%)  8192  (  5%)
 parser (global)                    :   0.00 (  0%)   0.01 (100%)   0.00 (  0%)  8192  (  5%)
 TOTAL                              :   0.00          0.01          0.00          146k
Extra diagnostic checks enabled; compiler may run slowly.
Configure with --enable-checking=release to disable checks.

Running testcases:

/home/mahad/Desktop/mahad/gccrs/gcc/testsuite/rust/compile/break1.rs:4:5: error: 'break' outside of a loop or labeled block [E0268]
compiler exited with status 1
PASS: rust/compile/break1.rs  (test for errors, line 4)
PASS: rust/compile/break1.rs (test for excess errors)

/home/mahad/Desktop/mahad/gccrs/gcc/testsuite/rust/compile/continue1.rs:6:9: error: 'continue' outside of a loop [E0268]
compiler exited with status 1
PASS: rust/compile/continue1.rs  (test for errors, line 6)
PASS: rust/compile/continue1.rs (test for excess errors)

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): refactored and call error function.

gcc/testsuite/ChangeLog:

* rust/compile/break1.rs: Modified to pass test case.
* rust/compile/continue1.rs: likewise.
* rust/compile/break_continue_outside_loop.rs: New test.

Refactored error description similiar to rustc.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
	refactored and call error function.

gcc/testsuite/ChangeLog:

	* rust/compile/break1.rs: Modified to pass test case.
	* rust/compile/continue1.rs: likewise.
	* rust/compile/break_continue_outside_loop.rs: New test.

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
@@ -1277,7 +1277,8 @@ TypeCheckExpr::visit (HIR::BreakExpr &expr)
{
if (!context->have_loop_context ())
{
rust_error_at (expr.get_locus (), "cannot %<break%> outside of a loop");
rust_error_at (expr.get_locus (), ErrorCode ("E0268"),
"%<break%> outside of a loop or labeled block");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed error message, similiar to rustc- E0268.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • rustc 1.49 has error message:
`break` outside of a loop
  • But, I updated the message according to rustc nightly version
`break` outside of a loop or labeled block
  • You can see difference on the compiler-explorer on the link above.

rust_error_at (expr.get_locus (),
"cannot %<continue%> outside of a loop");
rust_error_at (expr.get_locus (), ErrorCode ("E0268"),
"%<continue%> outside of a loop");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

same here.

@CohenArthur CohenArthur added enhancement diagnostic diagnostic static analysis labels Jul 20, 2023
Copy link
Member

@CohenArthur CohenArthur left a comment

Choose a reason for hiding this comment

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

Looks great! Thank you :)

@CohenArthur CohenArthur added this pull request to the merge queue Jul 20, 2023
Merged via the queue into Rust-GCC:master with commit 41e7dee Jul 20, 2023
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
None yet
Development

Successfully merging this pull request may close these issues.

2 participants