Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential name collisions in code generated by the table! macro #3146

Merged

Conversation

weiznich
Copy link
Member

This commit fixes potential name collisions in code generated by the
table! macro by prefixing function argument names with
__diesel_internal. The underlying problem here is that rust accepts
patterns as part of the function signature. This combined with the fact
that each zero sized struct (so all column/table structs) are type +
value at the same time results in problems as soon as the users tries to
create a column/table with the same name as one of the used arguments.
So for example if the users has a table that contains an alias column,
that would have caused a name collision inside of the following function
signature:

fn map(column: C, alias: &$crate::query_source::Alias<S>) -> Self::Out

Rustc would have interpreted alias here as zero sized struct literal,
which of course has a different type than the corresponding RHS. That
results in a compiler error about mismatched types.

This commit fixes this problem by prefixing the corresponding variables
with __diesel_internal, which is hopefully reasonably uncommon, so
that no one uses that as column name prefix.

This commit fixes potential name collisions in code generated by the
`table!` macro by prefixing function argument names with
`__diesel_internal`. The underlying problem here is that rust accepts
patterns as part of the function signature. This combined with the fact
that each zero sized struct (so all column/table structs) are type +
value at the same time results in problems as soon as the users tries to
create a column/table with the same name as one of the used arguments.
So for example if the users has a table that contains an `alias` column,
that would have caused a name collision inside of the following function
signature:

```rust
fn map(column: C, alias: &$crate::query_source::Alias<S>) -> Self::Out
```

Rustc would have interpreted `alias` here as zero sized struct literal,
which of course has a different type than the corresponding RHS. That
results in a compiler error about mismatched types.

This commit fixes this problem by prefixing the corresponding variables
with `__diesel_internal`, which is hopefully reasonably uncommon, so
that no one uses that as column name prefix.
@weiznich weiznich requested a review from a team April 23, 2022 06:35
@weiznich weiznich merged commit b0a2c1e into diesel-rs:master Apr 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants