Skip to content

Commit bf4ae80

Browse files
committed
Rust: Account for trait visibility when resolving paths and methods
1 parent 884b94a commit bf4ae80

File tree

8 files changed

+53
-31
lines changed

8 files changed

+53
-31
lines changed

rust/ql/lib/codeql/rust/elements/internal/AssocItemImpl.qll

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ module Impl {
2626
*/
2727
class AssocItem extends Generated::AssocItem {
2828
/** Holds if this item implements trait item `other`. */
29-
pragma[nomagic]
30-
predicate implements(AssocItem other) {
31-
exists(TraitItemNode t, ImplItemNode i, string name |
32-
other = t.getAssocItem(pragma[only_bind_into](name)) and
33-
t = i.resolveTraitTy() and
34-
this = i.getAssocItem(pragma[only_bind_into](name))
35-
)
36-
}
29+
predicate implements(AssocItem other) { this.(AssocItemNode).implements(_, other) }
3730
}
3831
}

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ abstract class ItemNode extends Locatable {
216216
// items made available through `use` are available to nodes that contain the `use`
217217
exists(UseItemNode use |
218218
use = this.getASuccessor(_, _) and
219-
result = use.(ItemNode).getASuccessor(name, kind)
219+
result = use.getASuccessor(name, kind)
220220
)
221221
or
222222
exists(ExternCrateItemNode ec | result = ec.(ItemNode).getASuccessor(name, kind) |
@@ -481,10 +481,20 @@ class ExternCrateItemNode extends ItemNode instanceof ExternCrate {
481481
}
482482

483483
/** An item that can occur in a trait or an `impl` block. */
484-
abstract private class AssocItemNode extends ItemNode instanceof AssocItem {
484+
abstract class AssocItemNode extends ItemNode instanceof AssocItem {
485485
/** Holds if this associated item has an implementation. */
486486
abstract predicate hasImplementation();
487487

488+
/** Holds if this item implements item `other` of `trait`. */
489+
pragma[nomagic]
490+
predicate implements(TraitItemNode trait, AssocItem other) {
491+
exists(ImplItemNode impl, string name |
492+
other = trait.getAssocItem(pragma[only_bind_into](name)) and
493+
trait = impl.resolveTraitTy() and
494+
this = impl.getAssocItem(pragma[only_bind_into](name))
495+
)
496+
}
497+
488498
override predicate hasCanonicalPath(Crate c) { this.hasCanonicalPathPrefix(c) }
489499

490500
bindingset[c]
@@ -1311,6 +1321,7 @@ private predicate declares(ItemNode item, Namespace ns, string name) {
13111321
class RelevantPath extends Path {
13121322
RelevantPath() { not this = any(VariableAccess va).(PathExpr).getPath() }
13131323

1324+
/** Holds if this is an unqualified path with the textual value `name`. */
13141325
pragma[nomagic]
13151326
predicate isUnqualified(string name) {
13161327
not exists(this.getQualifier()) and
@@ -1421,6 +1432,12 @@ private ItemNode unqualifiedPathLookup(RelevantPath p, Namespace ns, SuccessorKi
14211432
pragma[nomagic]
14221433
private predicate isUnqualifiedSelfPath(RelevantPath path) { path.isUnqualified("Self") }
14231434

1435+
/** Holds if the trait `trait` is visible at the element `element`. */
1436+
bindingset[element, trait]
1437+
predicate traitIsVisible(Element element, TraitItemNode trait) {
1438+
exists(ItemNode encl | encl.getADescendant*() = element and trait = encl.getASuccessor(_, _))
1439+
}
1440+
14241441
pragma[nomagic]
14251442
private ItemNode resolvePathCand0(RelevantPath path, Namespace ns) {
14261443
exists(ItemNode res |
@@ -1454,6 +1471,14 @@ private ItemNode resolvePathCandQualified(
14541471
q = resolvePathCandQualifier(qualifier, path, name) and
14551472
result = getASuccessor(q, name, ns, kind) and
14561473
kind.isExternalOrBoth()
1474+
|
1475+
// When the result is an associated item of a trait implementation the
1476+
// implemented trait must be visible.
1477+
exists(TraitItemNode trait |
1478+
result.(AssocItemNode).implements(trait, _) and traitIsVisible(path, trait)
1479+
)
1480+
or
1481+
not exists(ImplItemNode impl | impl.getAnAssocItem() = result and impl.(Impl).hasTrait())
14571482
)
14581483
}
14591484

@@ -1606,8 +1631,16 @@ private predicate useImportEdge(Use use, string name, ItemNode item, SuccessorKi
16061631
not tree.hasRename() and
16071632
name = item.getName()
16081633
or
1609-
name = tree.getRename().getName().getText() and
1610-
name != "_"
1634+
exists(Rename rename | rename = tree.getRename() |
1635+
name = rename.getName().getText()
1636+
or
1637+
// When the rename doesn't have a name it's an underscore import. This
1638+
// makes the imported item visible but unnameable. We represent this
1639+
// by using the name `_` which can never occur in a path. See also:
1640+
// https://doc.rust-lang.org/reference/items/use-declarations.html#r-items.use.as-underscore
1641+
not rename.hasName() and
1642+
name = "_"
1643+
)
16111644
)
16121645
)
16131646
)
@@ -1671,7 +1704,7 @@ private module Debug {
16711704
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
16721705
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
16731706
filepath.matches("%/main.rs") and
1674-
startline = 52
1707+
startline = [463 .. 511]
16751708
)
16761709
}
16771710

@@ -1693,7 +1726,7 @@ private module Debug {
16931726
useImportEdge(use, name, item, kind)
16941727
}
16951728

1696-
ItemNode debuggetASuccessor(ItemNode i, string name, SuccessorKind kind) {
1729+
ItemNode debugGetASuccessor(ItemNode i, string name, SuccessorKind kind) {
16971730
i = getRelevantLocatable() and
16981731
result = i.getASuccessor(name, kind)
16991732
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,7 @@ private predicate methodCandidate(Type type, string name, int arity, Impl impl)
18911891
*/
18921892
pragma[nomagic]
18931893
private predicate methodCandidateTrait(Type type, Trait trait, string name, int arity, Impl impl) {
1894-
trait = resolvePath(impl.(ImplItemNode).getTraitPath()) and
1894+
trait = impl.(ImplItemNode).resolveTraitTy() and
18951895
methodCandidate(type, name, arity, impl)
18961896
}
18971897

@@ -1912,7 +1912,12 @@ private module IsInstantiationOfInput implements IsInstantiationOfInputSig<Metho
19121912
methodCandidateTrait(rootType, mc.getTrait(), name, arity, impl)
19131913
or
19141914
not exists(mc.getTrait()) and
1915-
methodCandidate(rootType, name, arity, impl)
1915+
methodCandidate(rootType, name, arity, impl) and
1916+
(
1917+
traitIsVisible(mc, impl.(ImplItemNode).resolveTraitTy())
1918+
or
1919+
not exists(impl.(ImplItemNode).resolveTraitTy())
1920+
)
19161921
)
19171922
}
19181923

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
multipleCallTargets
22
| main.rs:118:9:118:11 | f(...) |
3-
| main.rs:494:13:494:27 | ...::a_method(...) |
4-
| main.rs:498:13:498:27 | ...::a_method(...) |
5-
| main.rs:502:13:502:27 | ...::a_method(...) |
63
| proc_macro.rs:9:5:9:10 | ...::new(...) |

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,15 @@ mod trait_visibility {
491491
let x = X; // $ item=X
492492
{
493493
use m::Foo; // $ item=Foo
494-
X::a_method(&x); // $ item=X_Foo::a_method SPURIOUS: item=X_Bar::a_method
494+
X::a_method(&x); // $ item=X_Foo::a_method
495495
}
496496
{
497497
use m::Bar; // $ item=Bar
498-
X::a_method(&x); // $ item=X_Bar::a_method SPURIOUS: item=X_Foo::a_method
498+
X::a_method(&x); // $ item=X_Bar::a_method
499499
}
500500
{
501501
use m::Bar as _; // $ item=Bar
502-
X::a_method(&x); // $ item=X_Bar::a_method SPURIOUS: item=X_Foo::a_method
502+
X::a_method(&x); // $ item=X_Bar::a_method
503503
}
504504
} // trait_visibility::f
505505
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,13 @@ resolvePath
236236
| main.rs:493:17:493:22 | ...::Foo | main.rs:465:9:467:9 | trait Foo |
237237
| main.rs:494:13:494:13 | X | main.rs:473:9:473:21 | struct X |
238238
| main.rs:494:13:494:23 | ...::a_method | main.rs:475:26:478:13 | fn a_method |
239-
| main.rs:494:13:494:23 | ...::a_method | main.rs:481:26:484:13 | fn a_method |
240239
| main.rs:497:17:497:17 | m | main.rs:464:5:486:5 | mod m |
241240
| main.rs:497:17:497:22 | ...::Bar | main.rs:469:9:471:9 | trait Bar |
242241
| main.rs:498:13:498:13 | X | main.rs:473:9:473:21 | struct X |
243-
| main.rs:498:13:498:23 | ...::a_method | main.rs:475:26:478:13 | fn a_method |
244242
| main.rs:498:13:498:23 | ...::a_method | main.rs:481:26:484:13 | fn a_method |
245243
| main.rs:501:17:501:17 | m | main.rs:464:5:486:5 | mod m |
246244
| main.rs:501:17:501:22 | ...::Bar | main.rs:469:9:471:9 | trait Bar |
247245
| main.rs:502:13:502:13 | X | main.rs:473:9:473:21 | struct X |
248-
| main.rs:502:13:502:23 | ...::a_method | main.rs:475:26:478:13 | fn a_method |
249246
| main.rs:502:13:502:23 | ...::a_method | main.rs:481:26:484:13 | fn a_method |
250247
| main.rs:515:10:515:16 | MyTrait | main.rs:508:5:510:5 | trait MyTrait |
251248
| main.rs:516:9:516:9 | S | main.rs:512:5:512:13 | struct S |

rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
multipleCallTargets
22
| dereference.rs:61:15:61:24 | e1.deref() |
3-
| main.rs:153:13:153:24 | x.a_method() |
4-
| main.rs:157:13:157:24 | x.a_method() |
5-
| main.rs:161:13:161:24 | x.a_method() |
63
| main.rs:2357:13:2357:31 | ...::from(...) |
74
| main.rs:2358:13:2358:31 | ...::from(...) |
85
| main.rs:2359:13:2359:31 | ...::from(...) |

rust/ql/test/library-tests/type-inference/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ mod trait_visibility {
150150
let x = X;
151151
{
152152
use m::Foo;
153-
x.a_method(); // $ target=Foo::a_method SPURIOUS: target=Bar::a_method
153+
x.a_method(); // $ target=Foo::a_method
154154
}
155155
{
156156
use m::Bar;
157-
x.a_method(); // $ target=Bar::a_method SPURIOUS: target=Foo::a_method
157+
x.a_method(); // $ target=Bar::a_method
158158
}
159159
{
160160
use m::Bar as _;
161-
x.a_method(); // $ target=Bar::a_method SPURIOUS: target=Foo::a_method
161+
x.a_method(); // $ target=Bar::a_method
162162
}
163163
{
164164
use m::Bar;

0 commit comments

Comments
 (0)