gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

gccrs:fix ICE with continue/break/return in while condition

Open Harishankar14 opened this issue 1 month ago • 9 comments

Fixes ICE when continue/break/return/loop are used as while conditions.

Fixes #3977

The predicate expression must be evaluated before type checking to ensure side effects occur even when the predicate has Never type. This prevents skipping function calls, panics, or other side effects in diverging predicates.

Proof of fix using -fdump-tree-gimple

As requested, here is the GIMPLE dump for the test case while loop {} {}. It confirms that the infinite loop side-effect is preserved correctly:

__attribute__((cdecl))
struct () test::main ()
{
  struct () D.107;

  <D.101>:
  <D.103>:
  {
    <D.104>:
    {
      <D.102>:
      goto <D.102>; // Side-effect correctly preserved
      if (0 != 0) goto <D.105>; else goto <D.106>;
      <D.106>:
      {

      }
    }
    goto <D.104>;
    <D.105>:
  }
  goto <D.103>;
  return D.107;
}

Here is a checklist to help you with your PR.

  • [ ] GCC development requires copyright assignment or the Developer's Certificate of Origin sign-off, see https://gcc.gnu.org/contribute.html or https://gcc.gnu.org/dco.html
  • [ ] Read contributing guidlines
  • [ ] make check-rust passes locally
  • [ ] Run clang-format
  • [ ] Added any relevant test cases to gcc/testsuite/rust/

Note that you can skip the above if you are just opening a WIP PR in order to get feedback.

*Please write a comment explaining your change. This is the message that will be part of the merge commit.

Harishankar14 avatar Nov 06 '25 20:11 Harishankar14

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=f4db2628116a78ca94e09a3cf1bcfb21

Having a predicate of type ! should work -- it gets coerced into a boolean. See https://doc.rust-lang.org/reference/types/never.html, though don't get confused by the last paragraph

powerboat9 avatar Nov 08 '25 03:11 powerboat9

@powerboat9 Thanks for the feedback! I dont really know how did i miss this.. I've updated the fix to properly handle never-type coercion. Instead of rejecting continue as a value, the compiler now:

Detects when the while predicate has type ! (never) Coerces it to bool by treating it as true (infinite loop) Issues a warning about unreachable code

Harishankar14 avatar Nov 09 '25 10:11 Harishankar14

@powerboat9, @philberty

Harishankar14 avatar Nov 09 '25 21:11 Harishankar14

@philberty LMK, if any changes required. Applied all the ones which you pointed out. :)

Harishankar14 avatar Nov 11 '25 13:11 Harishankar14

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=f4db2628116a78ca94e09a3cf1bcfb21

Having a predicate of type ! should work -- it gets coerced into a boolean. See https://doc.rust-lang.org/reference/types/never.html, though don't get confused by the last paragraph

@powerboat9 This should do, lmk your thoughts.

Harishankar14 avatar Nov 13 '25 09:11 Harishankar14

nearly there

Yeah completed as per your suggestions,waiting for your review @philberty

Harishankar14 avatar Nov 20 '25 16:11 Harishankar14

you need to fix the commit format CI is failing for you.

philberty avatar Nov 24 '25 19:11 philberty

you need to fix the commit format CI is failing for you.

Yep, done @philberty

Harishankar14 avatar Nov 24 '25 22:11 Harishankar14

OK I think this is ready but before we merge can you show an example of -fdump-tree-gimple in the git commit message of what we are doing here for this senario? But also put it in the github description

@philberty modified it, should be okay ?

Harishankar14 avatar Dec 01 '25 19:12 Harishankar14

@P-E-P

Harishankar14 avatar Dec 17 '25 01:12 Harishankar14