Skip to content

Commit 2a1d6c8

Browse files
committed
Auto merge of #61484 - nnethercote:avoid-more-hygiene-lookups, r=petrochenkov
Avoid more hygiene lookups Mostly by combining multiple `HygieneData::with` calls into a single call on hot paths. r? @petrochenkov
2 parents 817d2fe + 4c9ecbf commit 2a1d6c8

File tree

8 files changed

+282
-184
lines changed

8 files changed

+282
-184
lines changed

src/librustc/ty/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -3089,7 +3089,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
30893089
// comparison fails frequently, and we want to avoid the expensive
30903090
// `modern()` calls required for the span comparison whenever possible.
30913091
use_name.name == def_name.name &&
3092-
self.adjust_ident(use_name, def_parent_def_id).span.ctxt() == def_name.modern().span.ctxt()
3092+
use_name.span.ctxt().hygienic_eq(def_name.span.ctxt(),
3093+
self.expansion_that_defined(def_parent_def_id))
30933094
}
30943095

30953096
fn expansion_that_defined(self, scope: DefId) -> Mark {
@@ -3100,15 +3101,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
31003101
}
31013102

31023103
pub fn adjust_ident(self, mut ident: Ident, scope: DefId) -> Ident {
3103-
ident = ident.modern();
3104-
ident.span.adjust(self.expansion_that_defined(scope));
3104+
ident.span.modernize_and_adjust(self.expansion_that_defined(scope));
31053105
ident
31063106
}
31073107

31083108
pub fn adjust_ident_and_get_scope(self, mut ident: Ident, scope: DefId, block: hir::HirId)
31093109
-> (Ident, DefId) {
3110-
ident = ident.modern();
3111-
let scope = match ident.span.adjust(self.expansion_that_defined(scope)) {
3110+
let scope = match ident.span.modernize_and_adjust(self.expansion_that_defined(scope)) {
31123111
Some(actual_expansion) =>
31133112
self.hir().definitions().parent_module_of_macro_def(actual_expansion),
31143113
None => self.hir().get_module_parent_by_hir_id(block),

src/librustc/ty/query/on_disk_cache.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -844,9 +844,8 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<Span> for CacheEncoder<'enc, 'a, 'tcx
844844
if span_data.ctxt == SyntaxContext::empty() {
845845
TAG_NO_EXPANSION_INFO.encode(self)
846846
} else {
847-
let mark = span_data.ctxt.outer();
848-
849-
if let Some(expn_info) = mark.expn_info() {
847+
let (mark, expn_info) = span_data.ctxt.outer_and_expn_info();
848+
if let Some(expn_info) = expn_info {
850849
if let Some(pos) = self.expn_info_shorthands.get(&mark).cloned() {
851850
TAG_EXPANSION_INFO_SHORTHAND.encode(self)?;
852851
pos.encode(self)

src/librustc_codegen_ssa/mir/mod.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
128128
// Walk up the macro expansion chain until we reach a non-expanded span.
129129
// We also stop at the function body level because no line stepping can occur
130130
// at the level above that.
131-
let mut span = source_info.span;
132-
while span.ctxt() != NO_EXPANSION && span.ctxt() != self.mir.span.ctxt() {
133-
if let Some(info) = span.ctxt().outer_expn_info() {
134-
span = info.call_site;
135-
} else {
136-
break;
137-
}
138-
}
131+
let span = syntax_pos::hygiene::walk_chain(source_info.span, self.mir.span.ctxt());
139132
let scope = self.scope_metadata_for_loc(source_info.scope, span.lo());
140133
// Use span of the outermost expansion site, while keeping the original lexical scope.
141134
(scope, span)

src/librustc_resolve/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2330,14 +2330,12 @@ impl<'a> Resolver<'a> {
23302330
let orig_current_module = self.current_module;
23312331
match module {
23322332
ModuleOrUniformRoot::Module(module) => {
2333-
ident.span = ident.span.modern();
2334-
if let Some(def) = ident.span.adjust(module.expansion) {
2333+
if let Some(def) = ident.span.modernize_and_adjust(module.expansion) {
23352334
self.current_module = self.macro_def_scope(def);
23362335
}
23372336
}
23382337
ModuleOrUniformRoot::ExternPrelude => {
2339-
ident.span = ident.span.modern();
2340-
ident.span.adjust(Mark::root());
2338+
ident.span.modernize_and_adjust(Mark::root());
23412339
}
23422340
ModuleOrUniformRoot::CrateRootAndExternPrelude |
23432341
ModuleOrUniformRoot::CurrentScope => {
@@ -4525,7 +4523,7 @@ impl<'a> Resolver<'a> {
45254523
let mut ident = ident;
45264524
if ident.span.glob_adjust(
45274525
module.expansion,
4528-
binding.span.ctxt().modern(),
4526+
binding.span,
45294527
).is_none() {
45304528
continue
45314529
}

src/librustc_resolve/resolve_imports.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl<'a> Resolver<'a> {
388388
None => return Err((Undetermined, Weak::Yes)),
389389
};
390390
let (orig_current_module, mut ident) = (self.current_module, ident.modern());
391-
match ident.span.glob_adjust(module.expansion, glob_import.span.ctxt().modern()) {
391+
match ident.span.glob_adjust(module.expansion, glob_import.span) {
392392
Some(Some(def)) => self.current_module = self.macro_def_scope(def),
393393
Some(None) => {}
394394
None => continue,
@@ -605,8 +605,7 @@ impl<'a> Resolver<'a> {
605605
// Define `binding` in `module`s glob importers.
606606
for directive in module.glob_importers.borrow_mut().iter() {
607607
let mut ident = ident.modern();
608-
let scope = match ident.span.reverse_glob_adjust(module.expansion,
609-
directive.span.ctxt().modern()) {
608+
let scope = match ident.span.reverse_glob_adjust(module.expansion, directive.span) {
610609
Some(Some(def)) => self.macro_def_scope(def),
611610
Some(None) => directive.parent_scope.module,
612611
None => continue,
@@ -1359,8 +1358,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
13591358
resolution.borrow().binding().map(|binding| (ident, binding))
13601359
}).collect::<Vec<_>>();
13611360
for ((mut ident, ns), binding) in bindings {
1362-
let scope = match ident.span.reverse_glob_adjust(module.expansion,
1363-
directive.span.ctxt().modern()) {
1361+
let scope = match ident.span.reverse_glob_adjust(module.expansion, directive.span) {
13641362
Some(Some(def)) => self.macro_def_scope(def),
13651363
Some(None) => self.current_module,
13661364
None => continue,

src/libsyntax/parse/parser.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -2083,13 +2083,6 @@ impl<'a> Parser<'a> {
20832083
hi = path.span;
20842084
return Ok(self.mk_expr(lo.to(hi), ExprKind::Path(Some(qself), path), attrs));
20852085
}
2086-
if self.span.rust_2018() && self.check_keyword(kw::Async) {
2087-
return if self.is_async_block() { // check for `async {` and `async move {`
2088-
self.parse_async_block(attrs)
2089-
} else {
2090-
self.parse_lambda_expr(attrs)
2091-
};
2092-
}
20932086
if self.check_keyword(kw::Move) || self.check_keyword(kw::Static) {
20942087
return self.parse_lambda_expr(attrs);
20952088
}
@@ -2161,6 +2154,16 @@ impl<'a> Parser<'a> {
21612154
assert!(self.eat_keyword(kw::Try));
21622155
return self.parse_try_block(lo, attrs);
21632156
}
2157+
2158+
// Span::rust_2018() is somewhat expensive; don't get it repeatedly.
2159+
let is_span_rust_2018 = self.span.rust_2018();
2160+
if is_span_rust_2018 && self.check_keyword(kw::Async) {
2161+
return if self.is_async_block() { // check for `async {` and `async move {`
2162+
self.parse_async_block(attrs)
2163+
} else {
2164+
self.parse_lambda_expr(attrs)
2165+
};
2166+
}
21642167
if self.eat_keyword(kw::Return) {
21652168
if self.token.can_begin_expr() {
21662169
let e = self.parse_expr()?;
@@ -2196,7 +2199,7 @@ impl<'a> Parser<'a> {
21962199
db.span_label(self.span, "expected expression");
21972200
db.note("variable declaration using `let` is a statement");
21982201
return Err(db);
2199-
} else if self.span.rust_2018() && self.eat_keyword(kw::Await) {
2202+
} else if is_span_rust_2018 && self.eat_keyword(kw::Await) {
22002203
let (await_hi, e_kind) = self.parse_await_macro_or_alt(lo, self.prev_span)?;
22012204
hi = await_hi;
22022205
ex = e_kind;

0 commit comments

Comments
 (0)