Skip to content

macros: raw-identifier field names bypass projection-method collision check #989

@carllerche

Description

@carllerche

Summary

Field names written as raw identifiers (r#count, r#insert, …) bypass the
collision check that #987 added for the generated Query/Many/One/OptionOne
projection methods. The macro emits the projection method anyway, which then
clashes with the existing wrapper method and produces a duplicate-inherent-method
compile error instead of the intended skip + deprecation warning.

Root cause: both check sites compare field_ident.to_string() against
QUERY_RESERVED_METHODS / MANY_RESERVED_METHODS / ONE_RESERVED_METHODS.
syn::Ident::to_string() retains the leading r# for raw identifiers, so
r#count is compared as "r#count" and never matches "count". Rust then
treats the emitted fn r#count and the existing fn count as the same method.

Affected sites:

  • crates/toasty-macros/src/model/expand.rs:60projection_method_collides
    (used by the Query projection-method expansion).
  • crates/toasty-macros/src/model/expand/relation.rs:336
    reserved.contains(&field_ident.to_string().as_str()) in
    expand_wrapper_projection_methods for Many / One / OptionOne.

Both should strip a leading r# before the comparison.

Reproducer

#[derive(Debug, toasty::Model)]
struct Item {
    #[key]
    #[auto]
    id: i64,
    r#count: i64,
}

Expected: macro skips the projection method, emits the #[deprecated] marker;
user gets a compile-time warning pointing at .select(Item::fields().count()).

Actual: macro emits fn r#count(self) -> …, colliding with the wrapper's
existing count method (or, with Query::count_rows since the rename, the
collision is with whichever wrapper still defines count after future
additions). Compile fails with a duplicate-inherent-method error.

Database driver

none — bug is in toasty-macros.

Toasty version

main (current worktree-floofy-plotting-pie tip, ahead of 198b7d6d).

Additional context

Follow-up from review of #987
(#987 (comment)). The PR
introduced the collision-check machinery; this bug exists in that code path
from the start.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions