@@ -196,11 +196,11 @@ abstract class ItemNode extends Locatable {
196196 this = result .( ImplOrTraitItemNode ) .getAnItemInSelfScope ( )
197197 or
198198 name = "crate" and
199- this = result .( CrateItemNode ) .getARootModuleNode ( )
199+ this = result .( CrateItemNode ) .getASourceFile ( )
200200 or
201201 // todo: implement properly
202202 name = "$crate" and
203- result = any ( CrateItemNode crate | this = crate .getARootModuleNode ( ) ) .( Crate ) .getADependency * ( ) and
203+ result = any ( CrateItemNode crate | this = crate .getASourceFile ( ) ) .( Crate ) .getADependency * ( ) and
204204 result .( CrateItemNode ) .isPotentialDollarCrateTarget ( )
205205 }
206206
@@ -242,12 +242,6 @@ abstract private class ModuleLikeNode extends ItemNode {
242242 not mid instanceof ModuleLikeNode
243243 )
244244 }
245-
246- /**
247- * Holds if this is a root module, meaning either a source file or
248- * the entry module of a crate.
249- */
250- predicate isRoot ( ) { this instanceof SourceFileItemNode }
251245}
252246
253247private class SourceFileItemNode extends ModuleLikeNode , SourceFile {
@@ -269,16 +263,13 @@ private class SourceFileItemNode extends ModuleLikeNode, SourceFile {
269263
270264class CrateItemNode extends ItemNode instanceof Crate {
271265 /**
272- * Gets the module node that defines this crate.
273- *
274- * This is either a source file, when the crate is defined in source code,
275- * or a module, when the crate is defined in a dependency.
266+ * Gets the source file that defines this crate.
276267 */
277268 pragma [ nomagic]
278- ModuleLikeNode getModuleNode ( ) { result = super .getSourceFile ( ) }
269+ SourceFileItemNode getSourceFile ( ) { result = super .getSourceFile ( ) }
279270
280271 /**
281- * Gets a source file that belongs to this crate, if any .
272+ * Gets a source file that belongs to this crate.
282273 *
283274 * This is calculated as those source files that can be reached from the entry
284275 * file of this crate using zero or more `mod` imports, without going through
@@ -296,11 +287,6 @@ class CrateItemNode extends ItemNode instanceof Crate {
296287 )
297288 }
298289
299- /**
300- * Gets a root module node belonging to this crate.
301- */
302- ModuleLikeNode getARootModuleNode ( ) { result = this .getASourceFile ( ) }
303-
304290 pragma [ nomagic]
305291 predicate isPotentialDollarCrateTarget ( ) {
306292 exists ( string name , RelevantPath p |
@@ -657,7 +643,7 @@ private predicate modImport0(Module m, string name, Folder lookup) {
657643 // sibling import
658644 lookup = parent and
659645 (
660- m .getFile ( ) = any ( CrateItemNode c ) .getModuleNode ( ) . ( SourceFileItemNode ) .getFile ( )
646+ m .getFile ( ) = any ( CrateItemNode c ) .getSourceFile ( ) .getFile ( )
661647 or
662648 m .getFile ( ) .getBaseName ( ) = "mod.rs"
663649 )
@@ -745,25 +731,18 @@ private predicate fileImportEdge(Module mod, string name, ItemNode item) {
745731 */
746732pragma [ nomagic]
747733private predicate crateDefEdge ( CrateItemNode c , string name , ItemNode i ) {
748- i = c .getModuleNode ( ) .getASuccessorRec ( name ) and
734+ i = c .getSourceFile ( ) .getASuccessorRec ( name ) and
749735 not i instanceof Crate
750736}
751737
752738/**
753739 * Holds if `m` depends on crate `dep` named `name`.
754740 */
755741private predicate crateDependencyEdge ( ModuleLikeNode m , string name , CrateItemNode dep ) {
756- exists ( CrateItemNode c | dep = c .( Crate ) .getDependency ( name ) |
757- // entry module/entry source file
758- m = c .getModuleNode ( )
759- or
760- // entry/transitive source file
742+ exists ( CrateItemNode c |
743+ dep = c .( Crate ) .getDependency ( name ) and
761744 m = c .getASourceFile ( )
762745 )
763- or
764- // paths inside the crate graph use the name of the crate itself as prefix,
765- // although that is not valid in Rust
766- dep = any ( Crate c | name = c .getName ( ) and m = c .getSourceFile ( ) )
767746}
768747
769748private predicate useTreeDeclares ( UseTree tree , string name ) {
@@ -831,9 +810,9 @@ class RelevantPath extends Path {
831810
832811private predicate isModule ( ItemNode m ) { m instanceof Module }
833812
834- /** Holds if root module `root ` contains the module `m`. */
835- private predicate rootHasModule ( ItemNode root , ItemNode m ) =
836- doublyBoundedFastTC( hasChild / 2 , isRoot / 1 , isModule / 1 ) ( root , m )
813+ /** Holds if source file `source ` contains the module `m`. */
814+ private predicate rootHasModule ( SourceFileItemNode source , ItemNode m ) =
815+ doublyBoundedFastTC( hasChild / 2 , isSourceFile / 1 , isModule / 1 ) ( source , m )
837816
838817pragma [ nomagic]
839818private ItemNode getOuterScope ( ItemNode i ) {
@@ -886,14 +865,14 @@ private ItemNode getASuccessorFull(ItemNode pred, string name, Namespace ns) {
886865 ns = result .getNamespace ( )
887866}
888867
889- private predicate isRoot ( ItemNode root ) { root . ( ModuleLikeNode ) . isRoot ( ) }
868+ private predicate isSourceFile ( ItemNode source ) { source instanceof SourceFileItemNode }
890869
891870private predicate hasCratePath ( ItemNode i ) { any ( RelevantPath path ) .isCratePath ( _, i ) }
892871
893872private predicate hasChild ( ItemNode parent , ItemNode child ) { child .getImmediateParent ( ) = parent }
894873
895- private predicate rootHasCratePathTc ( ItemNode i1 , ItemNode i2 ) =
896- doublyBoundedFastTC( hasChild / 2 , isRoot / 1 , hasCratePath / 1 ) ( i1 , i2 )
874+ private predicate sourceFileHasCratePathTc ( ItemNode i1 , ItemNode i2 ) =
875+ doublyBoundedFastTC( hasChild / 2 , isSourceFile / 1 , hasCratePath / 1 ) ( i1 , i2 )
897876
898877/**
899878 * Holds if the unqualified path `p` references a keyword item named `name`, and
@@ -903,10 +882,10 @@ pragma[nomagic]
903882private predicate keywordLookup ( ItemNode encl , string name , Namespace ns , RelevantPath p ) {
904883 // For `($)crate`, jump directly to the root module
905884 exists ( ItemNode i | p .isCratePath ( name , i ) |
906- encl . ( ModuleLikeNode ) . isRoot ( ) and
885+ encl instanceof SourceFile and
907886 encl = i
908887 or
909- rootHasCratePathTc ( encl , i )
888+ sourceFileHasCratePathTc ( encl , i )
910889 )
911890 or
912891 name = [ "super" , "self" ] and
@@ -1121,12 +1100,8 @@ private predicate preludeEdge(SourceFile f, string name, ItemNode i) {
11211100private import codeql.rust.frameworks.stdlib.Bultins as Builtins
11221101
11231102pragma [ nomagic]
1124- private predicate builtinEdge ( ModuleLikeNode m , string name , ItemNode i ) {
1125- (
1126- m instanceof SourceFile
1127- or
1128- m = any ( CrateItemNode c ) .getModuleNode ( )
1129- ) and
1103+ private predicate builtinEdge ( SourceFile source , string name , ItemNode i ) {
1104+ exists ( source ) and
11301105 exists ( SourceFileItemNode builtins |
11311106 builtins .getFile ( ) .getParentContainer ( ) instanceof Builtins:: BuiltinsFolder and
11321107 i = builtins .getASuccessorRec ( name )
@@ -1138,8 +1113,8 @@ private module Debug {
11381113 private Locatable getRelevantLocatable ( ) {
11391114 exists ( string filepath , int startline , int startcolumn , int endline , int endcolumn |
11401115 result .getLocation ( ) .hasLocationInfo ( filepath , startline , startcolumn , endline , endcolumn ) and
1141- filepath .matches ( "%/test_logging .rs" ) and
1142- startline = 163
1116+ filepath .matches ( "%/main .rs" ) and
1117+ startline = 284
11431118 )
11441119 }
11451120
0 commit comments