From d5aec64c12db44ddcfbf8682b9ec4e515a6f9953 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 9 Jun 2021 14:37:10 +0200 Subject: [PATCH 1/2] Add proc_macro::Span::{before, after}. --- compiler/rustc_expand/src/proc_macro_server.rs | 6 ++++++ library/proc_macro/src/bridge/mod.rs | 2 ++ library/proc_macro/src/lib.rs | 12 ++++++++++++ 3 files changed, 20 insertions(+) diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index 92315c4d4f6..2507ad44307 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -738,6 +738,12 @@ impl server::Span for Rustc<'_> { let loc = self.sess.source_map().lookup_char_pos(span.hi()); LineColumn { line: loc.line, column: loc.col.to_usize() } } + fn before(&mut self, span: Self::Span) -> Self::Span { + span.shrink_to_lo() + } + fn after(&mut self, span: Self::Span) -> Self::Span { + span.shrink_to_hi() + } fn join(&mut self, first: Self::Span, second: Self::Span) -> Option { let self_loc = self.sess.source_map().lookup_char_pos(first.lo()); let other_loc = self.sess.source_map().lookup_char_pos(second.lo()); diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index a2953b68564..d46b8b52df4 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -160,6 +160,8 @@ macro_rules! with_api { fn source($self: $S::Span) -> $S::Span; fn start($self: $S::Span) -> LineColumn; fn end($self: $S::Span) -> LineColumn; + fn before($self: $S::Span) -> $S::Span; + fn after($self: $S::Span) -> $S::Span; fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>; fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span; fn source_text($self: $S::Span) -> Option; diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index 3990826ce42..b8ed3372aff 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -358,6 +358,18 @@ impl Span { self.0.end() } + /// Creates an empty span pointing to directly before this span. + #[unstable(feature = "proc_macro_span_shrink", issue = "none")] + pub fn before(&self) -> Span { + Span(self.0.before()) + } + + /// Creates an empty span pointing to directly after this span. + #[unstable(feature = "proc_macro_span_shrink", issue = "none")] + pub fn after(&self) -> Span { + Span(self.0.after()) + } + /// Creates a new span encompassing `self` and `other`. /// /// Returns `None` if `self` and `other` are from different files. From f9be6cd8988a48a532c99a9f5b887a6d354e5b2f Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 28 Jul 2021 16:34:45 +0200 Subject: [PATCH 2/2] Add tracking issue number to proc_macro_span_shrink. --- library/proc_macro/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index b8ed3372aff..6de61497021 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -359,13 +359,13 @@ impl Span { } /// Creates an empty span pointing to directly before this span. - #[unstable(feature = "proc_macro_span_shrink", issue = "none")] + #[unstable(feature = "proc_macro_span_shrink", issue = "87552")] pub fn before(&self) -> Span { Span(self.0.before()) } /// Creates an empty span pointing to directly after this span. - #[unstable(feature = "proc_macro_span_shrink", issue = "none")] + #[unstable(feature = "proc_macro_span_shrink", issue = "87552")] pub fn after(&self) -> Span { Span(self.0.after()) }