Skip to content

Commit b3ae6e5

Browse files
authored
fix(macros): prefix generated variable names in query_as!() (#1336)
closes #1322
1 parent e89cb09 commit b3ae6e5

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

sqlx-macros/src/query/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ where
320320
|&output::RustColumn {
321321
ref ident,
322322
ref type_,
323+
..
323324
}| quote!(#ident: #type_,),
324325
);
325326

sqlx-macros/src/query/output.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use syn::Token;
1414

1515
pub struct RustColumn {
1616
pub(super) ident: Ident,
17+
pub(super) var_name: Ident,
1718
pub(super) type_: ColumnType,
1819
}
1920

@@ -114,6 +115,9 @@ fn column_to_rust<DB: DatabaseExt>(describe: &Describe<DB>, i: usize) -> crate::
114115
};
115116

116117
Ok(RustColumn {
118+
// prefix the variable name we use in `quote_query_as!()` so it doesn't conflict
119+
// https://github.com/launchbadge/sqlx/issues/1322
120+
var_name: quote::format_ident!("sqlx_query_as_{}", decl.ident),
117121
ident: decl.ident,
118122
type_,
119123
})
@@ -129,7 +133,7 @@ pub fn quote_query_as<DB: DatabaseExt>(
129133
|(
130134
i,
131135
&RustColumn {
132-
ref ident,
136+
ref var_name,
133137
ref type_,
134138
..
135139
},
@@ -140,20 +144,21 @@ pub fn quote_query_as<DB: DatabaseExt>(
140144
// binding to a `let` avoids confusing errors about
141145
// "try expression alternatives have incompatible types"
142146
// it doesn't seem to hurt inference in the other branches
143-
let #ident = row.try_get_unchecked::<#type_, _>(#i)?;
147+
let #var_name = row.try_get_unchecked::<#type_, _>(#i)?;
144148
},
145149
// type was overridden to be a wildcard so we fallback to the runtime check
146-
(true, ColumnType::Wildcard) => quote! ( let #ident = row.try_get(#i)?; ),
150+
(true, ColumnType::Wildcard) => quote! ( let #var_name = row.try_get(#i)?; ),
147151
(true, ColumnType::OptWildcard) => {
148-
quote! ( let #ident = row.try_get::<::std::option::Option<_>, _>(#i)?; )
152+
quote! ( let #var_name = row.try_get::<::std::option::Option<_>, _>(#i)?; )
149153
}
150154
// macro is the `_unchecked!()` variant so this will die in decoding if it's wrong
151-
(false, _) => quote!( let #ident = row.try_get_unchecked(#i)?; ),
155+
(false, _) => quote!( let #var_name = row.try_get_unchecked(#i)?; ),
152156
}
153157
},
154158
);
155159

156160
let ident = columns.iter().map(|col| &col.ident);
161+
let var_name = columns.iter().map(|col| &col.var_name);
157162

158163
let db_path = DB::db_path();
159164
let row_path = DB::row_path();
@@ -172,7 +177,7 @@ pub fn quote_query_as<DB: DatabaseExt>(
172177

173178
#(#instantiations)*
174179

175-
Ok(#out_ty { #(#ident: #ident),* })
180+
Ok(#out_ty { #(#ident: #var_name),* })
176181
})
177182
}
178183
}

0 commit comments

Comments
 (0)