Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Span::unique_site #60106

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libproc_macro/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ macro_rules! with_api {
},
Span {
fn debug($self: $S::Span) -> String;
fn unique_site() -> $S::Span;
fn def_site() -> $S::Span;
fn call_site() -> $S::Span;
fn source_file($self: $S::Span) -> $S::SourceFile;
Expand Down
8 changes: 8 additions & 0 deletions src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ macro_rules! diagnostic_method {
}

impl Span {
/// A span that resolves at a unique site. Each call to this method will return a new unique
/// span that is inaccessible from other sites. For those familiar with Lisp, this is similar to
/// [gensym](http://www.lispworks.com/documentation/lw50/CLHS/Body/f_gensym.htm).
#[unstable(feature = "proc_macro_unique_site", issue = "54725")]
pub fn unique_site() -> Span {
Span(bridge::client::Span::unique_site())
}

/// A span that resolves at the macro definition site.
#[unstable(feature = "proc_macro_def_site", issue = "54724")]
pub fn def_site() -> Span {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2306,7 +2306,7 @@ impl<'a> Resolver<'a> {
let mut result = None;
// Find the last modern mark from the end if it exists.
while let Some(&(mark, transparency)) = iter.peek() {
if transparency == Transparency::Opaque {
if transparency >= Transparency::Opaque {
result = Some(mark);
iter.next();
} else {
Expand Down
12 changes: 11 additions & 1 deletion src/libsyntax_ext/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ use proc_macro::{Delimiter, Level, LineColumn, Spacing};
use rustc_data_structures::sync::Lrc;
use std::ascii;
use std::ops::Bound;
use std::sync::atomic;
use syntax::ast;
use syntax::ext::base::ExtCtxt;
use syntax::parse::lexer::comments;
use syntax::parse::{self, token, ParseSess};
use syntax::tokenstream::{self, DelimSpan, IsJoint::*, TokenStream, TreeAndJoint};
use syntax_pos::hygiene::{SyntaxContext, Transparency};
use syntax_pos::hygiene::{Mark, SyntaxContext, Transparency};
use syntax_pos::symbol::{keywords, Symbol};
use syntax_pos::{BytePos, FileName, MultiSpan, Pos, SourceFile, Span};

Expand Down Expand Up @@ -363,6 +364,7 @@ pub(crate) struct Rustc<'a> {
sess: &'a ParseSess,
def_site: Span,
call_site: Span,
mark: Mark,
}

impl<'a> Rustc<'a> {
Expand All @@ -379,6 +381,7 @@ impl<'a> Rustc<'a> {
sess: cx.parse_sess,
def_site: to_span(Transparency::Opaque),
call_site: to_span(Transparency::Transparent),
mark: cx.current_expansion.mark,
}
}
}
Expand Down Expand Up @@ -698,6 +701,13 @@ impl server::Span for Rustc<'_> {
fn debug(&mut self, span: Self::Span) -> String {
format!("{:?} bytes({}..{})", span.ctxt(), span.lo().0, span.hi().0)
}
fn unique_site(&mut self) -> Self::Span {
static UNIQUE: atomic::AtomicU64 = atomic::AtomicU64::new(0);
let transparency = Transparency::Unique(UNIQUE.fetch_add(1, atomic::Ordering::Relaxed));
self.mark.expn_info().unwrap().call_site.with_ctxt(
SyntaxContext::empty().apply_mark_with_transparency(self.mark, transparency),
)
}
fn def_site(&mut self) -> Self::Span {
self.def_site
}
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax_pos/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub enum Transparency {
/// Identifier produced by an opaque expansion is always resolved at definition-site.
/// Def-site spans in procedural macros, identifiers from `macro` by default use this.
Opaque,
Unique(u64),
}

impl Mark {
Expand Down Expand Up @@ -273,7 +274,7 @@ impl SyntaxContext {
pub fn apply_mark_with_transparency(self, mark: Mark, transparency: Transparency)
-> SyntaxContext {
assert_ne!(mark, Mark::root());
if transparency == Transparency::Opaque {
if transparency >= Transparency::Opaque {
return self.apply_mark_internal(mark, transparency);
}

Expand Down