Skip to content

Commit 5541ce0

Browse files
committed
Stop using proc_macro::Span::join().
1 parent e4d8b21 commit 5541ce0

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

macros/src/error.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ pub fn compile_error_msg(py: Python, error: PyErr, tokens: TokenStream) -> Token
1616
let line: Option<usize> = value.getattr("lineno").ok().and_then(|x| x.extract().ok());
1717
let msg: Option<String> = value.getattr("msg").ok().and_then(|x| x.extract().ok());
1818
if let (Some(line), Some(msg)) = (line, msg) {
19-
if let Some(span) = span_for_line(tokens.clone(), line) {
20-
let error = format!("python: {}", msg);
21-
return quote_spanned!(span.into() => compile_error!{#error});
19+
if let Some(spans) = spans_for_line(tokens.clone(), line) {
20+
return compile_error(spans, format!("python: {msg}"));
2221
}
2322
}
2423
}
@@ -27,9 +26,8 @@ pub fn compile_error_msg(py: Python, error: PyErr, tokens: TokenStream) -> Token
2726
if let Ok((file, line)) = get_traceback_info(tb) {
2827
if file == Span::call_site().file() {
2928
if let Ok(msg) = value.str() {
30-
if let Some(span) = span_for_line(tokens, line) {
31-
let error = format!("python: {}", msg);
32-
return quote_spanned!(span.into() => compile_error!{#error});
29+
if let Some(spans) = spans_for_line(tokens, line) {
30+
return compile_error(spans, format!("python: {msg}"));
3331
}
3432
}
3533
}
@@ -48,21 +46,22 @@ fn get_traceback_info(tb: &Bound<'_, PyTraceback>) -> PyResult<(String, usize)>
4846
Ok((file, line))
4947
}
5048

51-
/// Get a span for a specific line of input from a TokenStream.
52-
fn span_for_line(input: TokenStream, line: usize) -> Option<Span> {
49+
/// Get the first and last span for a specific line of input from a TokenStream.
50+
fn spans_for_line(input: TokenStream, line: usize) -> Option<(Span, Span)> {
5351
let mut spans = input
5452
.into_iter()
5553
.map(|x| x.span().unwrap())
5654
.skip_while(|span| span.start().line() < line)
5755
.take_while(|span| span.start().line() == line);
5856

59-
let mut result = spans.next()?;
60-
for span in spans {
61-
result = match result.join(span) {
62-
None => return Some(result),
63-
Some(span) => span,
64-
}
65-
}
57+
let first = spans.next()?;
58+
let last = spans.last().unwrap_or(first);
59+
60+
Some((first, last))
61+
}
6662

67-
Some(result)
63+
/// Create a compile_error!{} using two spans that mark the start and end of the error.
64+
fn compile_error(spans: (Span, Span), error: String) -> TokenStream {
65+
let path = quote_spanned!(spans.0.into() => ::core::compile_error);
66+
quote_spanned!(spans.1.into() => #path!{#error})
6867
}

0 commit comments

Comments
 (0)