Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment with subspan support #21

Open
Nemo157 opened this issue Sep 19, 2022 · 2 comments
Open

Experiment with subspan support #21

Nemo157 opened this issue Sep 19, 2022 · 2 comments

Comments

@Nemo157
Copy link
Member

Nemo157 commented Sep 19, 2022

https://doc.rust-lang.org/nightly/proc_macro/struct.Literal.html#method.subspan

Seems to work well with a hacked together usage:

error[E0425]: cannot find value `a` in this scope
  --> tests/tests.rs:15:46
   |
15 |         assert_eq!(stylish::plain::format!("{a}"), "1");
   |                                              ^ not found in this scope

Biggest blocker seems to be detecting the quotes, for simple " quoted strings this works:

diff --git macros/src/lib.rs macros/src/lib.rs
index a328ed3..9fde367 100644
--- macros/src/lib.rs
+++ macros/src/lib.rs
@@ -107,9 +107,9 @@ fn format_args_impl(
     let export: syn::Path = syn::parse_quote!(#krate::𓀄);

     let span = format.span();
-    let format_string = &format;
-    let format = format.value();
-    let (leftover, format) = Format::parse(&format).unwrap();
+    let format_lit = &format;
+    let format_string = format.value();
+    let (leftover, format) = Format::parse(&format_string).unwrap();
     assert!(leftover.is_empty());
     let num_positional_args = positional_args.len();
     let positional_args = positional_args.into_iter();
@@ -161,12 +161,14 @@ fn format_args_impl(
                             quote!(__stylish_named_args.#index)
                         } else {
                             let i = implicit_named_args_values.len();
+                            let start = (name.as_ptr() as usize) - (format_string.as_str().as_ptr() as usize) + 1;
+                            let end = start + name.len();
                             implicit_named_args_values.push(ExprPath {
                                 attrs: Vec::new(),
                                 qself: None,
                                 path: Ident::new(
                                     name,
-                                    Span::call_site().resolved_at(format_string.span()),
+                                    format_lit.token().subspan(start..end).unwrap_or_else(|| format_lit.span()),
                                 )
                                 .into(),
                             });

But it gets misaligned for r#"" strings:

error[E0425]: cannot find value `a` in this scope
  --> tests/tests.rs:16:46
   |
16 |         assert_eq!(stylish::plain::format!(r#"{a}"#), "1");
   |                                              ^ not found in this scope
@Nemo157
Copy link
Member Author

Nemo157 commented Sep 20, 2022

Another issue, escapes get interpreted making the offset off:

error[E0425]: cannot find value `a` in this scope
  --> tests/tests.rs:17:47
   |
17 |         assert_eq!(stylish::plain::format!("\u{0000}{a}"), "1");
   |                                               ^ not found in this scope

@Nemo157
Copy link
Member Author

Nemo157 commented Sep 20, 2022

Probably blocked on dtolnay/syn#1219

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant