Skip to content

Commit ebf899e

Browse files
redsun82Copilot
andcommitted
Rust: Split downgrade preservation test
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1a4db88 commit ebf899e

14 files changed

Lines changed: 225 additions & 30 deletions

File tree

codeql-workspace.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ provide:
33
- "*/ql/lib/qlpack.yml"
44
- "*/ql/test*/qlpack.yml"
55
- "*/ql/upgrade-tests/qlpack.yml"
6+
- "*/ql/downgrade-tests/qlpack.yml"
67
- "*/ql/examples/qlpack.yml"
78
- "*/ql/consistency-queries/qlpack.yml"
89
- "*/ql/automodel/src/qlpack.yml"

rust/ql/downgrade-tests/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.testproj/
2+
*.actual
3+
*.log
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.testproj/
2+
*.actual
3+
*.log
4+
Cargo.toml
5+
lib.rs
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Downgrade Regression Test for rust-analyzer 0.0.328 -> 0.0.301
2+
3+
This test verifies that the dbscheme downgrade script correctly preserves recoverable data when migrating databases from the new schema back to the old schema.
4+
5+
## Running the test
6+
7+
```bash
8+
./run-test.sh
9+
```
10+
11+
This will:
12+
1. Build the current extractor
13+
2. Create a database with the new schema from `upgrade_shapes.rs`
14+
3. Validate the new-schema extraction
15+
4. Downgrade the database to the old schema
16+
5. Check out the old commit (491c373e076)
17+
6. Verify that the recovered old-schema properties still match
18+
19+
## Files
20+
21+
- `new.ql` / `new.expected`: Query for the new schema (validates extraction)
22+
- `downgraded.ql` / `downgraded.expected`: Query for the downgraded old schema
23+
- `upgrade_shapes.rs`: Rust source containing test shapes for all affected schema elements
24+
- `run-test.sh`: Test runner script

rust/ql/upgrade-tests/66a489863649185f4a9770f894505803059a1312/downgraded.expected renamed to rust/ql/downgrade-tests/66a489863649185f4a9770f894505803059a1312/downgraded.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
formatArgsArgName
2-
| upgrade_shapes.rs:22:37:22:41 | FormatArgsArg | upgrade_shapes.rs:22:37:22:37 | (no string representation) | upgrade_shapes.rs:22:41:22:41 | 1 | 37 |
3-
| upgrade_shapes.rs:22:44:22:48 | FormatArgsArg | upgrade_shapes.rs:22:44:22:44 | (no string representation) | upgrade_shapes.rs:22:48:22:48 | 2 | 44 |
2+
| upgrade_shapes.rs:22:37:22:41 | FormatArgsArg | upgrade_shapes.rs:22:13:22:49 | (no string representation) | upgrade_shapes.rs:22:41:22:41 | 1 | 13 |
3+
| upgrade_shapes.rs:22:44:22:48 | FormatArgsArg | upgrade_shapes.rs:22:13:22:49 | (no string representation) | upgrade_shapes.rs:22:48:22:48 | 2 | 13 |
44
tryBlock
55
| upgrade_shapes.rs:21:13:21:21 | { ... } |
66
structFieldDefault

rust/ql/upgrade-tests/66a489863649185f4a9770f894505803059a1312/downgraded.ql renamed to rust/ql/downgrade-tests/66a489863649185f4a9770f894505803059a1312/downgraded.ql

File renamed without changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
formatArgNamePreserved
2+
| upgrade_shapes.rs:22:37:22:41 | FormatArgsArg | upgrade_shapes.rs:22:37:22:39 | FormatArgsArgName | upgrade_shapes.rs:22:41:22:41 | 1 | 37 | 1 |
3+
| upgrade_shapes.rs:22:44:22:48 | FormatArgsArg | upgrade_shapes.rs:22:44:22:46 | FormatArgsArgName | upgrade_shapes.rs:22:48:22:48 | 2 | 44 | 2 |
4+
tryBlockModifierPreserved
5+
| upgrade_shapes.rs:21:13:21:21 | { ... } | upgrade_shapes.rs:21:13:21:15 | TryBlockModifier |
6+
structFieldDefaultPreserved
7+
| upgrade_shapes.rs:9:5:9:17 | field: u8 | upgrade_shapes.rs:9:17:9:17 | ConstArg | upgrade_shapes.rs:9:17:9:17 | 1 |
8+
variantDiscriminantPreserved
9+
| upgrade_shapes.rs:13:5:13:9 | V | upgrade_shapes.rs:13:9:13:9 | ConstArg | upgrade_shapes.rs:13:9:13:9 | 2 |
10+
pathMetaPreserved
11+
| upgrade_shapes.rs:4:3:4:11 | PathMeta | path_meta |
12+
| upgrade_shapes.rs:7:10:7:18 | PathMeta | path_meta |
13+
keyValueMetaPreserved
14+
| upgrade_shapes.rs:5:3:5:15 | KeyValueMeta | key_value | upgrade_shapes.rs:5:15:5:15 | 1 |
15+
tokenTreeMetaPreserved
16+
| upgrade_shapes.rs:6:3:6:18 | TokenTreeMeta | token_tree | upgrade_shapes.rs:6:13:6:18 | TokenTree |
17+
unsafeMetaPreserved
18+
| upgrade_shapes.rs:7:3:7:19 | UnsafeMeta | upgrade_shapes.rs:7:10:7:18 | PathMeta | path_meta |
19+
traitAliasPreserved
20+
| upgrade_shapes.rs:16:1:18:12 | trait Alias | upgrade_shapes.rs:16:7:16:11 | Alias | upgrade_shapes.rs:16:18:16:22 | ... | upgrade_shapes.rs:17:1:18:11 | WhereClause |
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import codeql.rust.elements
2+
3+
private predicate inUpgradeShapesFile(Locatable loc) {
4+
loc.getFile().getBaseName() = "upgrade_shapes.rs"
5+
}
6+
7+
query predicate formatArgNamePreserved(
8+
FormatArgsArg arg, FormatArgsArgName argName, Expr expr, int argNameColumn, string exprText
9+
) {
10+
inUpgradeShapesFile(arg) and
11+
argName = arg.getArgName() and
12+
expr = arg.getExpr() and
13+
argNameColumn = argName.getLocation().getStartColumn() and
14+
exprText = expr.toString()
15+
}
16+
17+
query predicate tryBlockModifierPreserved(BlockExpr block, TryBlockModifier modifier) {
18+
inUpgradeShapesFile(block) and
19+
modifier = block.getTryBlockModifier() and
20+
modifier.isTry() and
21+
not modifier.hasTypeRepr()
22+
}
23+
24+
query predicate structFieldDefaultPreserved(StructField field, ConstArg defaultVal, Expr expr) {
25+
inUpgradeShapesFile(field) and
26+
defaultVal = field.getDefaultVal() and
27+
expr = defaultVal.getExpr()
28+
}
29+
30+
query predicate variantDiscriminantPreserved(Variant variant, ConstArg constArg, Expr expr) {
31+
inUpgradeShapesFile(variant) and
32+
constArg = variant.getConstArg() and
33+
expr = constArg.getExpr()
34+
}
35+
36+
query predicate pathMetaPreserved(PathMeta meta, string pathText) {
37+
inUpgradeShapesFile(meta) and
38+
pathText = meta.getPath().getText() and
39+
pathText = "path_meta"
40+
}
41+
42+
query predicate keyValueMetaPreserved(KeyValueMeta meta, string pathText, Expr expr) {
43+
inUpgradeShapesFile(meta) and
44+
pathText = meta.getPath().getText() and
45+
pathText = "key_value" and
46+
expr = meta.getExpr()
47+
}
48+
49+
query predicate tokenTreeMetaPreserved(TokenTreeMeta meta, string pathText, TokenTree tokenTree) {
50+
inUpgradeShapesFile(meta) and
51+
pathText = meta.getPath().getText() and
52+
pathText = "token_tree" and
53+
tokenTree = meta.getTokenTree()
54+
}
55+
56+
query predicate unsafeMetaPreserved(UnsafeMeta meta, PathMeta inner, string pathText) {
57+
inUpgradeShapesFile(meta) and
58+
meta.isUnsafe() and
59+
inner = meta.getMeta() and
60+
pathText = inner.getPath().getText() and
61+
pathText = "path_meta"
62+
}
63+
64+
query predicate traitAliasPreserved(
65+
Trait trait, Name name, TypeBoundList bounds, WhereClause whereClause
66+
) {
67+
inUpgradeShapesFile(trait) and
68+
name = trait.getName() and
69+
name.getText() = "Alias" and
70+
bounds = trait.getTypeBoundList() and
71+
whereClause = trait.getWhereClause() and
72+
not trait.hasAssocItemList()
73+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env bash
2+
# Manual regression test for the Rust dbscheme downgrade from rust-analyzer 0.0.328 to 0.0.301.
3+
# See README.md for details.
4+
5+
set -euo pipefail
6+
7+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8+
REPO_ROOT="$(git rev-parse --show-toplevel)"
9+
OLD_COMMIT="${OLD_COMMIT:-491c373e076}" # origin/main at time of this upgrade
10+
OLD_DBSCHEME="rust/downgrades/109496fd2f20f28a35e50b110859e74882ee80d6/rust.dbscheme"
11+
12+
cd "$REPO_ROOT"
13+
14+
if ! git diff --quiet HEAD || [ -n "$(git ls-files --others --exclude-standard)" ]; then
15+
echo "ERROR: Working directory has uncommitted changes." >&2
16+
echo "Please commit or stash your changes before running this test." >&2
17+
exit 1
18+
fi
19+
20+
ORIGINAL_REF="$(git rev-parse --abbrev-ref HEAD)"
21+
if [ "$ORIGINAL_REF" = "HEAD" ]; then
22+
ORIGINAL_REF="$(git rev-parse HEAD)"
23+
fi
24+
25+
restore_ref() {
26+
echo "==> Restoring original ref ($ORIGINAL_REF)..."
27+
git checkout --quiet "$ORIGINAL_REF"
28+
}
29+
trap 'restore_ref' EXIT
30+
31+
echo "==> Building current extractor (this may take a while)..."
32+
bazel run //rust:install
33+
34+
echo "==> Creating new-schema test database..."
35+
codeql test run \
36+
--search-path . \
37+
--keep-databases \
38+
"$SCRIPT_DIR/new.ql" "$@"
39+
40+
echo "==> Downgrading dataset to old schema..."
41+
DATASET_DIR=("$SCRIPT_DIR"/*.testproj/db-rust)
42+
if [[ ! -d "${DATASET_DIR[0]}" ]]; then
43+
echo "ERROR: No testproj found at $SCRIPT_DIR/*.testproj" >&2
44+
exit 1
45+
fi
46+
codeql dataset upgrade "${DATASET_DIR[0]}" \
47+
--allow-downgrades \
48+
--search-path rust \
49+
--target-dbscheme "$OLD_DBSCHEME"
50+
51+
echo "==> Checking out old commit ($OLD_COMMIT) for downgrade verification..."
52+
git checkout --quiet "$OLD_COMMIT"
53+
git checkout --quiet "$ORIGINAL_REF" -- rust/ql/downgrade-tests codeql-workspace.yml
54+
55+
echo "==> Running preservation test on downgraded dataset..."
56+
codeql test run \
57+
--search-path . \
58+
--dataset "${DATASET_DIR[0]}" \
59+
--check-databases \
60+
"$SCRIPT_DIR/downgraded.ql" "$@"
61+
62+
restore_ref
63+
trap '' EXIT
64+
65+
echo "==> All tests passed!"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![allow(dead_code)]
2+
#![feature(more_qualified_paths)]
3+
4+
#[path_meta]
5+
#[key_value = 1]
6+
#[token_tree(list)]
7+
#[unsafe(path_meta)]
8+
struct S {
9+
field: u8 = 1,
10+
}
11+
12+
enum E {
13+
V = 2,
14+
}
15+
16+
trait Alias<T> = Clone
17+
where
18+
T: Copy;
19+
20+
fn f() {
21+
let _ = try { 1 };
22+
let _ = format_args!("{b} {a}", a = 1, b = 2);
23+
}

0 commit comments

Comments
 (0)