From fd59a27bd2daf4995ed131cb59e2ea4ed66ecf76 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Fri, 13 Jun 2025 10:44:03 +0200 Subject: [PATCH 1/2] Rust: Add macro expr type inference test --- rust/ql/test/library-tests/type-inference/main.rs | 7 +++++++ .../type-inference/type-inference.expected | 14 +++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/rust/ql/test/library-tests/type-inference/main.rs b/rust/ql/test/library-tests/type-inference/main.rs index 87bd3b035b84..18e7af8965c7 100644 --- a/rust/ql/test/library-tests/type-inference/main.rs +++ b/rust/ql/test/library-tests/type-inference/main.rs @@ -1810,6 +1810,12 @@ mod indexers { } } +mod macros { + pub fn f() { + let x = format!("Hello, {}", "World!"); // $ MISSING: type=x:String + } +} + fn main() { field_access::f(); method_impl::f(); @@ -1832,4 +1838,5 @@ fn main() { async_::f(); impl_trait::f(); indexers::f(); + macros::f(); } 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 f97a93d32480..7088bc26f722 100644 --- a/rust/ql/test/library-tests/type-inference/type-inference.expected +++ b/rust/ql/test/library-tests/type-inference/type-inference.expected @@ -2642,9 +2642,13 @@ inferType | main.rs:1809:24:1809:25 | xs | | file://:0:0:0:0 | [] | | main.rs:1809:24:1809:25 | xs | [T;...] | main.rs:1764:5:1765:13 | S | | main.rs:1809:24:1809:25 | xs | [T] | main.rs:1764:5:1765:13 | S | -| main.rs:1815:5:1815:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo | -| main.rs:1816:5:1816:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo | -| main.rs:1816:20:1816:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo | -| main.rs:1816:41:1816:59 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo | -| main.rs:1832:5:1832:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future | +| main.rs:1815:25:1815:35 | "Hello, {}" | | {EXTERNAL LOCATION} | str | +| main.rs:1815:25:1815:45 | ...::format(...) | | {EXTERNAL LOCATION} | String | +| main.rs:1815:25:1815:45 | { ... } | | {EXTERNAL LOCATION} | String | +| main.rs:1815:38:1815:45 | "World!" | | {EXTERNAL LOCATION} | str | +| main.rs:1821:5:1821:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo | +| main.rs:1822:5:1822:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo | +| main.rs:1822:20:1822:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo | +| main.rs:1822:41:1822:59 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo | +| main.rs:1838:5:1838:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future | testFailures From 2f698d1590b0551e55b7bf3a79adaa71159eaad9 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Fri, 13 Jun 2025 11:04:39 +0200 Subject: [PATCH 2/2] Rust: Type inference for macro expressions --- rust/ql/lib/codeql/rust/internal/TypeInference.qll | 2 ++ rust/ql/test/library-tests/type-inference/main.rs | 2 +- .../test/library-tests/type-inference/type-inference.expected | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/rust/ql/lib/codeql/rust/internal/TypeInference.qll b/rust/ql/lib/codeql/rust/internal/TypeInference.qll index a8230d04d9d0..b15687a225c8 100644 --- a/rust/ql/lib/codeql/rust/internal/TypeInference.qll +++ b/rust/ql/lib/codeql/rust/internal/TypeInference.qll @@ -265,6 +265,8 @@ private predicate typeEquality(AstNode n1, TypePath prefix1, AstNode n2, TypePat n1 = be.getLhs() and n2 = be.getRhs() ) + or + n1 = n2.(MacroExpr).getMacroCall().getMacroCallExpansion() ) or n1 = n2.(RefExpr).getExpr() and diff --git a/rust/ql/test/library-tests/type-inference/main.rs b/rust/ql/test/library-tests/type-inference/main.rs index 18e7af8965c7..ea6165c582a3 100644 --- a/rust/ql/test/library-tests/type-inference/main.rs +++ b/rust/ql/test/library-tests/type-inference/main.rs @@ -1812,7 +1812,7 @@ mod indexers { mod macros { pub fn f() { - let x = format!("Hello, {}", "World!"); // $ MISSING: type=x:String + let x = format!("Hello, {}", "World!"); // $ MISSING: type=x:String -- needs https://github.com/github/codeql/pull/19658 } } 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 7088bc26f722..cfa952855d18 100644 --- a/rust/ql/test/library-tests/type-inference/type-inference.expected +++ b/rust/ql/test/library-tests/type-inference/type-inference.expected @@ -1021,8 +1021,12 @@ inferType | main.rs:912:19:912:22 | self | Snd | main.rs:910:15:910:17 | Snd | | main.rs:913:43:913:82 | MacroExpr | | main.rs:910:15:910:17 | Snd | | main.rs:913:50:913:81 | "PairNone has no second elemen... | | {EXTERNAL LOCATION} | str | +| main.rs:913:50:913:81 | MacroExpr | | main.rs:910:15:910:17 | Snd | +| main.rs:913:50:913:81 | { ... } | | main.rs:910:15:910:17 | Snd | | main.rs:914:43:914:81 | MacroExpr | | main.rs:910:15:910:17 | Snd | | main.rs:914:50:914:80 | "PairFst has no second element... | | {EXTERNAL LOCATION} | str | +| main.rs:914:50:914:80 | MacroExpr | | main.rs:910:15:910:17 | Snd | +| main.rs:914:50:914:80 | { ... } | | main.rs:910:15:910:17 | Snd | | main.rs:915:37:915:39 | snd | | main.rs:910:15:910:17 | Snd | | main.rs:915:45:915:47 | snd | | main.rs:910:15:910:17 | Snd | | main.rs:916:41:916:43 | snd | | main.rs:910:15:910:17 | Snd |