Skip to content

Commit fa56e01

Browse files
authored
Rollup merge of #111571 - jhpratt:proc-macro-span, r=m-ou-se
Implement proposed API for `proc_macro_span` As proposed in [#54725 (comment)](#54725 (comment)). I have omitted the byte-level API as it's already available as [`Span::byte_range`](https://doc.rust-lang.org/nightly/proc_macro/struct.Span.html#method.byte_range). `@rustbot` label +A-proc-macros r? `@m-ou-se`
2 parents 8882507 + 6159075 commit fa56e01

File tree

16 files changed

+68
-123
lines changed

16 files changed

+68
-123
lines changed

Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -2580,9 +2580,9 @@ checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
25802580

25812581
[[package]]
25822582
name = "proc-macro2"
2583-
version = "1.0.56"
2583+
version = "1.0.60"
25842584
source = "registry+https://github.com/rust-lang/crates.io-index"
2585-
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
2585+
checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406"
25862586
dependencies = [
25872587
"unicode-ident",
25882588
]

compiler/rustc_expand/src/proc_macro_server.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::base::ExtCtxt;
22
use pm::bridge::{
33
server, DelimSpan, Diagnostic, ExpnGlobals, Group, Ident, LitKind, Literal, Punct, TokenTree,
44
};
5-
use pm::{Delimiter, Level, LineColumn};
5+
use pm::{Delimiter, Level};
66
use rustc_ast as ast;
77
use rustc_ast::token;
88
use rustc_ast::tokenstream::{self, Spacing::*, TokenStream};
@@ -648,23 +648,22 @@ impl server::Span for Rustc<'_, '_> {
648648

649649
Range { start: relative_start_pos.0 as usize, end: relative_end_pos.0 as usize }
650650
}
651-
652-
fn start(&mut self, span: Self::Span) -> LineColumn {
653-
let loc = self.sess().source_map().lookup_char_pos(span.lo());
654-
LineColumn { line: loc.line, column: loc.col.to_usize() }
651+
fn start(&mut self, span: Self::Span) -> Self::Span {
652+
span.shrink_to_lo()
655653
}
656654

657-
fn end(&mut self, span: Self::Span) -> LineColumn {
658-
let loc = self.sess().source_map().lookup_char_pos(span.hi());
659-
LineColumn { line: loc.line, column: loc.col.to_usize() }
655+
fn end(&mut self, span: Self::Span) -> Self::Span {
656+
span.shrink_to_hi()
660657
}
661658

662-
fn before(&mut self, span: Self::Span) -> Self::Span {
663-
span.shrink_to_lo()
659+
fn line(&mut self, span: Self::Span) -> usize {
660+
let loc = self.sess().source_map().lookup_char_pos(span.lo());
661+
loc.line
664662
}
665663

666-
fn after(&mut self, span: Self::Span) -> Self::Span {
667-
span.shrink_to_hi()
664+
fn column(&mut self, span: Self::Span) -> usize {
665+
let loc = self.sess().source_map().lookup_char_pos(span.lo());
666+
loc.col.to_usize() + 1
668667
}
669668

670669
fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {

library/proc_macro/src/bridge/mod.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
#![deny(unsafe_code)]
1010

11-
use crate::{Delimiter, Level, LineColumn, Spacing};
11+
use crate::{Delimiter, Level, Spacing};
1212
use std::fmt;
1313
use std::hash::Hash;
1414
use std::marker;
@@ -95,10 +95,10 @@ macro_rules! with_api {
9595
fn parent($self: $S::Span) -> Option<$S::Span>;
9696
fn source($self: $S::Span) -> $S::Span;
9797
fn byte_range($self: $S::Span) -> Range<usize>;
98-
fn start($self: $S::Span) -> LineColumn;
99-
fn end($self: $S::Span) -> LineColumn;
100-
fn before($self: $S::Span) -> $S::Span;
101-
fn after($self: $S::Span) -> $S::Span;
98+
fn start($self: $S::Span) -> $S::Span;
99+
fn end($self: $S::Span) -> $S::Span;
100+
fn line($self: $S::Span) -> usize;
101+
fn column($self: $S::Span) -> usize;
102102
fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
103103
fn subspan($self: $S::Span, start: Bound<usize>, end: Bound<usize>) -> Option<$S::Span>;
104104
fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
@@ -299,7 +299,6 @@ mark_noop! {
299299
Delimiter,
300300
LitKind,
301301
Level,
302-
LineColumn,
303302
Spacing,
304303
}
305304

@@ -319,7 +318,6 @@ rpc_encode_decode!(
319318
Help,
320319
}
321320
);
322-
rpc_encode_decode!(struct LineColumn { line, column });
323321
rpc_encode_decode!(
324322
enum Spacing {
325323
Alone,

library/proc_macro/src/lib.rs

+18-53
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ mod diagnostic;
4343
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
4444
pub use diagnostic::{Diagnostic, Level, MultiSpan};
4545

46-
use std::cmp::Ordering;
4746
use std::ops::{Range, RangeBounds};
4847
use std::path::PathBuf;
4948
use std::str::FromStr;
@@ -494,28 +493,32 @@ impl Span {
494493
self.0.byte_range()
495494
}
496495

497-
/// Gets the starting line/column in the source file for this span.
496+
/// Creates an empty span pointing to directly before this span.
498497
#[unstable(feature = "proc_macro_span", issue = "54725")]
499-
pub fn start(&self) -> LineColumn {
500-
self.0.start().add_1_to_column()
498+
pub fn start(&self) -> Span {
499+
Span(self.0.start())
501500
}
502501

503-
/// Gets the ending line/column in the source file for this span.
502+
/// Creates an empty span pointing to directly after this span.
504503
#[unstable(feature = "proc_macro_span", issue = "54725")]
505-
pub fn end(&self) -> LineColumn {
506-
self.0.end().add_1_to_column()
504+
pub fn end(&self) -> Span {
505+
Span(self.0.end())
507506
}
508507

509-
/// Creates an empty span pointing to directly before this span.
510-
#[unstable(feature = "proc_macro_span_shrink", issue = "87552")]
511-
pub fn before(&self) -> Span {
512-
Span(self.0.before())
508+
/// The one-indexed line of the source file where the span starts.
509+
///
510+
/// To obtain the line of the span's end, use `span.end().line()`.
511+
#[unstable(feature = "proc_macro_span", issue = "54725")]
512+
pub fn line(&self) -> usize {
513+
self.0.line()
513514
}
514515

515-
/// Creates an empty span pointing to directly after this span.
516-
#[unstable(feature = "proc_macro_span_shrink", issue = "87552")]
517-
pub fn after(&self) -> Span {
518-
Span(self.0.after())
516+
/// The one-indexed column of the source file where the span starts.
517+
///
518+
/// To obtain the column of the span's end, use `span.end().column()`.
519+
#[unstable(feature = "proc_macro_span", issue = "54725")]
520+
pub fn column(&self) -> usize {
521+
self.0.column()
519522
}
520523

521524
/// Creates a new span encompassing `self` and `other`.
@@ -586,44 +589,6 @@ impl fmt::Debug for Span {
586589
}
587590
}
588591

589-
/// A line-column pair representing the start or end of a `Span`.
590-
#[unstable(feature = "proc_macro_span", issue = "54725")]
591-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
592-
pub struct LineColumn {
593-
/// The 1-indexed line in the source file on which the span starts or ends (inclusive).
594-
#[unstable(feature = "proc_macro_span", issue = "54725")]
595-
pub line: usize,
596-
/// The 1-indexed column (number of bytes in UTF-8 encoding) in the source
597-
/// file on which the span starts or ends (inclusive).
598-
#[unstable(feature = "proc_macro_span", issue = "54725")]
599-
pub column: usize,
600-
}
601-
602-
impl LineColumn {
603-
fn add_1_to_column(self) -> Self {
604-
LineColumn { line: self.line, column: self.column + 1 }
605-
}
606-
}
607-
608-
#[unstable(feature = "proc_macro_span", issue = "54725")]
609-
impl !Send for LineColumn {}
610-
#[unstable(feature = "proc_macro_span", issue = "54725")]
611-
impl !Sync for LineColumn {}
612-
613-
#[unstable(feature = "proc_macro_span", issue = "54725")]
614-
impl Ord for LineColumn {
615-
fn cmp(&self, other: &Self) -> Ordering {
616-
self.line.cmp(&other.line).then(self.column.cmp(&other.column))
617-
}
618-
}
619-
620-
#[unstable(feature = "proc_macro_span", issue = "54725")]
621-
impl PartialOrd for LineColumn {
622-
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
623-
Some(self.cmp(other))
624-
}
625-
}
626-
627592
/// The source file of a given `Span`.
628593
#[unstable(feature = "proc_macro_span", issue = "54725")]
629594
#[derive(Clone)]

src/bootstrap/Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,9 @@ dependencies = [
527527

528528
[[package]]
529529
name = "proc-macro2"
530-
version = "1.0.56"
530+
version = "1.0.60"
531531
source = "registry+https://github.com/rust-lang/crates.io-index"
532-
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
532+
checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406"
533533
dependencies = [
534534
"unicode-ident",
535535
]

src/tools/miri/Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
529529

530530
[[package]]
531531
name = "proc-macro2"
532-
version = "1.0.56"
532+
version = "1.0.60"
533533
source = "registry+https://github.com/rust-lang/crates.io-index"
534-
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
534+
checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406"
535535
dependencies = [
536536
"unicode-ident",
537537
]

src/tools/miri/cargo-miri/Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ checksum = "ece97ea872ece730aed82664c424eb4c8291e1ff2480247ccf7409044bc6479f"
179179

180180
[[package]]
181181
name = "proc-macro2"
182-
version = "1.0.56"
182+
version = "1.0.60"
183183
source = "registry+https://github.com/rust-lang/crates.io-index"
184-
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
184+
checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406"
185185
dependencies = [
186186
"unicode-ident",
187187
]

src/tools/miri/test-cargo-miri/Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ version = "0.1.0"
8383

8484
[[package]]
8585
name = "proc-macro2"
86-
version = "1.0.49"
86+
version = "1.0.60"
8787
source = "registry+https://github.com/rust-lang/crates.io-index"
88-
checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5"
88+
checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406"
8989
dependencies = [
9090
"unicode-ident",
9191
]

src/tools/miri/test_dependencies/Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
193193

194194
[[package]]
195195
name = "proc-macro2"
196-
version = "1.0.49"
196+
version = "1.0.60"
197197
source = "registry+https://github.com/rust-lang/crates.io-index"
198-
checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5"
198+
checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406"
199199
dependencies = [
200200
"unicode-ident",
201201
]

src/tools/rust-analyzer/Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1304,9 +1304,9 @@ version = "0.0.0"
13041304

13051305
[[package]]
13061306
name = "proc-macro2"
1307-
version = "1.0.56"
1307+
version = "1.0.60"
13081308
source = "registry+https://github.com/rust-lang/crates.io-index"
1309-
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
1309+
checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406"
13101310
dependencies = [
13111311
"unicode-ident",
13121312
]

src/tools/rust-analyzer/crates/proc-macro-srv/src/server.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
//!
99
//! FIXME: No span and source file information is implemented yet
1010
11-
use proc_macro::{
12-
bridge::{self, server},
13-
LineColumn,
14-
};
11+
use proc_macro::bridge::{self, server};
1512

1613
mod token_stream;
1714
pub use token_stream::TokenStream;
@@ -304,14 +301,6 @@ impl server::Span for RustAnalyzer {
304301
// FIXME handle span
305302
Range { start: 0, end: 0 }
306303
}
307-
fn start(&mut self, _span: Self::Span) -> LineColumn {
308-
// FIXME handle span
309-
LineColumn { line: 0, column: 0 }
310-
}
311-
fn end(&mut self, _span: Self::Span) -> LineColumn {
312-
// FIXME handle span
313-
LineColumn { line: 0, column: 0 }
314-
}
315304
fn join(&mut self, first: Self::Span, _second: Self::Span) -> Option<Self::Span> {
316305
// Just return the first span again, because some macros will unwrap the result.
317306
Some(first)
@@ -330,13 +319,23 @@ impl server::Span for RustAnalyzer {
330319
tt::TokenId::unspecified()
331320
}
332321

333-
fn after(&mut self, _self_: Self::Span) -> Self::Span {
322+
fn end(&mut self, _self_: Self::Span) -> Self::Span {
334323
tt::TokenId::unspecified()
335324
}
336325

337-
fn before(&mut self, _self_: Self::Span) -> Self::Span {
326+
fn start(&mut self, _self_: Self::Span) -> Self::Span {
338327
tt::TokenId::unspecified()
339328
}
329+
330+
fn line(&mut self, _span: Self::Span) -> usize {
331+
// FIXME handle line
332+
0
333+
}
334+
335+
fn column(&mut self, _span: Self::Span) -> usize {
336+
// FIXME handle column
337+
0
338+
}
340339
}
341340

342341
impl server::Symbol for RustAnalyzer {

tests/ui/macros/auxiliary/proc_macro_sequence.rs

-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ extern crate proc_macro;
88

99
use proc_macro::{quote, Span, TokenStream, TokenTree};
1010

11-
fn assert_same_span(a: Span, b: Span) {
12-
assert_eq!(a.start(), b.start());
13-
assert_eq!(a.end(), b.end());
14-
}
15-
1611
// This macro generates a macro with the same macro definition as `manual_foo` in
1712
// `same-sequence-span.rs` but with the same span for all sequences.
1813
#[proc_macro]

tests/ui/macros/same-sequence-span.stderr

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ LL | $(= $z:tt)*
1717
error: `$x:expr` may be followed by `$y:tt`, which is not allowed for `expr` fragments
1818
--> $DIR/same-sequence-span.rs:19:1
1919
|
20+
LL | | }
21+
| |_________________________________^ not allowed after `expr` fragments
22+
LL |
2023
LL | proc_macro_sequence::make_foo!();
2124
| ^-------------------------------
2225
| |
2326
| _in this macro invocation
2427
| |
25-
LL | |
26-
LL | |
27-
LL | | fn main() {}
28-
| |_________________________________^ not allowed after `expr` fragments
2928
|
3029
= note: allowed there are: `=>`, `,` or `;`
3130
= note: this error originates in the macro `proc_macro_sequence::make_foo` (in Nightly builds, run with -Z macro-backtrace for more info)

tests/ui/proc-macro/auxiliary/api/cmp.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
use proc_macro::{LineColumn, Punct, Spacing};
1+
use proc_macro::{Punct, Spacing};
22

33
pub fn test() {
4-
test_line_column_ord();
54
test_punct_eq();
65
}
76

8-
fn test_line_column_ord() {
9-
let line0_column0 = LineColumn { line: 0, column: 0 };
10-
let line0_column1 = LineColumn { line: 0, column: 1 };
11-
let line1_column0 = LineColumn { line: 1, column: 0 };
12-
assert!(line0_column0 < line0_column1);
13-
assert!(line0_column1 < line1_column0);
14-
}
15-
167
fn test_punct_eq() {
178
let colon_alone = Punct::new(':', Spacing::Alone);
189
assert_eq!(colon_alone, ':');

tests/ui/proc-macro/auxiliary/assert-span-pos.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ pub fn assert_span_pos(input: TokenStream) -> TokenStream {
2626
let line: usize = str1.parse().unwrap();
2727
let col: usize = str2.parse().unwrap();
2828

29-
let sp1s = sp1.start();
30-
if (line, col) != (sp1s.line, sp1s.column) {
29+
if (line, col) != (sp1.line(), sp1.column()) {
3130
let msg = format!("line/column mismatch: ({}, {}) != ({}, {})", line, col,
32-
sp1s.line, sp1s.column);
31+
sp1.line(), sp1.column());
3332
sp1.error(msg).emit();
3433
}
3534

tests/ui/proc-macro/auxiliary/macro-only-syntax.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn expect_brace(tokens: &mut token_stream::IntoIter) -> token_stream::IntoIter {
8181

8282
fn check_useful_span(token: TokenTree, expected_filename: &str) {
8383
let span = token.span();
84-
assert!(span.start().column < span.end().column);
84+
assert!(span.column() < span.end().column());
8585

8686
let source_path = span.source_file().path();
8787
let filename = source_path.components().last().unwrap();

0 commit comments

Comments
 (0)