Skip to content

Commit 400f736

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)](rust-lang/rust#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 e044356 + eb498ef commit 400f736

File tree

2 files changed

+23
-60
lines changed

2 files changed

+23
-60
lines changed

proc_macro/src/bridge/mod.rs

Lines changed: 5 additions & 7 deletions
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,

proc_macro/src/lib.rs

Lines changed: 18 additions & 53 deletions
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)]

0 commit comments

Comments
 (0)