Skip to content

Commit b31d5db

Browse files
committed
Rust: Add debug predicate for ItemNode
1 parent 4d91aff commit b31d5db

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

rust/ql/lib/codeql/rust/internal/PathResolution.qll

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ private ItemNode getAChildSuccessor(ItemNode item, string name, SuccessorKind ki
7878
)
7979
}
8080

81+
/**
82+
* Holds if `n` is superceded by an attribute macro expansion. That is, `n` is
83+
* an item or a transitive child of an item with an attribute macro expansion.
84+
*/
85+
predicate supercededByAttributeMacroExpansion(AstNode n) {
86+
n.(Item).hasAttributeMacroExpansion()
87+
or
88+
exists(AstNode parent |
89+
n.getParentNode() = parent and
90+
supercededByAttributeMacroExpansion(parent) and
91+
// Don't exclude expansions themselves as they supercede other nodes.
92+
not n = parent.(Item).getAttributeMacroExpansion() and
93+
// Don't consider attributes themselves to be superceded. E.g., in `#[a] fn
94+
// f() {}` the macro expansion supercedes `fn f() {}` but not `#[a]`.
95+
not n instanceof Attr
96+
)
97+
}
98+
8199
/**
82100
* An item that may be referred to by a path, and which is a node in
83101
* the _item graph_.
@@ -158,7 +176,7 @@ private ItemNode getAChildSuccessor(ItemNode item, string name, SuccessorKind ki
158176
abstract class ItemNode extends Locatable {
159177
ItemNode() {
160178
// Exclude items that are superceded by the expansion of an attribute macro.
161-
not this.(Item).hasAttributeMacroExpansion()
179+
not supercededByAttributeMacroExpansion(this)
162180
}
163181

164182
/** Gets the (original) name of this item. */
@@ -1773,6 +1791,8 @@ private module Debug {
17731791
path = p.toStringDebug()
17741792
}
17751793

1794+
predicate debugItemNode(ItemNode item) { item = getRelevantLocatable() }
1795+
17761796
ItemNode debugResolvePath(RelevantPath path) {
17771797
path = getRelevantLocatable() and
17781798
result = resolvePath(path)

rust/ql/test/library-tests/path-resolution/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ mod impl_with_attribute_macro {
778778

779779
pub fn test() {
780780
// This should resolve to the struct, not the associated type.
781-
let _x: Foo; // $ item=IFoo SPURIOUS: item=IATrait_i64_Foo
781+
let _x: Foo; // $ item=IFoo
782782
} // impl_with_attribute_macro::test
783783
}
784784

rust/ql/test/library-tests/path-resolution/path-resolution.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ resolvePath
354354
| main.rs:773:21:773:23 | i64 | {EXTERNAL LOCATION} | struct i64 |
355355
| main.rs:775:11:775:13 | i64 | {EXTERNAL LOCATION} | struct i64 |
356356
| main.rs:781:17:781:19 | Foo | main.rs:766:5:766:15 | struct Foo |
357-
| main.rs:781:17:781:19 | Foo | main.rs:773:27:776:9 | type Foo |
358357
| main.rs:786:5:786:6 | my | main.rs:1:1:1:7 | mod my |
359358
| main.rs:786:5:786:14 | ...::nested | my.rs:1:1:1:15 | mod nested |
360359
| main.rs:786:5:786:23 | ...::nested1 | my/nested.rs:1:1:17:1 | mod nested1 |

0 commit comments

Comments
 (0)