Skip to content

Commit 2c1d388

Browse files
Begin synthesizing spans (#1746)
I consulted other rust-lang devs and they agree proc-macros should use what I refer to as "synthetic" spans. These spans have their root context derived from `Span::mixed_site()` or `Span::call_site()` but have been given a location in the code somewhere. - Fixes remaining `used_underscore_binding` warnings. - Closes #1667
1 parent 8057092 commit 2c1d388

16 files changed

+107
-21
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ jobs:
434434

435435
# We can't do this with other lints because we need $PGRX_HOME
436436
- name: Clippy -Awarnings
437-
run: cargo clippy -p pgrx --features pg$PG_VER -- -Awarnings
437+
run: cargo clippy --tests --features pg$PG_VER --no-default-features -- -Awarnings
438438

439439
# This also requires $PGRX_HOME
440440
- name: Check doc-links

pgrx-macros/src/rewriter.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
1010
extern crate proc_macro;
1111

12-
use quote::{format_ident, quote, quote_spanned};
12+
use quote::{quote, quote_spanned};
1313
use std::mem;
1414
use std::ops::Deref;
1515
use std::str::FromStr;
@@ -20,6 +20,14 @@ use syn::{
2020
Visibility,
2121
};
2222

23+
macro_rules! format_ident {
24+
($s:literal, $e:expr) => {{
25+
let mut synthetic = $e.clone();
26+
synthetic.set_span(proc_macro2::Span::call_site().located_at($e.span()));
27+
quote::format_ident!($s, synthetic)
28+
}};
29+
}
30+
2331
pub fn extern_block(block: ItemForeignMod) -> proc_macro2::TokenStream {
2432
let mut stream = proc_macro2::TokenStream::new();
2533

@@ -60,6 +68,7 @@ pub fn item_fn_without_rewrite(mut func: ItemFn) -> syn::Result<proc_macro2::Tok
6068

6169
let arg_list = build_arg_list(&sig, false)?;
6270
let func_name = func.sig.ident.clone();
71+
let func_name = format_ident!("{}", func_name);
6372

6473
let prolog = if input_func_name == "__pgrx_private_shmem_hook"
6574
|| input_func_name == "__pgrx_private_shmem_request_hook"
@@ -90,7 +99,9 @@ pub fn item_fn_without_rewrite(mut func: ItemFn) -> syn::Result<proc_macro2::Tok
9099
quote! { #func_name::<#ty>(#arg_list) }
91100
};
92101

93-
Ok(quote_spanned! {func.span()=>
102+
let synthetic = proc_macro2::Span::mixed_site().located_at(func.span());
103+
104+
Ok(quote_spanned! {synthetic=>
94105
#prolog
95106
#(#attrs)*
96107
#vis #sig {
@@ -149,6 +160,7 @@ fn build_arg_list(sig: &Signature, suffix_arg_name: bool) -> syn::Result<proc_ma
149160
let ident = format_ident!("{}_", ident.ident);
150161
arg_list.extend(quote! { #ident, });
151162
} else {
163+
let ident = format_ident!("{}", ident.ident);
152164
arg_list.extend(quote! { #ident, });
153165
}
154166
} else {

pgrx-sql-entity-graph/src/finfo.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ pub fn finfo_v1_extern_c(
2727
let original_name = &original.sig.ident;
2828
let wrapper_symbol = format_ident!("{}_wrapper", original_name);
2929

30-
let tokens = quote_spanned! { original.sig.span() =>
30+
let synthetic = proc_macro2::Span::mixed_site();
31+
let synthetic = synthetic.located_at(original.sig.span());
32+
33+
let tokens = quote_spanned! { synthetic =>
3134
#[no_mangle]
3235
#[doc(hidden)]
3336
pub unsafe extern "C" fn #wrapper_symbol(#fcinfo: ::pgrx::pg_sys::FunctionCallInfo) -> ::pgrx::pg_sys::Datum {

pgrx-sql-entity-graph/src/pg_extern/mod.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,30 @@ use operator::{PgrxOperatorAttributeWithIdent, PgrxOperatorOpName};
4040
use search_path::SearchPathList;
4141

4242
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
43-
use quote::{format_ident, quote, quote_spanned};
43+
use quote::quote;
4444
use syn::parse::{Parse, ParseStream, Parser};
4545
use syn::punctuated::Punctuated;
4646
use syn::spanned::Spanned;
4747
use syn::{Meta, Token};
4848

49+
macro_rules! quote_spanned {
50+
($span:expr=> $($expansion:tt)*) => {
51+
{
52+
let synthetic = Span::mixed_site();
53+
let synthetic = synthetic.located_at($span);
54+
quote::quote_spanned! {synthetic=> $($expansion)* }
55+
}
56+
};
57+
}
58+
59+
macro_rules! format_ident {
60+
($s:literal, $e:expr) => {{
61+
let mut synthetic = $e.clone();
62+
synthetic.set_span(Span::call_site().located_at($e.span()));
63+
quote::format_ident!($s, synthetic)
64+
}};
65+
}
66+
4967
/// A parsed `#[pg_extern]` item.
5068
///
5169
/// It should be used with [`syn::parse::Parse`] functions.
@@ -377,7 +395,8 @@ impl PgExtern {
377395
let signature = &self.func.sig;
378396
let func_name = &signature.ident;
379397
// we do this odd dance so we can pass the same ident to macros that don't know each other
380-
let fcinfo_ident = syn::Ident::new("fcinfo", signature.ident.span());
398+
let synthetic_ident_span = Span::mixed_site().located_at(signature.ident.span());
399+
let fcinfo_ident = syn::Ident::new("fcinfo", synthetic_ident_span);
381400
let mut lifetimes = signature
382401
.generics
383402
.lifetimes()

pgrx-sql-entity-graph/src/pg_trigger/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::enrich::{ToEntityGraphTokens, ToRustCodeTokens};
2222
use crate::finfo::{finfo_v1_extern_c, finfo_v1_tokens};
2323
use crate::{CodeEnrichment, ToSqlConfig};
2424
use attribute::PgTriggerAttribute;
25-
use proc_macro2::{Ident, TokenStream as TokenStream2};
25+
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
2626
use quote::{format_ident, quote};
2727
use syn::{spanned::Spanned, ItemFn, Token};
2828

@@ -70,7 +70,8 @@ impl PgTrigger {
7070

7171
pub fn wrapper_tokens(&self) -> Result<ItemFn, syn::Error> {
7272
let function_ident = self.func.sig.ident.clone();
73-
let fcinfo_ident = Ident::new("_fcinfo", function_ident.span());
73+
let fcinfo_ident =
74+
Ident::new("_fcinfo", Span::mixed_site().located_at(function_ident.span()));
7475

7576
let tokens = quote! {
7677
fn _internal(fcinfo: ::pgrx::pg_sys::FunctionCallInfo) -> ::pgrx::pg_sys::Datum {

pgrx-tests/src/tests/attributes_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod tests {
1818
#[pg_test]
1919
#[ignore = "This test should be ignored."]
2020
fn test_for_ignore_attribute() {
21-
assert_eq!(true, true);
21+
assert_eq!(true, false);
2222
}
2323

2424
#[pg_test]

pgrx-tests/tests/compile-fail/aggregate-functions-dont-run-forever.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ error[E0521]: borrowed data escapes outside of function
99
| lifetime `'fcx` defined here
1010
| argument requires that `'fcx` must outlive `'static`
1111
|
12-
= note: this error originates in the attribute macro `pg_aggregate` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
= note: this error originates in the attribute macro `::pgrx::pg_extern` (in Nightly builds, run with -Z macro-backtrace for more info)
1313

1414
error[E0521]: borrowed data escapes outside of function
1515
--> tests/compile-fail/aggregate-functions-dont-run-forever.rs:47:1
@@ -22,7 +22,7 @@ error[E0521]: borrowed data escapes outside of function
2222
| lifetime `'fcx` defined here
2323
| argument requires that `'fcx` must outlive `'static`
2424
|
25-
= note: this error originates in the attribute macro `pg_aggregate` (in Nightly builds, run with -Z macro-backtrace for more info)
25+
= note: this error originates in the attribute macro `::pgrx::pg_extern` (in Nightly builds, run with -Z macro-backtrace for more info)
2626

2727
error[E0521]: borrowed data escapes outside of function
2828
--> tests/compile-fail/aggregate-functions-dont-run-forever.rs:70:1
@@ -35,7 +35,7 @@ error[E0521]: borrowed data escapes outside of function
3535
| lifetime `'fcx` defined here
3636
| argument requires that `'fcx` must outlive `'static`
3737
|
38-
= note: this error originates in the attribute macro `pg_aggregate` (in Nightly builds, run with -Z macro-backtrace for more info)
38+
= note: this error originates in the attribute macro `::pgrx::pg_extern` (in Nightly builds, run with -Z macro-backtrace for more info)
3939

4040
error[E0521]: borrowed data escapes outside of function
4141
--> tests/compile-fail/aggregate-functions-dont-run-forever.rs:113:1
@@ -48,7 +48,7 @@ error[E0521]: borrowed data escapes outside of function
4848
| lifetime `'fcx` defined here
4949
| argument requires that `'fcx` must outlive `'static`
5050
|
51-
= note: this error originates in the attribute macro `pg_aggregate` (in Nightly builds, run with -Z macro-backtrace for more info)
51+
= note: this error originates in the attribute macro `::pgrx::pg_extern` (in Nightly builds, run with -Z macro-backtrace for more info)
5252

5353
error[E0521]: borrowed data escapes outside of function
5454
--> tests/compile-fail/aggregate-functions-dont-run-forever.rs:141:1
@@ -61,4 +61,4 @@ error[E0521]: borrowed data escapes outside of function
6161
| lifetime `'fcx` defined here
6262
| argument requires that `'fcx` must outlive `'static`
6363
|
64-
= note: this error originates in the attribute macro `pg_aggregate` (in Nightly builds, run with -Z macro-backtrace for more info)
64+
= note: this error originates in the attribute macro `::pgrx::pg_extern` (in Nightly builds, run with -Z macro-backtrace for more info)

pgrx-tests/tests/compile-fail/eq-for-postgres_hash.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ note: required by a bound in `brokentype_hash`
3939
| ^^^^^^^^^^^^ required by this bound in `brokentype_hash`
4040
5 | pub struct BrokenType {
4141
| ---------- required by a bound in this function
42-
= note: this error originates in the derive macro `PostgresHash` (in Nightly builds, run with -Z macro-backtrace for more info)
42+
= note: this error originates in the attribute macro `::pgrx::pgrx_macros::pg_extern` which comes from the expansion of the derive macro `PostgresHash` (in Nightly builds, run with -Z macro-backtrace for more info)
4343
help: consider annotating `BrokenType` with `#[derive(Hash)]`
4444
|
4545
5 + #[derive(Hash)]
@@ -59,7 +59,7 @@ note: required by a bound in `brokentype_hash`
5959
| ^^^^^^^^^^^^ required by this bound in `brokentype_hash`
6060
5 | pub struct BrokenType {
6161
| ---------- required by a bound in this function
62-
= note: this error originates in the derive macro `PostgresHash` (in Nightly builds, run with -Z macro-backtrace for more info)
62+
= note: this error originates in the attribute macro `::pgrx::pgrx_macros::pg_extern` which comes from the expansion of the derive macro `PostgresHash` (in Nightly builds, run with -Z macro-backtrace for more info)
6363
help: consider annotating `BrokenType` with `#[derive(Eq)]`
6464
|
6565
5 + #[derive(Eq)]

pgrx-tests/tests/compile-fail/postgres-strings-arent-immortal.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0521]: borrowed data escapes outside of function
22
--> tests/compile-fail/postgres-strings-arent-immortal.rs:4:67
33
|
44
3 | #[pg_extern]
5-
| ------------ lifetime `'fcx` defined here
5+
| ------------
6+
| |
7+
| lifetime `'fcx` defined here
8+
| in this procedural macro expansion
69
4 | fn split(input: &'static str, pattern: &str) -> Vec<&'static str> {
710
| ___________________________________________________________________^
811
5 | | input.split_terminator(pattern).collect()
@@ -12,3 +15,5 @@ error[E0521]: borrowed data escapes outside of function
1215
| | `fcinfo` is a reference that is only valid in the function body
1316
| |_`fcinfo` escapes the function body here
1417
| argument requires that `'fcx` must outlive `'static`
18+
|
19+
= note: this error originates in the attribute macro `pg_extern` (in Nightly builds, run with -Z macro-backtrace for more info)

pgrx-tests/tests/compile-fail/table-iterators-arent-immortal.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0521]: borrowed data escapes outside of function
22
--> tests/compile-fail/table-iterators-arent-immortal.rs:6:78
33
|
44
3 | #[pg_extern]
5-
| ------------ lifetime `'fcx` defined here
5+
| ------------
6+
| |
7+
| lifetime `'fcx` defined here
8+
| in this procedural macro expansion
69
...
710
6 | ) -> TableIterator<(name!(a, &'static str), name!(b, Option<&'static str>))> {
811
| ______________________________________________________________________________^
@@ -13,3 +16,5 @@ error[E0521]: borrowed data escapes outside of function
1316
| | `fcinfo` is a reference that is only valid in the function body
1417
| |_`fcinfo` escapes the function body here
1518
| argument requires that `'fcx` must outlive `'static`
19+
|
20+
= note: this error originates in the attribute macro `pg_extern` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)