Skip to content

Commit 4095ac4

Browse files
chore: add deprecation notice for type ascription use (#2483)
* chore: add deprecation notice for type ascription use * docs: type ascription deprecation
1 parent 39acaf1 commit 4095ac4

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

sqlx-macros-core/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
feature(track_path)
2020
)]
2121

22-
use once_cell::sync::Lazy;
23-
2422
use crate::query::QueryDriver;
2523

2624
pub type Error = Box<dyn std::error::Error>;
@@ -54,6 +52,7 @@ where
5452
{
5553
#[cfg(feature = "_rt-tokio")]
5654
{
55+
use once_cell::sync::Lazy;
5756
use tokio::runtime::{self, Runtime};
5857

5958
// We need a single, persistent Tokio runtime since we're caching connections,

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::database::DatabaseExt;
22
use crate::query::QueryMacroInput;
33
use either::Either;
4-
use proc_macro2::TokenStream;
4+
use proc_macro2::{Ident, TokenStream};
55
use quote::{format_ident, quote, quote_spanned};
66
use sqlx_core::describe::Describe;
77
use syn::spanned::Spanned;
@@ -50,9 +50,11 @@ pub fn quote_args<DB: DatabaseExt>(
5050
.enumerate()
5151
.map(|(i, (param_ty, (name, expr)))| -> crate::Result<_> {
5252
let param_ty = match get_type_override(expr) {
53-
// 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
5454
// and we strip casts to wildcard
55-
Some(_) => return Ok(quote!()),
55+
Some((_, false)) => return Ok(quote!()),
56+
// type ascription is deprecated
57+
Some((ty, true)) => return Ok(create_warning(name.clone(), &ty, &expr)),
5658
None => {
5759
DB::param_type_for_id(&param_ty)
5860
.ok_or_else(|| {
@@ -114,11 +116,42 @@ pub fn quote_args<DB: DatabaseExt>(
114116
})
115117
}
116118

117-
fn get_type_override(expr: &Expr) -> Option<&Type> {
119+
fn create_warning(name: Ident, ty: &Type, expr: &Expr) -> TokenStream {
120+
let Expr::Type(ExprType { expr: stripped, .. }) = expr else {
121+
return quote!();
122+
};
123+
let current = quote!(#stripped: #ty).to_string();
124+
let fix = quote!(#stripped as #ty).to_string();
125+
let name = Ident::new(&format!("warning_{name}"), expr.span());
126+
127+
let message = format!(
128+
"
129+
\t\tType ascription pattern is deprecated, prefer casting
130+
\t\tTry changing from
131+
\t\t\t`{current}`
132+
\t\tto
133+
\t\t\t`{fix}`
134+
135+
\t\tSee <https://github.com/rust-lang/rfcs/pull/3307> for more information
136+
"
137+
);
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+
}
147+
)
148+
}
149+
150+
fn get_type_override(expr: &Expr) -> Option<(&Type, bool)> {
118151
match expr {
119152
Expr::Group(group) => get_type_override(&group.expr),
120-
Expr::Cast(cast) => Some(&cast.ty),
121-
Expr::Type(ascription) => Some(&ascription.ty),
153+
Expr::Cast(cast) => Some((&cast.ty, false)),
154+
Expr::Type(ascription) => Some((&ascription.ty, true)),
122155
_ => None,
123156
}
124157
}

sqlx-macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ sqlx-core = { workspace = true, default-features = false, features = ["any"] }
4343
sqlx-macros-core = { workspace = true }
4444

4545
proc-macro2 = { version = "1.0.36", default-features = false }
46-
syn = { version = "1.0.84", default-features = false, features = ["full"] }
46+
syn = { version = "1.0.84", default-features = false, features = ["parsing", "proc-macro"] }
4747
quote = { version = "1.0.14", default-features = false }

src/macros/mod.rs

Lines changed: 6 additions & 3 deletions
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)