Rust: Changes based on PR comments

This commit is contained in:
Simon Friis Vindum
2025-02-21 11:15:54 +01:00
parent 65bce9d7e0
commit df2a110b41
5 changed files with 72 additions and 21 deletions

View File

@@ -122,23 +122,17 @@ class BooleanCompletion extends ConditionalCompletion, TBooleanCompletion {
* does not mean that `pat` is irrefutable, as its children might be the cause
* of a failure.
*/
private predicate canCauseMatchFailure(Pat pat) {
pat instanceof LiteralPat
or
// NOTE: a `TupleStructPat` can cause a failure if it resolves to a an enum
// variant but not when it resolves to a tuple struct.
pat instanceof TupleStructPat
or
pat instanceof SlicePat
or
pat instanceof PathPat
or
pat instanceof OrPat
or
private predicate cannotCauseMatchFailure(Pat pat) {
pat instanceof RangePat or
// Identifier patterns that are in fact path patterns can cause failures. For
// instance `None`. Only if a `@ ...` part is present can we be sure that it's
// an actual identifier pattern.
pat = any(IdentPat p | not p.hasPat())
// instance `None`. Only if an `@ ...` part is present can we be sure that
// it's an actual identifier pattern.
pat = any(IdentPat p | p.hasPat()) or
pat instanceof WildcardPat or
pat instanceof RestPat or
pat instanceof RefPat or
pat instanceof TuplePat or
pat instanceof MacroPat
}
/**
@@ -168,7 +162,7 @@ private predicate guaranteedMatchPosition(Pat pat) {
}
private predicate guaranteedMatch(Pat pat) {
(not canCauseMatchFailure(pat) or guaranteedMatchPosition(pat)) and
(cannotCauseMatchFailure(pat) or guaranteedMatchPosition(pat)) and
// In `for` loops we use a no-match edge from the pattern to terminate the
// loop, hence we special case and always allow the no-match edge.
not pat = any(ForExpr for).getPat()

View File

@@ -18,8 +18,7 @@ module Impl {
*/
class Pat extends Generated::Pat {
/**
* If this pattern is immediately nested within another pattern, then get the
* parent pattern.
* Gets the pattern under which this pattern is immediately nested, if any.
*/
Pat getParentPat() {
result = getImmediateParent(this)

View File

@@ -612,9 +612,12 @@ dominates
| test.rs:387:5:391:5 | enter fn empty_struct_pattern | test.rs:387:5:391:5 | enter fn empty_struct_pattern |
| test.rs:393:5:398:5 | enter fn struct_pattern | test.rs:393:5:398:5 | enter fn struct_pattern |
| test.rs:393:5:398:5 | enter fn struct_pattern | test.rs:394:9:397:9 | match st { ... } |
| test.rs:393:5:398:5 | enter fn struct_pattern | test.rs:395:27:395:27 | 1 |
| test.rs:393:5:398:5 | enter fn struct_pattern | test.rs:395:34:395:34 | 0 |
| test.rs:393:5:398:5 | enter fn struct_pattern | test.rs:396:13:396:26 | MyStruct {...} |
| test.rs:394:9:397:9 | match st { ... } | test.rs:394:9:397:9 | match st { ... } |
| test.rs:395:27:395:27 | 1 | test.rs:395:27:395:27 | 1 |
| test.rs:395:27:395:27 | 1 | test.rs:395:34:395:34 | 0 |
| test.rs:395:34:395:34 | 0 | test.rs:395:34:395:34 | 0 |
| test.rs:396:13:396:26 | MyStruct {...} | test.rs:396:13:396:26 | MyStruct {...} |
| test.rs:400:5:407:5 | enter fn range_pattern | test.rs:400:5:407:5 | enter fn range_pattern |
@@ -727,11 +730,26 @@ dominates
| test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code | test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code |
| test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code | test.rs:461:9:464:9 | match pair { ... } |
| test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code | test.rs:462:18:462:34 | MyStruct {...} |
| test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code | test.rs:462:32:462:32 | _ |
| test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code | test.rs:463:13:463:13 | _ |
| test.rs:461:9:464:9 | match pair { ... } | test.rs:461:9:464:9 | match pair { ... } |
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:462:18:462:34 | MyStruct {...} |
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:462:32:462:32 | _ |
| test.rs:462:32:462:32 | _ | test.rs:462:32:462:32 | _ |
| test.rs:463:13:463:13 | _ | test.rs:463:13:463:13 | _ |
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:475:5:481:5 | enter fn enum_pattern |
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:476:9:480:9 | match e { ... } |
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:477:32:477:32 | _ |
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:478:13:478:27 | TupleVariant(...) |
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:478:26:478:26 | _ |
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:479:13:479:23 | UnitVariant |
| test.rs:476:9:480:9 | match e { ... } | test.rs:476:9:480:9 | match e { ... } |
| test.rs:477:32:477:32 | _ | test.rs:477:32:477:32 | _ |
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:478:13:478:27 | TupleVariant(...) |
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:478:26:478:26 | _ |
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:479:13:479:23 | UnitVariant |
| test.rs:478:26:478:26 | _ | test.rs:478:26:478:26 | _ |
| test.rs:479:13:479:23 | UnitVariant | test.rs:479:13:479:23 | UnitVariant |
| test.rs:485:5:490:5 | enter fn test_infinite_loop | test.rs:485:5:490:5 | enter fn test_infinite_loop |
| test.rs:485:5:490:5 | enter fn test_infinite_loop | test.rs:487:13:487:14 | TupleExpr |
| test.rs:487:13:487:14 | TupleExpr | test.rs:487:13:487:14 | TupleExpr |
@@ -1319,8 +1337,10 @@ postDominance
| test.rs:393:5:398:5 | enter fn struct_pattern | test.rs:393:5:398:5 | enter fn struct_pattern |
| test.rs:394:9:397:9 | match st { ... } | test.rs:393:5:398:5 | enter fn struct_pattern |
| test.rs:394:9:397:9 | match st { ... } | test.rs:394:9:397:9 | match st { ... } |
| test.rs:394:9:397:9 | match st { ... } | test.rs:395:27:395:27 | 1 |
| test.rs:394:9:397:9 | match st { ... } | test.rs:395:34:395:34 | 0 |
| test.rs:394:9:397:9 | match st { ... } | test.rs:396:13:396:26 | MyStruct {...} |
| test.rs:395:27:395:27 | 1 | test.rs:395:27:395:27 | 1 |
| test.rs:395:34:395:34 | 0 | test.rs:395:34:395:34 | 0 |
| test.rs:396:13:396:26 | MyStruct {...} | test.rs:396:13:396:26 | MyStruct {...} |
| test.rs:400:5:407:5 | enter fn range_pattern | test.rs:400:5:407:5 | enter fn range_pattern |
@@ -1416,10 +1436,22 @@ postDominance
| test.rs:461:9:464:9 | match pair { ... } | test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code |
| test.rs:461:9:464:9 | match pair { ... } | test.rs:461:9:464:9 | match pair { ... } |
| test.rs:461:9:464:9 | match pair { ... } | test.rs:462:18:462:34 | MyStruct {...} |
| test.rs:461:9:464:9 | match pair { ... } | test.rs:462:32:462:32 | _ |
| test.rs:461:9:464:9 | match pair { ... } | test.rs:463:13:463:13 | _ |
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:462:18:462:34 | MyStruct {...} |
| test.rs:462:32:462:32 | _ | test.rs:462:32:462:32 | _ |
| test.rs:463:13:463:13 | _ | test.rs:463:13:463:13 | _ |
| test.rs:475:5:481:5 | enter fn enum_pattern | test.rs:475:5:481:5 | enter fn enum_pattern |
| test.rs:476:9:480:9 | match e { ... } | test.rs:475:5:481:5 | enter fn enum_pattern |
| test.rs:476:9:480:9 | match e { ... } | test.rs:476:9:480:9 | match e { ... } |
| test.rs:476:9:480:9 | match e { ... } | test.rs:477:32:477:32 | _ |
| test.rs:476:9:480:9 | match e { ... } | test.rs:478:13:478:27 | TupleVariant(...) |
| test.rs:476:9:480:9 | match e { ... } | test.rs:478:26:478:26 | _ |
| test.rs:476:9:480:9 | match e { ... } | test.rs:479:13:479:23 | UnitVariant |
| test.rs:477:32:477:32 | _ | test.rs:477:32:477:32 | _ |
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:478:13:478:27 | TupleVariant(...) |
| test.rs:478:26:478:26 | _ | test.rs:478:26:478:26 | _ |
| test.rs:479:13:479:23 | UnitVariant | test.rs:479:13:479:23 | UnitVariant |
| test.rs:485:5:490:5 | enter fn test_infinite_loop | test.rs:485:5:490:5 | enter fn test_infinite_loop |
| test.rs:487:13:487:14 | TupleExpr | test.rs:487:13:487:14 | TupleExpr |
| test.rs:494:5:496:5 | enter fn say_hello | test.rs:494:5:496:5 | enter fn say_hello |
@@ -1682,7 +1714,8 @@ immediateDominator
| test.rs:369:26:369:28 | ret | test.rs:369:18:369:20 | ret |
| test.rs:370:13:370:16 | None | test.rs:367:5:373:5 | enter fn test_let_with_return |
| test.rs:394:9:397:9 | match st { ... } | test.rs:393:5:398:5 | enter fn struct_pattern |
| test.rs:395:34:395:34 | 0 | test.rs:393:5:398:5 | enter fn struct_pattern |
| test.rs:395:27:395:27 | 1 | test.rs:393:5:398:5 | enter fn struct_pattern |
| test.rs:395:34:395:34 | 0 | test.rs:395:27:395:27 | 1 |
| test.rs:396:13:396:26 | MyStruct {...} | test.rs:393:5:398:5 | enter fn struct_pattern |
| test.rs:401:9:406:9 | match 42 { ... } | test.rs:400:5:407:5 | enter fn range_pattern |
| test.rs:402:20:402:20 | 1 | test.rs:400:5:407:5 | enter fn range_pattern |
@@ -1725,7 +1758,13 @@ immediateDominator
| test.rs:456:13:456:13 | _ | test.rs:455:13:455:25 | [match(false)] 1 \| 2 |
| test.rs:461:9:464:9 | match pair { ... } | test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code |
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code |
| test.rs:462:32:462:32 | _ | test.rs:462:18:462:34 | MyStruct {...} |
| test.rs:463:13:463:13 | _ | test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code |
| test.rs:476:9:480:9 | match e { ... } | test.rs:475:5:481:5 | enter fn enum_pattern |
| test.rs:477:32:477:32 | _ | test.rs:475:5:481:5 | enter fn enum_pattern |
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:475:5:481:5 | enter fn enum_pattern |
| test.rs:478:26:478:26 | _ | test.rs:478:13:478:27 | TupleVariant(...) |
| test.rs:479:13:479:23 | UnitVariant | test.rs:478:13:478:27 | TupleVariant(...) |
| test.rs:487:13:487:14 | TupleExpr | test.rs:485:5:490:5 | enter fn test_infinite_loop |
| test.rs:511:28:516:9 | exit { ... } (normal) | test.rs:511:28:516:9 | enter { ... } |
| test.rs:512:13:514:13 | if b {...} | test.rs:511:28:516:9 | enter { ... } |
@@ -2236,6 +2275,8 @@ joinBlockPredecessor
| test.rs:370:13:370:16 | None | test.rs:369:18:369:20 | ret | 0 |
| test.rs:394:9:397:9 | match st { ... } | test.rs:395:34:395:34 | 0 | 0 |
| test.rs:394:9:397:9 | match st { ... } | test.rs:396:13:396:26 | MyStruct {...} | 1 |
| test.rs:396:13:396:26 | MyStruct {...} | test.rs:393:5:398:5 | enter fn struct_pattern | 1 |
| test.rs:396:13:396:26 | MyStruct {...} | test.rs:395:27:395:27 | 1 | 0 |
| test.rs:401:9:406:9 | match 42 { ... } | test.rs:402:20:402:20 | 1 | 0 |
| test.rs:401:9:406:9 | match 42 { ... } | test.rs:403:21:403:21 | 2 | 1 |
| test.rs:401:9:406:9 | match 42 { ... } | test.rs:404:20:404:20 | 3 | 2 |
@@ -2270,8 +2311,13 @@ joinBlockPredecessor
| test.rs:455:13:455:25 | [match(true)] 1 \| 2 | test.rs:455:13:455:25 | 2 | 0 |
| test.rs:455:13:455:25 | one_or_two!... | test.rs:455:13:455:25 | [match(false)] 1 \| 2 | 0 |
| test.rs:455:13:455:25 | one_or_two!... | test.rs:455:13:455:25 | [match(true)] 1 \| 2 | 1 |
| test.rs:461:9:464:9 | match pair { ... } | test.rs:462:18:462:34 | MyStruct {...} | 0 |
| test.rs:461:9:464:9 | match pair { ... } | test.rs:462:32:462:32 | _ | 0 |
| test.rs:461:9:464:9 | match pair { ... } | test.rs:463:13:463:13 | _ | 1 |
| test.rs:463:13:463:13 | _ | test.rs:460:5:465:5 | enter fn irrefutable_pattern_and_dead_code | 1 |
| test.rs:463:13:463:13 | _ | test.rs:462:18:462:34 | MyStruct {...} | 0 |
| test.rs:476:9:480:9 | match e { ... } | test.rs:477:32:477:32 | _ | 0 |
| test.rs:476:9:480:9 | match e { ... } | test.rs:478:26:478:26 | _ | 1 |
| test.rs:476:9:480:9 | match e { ... } | test.rs:479:13:479:23 | UnitVariant | 2 |
| test.rs:487:13:487:14 | TupleExpr | test.rs:485:5:490:5 | enter fn test_infinite_loop | 1 |
| test.rs:487:13:487:14 | TupleExpr | test.rs:487:13:487:14 | TupleExpr | 0 |
| test.rs:511:28:516:9 | exit { ... } (normal) | test.rs:512:13:514:13 | if b {...} | 1 |

View File

@@ -969,6 +969,7 @@ edges
| test.rs:394:9:397:9 | match st { ... } | test.rs:393:44:398:5 | { ... } | |
| test.rs:394:15:394:16 | st | test.rs:395:13:395:29 | MyStruct {...} | |
| test.rs:395:13:395:29 | MyStruct {...} | test.rs:395:27:395:27 | 1 | match |
| test.rs:395:13:395:29 | MyStruct {...} | test.rs:396:13:396:26 | MyStruct {...} | no-match |
| test.rs:395:27:395:27 | 1 | test.rs:395:27:395:27 | 1 | |
| test.rs:395:27:395:27 | 1 | test.rs:395:34:395:34 | 0 | match |
| test.rs:395:27:395:27 | 1 | test.rs:396:13:396:26 | MyStruct {...} | no-match |
@@ -1159,6 +1160,7 @@ edges
| test.rs:462:15:462:15 | n | test.rs:462:18:462:34 | MyStruct {...} | match |
| test.rs:462:15:462:15 | n | test.rs:463:13:463:13 | _ | no-match |
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:462:32:462:32 | _ | match |
| test.rs:462:18:462:34 | MyStruct {...} | test.rs:463:13:463:13 | _ | no-match |
| test.rs:462:32:462:32 | _ | test.rs:462:40:462:40 | n | match |
| test.rs:462:40:462:40 | n | test.rs:461:9:464:9 | match pair { ... } | |
| test.rs:463:13:463:13 | _ | test.rs:463:18:463:18 | 0 | match |
@@ -1172,8 +1174,16 @@ edges
| test.rs:476:9:480:9 | match e { ... } | test.rs:475:39:481:5 | { ... } | |
| test.rs:476:15:476:15 | e | test.rs:477:13:477:34 | StructVariant {...} | |
| test.rs:477:13:477:34 | StructVariant {...} | test.rs:477:32:477:32 | _ | match |
| test.rs:477:13:477:34 | StructVariant {...} | test.rs:478:13:478:27 | TupleVariant(...) | no-match |
| test.rs:477:32:477:32 | _ | test.rs:477:39:477:39 | 0 | match |
| test.rs:477:39:477:39 | 0 | test.rs:476:9:480:9 | match e { ... } | |
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:478:26:478:26 | _ | match |
| test.rs:478:13:478:27 | TupleVariant(...) | test.rs:479:13:479:23 | UnitVariant | no-match |
| test.rs:478:26:478:26 | _ | test.rs:478:32:478:32 | 1 | match |
| test.rs:478:32:478:32 | 1 | test.rs:476:9:480:9 | match e { ... } | |
| test.rs:479:13:479:23 | UnitVariant | test.rs:479:13:479:23 | UnitVariant | |
| test.rs:479:13:479:23 | UnitVariant | test.rs:479:28:479:28 | 2 | match |
| test.rs:479:28:479:28 | 2 | test.rs:476:9:480:9 | match e { ... } | |
| test.rs:485:5:490:5 | enter fn test_infinite_loop | test.rs:486:9:488:9 | ExprStmt | |
| test.rs:486:9:488:9 | ExprStmt | test.rs:487:13:487:14 | TupleExpr | |
| test.rs:486:14:488:9 | { ... } | test.rs:487:13:487:14 | TupleExpr | |

View File

@@ -401,6 +401,7 @@ edges
| main.rs:171:5:180:5 | match msg { ... } | main.rs:168:21:181:1 | { ... } | |
| main.rs:171:11:171:13 | msg | main.rs:172:9:174:9 | ...::Hello {...} | |
| main.rs:172:9:174:9 | ...::Hello {...} | main.rs:173:31:173:35 | RangePat | match |
| main.rs:172:9:174:9 | ...::Hello {...} | main.rs:175:9:175:38 | ...::Hello {...} | no-match |
| main.rs:173:17:173:27 | id_variable | main.rs:173:17:173:35 | id_variable @ ... | |
| main.rs:173:17:173:35 | id_variable @ ... | main.rs:174:14:174:22 | print_i64 | match |
| main.rs:173:31:173:31 | 3 | main.rs:173:31:173:31 | 3 | |
@@ -414,6 +415,7 @@ edges
| main.rs:174:14:174:35 | print_i64(...) | main.rs:171:5:180:5 | match msg { ... } | |
| main.rs:174:24:174:34 | id_variable | main.rs:174:14:174:35 | print_i64(...) | |
| main.rs:175:9:175:38 | ...::Hello {...} | main.rs:175:30:175:36 | RangePat | match |
| main.rs:175:9:175:38 | ...::Hello {...} | main.rs:178:9:178:29 | ...::Hello {...} | no-match |
| main.rs:175:30:175:31 | 10 | main.rs:175:30:175:31 | 10 | |
| main.rs:175:30:175:31 | 10 | main.rs:175:35:175:36 | 12 | match |
| main.rs:175:30:175:31 | 10 | main.rs:178:9:178:29 | ...::Hello {...} | no-match |