Skip to content

Commit

Permalink
docs: type ascription deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed May 2, 2023
1 parent 3b62b73 commit 505d0fa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
3 changes: 1 addition & 2 deletions sqlx-macros-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
feature(track_path)
)]

use once_cell::sync::Lazy;

use crate::query::QueryDriver;

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

// We need a single, persistent Tokio runtime since we're caching connections,
Expand Down
38 changes: 18 additions & 20 deletions sqlx-macros-core/src/query/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::database::DatabaseExt;
use crate::query::QueryMacroInput;
use either::Either;
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use quote::{format_ident, quote, quote_spanned};
use sqlx_core::describe::Describe;
use syn::spanned::Spanned;
use syn::{Expr, ExprCast, ExprGroup, ExprType, Type};
Expand Down Expand Up @@ -32,8 +32,6 @@ pub fn quote_args<DB: DatabaseExt>(
#(let #arg_name = &(#arg_expr);)*
};

let mut args_warnings: Vec<TokenStream> = vec![];

let args_check = match info.parameters() {
None | Some(Either::Right(_)) => {
// all we can do is check arity which we did
Expand All @@ -52,14 +50,11 @@ pub fn quote_args<DB: DatabaseExt>(
.enumerate()
.map(|(i, (param_ty, (name, expr)))| -> crate::Result<_> {
let param_ty = match get_type_override(expr) {
// cast or type ascription will fail to compile if the type does not match
// cast will fail to compile if the type does not match
// and we strip casts to wildcard
Some((_, false)) => return Ok(quote!()),
Some((ty, true)) => {
let warning = create_warning(name.clone(), ty.clone(), expr.clone());
args_warnings.push(warning);
return Ok(quote!())
},
// type ascription is deprecated
Some((ty, true)) => return Ok(create_warning(name.clone(), &ty, &expr)),
None => {
DB::param_type_for_id(&param_ty)
.ok_or_else(|| {
Expand Down Expand Up @@ -108,8 +103,6 @@ pub fn quote_args<DB: DatabaseExt>(
let args_count = input.arg_exprs.len();

Ok(quote! {
#(#args_warnings)*

#arg_bindings

#args_check
Expand All @@ -123,11 +116,13 @@ pub fn quote_args<DB: DatabaseExt>(
})
}

fn create_warning(name: Ident, ty: Type, expr: Expr) -> TokenStream {
let span = expr.span();
let stripped = strip_wildcard(expr).to_token_stream();
fn create_warning(name: Ident, ty: &Type, expr: &Expr) -> TokenStream {
let Expr::Type(ExprType { expr: stripped, .. }) = expr else {
return quote!();
};
let current = quote!(#stripped: #ty).to_string();
let fix = quote!(#stripped as #ty).to_string();
let name = Ident::new(&format!("warning_{name}"), expr.span());

let message = format!(
"
Expand All @@ -140,12 +135,15 @@ fn create_warning(name: Ident, ty: Type, expr: Expr) -> TokenStream {
\t\tSee <https://github.com/rust-lang/rfcs/pull/3307> for more information
"
);
let name = Ident::new(&format!("warning_{name}"), span);
quote_spanned!(span =>
#[deprecated(note = #message)]
#[allow(non_upper_case_globals)]
const #name: () = ();
let _ = #name;

quote_spanned!(expr.span() =>
// this shouldn't actually run
if false {
#[deprecated(note = #message)]
#[allow(non_upper_case_globals)]
const #name: () = ();
let _ = #name;
}
)
}

Expand Down
2 changes: 1 addition & 1 deletion sqlx-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ sqlx-core = { workspace = true, default-features = false, features = ["any"] }
sqlx-macros-core = { workspace = true }

proc-macro2 = { version = "1.0.36", default-features = false }
syn = { version = "1.0.84", default-features = false, features = ["full"] }
syn = { version = "1.0.84", default-features = false, features = ["parsing", "proc-macro"] }
quote = { version = "1.0.14", default-features = false }
9 changes: 6 additions & 3 deletions src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,12 @@
/// sqlx::query!("select $1::int4 as id", my_int as MyInt4)
/// ```
///
/// Using `expr as _` or `expr : _` simply signals to the macro to not type-check that bind expression,
/// and then that syntax is stripped from the expression so as to not trigger type errors
/// (or an unstable syntax feature in the case of the latter, which is called type ascription).
/// Using `expr as _` simply signals to the macro to not type-check that bind expression,
/// and then that syntax is stripped from the expression so as to not trigger type errors.
///
/// **NOTE:** type ascription syntax (`expr: _`) is deprecated and will be removed in a
/// future release. This is due to Rust's [RFC 3307](https://github.com/rust-lang/rfcs/pull/3307)
/// officially dropping support for the syntax.
///
/// ## Type Overrides: Output Columns
/// Type overrides are also available for output columns, utilizing the SQL standard's support
Expand Down

0 comments on commit 505d0fa

Please sign in to comment.