Fix record pattern and pretty-printing

This commit is contained in:
Chris Smowton
2023-11-22 14:36:21 +00:00
parent 419d530a06
commit 4bff7953fc
4 changed files with 45 additions and 10 deletions

View File

@@ -1672,6 +1672,8 @@ class LocalVariableDeclExpr extends Expr, @localvariabledeclexpr {
exists(InstanceOfExpr ioe | this.getParent() = ioe | result.isNthChildOf(ioe, 1))
or
exists(PatternCase pc | this.getParent() = pc | result.isNthChildOf(pc, -2))
or
exists(RecordPatternExpr rpe, int index | this.isNthChildOf(rpe, index) and result.isNthChildOf(rpe, -(index + 1)) )
}
/** Gets the name of the variable declared by this local variable declaration expression. */

View File

@@ -1078,12 +1078,30 @@ private class PpRecordPattern extends PpAst, RecordPatternExpr {
or
i = 1 and result = "("
or
i = 1 + ((any(int x | x >= 1 and exists(this.getSubPattern(x)))) * 2) and result = ", "
i = 1 + ((any(int x | x >= 1 and exists(this.getSubPattern(x)))) * 4) and result = ", "
or
i = 1 + (count(this.getSubPattern(_)) * 2) and result = ")"
i = 1 + (count(this.getSubPattern(_)) * 4) and result = ")"
or
exists(int x, LocalVariableDeclExpr v, int offset | v = this.getSubPattern(x) |
i = (offset) + (x * 4) and
(
offset = 2 and not exists(v.getTypeAccess()) and result = "var"
or
offset = 3 and result = " "
)
)
}
override PpAst getChild(int i) {
exists(int x | result = this.getSubPattern(x) | i = 2 + (x * 2))
exists(int x, PatternExpr subPattern, int offset | subPattern = this.getSubPattern(x) |
i = (offset) + (x * 4) and
(
result = subPattern.(RecordPatternExpr) and offset = 2
or
result = subPattern.(LocalVariableDeclExpr).getTypeAccess() and offset = 2
or
result = subPattern.(LocalVariableDeclExpr) and offset = 4
)
)
}
}

View File

@@ -38,6 +38,13 @@ public class Test {
if (o instanceof String s) { }
if (o instanceof R(S(int x), String y)) { }
switch(o) {
case R(S(var x), var y) -> { }
case null, default -> { }
}
if (o instanceof R(S(var x), var y)) { }
}
}

View File

@@ -46,7 +46,7 @@
| Test.java:1:14:1:17 | Test | 45 | switch (o) { |
| Test.java:1:14:1:17 | Test | 46 | case String s: |
| Test.java:1:14:1:17 | Test | 47 | break; |
| Test.java:1:14:1:17 | Test | 48 | case R(S(x), y): |
| Test.java:1:14:1:17 | Test | 48 | case R(S(int x), String y): |
| Test.java:1:14:1:17 | Test | 49 | break; |
| Test.java:1:14:1:17 | Test | 50 | default: |
| Test.java:1:14:1:17 | Test | 51 | break; |
@@ -54,7 +54,7 @@
| Test.java:1:14:1:17 | Test | 53 | switch (o) { |
| Test.java:1:14:1:17 | Test | 54 | case String s -> { |
| Test.java:1:14:1:17 | Test | 55 | } |
| Test.java:1:14:1:17 | Test | 56 | case R(S(x), y) -> { |
| Test.java:1:14:1:17 | Test | 56 | case R(S(int x), String y) -> { |
| Test.java:1:14:1:17 | Test | 57 | } |
| Test.java:1:14:1:17 | Test | 58 | case default -> { |
| Test.java:1:14:1:17 | Test | 59 | } |
@@ -62,19 +62,27 @@
| Test.java:1:14:1:17 | Test | 61 | var a = switch (o) { |
| Test.java:1:14:1:17 | Test | 62 | case String s: |
| Test.java:1:14:1:17 | Test | 63 | yield 1; |
| Test.java:1:14:1:17 | Test | 64 | case R(S(x), y): |
| Test.java:1:14:1:17 | Test | 64 | case R(S(int x), String y): |
| Test.java:1:14:1:17 | Test | 65 | yield x; |
| Test.java:1:14:1:17 | Test | 66 | case default: |
| Test.java:1:14:1:17 | Test | 67 | yield 2; |
| Test.java:1:14:1:17 | Test | 68 | }; |
| Test.java:1:14:1:17 | Test | 69 | var b = switch (o) { |
| Test.java:1:14:1:17 | Test | 70 | case String s -> 1; |
| Test.java:1:14:1:17 | Test | 71 | case R(S(x), y) -> x; |
| Test.java:1:14:1:17 | Test | 71 | case R(S(int x), String y) -> x; |
| Test.java:1:14:1:17 | Test | 72 | default -> 2; |
| Test.java:1:14:1:17 | Test | 73 | }; |
| Test.java:1:14:1:17 | Test | 74 | if (o instanceof String s) { |
| Test.java:1:14:1:17 | Test | 75 | } |
| Test.java:1:14:1:17 | Test | 76 | if (o instanceof R(S(x), y)) { |
| Test.java:1:14:1:17 | Test | 76 | if (o instanceof R(S(int x), String y)) { |
| Test.java:1:14:1:17 | Test | 77 | } |
| Test.java:1:14:1:17 | Test | 78 | } |
| Test.java:1:14:1:17 | Test | 79 | } |
| Test.java:1:14:1:17 | Test | 78 | switch (o) { |
| Test.java:1:14:1:17 | Test | 79 | case R(S(var x), var y) -> { |
| Test.java:1:14:1:17 | Test | 80 | } |
| Test.java:1:14:1:17 | Test | 81 | case default -> { |
| Test.java:1:14:1:17 | Test | 82 | } |
| Test.java:1:14:1:17 | Test | 83 | } |
| Test.java:1:14:1:17 | Test | 84 | if (o instanceof R(S(var x), var y)) { |
| Test.java:1:14:1:17 | Test | 85 | } |
| Test.java:1:14:1:17 | Test | 86 | } |
| Test.java:1:14:1:17 | Test | 87 | } |