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

Break/continue expressions #2546

Merged
merged 3 commits into from
Aug 23, 2022
Merged

Break/continue expressions #2546

merged 3 commits into from
Aug 23, 2022

Conversation

canndrew
Copy link
Contributor

Make break and continue expressions rather than declarations in the compiler's internal representation of the AST.

@sezna
Copy link
Contributor

sezna commented Aug 16, 2022

The code looks reasonable, quick question: is there a motivation for this, or some sort of documentation as to why this is the better way?

sezna
sezna previously approved these changes Aug 16, 2022
Copy link
Contributor

@sezna sezna left a comment

Choose a reason for hiding this comment

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

Ah I see the other PR now. #2536

otrho
otrho previously approved these changes Aug 17, 2022
@@ -1101,6 +1100,9 @@ fn connect_expression(
}
Ok(vec![while_loop_exit])
}
// TODO: do we need to create a node here and connect the leaves to it?
Copy link
Contributor

Choose a reason for hiding this comment

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

Dunno.

@canndrew canndrew dismissed stale reviews from otrho and sezna via f6590bc August 17, 2022 09:47
@canndrew canndrew force-pushed the canndrew/break-continue-exprs branch from 889b01d to f6590bc Compare August 17, 2022 09:47
graph.add_edge(entry_node, break_to_node, "".into());
Ok(vec![break_to_node])
}
None => Err(CompileError::BreakOutsideLoop { span }),
Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose this error will be emitted later in ir_generation so that's okay. However, are the graph connections above no longer needed? The idea was to transition the control flow to break_to_node or continue_to_node because everything after a break or continue in a given scope is essentially dead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This PR shouldn't change the current behaviour at all since the way break and continue expressions are handled is (if I made no mistakes) exactly the same as if they were enclosed in a single-declaration CodeBlock. I think the problem here is that break_to_node and break_to_continue weren't getting plumbed through to where they were needed anyway, so those variables ended up becoming unused and so I removed them.

@canndrew canndrew force-pushed the canndrew/break-continue-exprs branch from bd1ba31 to 86da50f Compare August 18, 2022 09:46
@@ -1101,6 +1100,9 @@ fn connect_expression(
}
Ok(vec![while_loop_exit])
}
// TODO: do we need to create a node here and connect the leaves to it?
Copy link
Contributor

Choose a reason for hiding this comment

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

if there's no node, we can't ever report break or continue as dead code. That's alright, but the proper thing to do here is to connect the current leaves to the break and continue, and then return an empty vec for leaves as a result. That means that the points leading up to the break/continue are connected, and then the path breaks since everything directly after break/continue is actually dead code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sezna: You mean like this?

609f707#diff-8877b4bcb40460d28f3808f38d7efae417662ed07fe5462b224207d08c910130R1104

Note that some of the other cases here which I looked at for guidance seem a bit off. WhileLoop for instance only connects leaves[0] and ignores any others. Array connects it's sub-expressions as if they're all going to be executed in parallel rather than sequentially. I didn't look at any others.

Copy link
Contributor

Choose a reason for hiding this comment

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

The purpose of dead code analysis isn't to track execution, but rather usage. So arrays just edge to all of the things they reference so we know that isn't dead code, since the array references them. It doesn't matter if the edges are parallel or not -- just that the item was referenced and is therefore not dead.

Copy link
Contributor

Choose a reason for hiding this comment

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

You mean like this?

yup LGTM

mohammadfawaz
mohammadfawaz previously approved these changes Aug 19, 2022
Copy link
Contributor

@mohammadfawaz mohammadfawaz left a comment

Choose a reason for hiding this comment

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

Seems reasonable, thanks!

@sezna sezna merged commit d384c2b into master Aug 23, 2022
@sezna sezna deleted the canndrew/break-continue-exprs branch August 23, 2022 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants