Skip to content

Commit 51fa620

Browse files
committed
Use proc_macro::Span instead of proc_macro2::Span.
proc_macro2's spans are sometimes empty/zero.
1 parent b1a59aa commit 51fa620

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

macros/src/embed_python.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use proc_macro2::{Delimiter, Ident, LineColumn, Spacing, Span, TokenStream, TokenTree};
1+
use proc_macro::{LineColumn, Span};
2+
use proc_macro2::{Delimiter, Ident, Spacing, TokenStream, TokenTree};
23
use quote::quote_spanned;
34
use std::collections::BTreeMap;
45
use std::fmt::Write;
@@ -30,7 +31,7 @@ impl EmbedPython {
3031
}
3132
let first_indent = *self.first_indent.get_or_insert(loc.column);
3233
let indent = loc.column.checked_sub(first_indent);
33-
let indent = indent.ok_or_else(|| quote_spanned!(span => compile_error!{"Invalid indentation"}))?;
34+
let indent = indent.ok_or_else(|| quote_spanned!(span.into() => compile_error!{"Invalid indentation"}))?;
3435
for _ in 0..indent {
3536
self.python.push(' ');
3637
}
@@ -49,8 +50,8 @@ impl EmbedPython {
4950
let mut tokens = input.into_iter();
5051

5152
while let Some(token) = tokens.next() {
52-
let span = token.span();
53-
self.add_whitespace(span, token.span().start())?;
53+
let span = token.span().unwrap();
54+
self.add_whitespace(span, span.start())?;
5455

5556
match &token {
5657
TokenTree::Group(x) => {
@@ -63,7 +64,7 @@ impl EmbedPython {
6364
self.python.push_str(start);
6465
self.loc.column += start.len();
6566
self.add(x.stream())?;
66-
let mut end_loc = token.span().end();
67+
let mut end_loc = token.span().unwrap().end();
6768
end_loc.column = end_loc.column.saturating_sub(end.len());
6869
self.add_whitespace(span, end_loc)?;
6970
self.python.push_str(end);
@@ -104,11 +105,11 @@ impl EmbedPython {
104105
}
105106
TokenTree::Ident(x) => {
106107
write!(&mut self.python, "{}", x).unwrap();
107-
self.loc = token.span().end();
108+
self.loc = token.span().unwrap().end();
108109
}
109110
TokenTree::Literal(x) => {
110111
write!(&mut self.python, "{}", x).unwrap();
111-
self.loc = token.span().end();
112+
self.loc = token.span().unwrap().end();
112113
}
113114
}
114115
}

macros/src/error.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use proc_macro2::{Span, TokenStream};
1+
use proc_macro::Span;
2+
use proc_macro2::TokenStream;
23
use pyo3::type_object::PyTypeObject;
34
use pyo3::{AsPyRef, PyAny, PyErr, PyResult, Python, ToPyObject};
45
use quote::{quote, quote_spanned};
@@ -18,18 +19,18 @@ pub fn compile_error_msg(py: Python, error: PyErr, tokens: TokenStream) -> Token
1819
if let (Some(line), Some(msg)) = (line, msg) {
1920
if let Some(span) = span_for_line(tokens.clone(), line) {
2021
let error = format!("python: {}", msg);
21-
return quote_spanned!(span => compile_error!{#error});
22+
return quote_spanned!(span.into() => compile_error!{#error});
2223
}
2324
}
2425
}
2526

2627
if let Some(tb) = &error.ptraceback {
2728
if let Ok((file, line)) = get_traceback_info(tb.as_ref(py)) {
28-
if file == Span::call_site().unwrap().source_file().path().to_string_lossy() {
29+
if file == Span::call_site().source_file().path().to_string_lossy() {
2930
if let Ok(msg) = value.as_ref(py).str() {
3031
if let Some(span) = span_for_line(tokens, line) {
3132
let error = format!("python: {}", msg);
32-
return quote_spanned!(span => compile_error!{#error});
33+
return quote_spanned!(span.into() => compile_error!{#error});
3334
}
3435
}
3536
}
@@ -64,5 +65,5 @@ fn span_for_line(input: TokenStream, line: usize) -> Option<Span> {
6465
}
6566
}
6667

67-
Some(Span::from(result))
68+
Some(result)
6869
}

macros/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
extern crate proc_macro;
66

77
use self::embed_python::EmbedPython;
8-
use proc_macro::TokenStream as TokenStream1;
9-
use proc_macro2::{Literal, Span, TokenStream};
8+
use proc_macro::{Span, TokenStream as TokenStream1};
9+
use proc_macro2::{Literal, TokenStream};
1010
use pyo3::{ffi, types::PyBytes, AsPyPointer, FromPyPointer, PyObject, Python};
1111
use quote::quote;
1212
use std::ffi::CString;
@@ -20,7 +20,7 @@ fn python_impl(input: TokenStream) -> Result<TokenStream, TokenStream> {
2020

2121
check_no_attribute(input.clone())?;
2222

23-
let filename = Span::call_site().unwrap().source_file().path().to_string_lossy().into_owned();
23+
let filename = Span::call_site().source_file().path().to_string_lossy().into_owned();
2424

2525
let mut x = EmbedPython::new();
2626

@@ -65,7 +65,7 @@ fn python_impl(input: TokenStream) -> Result<TokenStream, TokenStream> {
6565
fn ct_python_impl(input: TokenStream) -> Result<TokenStream, TokenStream> {
6666
let tokens = input.clone();
6767

68-
let filename = Span::call_site().unwrap().source_file().path().to_string_lossy().into_owned();
68+
let filename = Span::call_site().source_file().path().to_string_lossy().into_owned();
6969

7070
let mut x = EmbedPython::new();
7171

0 commit comments

Comments
 (0)