Skip to content

Commit 40ade0b

Browse files
committed
docs: type ascription deprecation
1 parent 3b62b73 commit 40ade0b

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

sqlx-macros-core/src/query/args.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ pub fn quote_args<DB: DatabaseExt>(
3232
#(let #arg_name = &(#arg_expr);)*
3333
};
3434

35-
let mut args_warnings: Vec<TokenStream> = vec![];
36-
3735
let args_check = match info.parameters() {
3836
None | Some(Either::Right(_)) => {
3937
// all we can do is check arity which we did
@@ -52,14 +50,11 @@ pub fn quote_args<DB: DatabaseExt>(
5250
.enumerate()
5351
.map(|(i, (param_ty, (name, expr)))| -> crate::Result<_> {
5452
let param_ty = match get_type_override(expr) {
55-
// cast or type ascription will fail to compile if the type does not match
53+
// cast will fail to compile if the type does not match
5654
// and we strip casts to wildcard
5755
Some((_, false)) => return Ok(quote!()),
58-
Some((ty, true)) => {
59-
let warning = create_warning(name.clone(), ty.clone(), expr.clone());
60-
args_warnings.push(warning);
61-
return Ok(quote!())
62-
},
56+
// type ascription is deprecated
57+
Some((ty, true)) => return Ok(create_warning(name.clone(), &ty, &expr)),
6358
None => {
6459
DB::param_type_for_id(&param_ty)
6560
.ok_or_else(|| {
@@ -108,8 +103,6 @@ pub fn quote_args<DB: DatabaseExt>(
108103
let args_count = input.arg_exprs.len();
109104

110105
Ok(quote! {
111-
#(#args_warnings)*
112-
113106
#arg_bindings
114107

115108
#args_check
@@ -123,11 +116,13 @@ pub fn quote_args<DB: DatabaseExt>(
123116
})
124117
}
125118

126-
fn create_warning(name: Ident, ty: Type, expr: Expr) -> TokenStream {
127-
let span = expr.span();
128-
let stripped = strip_wildcard(expr).to_token_stream();
119+
fn create_warning(name: Ident, ty: &Type, expr: &Expr) -> TokenStream {
120+
let Expr::Type(ExprType { expr: stripped, .. }) = expr else {
121+
return quote!();
122+
};
129123
let current = quote!(#stripped: #ty).to_string();
130124
let fix = quote!(#stripped as #ty).to_string();
125+
let name = Ident::new(&format!("warning_{name}"), expr.span());
131126

132127
let message = format!(
133128
"
@@ -140,12 +135,15 @@ fn create_warning(name: Ident, ty: Type, expr: Expr) -> TokenStream {
140135
\t\tSee <https://github.com/rust-lang/rfcs/pull/3307> for more information
141136
"
142137
);
143-
let name = Ident::new(&format!("warning_{name}"), span);
144-
quote_spanned!(span =>
145-
#[deprecated(note = #message)]
146-
#[allow(non_upper_case_globals)]
147-
const #name: () = ();
148-
let _ = #name;
138+
139+
quote_spanned!(expr.span() =>
140+
// this shouldn't actually run
141+
if false {
142+
#[deprecated(note = #message)]
143+
#[allow(non_upper_case_globals)]
144+
const #name: () = ();
145+
let _ = #name;
146+
}
149147
)
150148
}
151149

src/macros/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,12 @@
151151
/// sqlx::query!("select $1::int4 as id", my_int as MyInt4)
152152
/// ```
153153
///
154-
/// Using `expr as _` or `expr : _` simply signals to the macro to not type-check that bind expression,
155-
/// and then that syntax is stripped from the expression so as to not trigger type errors
156-
/// (or an unstable syntax feature in the case of the latter, which is called type ascription).
154+
/// Using `expr as _` simply signals to the macro to not type-check that bind expression,
155+
/// and then that syntax is stripped from the expression so as to not trigger type errors.
156+
///
157+
/// **NOTE:** type ascription syntax (`expr: _`) is deprecated and will be removed in a
158+
/// future release. This is due to Rust's [RFC 3307](https://github.com/rust-lang/rfcs/pull/3307)
159+
/// officially dropping support for the syntax.
157160
///
158161
/// ## Type Overrides: Output Columns
159162
/// Type overrides are also available for output columns, utilizing the SQL standard's support

0 commit comments

Comments
 (0)