You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Rust: a type generated by a derive macro is placed inside its struct (wrong canonical path, name not resolved) #23146
In CodeQL for Rust, a type that a derive macro generates is treated as if it were inside the struct it was derived from. In Rust it sits beside that struct, in the same module. As a result:
Its canonical path is wrong: demo::Config::ConfigBuilder instead of demo::ConfigBuilder.
Code that uses the generated type by name does not resolve to it.
Minimal example
Crate mk (proc-macro = true): a derive that generates <Name>Builder.
use proc_macro::{TokenStream,TokenTree};#[proc_macro_derive(Builder)]pubfnderive_builder(input:TokenStream) -> TokenStream{letmut tokens = input.into_iter();whileletSome(t) = tokens.next(){ifletTokenTree::Ident(i) = &t {if i.to_string() == "struct"{let name = tokens.next().unwrap().to_string();returnformat!("pub struct {name}Builder;").parse().unwrap();}}}TokenStream::new()}
Crate demo (depends on mk):
use mk::Builder;#[derive(Builder)]pubstructConfig{pubx:u32,}pubfnmake() -> ConfigBuilder{ConfigBuilder}
cargo build succeeds, so ConfigBuilder really is demo::ConfigBuilder.
Name resolution is affected too. With resolvePath from codeql.rust.internal.PathResolution, both uses of ConfigBuilder in make() (lines 8 and 9) resolve to nothing, while mk, derive and u32 in the same file resolve. (Builder in use mk::Builder is also unresolved; that may be a separate issue.)
Seen in real projects
wasmtime, crates/fuzzing/src/generators/exception_ops.rs: #[derive(Mutate)] on ExceptionOpsLimits gives ...::exception_ops::ExceptionOpsLimits::ExceptionOpsLimitsMutator.
handlebars, src/template.rs: the derive_builder output TemplateBuilderError gets handlebars::template::Template::TemplateBuilderError.
Likely cause
In rust/ql/lib/codeql/rust/internal/PathResolution.qll, an item's canonical-path prefix and its lookup scope come from its immediate parent item. A derive macro's expansion is stored under the struct (getDeriveMacroExpansion), so the struct, not the module, becomes the generated type's parent.
Activity