Skip to content

Commit 38fee30

Browse files
committed
proc_macro: remove the __internal module.
1 parent e305994 commit 38fee30

File tree

4 files changed

+66
-154
lines changed

4 files changed

+66
-154
lines changed

src/libproc_macro/lib.rs

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,101 +1145,3 @@ impl fmt::Debug for Literal {
11451145
self.0.fmt(f)
11461146
}
11471147
}
1148-
1149-
/// Permanently unstable internal implementation details of this crate. This
1150-
/// should not be used.
1151-
///
1152-
/// These methods are used by the rest of the compiler to generate instances of
1153-
/// `TokenStream` to hand to macro definitions, as well as consume the output.
1154-
///
1155-
/// Note that this module is also intentionally separate from the rest of the
1156-
/// crate. This allows the `#[unstable]` directive below to naturally apply to
1157-
/// all of the contents.
1158-
#[unstable(feature = "proc_macro_internals", issue = "27812")]
1159-
#[doc(hidden)]
1160-
pub mod __internal {
1161-
use std::cell::Cell;
1162-
use std::ptr;
1163-
1164-
use syntax::errors::DiagnosticBuilder;
1165-
use syntax::ext::base::ExtCtxt;
1166-
use syntax::parse::ParseSess;
1167-
use syntax_pos::{BytePos, Loc, DUMMY_SP, Span};
1168-
use syntax_pos::hygiene::{SyntaxContext, Transparency};
1169-
1170-
use super::LexError;
1171-
1172-
pub fn lookup_char_pos(pos: BytePos) -> Loc {
1173-
with_sess(|sess, _| sess.source_map().lookup_char_pos(pos))
1174-
}
1175-
1176-
pub fn parse_to_lex_err(mut err: DiagnosticBuilder) -> LexError {
1177-
err.cancel();
1178-
LexError { _inner: () }
1179-
}
1180-
1181-
#[derive(Clone, Copy)]
1182-
pub struct ProcMacroData {
1183-
pub def_site: Span,
1184-
pub call_site: Span,
1185-
}
1186-
1187-
#[derive(Clone, Copy)]
1188-
struct ProcMacroSess {
1189-
parse_sess: *const ParseSess,
1190-
data: ProcMacroData,
1191-
}
1192-
1193-
// Emulate scoped_thread_local!() here essentially
1194-
thread_local! {
1195-
static CURRENT_SESS: Cell<ProcMacroSess> = Cell::new(ProcMacroSess {
1196-
parse_sess: ptr::null(),
1197-
data: ProcMacroData { def_site: DUMMY_SP, call_site: DUMMY_SP },
1198-
});
1199-
}
1200-
1201-
pub fn set_sess<F, R>(cx: &ExtCtxt, f: F) -> R
1202-
where F: FnOnce() -> R
1203-
{
1204-
struct Reset { prev: ProcMacroSess }
1205-
1206-
impl Drop for Reset {
1207-
fn drop(&mut self) {
1208-
CURRENT_SESS.with(|p| p.set(self.prev));
1209-
}
1210-
}
1211-
1212-
CURRENT_SESS.with(|p| {
1213-
let _reset = Reset { prev: p.get() };
1214-
1215-
// No way to determine def location for a proc macro right now, so use call location.
1216-
let location = cx.current_expansion.mark.expn_info().unwrap().call_site;
1217-
let to_span = |transparency| location.with_ctxt(
1218-
SyntaxContext::empty().apply_mark_with_transparency(cx.current_expansion.mark,
1219-
transparency));
1220-
p.set(ProcMacroSess {
1221-
parse_sess: cx.parse_sess,
1222-
data: ProcMacroData {
1223-
def_site: to_span(Transparency::Opaque),
1224-
call_site: to_span(Transparency::Transparent),
1225-
},
1226-
});
1227-
f()
1228-
})
1229-
}
1230-
1231-
pub fn in_sess() -> bool
1232-
{
1233-
!CURRENT_SESS.with(|sess| sess.get()).parse_sess.is_null()
1234-
}
1235-
1236-
pub fn with_sess<F, R>(f: F) -> R
1237-
where F: FnOnce(&ParseSess, &ProcMacroData) -> R
1238-
{
1239-
let sess = CURRENT_SESS.with(|sess| sess.get());
1240-
if sess.parse_sess.is_null() {
1241-
panic!("procedural macro API is used outside of a procedural macro");
1242-
}
1243-
f(unsafe { &*sess.parse_sess }, &sess.data)
1244-
}
1245-
}

src/libproc_macro/rustc.rs

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
// except according to those terms.
1010

1111
use bridge::{server, TokenTree};
12-
use {Delimiter, Level, LineColumn, Spacing, __internal};
12+
use {Delimiter, Level, LineColumn, Spacing};
1313

1414
use rustc_data_structures::sync::Lrc;
1515
use rustc_errors::{self as errors, Diagnostic, DiagnosticBuilder};
1616
use std::ascii;
1717
use std::ops::Bound;
1818
use syntax::ast;
19+
use syntax::ext::base::ExtCtxt;
1920
use syntax::parse::lexer::comments;
20-
use syntax::parse::{self, token};
21+
use syntax::parse::{self, token, ParseSess};
2122
use syntax::tokenstream::{self, DelimSpan, TokenStream};
23+
use syntax_pos::hygiene::{SyntaxContext, Transparency};
2224
use syntax_pos::symbol::{keywords, Symbol};
2325
use syntax_pos::{BytePos, FileName, MultiSpan, Pos, SourceFile, Span};
2426

@@ -43,7 +45,7 @@ impl Delimiter {
4345
}
4446

4547
impl TokenTree<Group, Punct, Ident, Literal> {
46-
fn from_internal(stream: TokenStream, stack: &mut Vec<Self>) -> Self {
48+
fn from_internal(stream: TokenStream, sess: &ParseSess, stack: &mut Vec<Self>) -> Self {
4749
use syntax::parse::token::*;
4850

4951
let (tree, joint) = stream.as_tree();
@@ -188,14 +190,14 @@ impl TokenTree<Group, Punct, Ident, Literal> {
188190
})
189191
}
190192

191-
Interpolated(_) => __internal::with_sess(|sess, _| {
193+
Interpolated(_) => {
192194
let stream = token.interpolated_to_tokenstream(sess, span);
193195
TokenTree::Group(Group {
194196
delimiter: Delimiter::None,
195197
stream,
196198
span: DelimSpan::from_single(span),
197199
})
198-
}),
200+
}
199201

200202
DotEq => op!('.', '='),
201203
OpenDelim(..) | CloseDelim(..) => unreachable!(),
@@ -337,9 +339,31 @@ pub struct Literal {
337339
span: Span,
338340
}
339341

340-
pub struct Rustc;
342+
pub struct Rustc<'a> {
343+
sess: &'a ParseSess,
344+
def_site: Span,
345+
call_site: Span,
346+
}
347+
348+
impl<'a> Rustc<'a> {
349+
pub fn new(cx: &'a ExtCtxt) -> Self {
350+
// No way to determine def location for a proc macro right now, so use call location.
351+
let location = cx.current_expansion.mark.expn_info().unwrap().call_site;
352+
let to_span = |transparency| {
353+
location.with_ctxt(
354+
SyntaxContext::empty()
355+
.apply_mark_with_transparency(cx.current_expansion.mark, transparency),
356+
)
357+
};
358+
Rustc {
359+
sess: cx.parse_sess,
360+
def_site: to_span(Transparency::Opaque),
361+
call_site: to_span(Transparency::Transparent),
362+
}
363+
}
364+
}
341365

342-
impl server::Types for Rustc {
366+
impl server::Types for Rustc<'_> {
343367
type TokenStream = TokenStream;
344368
type TokenStreamBuilder = tokenstream::TokenStreamBuilder;
345369
type TokenStreamIter = TokenStreamIter;
@@ -353,22 +377,20 @@ impl server::Types for Rustc {
353377
type Span = Span;
354378
}
355379

356-
impl server::TokenStream for Rustc {
380+
impl server::TokenStream for Rustc<'_> {
357381
fn new(&mut self) -> Self::TokenStream {
358382
TokenStream::empty()
359383
}
360384
fn is_empty(&mut self, stream: &Self::TokenStream) -> bool {
361385
stream.is_empty()
362386
}
363387
fn from_str(&mut self, src: &str) -> Self::TokenStream {
364-
::__internal::with_sess(|sess, data| {
365-
parse::parse_stream_from_source_str(
366-
FileName::ProcMacroSourceCode,
367-
src.to_string(),
368-
sess,
369-
Some(data.call_site),
370-
)
371-
})
388+
parse::parse_stream_from_source_str(
389+
FileName::ProcMacroSourceCode,
390+
src.to_string(),
391+
self.sess,
392+
Some(self.call_site),
393+
)
372394
}
373395
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
374396
stream.to_string()
@@ -387,7 +409,7 @@ impl server::TokenStream for Rustc {
387409
}
388410
}
389411

390-
impl server::TokenStreamBuilder for Rustc {
412+
impl server::TokenStreamBuilder for Rustc<'_> {
391413
fn new(&mut self) -> Self::TokenStreamBuilder {
392414
tokenstream::TokenStreamBuilder::new()
393415
}
@@ -399,15 +421,15 @@ impl server::TokenStreamBuilder for Rustc {
399421
}
400422
}
401423

402-
impl server::TokenStreamIter for Rustc {
424+
impl server::TokenStreamIter for Rustc<'_> {
403425
fn next(
404426
&mut self,
405427
iter: &mut Self::TokenStreamIter,
406428
) -> Option<TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>> {
407429
loop {
408430
let tree = iter.stack.pop().or_else(|| {
409431
let next = iter.cursor.next_as_stream()?;
410-
Some(TokenTree::from_internal(next, &mut iter.stack))
432+
Some(TokenTree::from_internal(next, self.sess, &mut iter.stack))
411433
})?;
412434
// HACK: The condition "dummy span + group with empty delimiter" represents an AST
413435
// fragment approximately converted into a token stream. This may happen, for
@@ -426,7 +448,7 @@ impl server::TokenStreamIter for Rustc {
426448
}
427449
}
428450

429-
impl server::Group for Rustc {
451+
impl server::Group for Rustc<'_> {
430452
fn new(&mut self, delimiter: Delimiter, stream: Self::TokenStream) -> Self::Group {
431453
Group {
432454
delimiter,
@@ -454,7 +476,7 @@ impl server::Group for Rustc {
454476
}
455477
}
456478

457-
impl server::Punct for Rustc {
479+
impl server::Punct for Rustc<'_> {
458480
fn new(&mut self, ch: char, spacing: Spacing) -> Self::Punct {
459481
Punct {
460482
ch,
@@ -480,7 +502,7 @@ impl server::Punct for Rustc {
480502
}
481503
}
482504

483-
impl server::Ident for Rustc {
505+
impl server::Ident for Rustc<'_> {
484506
fn new(&mut self, string: &str, span: Self::Span, is_raw: bool) -> Self::Ident {
485507
let sym = Symbol::intern(string);
486508
if is_raw
@@ -499,7 +521,7 @@ impl server::Ident for Rustc {
499521
}
500522
}
501523

502-
impl server::Literal for Rustc {
524+
impl server::Literal for Rustc<'_> {
503525
// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
504526
fn debug(&mut self, literal: &Self::Literal) -> String {
505527
format!("{:?}", literal)
@@ -616,7 +638,7 @@ impl server::Literal for Rustc {
616638
}
617639
}
618640

619-
impl server::SourceFile for Rustc {
641+
impl<'a> server::SourceFile for Rustc<'a> {
620642
fn eq(&mut self, file1: &Self::SourceFile, file2: &Self::SourceFile) -> bool {
621643
Lrc::ptr_eq(file1, file2)
622644
}
@@ -634,7 +656,7 @@ impl server::SourceFile for Rustc {
634656
}
635657
}
636658

637-
impl server::MultiSpan for Rustc {
659+
impl server::MultiSpan for Rustc<'_> {
638660
fn new(&mut self) -> Self::MultiSpan {
639661
vec![]
640662
}
@@ -643,7 +665,7 @@ impl server::MultiSpan for Rustc {
643665
}
644666
}
645667

646-
impl server::Diagnostic for Rustc {
668+
impl server::Diagnostic for Rustc<'_> {
647669
fn new(&mut self, level: Level, msg: &str, spans: Self::MultiSpan) -> Self::Diagnostic {
648670
let mut diag = Diagnostic::new(level.to_internal(), msg);
649671
diag.set_span(MultiSpan::from_spans(spans));
@@ -659,24 +681,22 @@ impl server::Diagnostic for Rustc {
659681
diag.sub(level.to_internal(), msg, MultiSpan::from_spans(spans), None);
660682
}
661683
fn emit(&mut self, diag: Self::Diagnostic) {
662-
::__internal::with_sess(move |sess, _| {
663-
DiagnosticBuilder::new_diagnostic(&sess.span_diagnostic, diag).emit()
664-
});
684+
DiagnosticBuilder::new_diagnostic(&self.sess.span_diagnostic, diag).emit()
665685
}
666686
}
667687

668-
impl server::Span for Rustc {
688+
impl server::Span for Rustc<'_> {
669689
fn debug(&mut self, span: Self::Span) -> String {
670690
format!("{:?} bytes({}..{})", span.ctxt(), span.lo().0, span.hi().0)
671691
}
672692
fn def_site(&mut self) -> Self::Span {
673-
::__internal::with_sess(|_, data| data.def_site)
693+
self.def_site
674694
}
675695
fn call_site(&mut self) -> Self::Span {
676-
::__internal::with_sess(|_, data| data.call_site)
696+
self.call_site
677697
}
678698
fn source_file(&mut self, span: Self::Span) -> Self::SourceFile {
679-
::__internal::lookup_char_pos(span.lo()).file
699+
self.sess.source_map().lookup_char_pos(span.lo()).file
680700
}
681701
fn parent(&mut self, span: Self::Span) -> Option<Self::Span> {
682702
span.ctxt().outer().expn_info().map(|i| i.call_site)
@@ -685,22 +705,22 @@ impl server::Span for Rustc {
685705
span.source_callsite()
686706
}
687707
fn start(&mut self, span: Self::Span) -> LineColumn {
688-
let loc = ::__internal::lookup_char_pos(span.lo());
708+
let loc = self.sess.source_map().lookup_char_pos(span.lo());
689709
LineColumn {
690710
line: loc.line,
691711
column: loc.col.to_usize(),
692712
}
693713
}
694714
fn end(&mut self, span: Self::Span) -> LineColumn {
695-
let loc = ::__internal::lookup_char_pos(span.hi());
715+
let loc = self.sess.source_map().lookup_char_pos(span.hi());
696716
LineColumn {
697717
line: loc.line,
698718
column: loc.col.to_usize(),
699719
}
700720
}
701721
fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
702-
let self_loc = ::__internal::lookup_char_pos(first.lo());
703-
let other_loc = ::__internal::lookup_char_pos(second.lo());
722+
let self_loc = self.sess.source_map().lookup_char_pos(first.lo());
723+
let other_loc = self.sess.source_map().lookup_char_pos(second.lo());
704724

705725
if self_loc.file.name != other_loc.file.name {
706726
return None;

src/libsyntax_ext/deriving/custom.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,9 @@ impl MultiItemModifier for ProcMacroDerive {
7777
let item = ecx.resolver.eliminate_crate_var(item);
7878
let token = Token::interpolated(token::NtItem(item));
7979
let input = tokenstream::TokenTree::Token(DUMMY_SP, token).into();
80-
let server = ::proc_macro::rustc::Rustc;
81-
let res = ::proc_macro::__internal::set_sess(ecx, || {
82-
self.client.run(&EXEC_STRATEGY, server, input)
83-
});
8480

85-
let stream = match res {
81+
let server = ::proc_macro::rustc::Rustc::new(ecx);
82+
let stream = match self.client.run(&EXEC_STRATEGY, server, input) {
8683
Ok(stream) => stream,
8784
Err(e) => {
8885
let msg = "proc-macro derive panicked";
@@ -103,13 +100,14 @@ impl MultiItemModifier for ProcMacroDerive {
103100
let mut items = vec![];
104101

105102
loop {
106-
match parser.parse_item().map_err(::proc_macro::__internal::parse_to_lex_err) {
103+
match parser.parse_item() {
107104
Ok(None) => break,
108105
Ok(Some(item)) => {
109106
items.push(Annotatable::Item(item))
110107
}
111-
Err(_) => {
108+
Err(mut err) => {
112109
// FIXME: handle this better
110+
err.cancel();
113111
ecx.struct_span_fatal(span, msg).emit();
114112
FatalError.raise();
115113
}

0 commit comments

Comments
 (0)