From 766af355b53b3eb414b2f23c5578c137144b2774 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Mon, 19 May 2025 14:14:57 +0200 Subject: [PATCH] Rust: Follow-up work to make path resolution and type inference tests pass again --- rust/ql/lib/codeql/files/FileSystem.qll | 4 +- .../rust/elements/internal/AstNodeImpl.qll | 19 ++++- .../rust/elements/internal/LocatableImpl.qll | 2 +- .../rust/elements/internal/LocationImpl.qll | 73 +++++++++++++++++-- .../rust/elements/internal/MacroCallImpl.qll | 16 ++++ .../codeql/rust/frameworks/stdlib/Stdlib.qll | 13 ++-- .../codeql/rust/internal/PathResolution.qll | 59 +++++---------- .../codeql/rust/internal/TypeInference.qll | 2 +- .../PathResolutionInlineExpectationsTest.qll | 5 +- rust/ql/test/TestUtils.qll | 14 +++- .../test/library-tests/path-resolution/my.rs | 10 +-- .../path-resolution/path-resolution.expected | 10 +-- .../path-resolution/path-resolution.ql | 4 +- .../type-inference/type-inference.expected | 66 +++++++++-------- .../type-inference/type-inference.ql | 12 ++- 15 files changed, 198 insertions(+), 111 deletions(-) diff --git a/rust/ql/lib/codeql/files/FileSystem.qll b/rust/ql/lib/codeql/files/FileSystem.qll index 5a60d28418eb..05890128757f 100644 --- a/rust/ql/lib/codeql/files/FileSystem.qll +++ b/rust/ql/lib/codeql/files/FileSystem.qll @@ -40,7 +40,9 @@ module Folder = Impl::Folder; class File extends Container, Impl::File { /** Holds if this file was extracted from ordinary source code. */ predicate fromSource() { - exists(ExtractorStep s | s.getAction() = "Extract" and s.getFile() = this) + exists(ExtractorStep s | s.getAction() = "Extract" and s.getFile() = this) and + // instead use special extractor step for dependencies? + exists(this.getRelativePath()) } /** diff --git a/rust/ql/lib/codeql/rust/elements/internal/AstNodeImpl.qll b/rust/ql/lib/codeql/rust/elements/internal/AstNodeImpl.qll index f75294f3d10e..b80da6d7084f 100644 --- a/rust/ql/lib/codeql/rust/elements/internal/AstNodeImpl.qll +++ b/rust/ql/lib/codeql/rust/elements/internal/AstNodeImpl.qll @@ -15,6 +15,7 @@ module Impl { private import rust private import codeql.rust.elements.internal.generated.ParentChild private import codeql.rust.controlflow.ControlFlowGraph + private import codeql.rust.elements.internal.MacroCallImpl::Impl as MacroCallImpl /** * Gets the immediate parent of a non-`AstNode` element `e`. @@ -59,10 +60,20 @@ module Impl { } /** Holds if this node is inside a macro expansion. */ - predicate isInMacroExpansion() { - this = any(MacroCall mc).getMacroCallExpansion() - or - this.getParentNode().isInMacroExpansion() + predicate isInMacroExpansion() { MacroCallImpl::isInMacroExpansion(_, this) } + + /** + * Holds if this node exists only as the result of a macro expansion. + * + * This is the same as `isInMacroExpansion()`, but excludes AST nodes corresponding + * to macro arguments. + */ + pragma[nomagic] + predicate isFromMacroExpansion() { + exists(MacroCall mc | + MacroCallImpl::isInMacroExpansion(mc, this) and + not this = mc.getATokenTreeNode() + ) } /** diff --git a/rust/ql/lib/codeql/rust/elements/internal/LocatableImpl.qll b/rust/ql/lib/codeql/rust/elements/internal/LocatableImpl.qll index ed349df48681..fcb5289e0493 100644 --- a/rust/ql/lib/codeql/rust/elements/internal/LocatableImpl.qll +++ b/rust/ql/lib/codeql/rust/elements/internal/LocatableImpl.qll @@ -43,7 +43,7 @@ module Impl { File getFile() { result = this.getLocation().getFile() } /** Holds if this element is from source code. */ - predicate fromSource() { exists(this.getFile().getRelativePath()) } + predicate fromSource() { this.getFile().fromSource() } } private @location_default getDbLocation(Locatable l) { diff --git a/rust/ql/lib/codeql/rust/elements/internal/LocationImpl.qll b/rust/ql/lib/codeql/rust/elements/internal/LocationImpl.qll index 52daf46863b6..65cc6b3bd7c4 100644 --- a/rust/ql/lib/codeql/rust/elements/internal/LocationImpl.qll +++ b/rust/ql/lib/codeql/rust/elements/internal/LocationImpl.qll @@ -77,13 +77,76 @@ module LocationImpl { ) } - /** Holds if this location starts strictly before the specified location. */ + /** Holds if this location starts before location `that`. */ pragma[inline] - predicate strictlyBefore(Location other) { - this.getStartLine() < other.getStartLine() - or - this.getStartLine() = other.getStartLine() and this.getStartColumn() < other.getStartColumn() + predicate startsBefore(Location that) { + exists(string f, int sl1, int sc1, int sl2, int sc2 | + this.hasLocationInfo(f, sl1, sc1, _, _) and + that.hasLocationInfo(f, sl2, sc2, _, _) + | + sl1 < sl2 + or + sl1 = sl2 and sc1 <= sc2 + ) + } + + /** Holds if this location starts strictly before location `that`. */ + pragma[inline] + predicate startsStrictlyBefore(Location that) { + exists(string f, int sl1, int sc1, int sl2, int sc2 | + this.hasLocationInfo(f, sl1, sc1, _, _) and + that.hasLocationInfo(f, sl2, sc2, _, _) + | + sl1 < sl2 + or + sl1 = sl2 and sc1 < sc2 + ) + } + + /** Holds if this location ends after location `that`. */ + pragma[inline] + predicate endsAfter(Location that) { + exists(string f, int el1, int ec1, int el2, int ec2 | + this.hasLocationInfo(f, _, _, el1, ec1) and + that.hasLocationInfo(f, _, _, el2, ec2) + | + el1 > el2 + or + el1 = el2 and ec1 >= ec2 + ) } + + /** Holds if this location ends strictly after location `that`. */ + pragma[inline] + predicate endsStrictlyAfter(Location that) { + exists(string f, int el1, int ec1, int el2, int ec2 | + this.hasLocationInfo(f, _, _, el1, ec1) and + that.hasLocationInfo(f, _, _, el2, ec2) + | + el1 > el2 + or + el1 = el2 and ec1 > ec2 + ) + } + + /** + * Holds if this location contains location `that`, meaning that it starts + * before and ends after it. + */ + pragma[inline] + predicate contains(Location that) { this.startsBefore(that) and this.endsAfter(that) } + + /** + * Holds if this location strictlycontains location `that`, meaning that it starts + * strictly before and ends strictly after it. + */ + pragma[inline] + predicate strictlyContains(Location that) { + this.startsStrictlyBefore(that) and this.endsStrictlyAfter(that) + } + + /** Holds if this location is from source code. */ + predicate fromSource() { this.getFile().fromSource() } } class LocationDefault extends Location, TLocationDefault { diff --git a/rust/ql/lib/codeql/rust/elements/internal/MacroCallImpl.qll b/rust/ql/lib/codeql/rust/elements/internal/MacroCallImpl.qll index c28d08f540b1..f8f96315fd4c 100644 --- a/rust/ql/lib/codeql/rust/elements/internal/MacroCallImpl.qll +++ b/rust/ql/lib/codeql/rust/elements/internal/MacroCallImpl.qll @@ -11,6 +11,15 @@ private import codeql.rust.elements.internal.generated.MacroCall * be referenced directly. */ module Impl { + private import rust + + pragma[nomagic] + predicate isInMacroExpansion(MacroCall mc, AstNode n) { + n = mc.getMacroCallExpansion() + or + isInMacroExpansion(mc, n.getParentNode()) + } + // the following QLdoc is generated: if you need to edit it, do it in the schema file /** * A MacroCall. For example: @@ -20,5 +29,12 @@ module Impl { */ class MacroCall extends Generated::MacroCall { override string toStringImpl() { result = this.getPath().toAbbreviatedString() + "!..." } + + /** Gets an AST node whose location is inside the token tree belonging to this macro call. */ + pragma[nomagic] + AstNode getATokenTreeNode() { + isInMacroExpansion(this, result) and + this.getTokenTree().getLocation().contains(result.getLocation()) + } } } diff --git a/rust/ql/lib/codeql/rust/frameworks/stdlib/Stdlib.qll b/rust/ql/lib/codeql/rust/frameworks/stdlib/Stdlib.qll index 33cc6c359d8c..2054f7d640f4 100644 --- a/rust/ql/lib/codeql/rust/frameworks/stdlib/Stdlib.qll +++ b/rust/ql/lib/codeql/rust/frameworks/stdlib/Stdlib.qll @@ -7,6 +7,7 @@ private import codeql.rust.Concepts private import codeql.rust.controlflow.ControlFlowGraph as Cfg private import codeql.rust.controlflow.CfgNodes as CfgNodes private import codeql.rust.dataflow.DataFlow +private import codeql.rust.internal.PathResolution /** * A call to the `starts_with` method on a `Path`. @@ -32,10 +33,8 @@ class OptionEnum extends Enum { // todo: replace with canonical path, once calculated in QL exists(Crate core, Module m | core.getName() = "core" and - m = core.getSourceFile().getAnItem() and - m.getName().getText() = "option" and - this = m.getItemList().getAnItem() and - this.getName().getText() = "Option" + m = core.getSourceFile().(ItemNode).getASuccessor("option") and + this = m.(ItemNode).getASuccessor("Option") ) } @@ -53,10 +52,8 @@ class ResultEnum extends Enum { // todo: replace with canonical path, once calculated in QL exists(Crate core, Module m | core.getName() = "core" and - m = core.getSourceFile().getAnItem() and - m.getName().getText() = "result" and - this = m.getItemList().getAnItem() and - this.getName().getText() = "Result" + m = core.getSourceFile().(ItemNode).getASuccessor("result") and + this = m.(ItemNode).getASuccessor("Result") ) } diff --git a/rust/ql/lib/codeql/rust/internal/PathResolution.qll b/rust/ql/lib/codeql/rust/internal/PathResolution.qll index 3cd185e7dfd9..ceb039ab7255 100644 --- a/rust/ql/lib/codeql/rust/internal/PathResolution.qll +++ b/rust/ql/lib/codeql/rust/internal/PathResolution.qll @@ -194,11 +194,11 @@ abstract class ItemNode extends Locatable { this = result.(ImplOrTraitItemNode).getAnItemInSelfScope() or name = "crate" and - this = result.(CrateItemNode).getARootModuleNode() + this = result.(CrateItemNode).getASourceFile() or // todo: implement properly name = "$crate" and - result = any(CrateItemNode crate | this = crate.getARootModuleNode()).(Crate).getADependency*() and + result = any(CrateItemNode crate | this = crate.getASourceFile()).(Crate).getADependency*() and result.(CrateItemNode).isPotentialDollarCrateTarget() } @@ -240,12 +240,6 @@ abstract private class ModuleLikeNode extends ItemNode { not mid instanceof ModuleLikeNode ) } - - /** - * Holds if this is a root module, meaning either a source file or - * the entry module of a crate. - */ - predicate isRoot() { this instanceof SourceFileItemNode } } private class SourceFileItemNode extends ModuleLikeNode, SourceFile { @@ -267,16 +261,13 @@ private class SourceFileItemNode extends ModuleLikeNode, SourceFile { class CrateItemNode extends ItemNode instanceof Crate { /** - * Gets the module node that defines this crate. - * - * This is either a source file, when the crate is defined in source code, - * or a module, when the crate is defined in a dependency. + * Gets the source file that defines this crate. */ pragma[nomagic] - ModuleLikeNode getModuleNode() { result = super.getSourceFile() } + SourceFileItemNode getSourceFile() { result = super.getSourceFile() } /** - * Gets a source file that belongs to this crate, if any. + * Gets a source file that belongs to this crate. * * This is calculated as those source files that can be reached from the entry * file of this crate using zero or more `mod` imports, without going through @@ -294,11 +285,6 @@ class CrateItemNode extends ItemNode instanceof Crate { ) } - /** - * Gets a root module node belonging to this crate. - */ - ModuleLikeNode getARootModuleNode() { result = this.getASourceFile() } - pragma[nomagic] predicate isPotentialDollarCrateTarget() { exists(string name, RelevantPath p | @@ -710,7 +696,7 @@ private predicate modImport0(Module m, string name, Folder lookup) { // sibling import lookup = parent and ( - m.getFile() = any(CrateItemNode c).getModuleNode().(SourceFileItemNode).getFile() + m.getFile() = any(CrateItemNode c).getSourceFile().getFile() or m.getFile().getBaseName() = "mod.rs" ) @@ -798,7 +784,7 @@ private predicate fileImportEdge(Module mod, string name, ItemNode item) { */ pragma[nomagic] private predicate crateDefEdge(CrateItemNode c, string name, ItemNode i) { - i = c.getModuleNode().getASuccessorRec(name) and + i = c.getSourceFile().getASuccessorRec(name) and not i instanceof Crate } @@ -806,17 +792,10 @@ private predicate crateDefEdge(CrateItemNode c, string name, ItemNode i) { * Holds if `m` depends on crate `dep` named `name`. */ private predicate crateDependencyEdge(ModuleLikeNode m, string name, CrateItemNode dep) { - exists(CrateItemNode c | dep = c.(Crate).getDependency(name) | - // entry module/entry source file - m = c.getModuleNode() - or - // entry/transitive source file + exists(CrateItemNode c | + dep = c.(Crate).getDependency(name) and m = c.getASourceFile() ) - or - // paths inside the crate graph use the name of the crate itself as prefix, - // although that is not valid in Rust - dep = any(Crate c | name = c.getName() and m = c.getSourceFile()) } private predicate useTreeDeclares(UseTree tree, string name) { @@ -884,9 +863,9 @@ class RelevantPath extends Path { private predicate isModule(ItemNode m) { m instanceof Module } -/** Holds if root module `root` contains the module `m`. */ -private predicate rootHasModule(ItemNode root, ItemNode m) = - doublyBoundedFastTC(hasChild/2, isRoot/1, isModule/1)(root, m) +/** Holds if source file `source` contains the module `m`. */ +private predicate rootHasModule(SourceFileItemNode source, ItemNode m) = + doublyBoundedFastTC(hasChild/2, isSourceFile/1, isModule/1)(source, m) pragma[nomagic] private ItemNode getOuterScope(ItemNode i) { @@ -939,14 +918,14 @@ private ItemNode getASuccessorFull(ItemNode pred, string name, Namespace ns) { ns = result.getNamespace() } -private predicate isRoot(ItemNode root) { root.(ModuleLikeNode).isRoot() } +private predicate isSourceFile(ItemNode source) { source instanceof SourceFileItemNode } private predicate hasCratePath(ItemNode i) { any(RelevantPath path).isCratePath(_, i) } private predicate hasChild(ItemNode parent, ItemNode child) { child.getImmediateParent() = parent } -private predicate rootHasCratePathTc(ItemNode i1, ItemNode i2) = - doublyBoundedFastTC(hasChild/2, isRoot/1, hasCratePath/1)(i1, i2) +private predicate sourceFileHasCratePathTc(ItemNode i1, ItemNode i2) = + doublyBoundedFastTC(hasChild/2, isSourceFile/1, hasCratePath/1)(i1, i2) /** * Holds if the unqualified path `p` references a keyword item named `name`, and @@ -956,10 +935,10 @@ pragma[nomagic] private predicate keywordLookup(ItemNode encl, string name, Namespace ns, RelevantPath p) { // For `($)crate`, jump directly to the root module exists(ItemNode i | p.isCratePath(name, i) | - encl.(ModuleLikeNode).isRoot() and + encl instanceof SourceFile and encl = i or - rootHasCratePathTc(encl, i) + sourceFileHasCratePathTc(encl, i) ) or name = ["super", "self"] and @@ -1176,8 +1155,8 @@ private module Debug { private Locatable getRelevantLocatable() { exists(string filepath, int startline, int startcolumn, int endline, int endcolumn | result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and - filepath.matches("%/test_logging.rs") and - startline = 163 + filepath.matches("%/main.rs") and + startline = 284 ) } diff --git a/rust/ql/lib/codeql/rust/internal/TypeInference.qll b/rust/ql/lib/codeql/rust/internal/TypeInference.qll index cb9450a84d77..980c848439ea 100644 --- a/rust/ql/lib/codeql/rust/internal/TypeInference.qll +++ b/rust/ql/lib/codeql/rust/internal/TypeInference.qll @@ -1042,7 +1042,7 @@ private module Debug { exists(string filepath, int startline, int startcolumn, int endline, int endcolumn | result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and filepath.matches("%/main.rs") and - startline = 28 + startline = 948 ) } diff --git a/rust/ql/lib/utils/test/PathResolutionInlineExpectationsTest.qll b/rust/ql/lib/utils/test/PathResolutionInlineExpectationsTest.qll index cd82003feac1..e6cf20da84dc 100644 --- a/rust/ql/lib/utils/test/PathResolutionInlineExpectationsTest.qll +++ b/rust/ql/lib/utils/test/PathResolutionInlineExpectationsTest.qll @@ -18,7 +18,8 @@ private module ResolveTest implements TestSig { private predicate commmentAt(string text, string filepath, int line) { exists(Comment c | c.getLocation().hasLocationInfo(filepath, line, _, _, _) and - c.getCommentText().trim() = text + c.getCommentText().trim() = text and + c.fromSource() ) } @@ -35,6 +36,8 @@ private module ResolveTest implements TestSig { exists(AstNode n | not n = any(Path parent).getQualifier() and location = n.getLocation() and + n.fromSource() and + not n.isFromMacroExpansion() and element = n.toString() and tag = "item" | diff --git a/rust/ql/test/TestUtils.qll b/rust/ql/test/TestUtils.qll index f5b1f846657a..586989321e16 100644 --- a/rust/ql/test/TestUtils.qll +++ b/rust/ql/test/TestUtils.qll @@ -1,12 +1,20 @@ private import rust -predicate toBeTested(Element e) { not e instanceof CrateElement and not e instanceof Builtin } +predicate toBeTested(Element e) { + not e instanceof CrateElement and + not e instanceof Builtin and + ( + not e instanceof Locatable + or + e.(Locatable).fromSource() + ) and + not e.(AstNode).isFromMacroExpansion() +} class CrateElement extends Element { CrateElement() { this instanceof Crate or - this instanceof NamedCrate or - any(Crate c).getSourceFile() = this.(AstNode).getParentNode*() + this instanceof NamedCrate } } diff --git a/rust/ql/test/library-tests/path-resolution/my.rs b/rust/ql/test/library-tests/path-resolution/my.rs index 8a94c169f6fe..a17cedc2608c 100644 --- a/rust/ql/test/library-tests/path-resolution/my.rs +++ b/rust/ql/test/library-tests/path-resolution/my.rs @@ -16,13 +16,13 @@ mod my4 { } pub use my4::my5::f as nested_f; // $ item=I201 - +#[rustfmt::skip] type Result< T, // T > = ::std::result::Result< T, // $ item=T - String, ->; // my::Result + String,> // $ item=Result +; // my::Result fn int_div( x: i32, // @@ -30,7 +30,7 @@ fn int_div( ) -> Result // $ item=my::Result { if y == 0 { - return Err("Div by zero".to_string()); + return Err("Div by zero".to_string()); // $ item=Err } - Ok(x / y) + Ok(x / y) // $ item=Ok } diff --git a/rust/ql/test/library-tests/path-resolution/path-resolution.expected b/rust/ql/test/library-tests/path-resolution/path-resolution.expected index 90af94e91d00..e8ce954a7d49 100644 --- a/rust/ql/test/library-tests/path-resolution/path-resolution.expected +++ b/rust/ql/test/library-tests/path-resolution/path-resolution.expected @@ -349,12 +349,12 @@ resolvePath | my.rs:18:9:18:16 | ...::my5 | my.rs:15:5:15:16 | mod my5 | | my.rs:18:9:18:19 | ...::f | my/my4/my5/mod.rs:1:1:3:1 | fn f | | my.rs:22:5:22:9 | std | file:///RUSTUP_HOME/toolchain/lib/rustlib/src/rust/library/std/src/lib.rs:0:0:0:0 | Crate(std@0.0.0) | -| my.rs:22:5:22:17 | ...::result | file://:0:0:0:0 | mod result | -| my.rs:22:5:25:1 | ...::Result::<...> | file://:0:0:0:0 | enum Result | +| my.rs:22:5:22:17 | ...::result | file:///RUSTUP_HOME/toolchain/lib/rustlib/src/rust/library/core/src/lib.rs:356:1:356:15 | mod result | +| my.rs:22:5:24:12 | ...::Result::<...> | file:///RUSTUP_HOME/toolchain/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | enum Result | | my.rs:23:5:23:5 | T | my.rs:21:5:21:5 | T | -| my.rs:30:6:30:16 | Result::<...> | my.rs:20:1:25:2 | type Result<...> | -| my.rs:33:16:33:18 | Err | file://:0:0:0:0 | Err | -| my.rs:35:5:35:6 | Ok | file://:0:0:0:0 | Ok | +| my.rs:30:6:30:16 | Result::<...> | my.rs:18:34:25:1 | type Result<...> | +| my.rs:33:16:33:18 | Err | file:///RUSTUP_HOME/toolchain/lib/rustlib/src/rust/library/core/src/result.rs:534:5:537:56 | Err | +| my.rs:35:5:35:6 | Ok | file:///RUSTUP_HOME/toolchain/lib/rustlib/src/rust/library/core/src/result.rs:529:5:532:55 | Ok | | my/nested.rs:9:13:9:13 | f | my/nested.rs:3:9:5:9 | fn f | | my/nested.rs:15:9:15:15 | nested2 | my/nested.rs:2:5:11:5 | mod nested2 | | my/nested.rs:15:9:15:18 | ...::f | my/nested.rs:3:9:5:9 | fn f | diff --git a/rust/ql/test/library-tests/path-resolution/path-resolution.ql b/rust/ql/test/library-tests/path-resolution/path-resolution.ql index bd522597a2e4..3ba34f6a9a30 100644 --- a/rust/ql/test/library-tests/path-resolution/path-resolution.ql +++ b/rust/ql/test/library-tests/path-resolution/path-resolution.ql @@ -17,5 +17,7 @@ class ItemNodeLoc extends Locatable instanceof ItemNode { } query predicate resolvePath(Path p, ItemNodeLoc i) { - toBeTested(p) and not p.isInMacroExpansion() and i = resolvePath(p) + toBeTested(p) and + not p.isFromMacroExpansion() and + i = resolvePath(p) } diff --git a/rust/ql/test/library-tests/type-inference/type-inference.expected b/rust/ql/test/library-tests/type-inference/type-inference.expected index 42e5d90701b9..c459059201ea 100644 --- a/rust/ql/test/library-tests/type-inference/type-inference.expected +++ b/rust/ql/test/library-tests/type-inference/type-inference.expected @@ -597,7 +597,9 @@ inferType | main.rs:658:19:658:22 | self | Fst | main.rs:656:10:656:12 | Fst | | main.rs:658:19:658:22 | self | Snd | main.rs:656:15:656:17 | Snd | | main.rs:659:43:659:82 | MacroExpr | | main.rs:656:15:656:17 | Snd | +| main.rs:659:50:659:81 | MacroExpr | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:549:1:584:1 | Arguments | | main.rs:660:43:660:81 | MacroExpr | | main.rs:656:15:656:17 | Snd | +| main.rs:660:50:660:80 | MacroExpr | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:549:1:584:1 | Arguments | | main.rs:661:37:661:39 | snd | | main.rs:656:15:656:17 | Snd | | main.rs:661:45:661:47 | snd | | main.rs:656:15:656:17 | Snd | | main.rs:662:41:662:43 | snd | | main.rs:656:15:656:17 | Snd | @@ -1005,92 +1007,92 @@ inferType | main.rs:918:15:918:16 | &x | | file://:0:0:0:0 | & | | main.rs:918:15:918:16 | &x | &T | main.rs:894:5:894:13 | S | | main.rs:918:16:918:16 | x | | main.rs:894:5:894:13 | S | -| main.rs:932:43:935:5 | { ... } | | file://:0:0:0:0 | Result | +| main.rs:932:43:935:5 | { ... } | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:932:43:935:5 | { ... } | E | main.rs:925:5:926:14 | S1 | | main.rs:932:43:935:5 | { ... } | T | main.rs:925:5:926:14 | S1 | | main.rs:933:13:933:13 | x | | main.rs:925:5:926:14 | S1 | -| main.rs:933:17:933:30 | ...::Ok(...) | | file://:0:0:0:0 | Result | +| main.rs:933:17:933:30 | ...::Ok(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:933:17:933:30 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 | | main.rs:933:17:933:31 | TryExpr | | main.rs:925:5:926:14 | S1 | | main.rs:933:28:933:29 | S1 | | main.rs:925:5:926:14 | S1 | -| main.rs:934:9:934:22 | ...::Ok(...) | | file://:0:0:0:0 | Result | +| main.rs:934:9:934:22 | ...::Ok(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:934:9:934:22 | ...::Ok(...) | E | main.rs:925:5:926:14 | S1 | | main.rs:934:9:934:22 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 | | main.rs:934:20:934:21 | S1 | | main.rs:925:5:926:14 | S1 | -| main.rs:938:46:942:5 | { ... } | | file://:0:0:0:0 | Result | +| main.rs:938:46:942:5 | { ... } | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:938:46:942:5 | { ... } | E | main.rs:928:5:929:14 | S2 | | main.rs:938:46:942:5 | { ... } | T | main.rs:925:5:926:14 | S1 | -| main.rs:939:13:939:13 | x | | file://:0:0:0:0 | Result | +| main.rs:939:13:939:13 | x | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:939:13:939:13 | x | T | main.rs:925:5:926:14 | S1 | -| main.rs:939:17:939:30 | ...::Ok(...) | | file://:0:0:0:0 | Result | +| main.rs:939:17:939:30 | ...::Ok(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:939:17:939:30 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 | | main.rs:939:28:939:29 | S1 | | main.rs:925:5:926:14 | S1 | | main.rs:940:13:940:13 | y | | main.rs:925:5:926:14 | S1 | -| main.rs:940:17:940:17 | x | | file://:0:0:0:0 | Result | +| main.rs:940:17:940:17 | x | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:940:17:940:17 | x | T | main.rs:925:5:926:14 | S1 | | main.rs:940:17:940:18 | TryExpr | | main.rs:925:5:926:14 | S1 | -| main.rs:941:9:941:22 | ...::Ok(...) | | file://:0:0:0:0 | Result | +| main.rs:941:9:941:22 | ...::Ok(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:941:9:941:22 | ...::Ok(...) | E | main.rs:928:5:929:14 | S2 | | main.rs:941:9:941:22 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 | | main.rs:941:20:941:21 | S1 | | main.rs:925:5:926:14 | S1 | -| main.rs:945:40:950:5 | { ... } | | file://:0:0:0:0 | Result | +| main.rs:945:40:950:5 | { ... } | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:945:40:950:5 | { ... } | E | main.rs:928:5:929:14 | S2 | | main.rs:945:40:950:5 | { ... } | T | main.rs:925:5:926:14 | S1 | -| main.rs:946:13:946:13 | x | | file://:0:0:0:0 | Result | -| main.rs:946:13:946:13 | x | T | file://:0:0:0:0 | Result | +| main.rs:946:13:946:13 | x | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | +| main.rs:946:13:946:13 | x | T | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:946:13:946:13 | x | T.T | main.rs:925:5:926:14 | S1 | -| main.rs:946:17:946:42 | ...::Ok(...) | | file://:0:0:0:0 | Result | -| main.rs:946:17:946:42 | ...::Ok(...) | T | file://:0:0:0:0 | Result | +| main.rs:946:17:946:42 | ...::Ok(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | +| main.rs:946:17:946:42 | ...::Ok(...) | T | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:946:17:946:42 | ...::Ok(...) | T.T | main.rs:925:5:926:14 | S1 | -| main.rs:946:28:946:41 | ...::Ok(...) | | file://:0:0:0:0 | Result | +| main.rs:946:28:946:41 | ...::Ok(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:946:28:946:41 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 | | main.rs:946:39:946:40 | S1 | | main.rs:925:5:926:14 | S1 | -| main.rs:948:17:948:17 | x | | file://:0:0:0:0 | Result | -| main.rs:948:17:948:17 | x | T | file://:0:0:0:0 | Result | +| main.rs:948:17:948:17 | x | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | +| main.rs:948:17:948:17 | x | T | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:948:17:948:17 | x | T.T | main.rs:925:5:926:14 | S1 | -| main.rs:948:17:948:18 | TryExpr | | file://:0:0:0:0 | Result | +| main.rs:948:17:948:18 | TryExpr | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:948:17:948:18 | TryExpr | T | main.rs:925:5:926:14 | S1 | -| main.rs:948:17:948:29 | ... .map(...) | | file://:0:0:0:0 | Result | -| main.rs:949:9:949:22 | ...::Ok(...) | | file://:0:0:0:0 | Result | +| main.rs:948:17:948:29 | ... .map(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | +| main.rs:949:9:949:22 | ...::Ok(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:949:9:949:22 | ...::Ok(...) | E | main.rs:928:5:929:14 | S2 | | main.rs:949:9:949:22 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 | | main.rs:949:20:949:21 | S1 | | main.rs:925:5:926:14 | S1 | -| main.rs:953:30:953:34 | input | | file://:0:0:0:0 | Result | +| main.rs:953:30:953:34 | input | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:953:30:953:34 | input | E | main.rs:925:5:926:14 | S1 | | main.rs:953:30:953:34 | input | T | main.rs:953:20:953:27 | T | -| main.rs:953:69:960:5 | { ... } | | file://:0:0:0:0 | Result | +| main.rs:953:69:960:5 | { ... } | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:953:69:960:5 | { ... } | E | main.rs:925:5:926:14 | S1 | | main.rs:953:69:960:5 | { ... } | T | main.rs:953:20:953:27 | T | | main.rs:954:13:954:17 | value | | main.rs:953:20:953:27 | T | -| main.rs:954:21:954:25 | input | | file://:0:0:0:0 | Result | +| main.rs:954:21:954:25 | input | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:954:21:954:25 | input | E | main.rs:925:5:926:14 | S1 | | main.rs:954:21:954:25 | input | T | main.rs:953:20:953:27 | T | | main.rs:954:21:954:26 | TryExpr | | main.rs:953:20:953:27 | T | -| main.rs:955:22:955:38 | ...::Ok(...) | | file://:0:0:0:0 | Result | +| main.rs:955:22:955:38 | ...::Ok(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:955:22:955:38 | ...::Ok(...) | T | main.rs:953:20:953:27 | T | -| main.rs:955:22:958:10 | ... .and_then(...) | | file://:0:0:0:0 | Result | +| main.rs:955:22:958:10 | ... .and_then(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:955:33:955:37 | value | | main.rs:953:20:953:27 | T | -| main.rs:955:53:958:9 | { ... } | | file://:0:0:0:0 | Result | +| main.rs:955:53:958:9 | { ... } | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:955:53:958:9 | { ... } | E | main.rs:925:5:926:14 | S1 | -| main.rs:957:13:957:34 | ...::Ok::<...>(...) | | file://:0:0:0:0 | Result | +| main.rs:957:13:957:34 | ...::Ok::<...>(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:957:13:957:34 | ...::Ok::<...>(...) | E | main.rs:925:5:926:14 | S1 | -| main.rs:959:9:959:23 | ...::Err(...) | | file://:0:0:0:0 | Result | +| main.rs:959:9:959:23 | ...::Err(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:959:9:959:23 | ...::Err(...) | E | main.rs:925:5:926:14 | S1 | | main.rs:959:9:959:23 | ...::Err(...) | T | main.rs:953:20:953:27 | T | | main.rs:959:21:959:22 | S1 | | main.rs:925:5:926:14 | S1 | -| main.rs:963:37:963:52 | try_same_error(...) | | file://:0:0:0:0 | Result | +| main.rs:963:37:963:52 | try_same_error(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:963:37:963:52 | try_same_error(...) | E | main.rs:925:5:926:14 | S1 | | main.rs:963:37:963:52 | try_same_error(...) | T | main.rs:925:5:926:14 | S1 | -| main.rs:967:37:967:55 | try_convert_error(...) | | file://:0:0:0:0 | Result | +| main.rs:967:37:967:55 | try_convert_error(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:967:37:967:55 | try_convert_error(...) | E | main.rs:928:5:929:14 | S2 | | main.rs:967:37:967:55 | try_convert_error(...) | T | main.rs:925:5:926:14 | S1 | -| main.rs:971:37:971:49 | try_chained(...) | | file://:0:0:0:0 | Result | +| main.rs:971:37:971:49 | try_chained(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:971:37:971:49 | try_chained(...) | E | main.rs:928:5:929:14 | S2 | | main.rs:971:37:971:49 | try_chained(...) | T | main.rs:925:5:926:14 | S1 | -| main.rs:975:37:975:63 | try_complex(...) | | file://:0:0:0:0 | Result | +| main.rs:975:37:975:63 | try_complex(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:975:37:975:63 | try_complex(...) | E | main.rs:925:5:926:14 | S1 | | main.rs:975:37:975:63 | try_complex(...) | T | main.rs:925:5:926:14 | S1 | -| main.rs:975:49:975:62 | ...::Ok(...) | | file://:0:0:0:0 | Result | +| main.rs:975:49:975:62 | ...::Ok(...) | | file:///Users/hvitved/.rustup/toolchains/1.85-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:520:1:538:1 | Result | | main.rs:975:49:975:62 | ...::Ok(...) | E | main.rs:925:5:926:14 | S1 | | main.rs:975:49:975:62 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 | | main.rs:975:60:975:61 | S1 | | main.rs:925:5:926:14 | S1 | diff --git a/rust/ql/test/library-tests/type-inference/type-inference.ql b/rust/ql/test/library-tests/type-inference/type-inference.ql index 2652699558ff..1c7194e5297f 100644 --- a/rust/ql/test/library-tests/type-inference/type-inference.ql +++ b/rust/ql/test/library-tests/type-inference/type-inference.ql @@ -5,16 +5,18 @@ import TypeInference query predicate inferType(AstNode n, TypePath path, Type t) { t = TypeInference::inferType(n, path) and - n.fromSource() + n.fromSource() and + not n.isFromMacroExpansion() } module ResolveTest implements TestSig { string getARelevantTag() { result = ["method", "fieldof"] } private predicate functionHasValue(Function f, string value) { - f.getAPrecedingComment().getCommentText() = value + f.getAPrecedingComment().getCommentText() = value and + f.fromSource() or - not exists(f.getAPrecedingComment()) and + not any(f.getAPrecedingComment()).fromSource() and // TODO: Default to canonical path once that is available value = f.getName().getText() } @@ -22,7 +24,9 @@ module ResolveTest implements TestSig { predicate hasActualResult(Location location, string element, string tag, string value) { exists(AstNode source, AstNode target | location = source.getLocation() and - element = source.toString() + element = source.toString() and + source.fromSource() and + not source.isFromMacroExpansion() | target = resolveMethodCallExpr(source) and functionHasValue(target, value) and