Skip to content

Commit 9d7d24d

Browse files
authored
Rollup merge of #76057 - matklad:remove-retokenize, r=petrochenkov
Move retokenize hack to save_analysis closes #76046
2 parents 11193ca + 6621895 commit 9d7d24d

File tree

5 files changed

+36
-88
lines changed

5 files changed

+36
-88
lines changed

Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -3927,8 +3927,8 @@ dependencies = [
39273927
"rustc_data_structures",
39283928
"rustc_hir",
39293929
"rustc_hir_pretty",
3930+
"rustc_lexer",
39303931
"rustc_middle",
3931-
"rustc_parse",
39323932
"rustc_session",
39333933
"rustc_span",
39343934
"serde_json",

src/librustc_parse/lexer/mod.rs

+2-28
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,10 @@ impl<'a> StringReader<'a> {
4646
source_file: Lrc<rustc_span::SourceFile>,
4747
override_span: Option<Span>,
4848
) -> Self {
49-
// Make sure external source is loaded first, before accessing it.
50-
// While this can't show up during normal parsing, `retokenize` may
51-
// be called with a source file from an external crate.
52-
sess.source_map().ensure_source_file_source_present(Lrc::clone(&source_file));
53-
54-
let src = if let Some(src) = &source_file.src {
55-
Lrc::clone(&src)
56-
} else if let Some(src) = source_file.external_src.borrow().get_source() {
57-
Lrc::clone(&src)
58-
} else {
49+
let src = source_file.src.clone().unwrap_or_else(|| {
5950
sess.span_diagnostic
6051
.bug(&format!("cannot lex `source_file` without source: {}", source_file.name));
61-
};
52+
});
6253

6354
StringReader {
6455
sess,
@@ -70,23 +61,6 @@ impl<'a> StringReader<'a> {
7061
}
7162
}
7263

73-
pub fn retokenize(sess: &'a ParseSess, mut span: Span) -> Self {
74-
let begin = sess.source_map().lookup_byte_offset(span.lo());
75-
let end = sess.source_map().lookup_byte_offset(span.hi());
76-
77-
// Make the range zero-length if the span is invalid.
78-
if begin.sf.start_pos != end.sf.start_pos {
79-
span = span.shrink_to_lo();
80-
}
81-
82-
let mut sr = StringReader::new(sess, begin.sf, None);
83-
84-
// Seek the lexer to the right byte range.
85-
sr.end_src_index = sr.src_index(span.hi());
86-
87-
sr
88-
}
89-
9064
fn mk_sp(&self, lo: BytePos, hi: BytePos) -> Span {
9165
self.override_span.unwrap_or_else(|| Span::with_root_ctxt(lo, hi))
9266
}

src/librustc_save_analysis/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ rustc_ast_pretty = { path = "../librustc_ast_pretty" }
1616
rustc_data_structures = { path = "../librustc_data_structures" }
1717
rustc_hir = { path = "../librustc_hir" }
1818
rustc_hir_pretty = { path = "../librustc_hir_pretty" }
19-
rustc_parse = { path = "../librustc_parse" }
19+
rustc_lexer = { path = "../librustc_lexer" }
2020
serde_json = "1"
2121
rustc_session = { path = "../librustc_session" }
2222
rustc_span = { path = "../librustc_span" }

src/librustc_save_analysis/dump_visitor.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! recording the output.
1515
1616
use rustc_ast as ast;
17-
use rustc_ast::{token, walk_list};
17+
use rustc_ast::walk_list;
1818
use rustc_data_structures::fx::FxHashSet;
1919
use rustc_hir as hir;
2020
use rustc_hir::def::{DefKind as HirDefKind, Res};
@@ -1207,9 +1207,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
12071207

12081208
// Otherwise it's a span with wrong macro expansion info, which
12091209
// we don't want to track anyway, since it's probably macro-internal `use`
1210-
if let Some(sub_span) =
1211-
self.span.sub_span_of_token(item.span, token::BinOp(token::Star))
1212-
{
1210+
if let Some(sub_span) = self.span.sub_span_of_star(item.span) {
12131211
if !self.span.filter_generated(item.span) {
12141212
let access = access_from!(self.save_ctxt, item, item.hir_id);
12151213
let span = self.span_from_span(sub_span);

src/librustc_save_analysis/span_utils.rs

+30-54
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::generated_code;
2-
use rustc_ast::token::{self, TokenKind};
3-
use rustc_parse::lexer::{self, StringReader};
2+
use rustc_data_structures::sync::Lrc;
3+
use rustc_lexer::{tokenize, TokenKind};
44
use rustc_session::Session;
55
use rustc_span::*;
66

@@ -43,61 +43,37 @@ impl<'a> SpanUtils<'a> {
4343
}
4444
}
4545

46-
pub fn retokenise_span(&self, span: Span) -> StringReader<'a> {
47-
lexer::StringReader::retokenize(&self.sess.parse_sess, span)
48-
}
49-
50-
pub fn sub_span_of_token(&self, span: Span, tok: TokenKind) -> Option<Span> {
51-
let mut toks = self.retokenise_span(span);
52-
loop {
53-
let next = toks.next_token();
54-
if next == token::Eof {
55-
return None;
56-
}
57-
if next == tok {
58-
return Some(next.span);
59-
}
46+
/// Finds the span of `*` token withing the larger `span`.
47+
pub fn sub_span_of_star(&self, mut span: Span) -> Option<Span> {
48+
let begin = self.sess.source_map().lookup_byte_offset(span.lo());
49+
let end = self.sess.source_map().lookup_byte_offset(span.hi());
50+
// Make the range zero-length if the span is invalid.
51+
if begin.sf.start_pos != end.sf.start_pos {
52+
span = span.shrink_to_lo();
6053
}
61-
}
6254

63-
// // Return the name for a macro definition (identifier after first `!`)
64-
// pub fn span_for_macro_def_name(&self, span: Span) -> Option<Span> {
65-
// let mut toks = self.retokenise_span(span);
66-
// loop {
67-
// let ts = toks.real_token();
68-
// if ts == token::Eof {
69-
// return None;
70-
// }
71-
// if ts == token::Not {
72-
// let ts = toks.real_token();
73-
// if ts.kind.is_ident() {
74-
// return Some(ts.sp);
75-
// } else {
76-
// return None;
77-
// }
78-
// }
79-
// }
80-
// }
55+
let sf = Lrc::clone(&begin.sf);
8156

82-
// // Return the name for a macro use (identifier before first `!`).
83-
// pub fn span_for_macro_use_name(&self, span:Span) -> Option<Span> {
84-
// let mut toks = self.retokenise_span(span);
85-
// let mut prev = toks.real_token();
86-
// loop {
87-
// if prev == token::Eof {
88-
// return None;
89-
// }
90-
// let ts = toks.real_token();
91-
// if ts == token::Not {
92-
// if prev.kind.is_ident() {
93-
// return Some(prev.sp);
94-
// } else {
95-
// return None;
96-
// }
97-
// }
98-
// prev = ts;
99-
// }
100-
// }
57+
self.sess.source_map().ensure_source_file_source_present(Lrc::clone(&sf));
58+
let src =
59+
sf.src.clone().or_else(|| sf.external_src.borrow().get_source().map(Lrc::clone))?;
60+
let to_index = |pos: BytePos| -> usize { (pos - sf.start_pos).0 as usize };
61+
let text = &src[to_index(span.lo())..to_index(span.hi())];
62+
let start_pos = {
63+
let mut pos = 0;
64+
tokenize(text)
65+
.map(|token| {
66+
let start = pos;
67+
pos += token.len;
68+
(start, token)
69+
})
70+
.find(|(_pos, token)| token.kind == TokenKind::Star)?
71+
.0
72+
};
73+
let lo = span.lo() + BytePos(start_pos as u32);
74+
let hi = lo + BytePos(1);
75+
Some(span.with_lo(lo).with_hi(hi))
76+
}
10177

10278
/// Return true if the span is generated code, and
10379
/// it is not a subspan of the root callsite.

0 commit comments

Comments
 (0)