From 5d39237e16a151912dbb55a7fbfbf96db89c8e92 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Mon, 23 Nov 2026 15:52:47 +0100 Subject: [PATCH 1/2] Rust: Add derive path regression tests Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../path-resolution/derive_macro.rs | 41 ++++++++++++++++ .../library-tests/path-resolution/main.rs | 2 + .../path-resolution/path-resolution.expected | 48 +++++++++++++++++-- .../path-resolution/path-resolution.ql | 5 ++ .../path-resolution/proc_macro.rs | 44 +++++++++++++++++ 5 files changed, 135 insertions(+), 5 deletions(-) create mode 100644 rust/ql/test/library-tests/path-resolution/derive_macro.rs diff --git a/rust/ql/test/library-tests/path-resolution/derive_macro.rs b/rust/ql/test/library-tests/path-resolution/derive_macro.rs new file mode 100644 index 000000000000..2cd0865b1afe --- /dev/null +++ b/rust/ql/test/library-tests/path-resolution/derive_macro.rs @@ -0,0 +1,41 @@ +macro_rules! build_via_macro { + () => { + pub struct MacroGenerated; + }; +} + +#[derive(proc_macro::GenerateBuilder)] // $ item=derive +pub struct Config; + +pub fn make() -> ConfigBuilder { // $ MISSING: item=ConfigBuilder + ConfigBuilder // $ MISSING: item=ConfigBuilder +} + +#[derive(proc_macro::GenerateBuilderViaMacro)] // $ item=derive +struct MacroDerive; + +pub fn make_macro_generated() -> MacroGenerated { // $ MISSING: item=MacroGenerated + MacroGenerated // $ MISSING: item=MacroGenerated +} + +#[derive(proc_macro::GenerateBuilderViaAttribute)] // $ item=derive +struct AttributeDerive; + +pub fn make_attribute_generated() -> AttributeGenerated { // $ MISSING: item=AttributeGenerated + AttributeGenerated // $ MISSING: item=AttributeGenerated +} + +mod glob_shadowing { + mod helper { + pub struct GlobCollision; + } + + struct GlobCollision; + + #[derive(proc_macro::GenerateGlobUse)] // $ item=derive + struct DeriveGlobUse; + + fn check() { + let _: GlobCollision; // $ item=GlobCollision + } +} diff --git a/rust/ql/test/library-tests/path-resolution/main.rs b/rust/ql/test/library-tests/path-resolution/main.rs index 781641e98ce0..cbef6a1dffea 100644 --- a/rust/ql/test/library-tests/path-resolution/main.rs +++ b/rust/ql/test/library-tests/path-resolution/main.rs @@ -1191,3 +1191,5 @@ fn main() { impl_with_attribute_macro::test(); // $ item=impl_with_attribute_macro::test patterns::test(); // $ item=patterns::test } + +mod derive_macro; 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 9df115b9bdb1..91781852199b 100644 --- a/rust/ql/test/library-tests/path-resolution/path-resolution.expected +++ b/rust/ql/test/library-tests/path-resolution/path-resolution.expected @@ -1,6 +1,13 @@ +derive_generated_canonical_paths +| derive_macro.rs:8:12:8:17 | struct ConfigBuilder | test::derive_macro::Config::ConfigBuilder | +| derive_macro.rs:14:3:14:45 | struct MacroGenerated | test::derive_macro::MacroDerive::MacroGenerated | +| derive_macro.rs:21:3:21:49 | struct AttributeGenerated | test::derive_macro::AttributeDerive::AttributeGenerated | mod +| derive_macro.rs:28:1:41:1 | mod glob_shadowing | +| derive_macro.rs:29:5:31:5 | mod helper | | invalid/main.rs:8:1:18:1 | mod self_import_from_struct | -| lib.rs:1:1:1:11 | mod my | +| lib.rs:1:1:1:21 | mod derive_macro | +| lib.rs:2:1:2:11 | mod my | | main.rs:1:1:1:7 | mod my | | main.rs:8:1:8:8 | mod my2 | | main.rs:20:1:44:1 | mod m1 | @@ -39,6 +46,7 @@ mod | main.rs:1033:1:1077:1 | mod self_constructors | | main.rs:1079:1:1108:1 | mod self_types | | main.rs:1110:1:1154:1 | mod const_static | +| main.rs:1195:1:1195:17 | mod derive_macro | | my2/mod.rs:1:1:1:16 | mod nested2 | | my2/mod.rs:20:1:20:12 | mod my3 | | my2/mod.rs:22:1:23:10 | mod mymod | @@ -54,6 +62,11 @@ mod | my/nested.rs:1:1:17:1 | mod nested1 | | my/nested.rs:2:5:11:5 | mod nested2 | resolvePath +| derive_macro.rs:7:3:7:8 | derive | {EXTERNAL LOCATION} | MacroDef | +| derive_macro.rs:14:3:14:8 | derive | {EXTERNAL LOCATION} | MacroDef | +| derive_macro.rs:21:3:21:8 | derive | {EXTERNAL LOCATION} | MacroDef | +| derive_macro.rs:35:7:35:12 | derive | {EXTERNAL LOCATION} | MacroDef | +| derive_macro.rs:39:16:39:28 | GlobCollision | derive_macro.rs:33:5:33:25 | struct GlobCollision | | invalid/main.rs:6:9:6:9 | A | invalid/main.rs:3:11:4:9 | struct A | | invalid/main.rs:15:9:15:12 | self | invalid/main.rs:8:1:18:1 | mod self_import_from_struct | | invalid/main.rs:15:9:15:22 | ...::MyStruct | invalid/main.rs:12:5:12:20 | struct MyStruct | @@ -83,7 +96,7 @@ resolvePath | main.rs:37:17:37:24 | ...::f | main.rs:26:9:28:9 | fn f | | main.rs:39:17:39:23 | println | {EXTERNAL LOCATION} | MacroRules | | main.rs:40:17:40:17 | f | main.rs:26:9:28:9 | fn f | -| main.rs:47:9:47:13 | super | main.rs:1:1:1193:2 | SourceFile | +| main.rs:47:9:47:13 | super | main.rs:1:1:1195:18 | SourceFile | | main.rs:47:9:47:17 | ...::m1 | main.rs:20:1:44:1 | mod m1 | | main.rs:47:9:47:21 | ...::m2 | main.rs:25:5:43:5 | mod m2 | | main.rs:47:9:47:24 | ...::g | main.rs:30:9:34:9 | fn g | @@ -98,7 +111,7 @@ resolvePath | main.rs:68:17:68:19 | Foo | main.rs:66:9:66:21 | struct Foo | | main.rs:71:13:71:15 | Foo | main.rs:60:5:60:17 | struct Foo | | main.rs:73:5:73:5 | f | main.rs:62:5:69:5 | fn f | -| main.rs:75:5:75:8 | self | main.rs:1:1:1193:2 | SourceFile | +| main.rs:75:5:75:8 | self | main.rs:1:1:1195:18 | SourceFile | | main.rs:75:5:75:11 | ...::i | main.rs:78:1:90:1 | fn i | | main.rs:79:5:79:11 | println | {EXTERNAL LOCATION} | MacroRules | | main.rs:81:13:81:15 | Foo | main.rs:55:1:55:13 | struct Foo | @@ -120,7 +133,7 @@ resolvePath | main.rs:112:9:112:15 | println | {EXTERNAL LOCATION} | MacroRules | | main.rs:118:9:118:15 | println | {EXTERNAL LOCATION} | MacroRules | | main.rs:122:9:122:15 | println | {EXTERNAL LOCATION} | MacroRules | -| main.rs:125:13:125:17 | super | main.rs:1:1:1193:2 | SourceFile | +| main.rs:125:13:125:17 | super | main.rs:1:1:1195:18 | SourceFile | | main.rs:125:13:125:21 | ...::m5 | main.rs:110:1:114:1 | mod m5 | | main.rs:126:9:126:9 | f | main.rs:111:5:113:5 | fn f | | main.rs:126:9:126:9 | f | main.rs:117:5:119:5 | fn f | @@ -722,7 +735,7 @@ resolvePath | my2/my3/mod.rs:3:5:3:5 | g | my2/mod.rs:3:1:6:1 | fn g | | my2/my3/mod.rs:4:5:4:5 | h | main.rs:57:1:76:1 | fn h | | my2/my3/mod.rs:7:5:7:9 | super | my2/mod.rs:1:1:25:34 | SourceFile | -| my2/my3/mod.rs:7:5:7:16 | ...::super | main.rs:1:1:1193:2 | SourceFile | +| my2/my3/mod.rs:7:5:7:16 | ...::super | main.rs:1:1:1195:18 | SourceFile | | my2/my3/mod.rs:7:5:7:19 | ...::h | main.rs:57:1:76:1 | fn h | | my2/my3/mod.rs:8:5:8:9 | super | my2/mod.rs:1:1:25:34 | SourceFile | | my2/my3/mod.rs:8:5:8:12 | ...::g | my2/mod.rs:3:1:6:1 | fn g | @@ -789,4 +802,29 @@ resolvePath | proc_macro.rs:16:24:16:34 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | | proc_macro.rs:16:43:16:53 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | | proc_macro.rs:16:59:16:69 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | +| proc_macro.rs:21:32:21:42 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | +| proc_macro.rs:21:48:21:58 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | +| proc_macro.rs:22:15:22:17 | syn | {EXTERNAL LOCATION} | Crate(syn@2.0.103) | +| proc_macro.rs:22:15:22:36 | ...::parse_macro_input | {EXTERNAL LOCATION} | MacroRules | +| proc_macro.rs:22:48:22:50 | syn | {EXTERNAL LOCATION} | Crate(syn@2.0.103) | +| proc_macro.rs:22:48:22:63 | ...::DeriveInput | {EXTERNAL LOCATION} | struct DeriveInput | +| proc_macro.rs:22:48:22:63 | ...::parse::<...> | {EXTERNAL LOCATION} | fn parse | +| proc_macro.rs:24:24:24:26 | syn | {EXTERNAL LOCATION} | Crate(syn@2.0.103) | +| proc_macro.rs:24:24:24:33 | ...::Ident | {EXTERNAL LOCATION} | struct Ident | +| proc_macro.rs:24:24:24:38 | ...::new | {EXTERNAL LOCATION} | fn new | +| proc_macro.rs:24:41:24:46 | format | {EXTERNAL LOCATION} | MacroRules | +| proc_macro.rs:25:5:25:9 | quote | {EXTERNAL LOCATION} | MacroRules | +| proc_macro.rs:32:43:32:53 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | +| proc_macro.rs:32:59:32:69 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | +| proc_macro.rs:33:5:33:9 | quote | {EXTERNAL LOCATION} | MacroRules | +| proc_macro.rs:40:47:40:57 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | +| proc_macro.rs:40:63:40:73 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | +| proc_macro.rs:41:5:41:9 | quote | {EXTERNAL LOCATION} | MacroRules | +| proc_macro.rs:49:27:49:37 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | +| proc_macro.rs:49:47:49:57 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | +| proc_macro.rs:49:63:49:73 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | +| proc_macro.rs:50:5:50:9 | quote | {EXTERNAL LOCATION} | MacroRules | +| proc_macro.rs:57:34:57:44 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | +| proc_macro.rs:57:50:57:60 | TokenStream | {EXTERNAL LOCATION} | struct TokenStream | +| proc_macro.rs:58:5:58:9 | quote | {EXTERNAL LOCATION} | MacroRules | testFailures 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 f7955929b839..5d23f5ce69af 100644 --- a/rust/ql/test/library-tests/path-resolution/path-resolution.ql +++ b/rust/ql/test/library-tests/path-resolution/path-resolution.ql @@ -5,6 +5,11 @@ import TestUtils query predicate mod(Module m) { toBeTested(m) } +query predicate derive_generated_canonical_paths(TypeItem i, string path) { + i.getName().getText() = ["ConfigBuilder", "MacroGenerated", "AttributeGenerated"] and + exists(Crate c | c.getName() = "test" and path = i.getCanonicalPath(c)) +} + query predicate resolvePath(PathExt p, ItemNode i) { toBeTested(p) and not p.isFromMacroExpansion() and diff --git a/rust/ql/test/library-tests/path-resolution/proc_macro.rs b/rust/ql/test/library-tests/path-resolution/proc_macro.rs index 11e3a7024ae6..4bb466609fd8 100644 --- a/rust/ql/test/library-tests/path-resolution/proc_macro.rs +++ b/rust/ql/test/library-tests/path-resolution/proc_macro.rs @@ -16,3 +16,47 @@ pub fn add_suffix(attr: TokenStream, item: TokenStream) -> TokenStream { pub fn identity(_attr: TokenStream, item: TokenStream) -> TokenStream { item } + +#[proc_macro_derive(GenerateBuilder)] +pub fn generate_builder(input: TokenStream) -> TokenStream { + let ast = syn::parse_macro_input!(input as syn::DeriveInput); + let name = &ast.ident; + let builder_name = syn::Ident::new(&format!("{}Builder", name), name.span()); + quote! { + pub struct #builder_name; + } + .into() +} + +#[proc_macro_derive(GenerateBuilderViaMacro)] +pub fn generate_builder_via_macro(_input: TokenStream) -> TokenStream { + quote! { + build_via_macro!(); + } + .into() +} + +#[proc_macro_derive(GenerateBuilderViaAttribute)] +pub fn generate_builder_via_attribute(_input: TokenStream) -> TokenStream { + quote! { + #[proc_macro::make_struct] + pub struct AttributeGenerated; + } + .into() +} + +#[proc_macro_attribute] +pub fn make_struct(_attr: TokenStream, _item: TokenStream) -> TokenStream { + quote! { + pub struct AttributeGenerated; + } + .into() +} + +#[proc_macro_derive(GenerateGlobUse)] +pub fn generate_glob_use(_input: TokenStream) -> TokenStream { + quote! { + use helper::*; + } + .into() +} From ca1733f8daa628678d1c96a76a81b09f877455a1 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Mon, 23 Nov 2026 15:54:47 +0100 Subject: [PATCH 2/2] Rust: Fix paths of derive-generated items Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../codeql/rust/internal/PathResolution.qll | 21 +++++++++++++++++-- .../path-resolution/derive_macro.rs | 12 +++++------ .../path-resolution/path-resolution.expected | 12 ++++++++--- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/rust/ql/lib/codeql/rust/internal/PathResolution.qll b/rust/ql/lib/codeql/rust/internal/PathResolution.qll index 30ddb6f12c36..56e6479dcf58 100644 --- a/rust/ql/lib/codeql/rust/internal/PathResolution.qll +++ b/rust/ql/lib/codeql/rust/internal/PathResolution.qll @@ -170,6 +170,16 @@ private module UseOption = Option; private class UseOption = UseOption::Option; +private predicate isDerivedExpansionItem(TypeItem type, Item item) { + item = type.getDeriveMacroExpansion(_).getItem(_) + or + exists(Item wrapper | isDerivedExpansionItem(type, wrapper) | + item = wrapper.(MacroCall).getMacroCallExpansion().(MacroItems).getItem(_) + or + item = wrapper.getAttributeMacroExpansion().getItem(_) + ) +} + /** * An item that may be referred to by a path, and which is a node in * the _item graph_. @@ -300,9 +310,16 @@ abstract class ItemNode extends Locatable { ) } + private ItemNode getDerivedTypeItemParent() { isDerivedExpansionItem(result, this) } + /** Gets the immediately enclosing item of this item, if any. */ pragma[nomagic] - ItemNode getImmediateParent() { this = result.getADescendant() } + ItemNode getImmediateParent() { + result = this.getDerivedTypeItemParent().getImmediateParent() + or + not exists(this.getDerivedTypeItemParent()) and + this = result.getADescendant() + } /** Gets a child item of this item, if any. */ ItemNode getAChild() { this = result.getImmediateParent() } @@ -2241,7 +2258,7 @@ private predicate useImportEdge(Use use, string name, ItemNode item, SuccessorKi if tree.isGlob() then exists(ItemNode encl, Namespace ns, SuccessorKind kind1, UseOption useOpt | - encl.getADescendant() = use and + encl = use.(ItemNode).getImmediateParent() and item = getASuccessor(used, name, ns, kind1, useOpt) and checkQualifiedVisibility(use, item, kind1, useOpt) and // glob imports can be shadowed diff --git a/rust/ql/test/library-tests/path-resolution/derive_macro.rs b/rust/ql/test/library-tests/path-resolution/derive_macro.rs index 2cd0865b1afe..2f4aa38f0125 100644 --- a/rust/ql/test/library-tests/path-resolution/derive_macro.rs +++ b/rust/ql/test/library-tests/path-resolution/derive_macro.rs @@ -7,22 +7,22 @@ macro_rules! build_via_macro { #[derive(proc_macro::GenerateBuilder)] // $ item=derive pub struct Config; -pub fn make() -> ConfigBuilder { // $ MISSING: item=ConfigBuilder - ConfigBuilder // $ MISSING: item=ConfigBuilder +pub fn make() -> ConfigBuilder { // $ item=ConfigBuilder + ConfigBuilder // $ item=ConfigBuilder } #[derive(proc_macro::GenerateBuilderViaMacro)] // $ item=derive struct MacroDerive; -pub fn make_macro_generated() -> MacroGenerated { // $ MISSING: item=MacroGenerated - MacroGenerated // $ MISSING: item=MacroGenerated +pub fn make_macro_generated() -> MacroGenerated { // $ item=MacroGenerated + MacroGenerated // $ item=MacroGenerated } #[derive(proc_macro::GenerateBuilderViaAttribute)] // $ item=derive struct AttributeDerive; -pub fn make_attribute_generated() -> AttributeGenerated { // $ MISSING: item=AttributeGenerated - AttributeGenerated // $ MISSING: item=AttributeGenerated +pub fn make_attribute_generated() -> AttributeGenerated { // $ item=AttributeGenerated + AttributeGenerated // $ item=AttributeGenerated } mod glob_shadowing { 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 91781852199b..04fd7cb95120 100644 --- a/rust/ql/test/library-tests/path-resolution/path-resolution.expected +++ b/rust/ql/test/library-tests/path-resolution/path-resolution.expected @@ -1,7 +1,7 @@ derive_generated_canonical_paths -| derive_macro.rs:8:12:8:17 | struct ConfigBuilder | test::derive_macro::Config::ConfigBuilder | -| derive_macro.rs:14:3:14:45 | struct MacroGenerated | test::derive_macro::MacroDerive::MacroGenerated | -| derive_macro.rs:21:3:21:49 | struct AttributeGenerated | test::derive_macro::AttributeDerive::AttributeGenerated | +| derive_macro.rs:8:12:8:17 | struct ConfigBuilder | test::derive_macro::ConfigBuilder | +| derive_macro.rs:14:3:14:45 | struct MacroGenerated | test::derive_macro::MacroGenerated | +| derive_macro.rs:21:3:21:49 | struct AttributeGenerated | test::derive_macro::AttributeGenerated | mod | derive_macro.rs:28:1:41:1 | mod glob_shadowing | | derive_macro.rs:29:5:31:5 | mod helper | @@ -63,8 +63,14 @@ mod | my/nested.rs:2:5:11:5 | mod nested2 | resolvePath | derive_macro.rs:7:3:7:8 | derive | {EXTERNAL LOCATION} | MacroDef | +| derive_macro.rs:10:18:10:30 | ConfigBuilder | derive_macro.rs:8:12:8:17 | struct ConfigBuilder | +| derive_macro.rs:11:5:11:17 | ConfigBuilder | derive_macro.rs:8:12:8:17 | struct ConfigBuilder | | derive_macro.rs:14:3:14:8 | derive | {EXTERNAL LOCATION} | MacroDef | +| derive_macro.rs:17:34:17:47 | MacroGenerated | derive_macro.rs:14:3:14:45 | struct MacroGenerated | +| derive_macro.rs:18:5:18:18 | MacroGenerated | derive_macro.rs:14:3:14:45 | struct MacroGenerated | | derive_macro.rs:21:3:21:8 | derive | {EXTERNAL LOCATION} | MacroDef | +| derive_macro.rs:24:38:24:55 | AttributeGenerated | derive_macro.rs:21:3:21:49 | struct AttributeGenerated | +| derive_macro.rs:25:5:25:22 | AttributeGenerated | derive_macro.rs:21:3:21:49 | struct AttributeGenerated | | derive_macro.rs:35:7:35:12 | derive | {EXTERNAL LOCATION} | MacroDef | | derive_macro.rs:39:16:39:28 | GlobCollision | derive_macro.rs:33:5:33:25 | struct GlobCollision | | invalid/main.rs:6:9:6:9 | A | invalid/main.rs:3:11:4:9 | struct A |