Rustc behaviour with loops in const/static context
Nice work but i just realised yes this need another change sorry. This null_tree check should still stay but you need another check the error should be:
error[E0658]: `loop` is not allowed in a `static`
--> <source>:1:17
|
1 | static _X: () = loop {};
| ^^^^^^^
|
= note: see issue #52000 <https://github.com/rust-lang/rust/issues/52000> for more information
error: aborting due to previous error
So what you need to do is add a catch here: https://github.com/Rust-GCC/gccrs/blob/fc6b54365731c2ebfa1974058c48772e03203fdd/gcc/rust/backend/rust-compile-expr.cc#L671
if (ctx->const_context_p ())
{
rich_location r;
rust_error_at (r, ErrorCode::0658, "%<loop%> is not allowed in const context");
return;
}
Originally posted by @philberty in https://github.com/Rust-GCC/gccrs/issues/3781#issuecomment-2862764681
It looks like most loops are allowed since 1.46.0, seemingly with the single exception of for loops.
good find!
I wonder is it if iterators are not fully const in libcore yet or something
Hi, I’d like to work on this issue! @philberty
Just to clarify, should this patch reject all loop inside const and static (with if (ctx->const_context_p ())),
or should it also allow loop inside const fn()?
I tried using if (ctx->const_context_p () && !ctx->in_fn ()) to allow loops in const fn, but it doesn't seem to work.
Should I include that support as part of this patch as well, or leave it for a separate issue?
Thank you!