Improve resolution and compilation of control flow loops and labels#4522
Conversation
|
cc @powerboat9 |
| res += i; | ||
| i += 1; | ||
| if res + i >= 99 { | ||
| break 'calculation; |
There was a problem hiding this comment.
Does continue 'calculation work?
There was a problem hiding this comment.
i don't think because the label now after the loop.
i will check it then ping u for review
There was a problem hiding this comment.
its impossible to create a single label for both continue and break. we have to create two labels one at the start and one at the end. right now i am hacking gcc/go to see how this problem is solved and yeah there is separation.
There was a problem hiding this comment.
this will require more work to be done since we need to modify [loop,while,continue,break] and a lot of testing also. once its ready i will make it open again.
dfc3c94 to
d6e32c3
Compare
|
@powerboat9 this is the core part of the patch, it still not ready. |
d3de692 to
75570ed
Compare
|
ping ? |
|
Could you squash the commits? |
|
ok i will but i am asking if there something i should i update or not ? |
75570ed to
426f0bb
Compare
done :) |
|
ping ? |
|
is there any feedback on this ? |
| compiled_break_labels[id] = label; | ||
| } | ||
|
|
||
| bool lookup_break_label (HirId id, tree *label) |
There was a problem hiding this comment.
Even if the old API uses an out pointer with a bool return value most of the new APIs try to use optional when available.
There was a problem hiding this comment.
Even if the old API uses an out pointer with a bool return value most of the new APIs try to use optional when available.
i suggest to open a new PR to refactor the old APIs. what do u think ?
There was a problem hiding this comment.
yes that would be a good idea :) do you want to work on that?
There was a problem hiding this comment.
yes just mention this comment in an issue and assign it to me.
| compiled_continue_labels[id] = label; | ||
| } | ||
|
|
||
| bool lookup_continue_label (HirId id, tree *label) |
426f0bb to
7383411
Compare
|
Drive-by comment on changelog... Looks like the first entry is not correct: As you're adding a bunch of new functions, I think there should be entries like: What do you think? |
027469a to
35596f8
Compare
I have used the mklog script :) , i have added entries for the new functions manually. |
|
|
||
| tree pop_loop_end_label () | ||
| { | ||
| tree pop = loop_end_labels.back (); |
There was a problem hiding this comment.
This is undefined behavior if the vector is empty. Add an assertion that it isn't like you did in peek_loop_end_label
| std::vector<::std::vector<DropCandidate>> block_drop_candidates; | ||
| std::vector<::Bvariable *> loop_value_stack; | ||
| std::vector<tree> loop_begin_labels; | ||
| std::vector<tree> loop_end_labels; |
There was a problem hiding this comment.
Would it make sense to use something like the StackedContexts<T> class here?
There was a problem hiding this comment.
i think yes but i prefer to open a new refactor PR for compilation-context to make use of it and also update the old APIs.
| loop_label.get_lifetime ().get_mappings ().get_hirid (), label); | ||
| // Associate the loop's result temporary with the label so that a | ||
| // `break 'label value` can locate it (see visit (HIR::BreakExpr)). | ||
| loop_label = tl::optional<HIR::LoopLabel> (expr.get_loop_label ()); |
There was a problem hiding this comment.
You don't need the constructor here
| loop_label = tl::optional<HIR::LoopLabel> (expr.get_loop_label ()); | |
| loop_label = expr.get_loop_label (); |
b73ece7 to
7a55fac
Compare
|
ci failure ? but its ok on my machine |
|
Looks like it's either a gcc5 specific issue, or some undefined behavior is semi-randomly causing problems. Maybe try running with valgrind? |
7a55fac to
c2a3c97
Compare
|
@powerboat9 i just rerun the CI again and its now green. |
| 'high: loop { | ||
| dump_number(3); | ||
| break 'mid; | ||
| dump_number(3); |
There was a problem hiding this comment.
I would dump another number here to explicitly tell the user were something has gone wrong when the compiler breaks.
| break 'mid; | ||
| dump_number(3); | ||
| } | ||
| dump_number(2); |
c2a3c97 to
886ef77
Compare
This batch fixes and improves the handling of loop expressions, particularly focusing on `break` and `continue` statements within labeled and nested loops. gcc/rust/ChangeLog: * backend/rust-compile-context.h (RUST_COMPILE_CONTEXT): loop labels context functions. - insert_break_label - lookup_break_label - insert_continue_label - lookup_continue_label - push/pop/peek end_label (similar to begin_label). * backend/rust-compile-expr.cc (CompileExpr::visit): handle loop labels construction in WhileExpr, ForExpr, ContinueExpr and BreakExpr. (CompileExpr::construct_block_label): construct break label instead of normal label to unify handling break statement. (CompileExpr::construct_loop_labels): construct while/for loops labels (continue label before loop, break label after loop). * backend/rust-compile-expr.h (RUST_COMPILE_EXPR): construct_loop_labels function header. * resolve/rust-default-resolver.cc (DefaultResolver::visit): Fix labels. * resolve/rust-default-resolver.h: Fix labels. * resolve/rust-name-resolution-context.cc (NameResolutionContext::insert): Fix labels. (NameResolutionContext::insert_shadowable): Fix labels. (NameResolutionContext::insert_globbed): Fix labels. (NameResolutionContext::scoped): Fix labels. gcc/testsuite/ChangeLog: * rust/execute/cf-break-continue.rs: New test. * rust/execute/cf-label-shadowing.rs: New test. * rust/execute/cf-labeled-break-nested.rs: New test. * rust/execute/cf-labeled-continue-nested.rs: New test. * rust/execute/cf-labeled-loops.rs: New test. * rust/execute/cf-loop-break-continue.rs: New test. * rust/execute/cf-mixed-labeled-unlabeled.rs: New test. * rust/execute/cf-nested-loops.rs: New test. Signed-off-by: Islam-Imad <islamimad404@gmail.com>
886ef77 to
7399903
Compare
|
the test is updated @P-E-P |
This patch fixes and improves the handling of loop expressions, particularly
focusing on
breakandcontinuestatements within labeled and nested loops.closes #4521
closes #4530