Skip to content

Rust: a type generated by a derive macro is placed inside its struct (wrong canonical path, name not resolved) #23146

Open

Description

@bdrodes

Description of the issue

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:

  1. Its canonical path is wrong: demo::Config::ConfigBuilder instead of demo::ConfigBuilder.
  2. 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)]
pub fn derive_builder(input: TokenStream) -> TokenStream {
    let mut tokens = input.into_iter();
    while let Some(t) = tokens.next() {
        if let TokenTree::Ident(i) = &t {
            if i.to_string() == "struct" {
                let name = tokens.next().unwrap().to_string();
                return format!("pub struct {name}Builder;").parse().unwrap();
            }
        }
    }
    TokenStream::new()
}

Crate demo (depends on mk):

use mk::Builder;

#[derive(Builder)]
pub struct Config {
    pub x: u32,
}

pub fn make() -> ConfigBuilder {
    ConfigBuilder
}

cargo build succeeds, so ConfigBuilder really is demo::ConfigBuilder.

Query

import rust

from Struct s
where exists(s.getLocation().getFile().getRelativePath())
select s.getName().getText(), s.getCanonicalPath()
Struct Actual canonical path Expected
Config demo::Config demo::Config
ConfigBuilder demo::Config::ConfigBuilder 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.

Versions

CodeQL CLI 2.27.0, codeql/rust-all 0.2.25, Rust 1.99.0, Windows.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions