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

Rust: Include parameters and patterns in the CFG #17686

Merged
merged 5 commits into from
Oct 11, 2024

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Oct 8, 2024

This PR adds parameters and patterns to the CFG, needed for SSA. Unlike expressions, which construct values, patterns destruct values, so they are modeled pre-order in the CFG. The first exception being identifier patterns

match value {
   x @ Option::None =>  // ..
   // ...
}

where we need to first match against Option::None and only bind x in case of a match, and the second exception being | patterns, which are modeled like logical disjunction expressions.

Examples

Simple parameters

fn mutate_param(x : &mut i64) { // x
    *x = // $ read_access=x
        *x + // $ read_access=x
        *x; // $ read_access=x
}
Before
flowchart TD
1["enter mutate_param"]
10["... + ..."]
11["x"]
12["* ..."]
13["x"]
2["exit mutate_param"]
3["exit mutate_param (normal)"]
4["BlockExpr"]
5["* ..."]
6["... = ..."]
7["ExprStmt"]
8["x"]
9["* ..."]

1 --> 7
3 --> 2
4 --> 3
5 --> 11
6 --> 4
7 --> 8
8 --> 5
9 --> 13
10 --> 6
11 --> 9
12 --> 10
13 --> 12
Loading
After
flowchart TD
1["enter mutate_param"]
10["x"]
11["* ..."]
12["... + ..."]
13["x"]
14["* ..."]
15["x"]
2["exit mutate_param"]
3["exit mutate_param (normal)"]
4["x"]
5["Param"]
6["BlockExpr"]
7["* ..."]
8["... = ..."]
9["ExprStmt"]

1 --> 4
3 --> 2
4 -- match --> 5
5 --> 9
6 --> 3
7 --> 13
8 --> 6
9 --> 10
10 --> 7
11 --> 15
12 --> 8
13 --> 11
14 --> 12
15 --> 14
Loading

Match expression

fn match_pattern5() {
    let either = Either::Left(32); // either
    match either { // $ read_access=either
        Either::Left(a3) | Either::Right(a3) // a3
            => print_i64(a3), // $ read_access=a3
    }
}
Before
flowchart TD
1["enter match_pattern5"]
10["MatchExpr"]
11["either"]
12["OrPat"]
13["PathExpr"]
14["CallExpr"]
15["a3"]
2["exit match_pattern5"]
3["exit match_pattern5 (normal)"]
4["BlockExpr"]
5["LetStmt"]
6["either"]
7["PathExpr"]
8["CallExpr"]
9["32"]

1 --> 5
3 --> 2
4 --> 3
5 --> 7
6 -- match, no-match --> 11
7 --> 9
8 --> 6
9 --> 8
10 --> 4
11 --> 12
12 -- match --> 13
13 --> 15
14 --> 10
15 --> 14
Loading
After
flowchart TD
1["enter match_pattern5"]
10["MatchExpr"]
11["either"]
12["TupleStructPat"]
13["[match(true)] OrPat"]
14["a3"]
15["TupleStructPat"]
16["a3"]
17["PathExpr"]
18["CallExpr"]
19["a3"]
2["exit match_pattern5"]
3["exit match_pattern5 (normal)"]
4["BlockExpr"]
5["LetStmt"]
6["either"]
7["PathExpr"]
8["CallExpr"]
9["32"]

1 --> 5
3 --> 2
4 --> 3
5 --> 7
6 -- match --> 11
7 --> 9
8 --> 6
9 --> 8
10 --> 4
11 --> 12
12 -- match --> 14
12 -- no-match --> 15
13 -- match --> 17
14 -- match --> 13
15 -- match --> 16
16 -- match --> 13
17 --> 19
18 --> 10
19 --> 18
Loading

Parameter pattern matching

fn param_pattern2(
    (Either::Left(a9) | Either::Right(a9)): Either // a9
) -> () {
    print_i64(a9); // $ read_access=a9
}
Before
flowchart TD
1["enter param_pattern2"]
2["exit param_pattern2"]
3["exit param_pattern2 (normal)"]
4["BlockExpr"]
5["PathExpr"]
6["CallExpr"]
7["ExprStmt"]
8["a9"]

1 --> 7
3 --> 2
4 --> 3
5 --> 8
6 --> 4
7 --> 5
8 --> 6
Loading
After
flowchart TD
1["enter param_pattern2"]
10["BlockExpr"]
11["PathExpr"]
12["CallExpr"]
13["ExprStmt"]
14["a9"]
2["exit param_pattern2"]
3["exit param_pattern2 (normal)"]
4["Param"]
5["TupleStructPat"]
6["[match(true)] OrPat"]
7["a9"]
8["TupleStructPat"]
9["a9"]

1 --> 5
3 --> 2
4 --> 13
5 -- match --> 7
5 -- no-match --> 8
6 -- match --> 4
7 -- match --> 6
8 -- match --> 9
9 -- match --> 6
10 --> 3
11 --> 14
12 --> 10
13 --> 11
14 --> 12
Loading

@github-actions github-actions bot added C# Ruby Rust Pull requests that update Rust code Swift labels Oct 8, 2024
@hvitved hvitved force-pushed the rust/cfg-patterns branch 9 times, most recently from da0bcd6 to 9ed1394 Compare October 10, 2024 11:22
@hvitved hvitved marked this pull request as ready for review October 10, 2024 11:40
@@ -117,13 +117,61 @@ class BooleanCompletion extends ConditionalCompletion, TBooleanCompletion {
override string toString() { result = "boolean(" + value + ")" }
}

/** Holds if node `pat` has the constant match value `value`. */
Copy link
Contributor

Choose a reason for hiding this comment

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

I think Rust use the term irrefutable pattern for a pattern that cannot fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is correct use of "irrefutable". Irrefutable is property of a pattern, but this predicate is a property of a pattern as an AST node and considers the surrounding AST context as well.

For instance, in

match b { Some(a) => 1, None => 0 }

this predicate holds for None (since the last pattern in a match arm is guaranteed to succeed due to exhaustiveness) but None is not an irrefutable pattern.

I think we should rename this predicate (not sure to what though). We could also extract a proper isIrrefutablePattern predicate from this predicate (but it will probably be a conservative approximation as refutability for custom types depends on the type definition. E.g. the pattern Foo(a) is irrefutable if Foo the only case in the enum and refutable otherwise).

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, you're right. Perhaps isExhaustiveMatch or similar. An irrefutable pattern is exhaustive on its own, and a last pattern in match should be exhaustive.

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds like a great name to me. I'll do a PR :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Done, PR is here #17751

/** Holds if node `pat` has the constant match value `value`. */
pragma[nomagic]
private predicate isMatchConstant(Pat pat, boolean value) {
value = true and
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps remove the value and it seems to be always true

// parameter patterns must be exhaustive
pat = any(Param p).getPat()
) and
not pat = any(ForExpr for).getPat() // workaround until `for` loops are desugared
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I understand why you exclude patterns in for here, they should also never fail

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thats because of how we currently model for loops; the pattern is used to represent the condition.

@@ -141,11 +138,10 @@ class LogicalAndTree extends PostOrderTree, LogicalAndExpr {

class BlockExprTree extends StandardPostOrderTree, BlockExpr {
override AstNode getChildNode(int i) {
result = super.getStmtList().getStatement(i)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change? I see inconsistent use of instanceof SomeClass and extends ..., SomeClass in this file. Wouldn't it be better to consistently use instanceof ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Better to consistently use extends.

// Closures have their own CFG scope, so we need to make sure that their
// CFG is not mixed with the surrounding CFG. This is done by retrofitting
// `first`, `propagatesAbnormal`, and `succ` below.
class ClosureExprTree extends StandardPostOrderTree, ClosureExpr {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I like the way this is formulated. If I understand correctly you want the succ relation of a StandardOrderTree and the first and last predicates of a LeafTree. Perhaps it would be clearer to make this class extend StandardOrderTree and avoid the not succ = this restriction in succ and add an explicit last predicate. It's about the same amount of lines and you avoid the tweaks to "undo" the behaviour of a Post order tree.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, yes, I had completely forgotten about StandardTree.

* Provides `ControlFlowTree`s for patterns.
*
* Since patterns destruct values, they are modeled in pre-order, except for
* `OrPat`s and `IdentPat`s.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are ident and or-patterns different?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because in e @ pat we only "evalute" e when pat matches. And in pat1 | pat2 we need it to be post-order in order to get the correct dominance information needed for guards.


class LiteralPatTree extends LeafTree, LiteralPat { }

class MacroPatTree extends LeafTree, MacroPat { } // todo
Copy link
Contributor

Choose a reason for hiding this comment

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

Done, in another PR ;-)

override Pat getPat(int i) { result = this.getField(i) }
}

class ConstBlockPatTree extends LeafTree, ConstBlockPat { } // todo?
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the constant value inside is evaluated compile-time, so perhaps having this as a LeafTree is the right thing to do.

@hvitved hvitved merged commit ac8b973 into github:main Oct 11, 2024
14 checks passed
@hvitved hvitved deleted the rust/cfg-patterns branch October 11, 2024 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants