Skip to content

Commit 0bfbaa6

Browse files
committed
Auto merge of #61253 - nnethercote:avoid-hygiene_data-lookups, r=petrochenkov
Avoid `hygiene_data` lookups These commits mostly introduce compound operations that allow two close adjacent `hygiene_data` lookups to be combined. r? @petrochenkov
2 parents aee7012 + 95ea7fd commit 0bfbaa6

File tree

21 files changed

+100
-62
lines changed

21 files changed

+100
-62
lines changed

src/librustc/lint/internal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
113113
.help("try using `Ty` instead")
114114
.emit();
115115
} else {
116-
if ty.span.ctxt().outer().expn_info().is_some() {
116+
if ty.span.ctxt().outer_expn_info().is_some() {
117117
return;
118118
}
119119
if let Some(t) = is_ty_or_ty_ctxt(cx, ty) {

src/librustc/lint/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ pub fn provide(providers: &mut Providers<'_>) {
880880
/// This is used to test whether a lint should not even begin to figure out whether it should
881881
/// be reported on the current node.
882882
pub fn in_external_macro(sess: &Session, span: Span) -> bool {
883-
let info = match span.ctxt().outer().expn_info() {
883+
let info = match span.ctxt().outer_expn_info() {
884884
Some(info) => info,
885885
// no ExpnInfo means this span doesn't come from a macro
886886
None => return false,
@@ -908,7 +908,7 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
908908

909909
/// Returns whether `span` originates in a derive macro's expansion
910910
pub fn in_derive_expansion(span: Span) -> bool {
911-
let info = match span.ctxt().outer().expn_info() {
911+
let info = match span.ctxt().outer_expn_info() {
912912
Some(info) => info,
913913
// no ExpnInfo means this span doesn't come from a macro
914914
None => return false,

src/librustc/traits/error_reporting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
6565
format: ExpnFormat::CompilerDesugaring(_),
6666
def_site: Some(def_span),
6767
..
68-
}) = span.ctxt().outer().expn_info() {
68+
}) = span.ctxt().outer_expn_info() {
6969
span = def_span;
7070
}
7171

src/librustc/ty/mod.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2886,7 +2886,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
28862886

28872887
pub fn find_field_index(self, ident: Ident, variant: &VariantDef) -> Option<usize> {
28882888
variant.fields.iter().position(|field| {
2889-
self.adjust_ident(ident, variant.def_id, hir::DUMMY_HIR_ID).0 == field.ident.modern()
2889+
self.hygienic_eq(ident, field.ident, variant.def_id)
28902890
})
28912891
}
28922892

@@ -3085,19 +3085,32 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
30853085
/// its supposed definition name (`def_name`). The method also needs `DefId` of the supposed
30863086
/// definition's parent/scope to perform comparison.
30873087
pub fn hygienic_eq(self, use_name: Ident, def_name: Ident, def_parent_def_id: DefId) -> bool {
3088-
self.adjust_ident(use_name, def_parent_def_id, hir::DUMMY_HIR_ID).0 == def_name.modern()
3088+
// We could use `Ident::eq` here, but we deliberately don't. The name
3089+
// comparison fails frequently, and we want to avoid the expensive
3090+
// `modern()` calls required for the span comparison whenever possible.
3091+
use_name.name == def_name.name &&
3092+
self.adjust_ident(use_name, def_parent_def_id).span.ctxt() == def_name.modern().span.ctxt()
30893093
}
30903094

3091-
pub fn adjust_ident(self, mut ident: Ident, scope: DefId, block: hir::HirId) -> (Ident, DefId) {
3092-
ident = ident.modern();
3093-
let target_expansion = match scope.krate {
3095+
fn expansion_that_defined(self, scope: DefId) -> Mark {
3096+
match scope.krate {
30943097
LOCAL_CRATE => self.hir().definitions().expansion_that_defined(scope.index),
30953098
_ => Mark::root(),
3096-
};
3097-
let scope = match ident.span.adjust(target_expansion) {
3099+
}
3100+
}
3101+
3102+
pub fn adjust_ident(self, mut ident: Ident, scope: DefId) -> Ident {
3103+
ident = ident.modern();
3104+
ident.span.adjust(self.expansion_that_defined(scope));
3105+
ident
3106+
}
3107+
3108+
pub fn adjust_ident_and_get_scope(self, mut ident: Ident, scope: DefId, block: hir::HirId)
3109+
-> (Ident, DefId) {
3110+
ident = ident.modern();
3111+
let scope = match ident.span.adjust(self.expansion_that_defined(scope)) {
30983112
Some(actual_expansion) =>
30993113
self.hir().definitions().parent_module_of_macro_def(actual_expansion),
3100-
None if block == hir::DUMMY_HIR_ID => DefId::local(CRATE_DEF_INDEX), // Dummy DefId
31013114
None => self.hir().get_module_parent_by_hir_id(block),
31023115
};
31033116
(ident, scope)

src/librustc_codegen_ssa/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
131131
// at the level above that.
132132
let mut span = source_info.span;
133133
while span.ctxt() != NO_EXPANSION && span.ctxt() != self.mir.span.ctxt() {
134-
if let Some(info) = span.ctxt().outer().expn_info() {
134+
if let Some(info) = span.ctxt().outer_expn_info() {
135135
span = info.call_site;
136136
} else {
137137
break;

src/librustc_lint/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns {
158158
if fieldpat.node.is_shorthand {
159159
continue;
160160
}
161-
if fieldpat.span.ctxt().outer().expn_info().is_some() {
161+
if fieldpat.span.ctxt().outer_expn_info().is_some() {
162162
// Don't lint if this is a macro expansion: macro authors
163163
// shouldn't have to worry about this kind of style issue
164164
// (Issue #49588)
@@ -1003,7 +1003,7 @@ impl UnreachablePub {
10031003
let mut applicability = Applicability::MachineApplicable;
10041004
match vis.node {
10051005
hir::VisibilityKind::Public if !cx.access_levels.is_reachable(id) => {
1006-
if span.ctxt().outer().expn_info().is_some() {
1006+
if span.ctxt().outer_expn_info().is_some() {
10071007
applicability = Applicability::MaybeIncorrect;
10081008
}
10091009
let def_span = cx.tcx.sess.source_map().def_span(span);

src/librustc_lint/unused.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,8 @@ impl EarlyLintPass for UnusedParens {
391391
// trigger in situations that macro authors shouldn't have to care about, e.g.,
392392
// when a parenthesized token tree matched in one macro expansion is matched as
393393
// an expression in another and used as a fn/method argument (Issue #47775)
394-
if e.span.ctxt().outer().expn_info()
395-
.map_or(false, |info| info.call_site.ctxt().outer()
396-
.expn_info().is_some()) {
394+
if e.span.ctxt().outer_expn_info()
395+
.map_or(false, |info| info.call_site.ctxt().outer_expn_info().is_some()) {
397396
return;
398397
}
399398
let msg = format!("{} argument", call_kind);

src/librustc_privacy/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
845845
field: &'tcx ty::FieldDef) { // definition of the field
846846
let ident = Ident::new(kw::Invalid, use_ctxt);
847847
let current_hir = self.current_item;
848-
let def_id = self.tcx.adjust_ident(ident, def.did, current_hir).1;
848+
let def_id = self.tcx.adjust_ident_and_get_scope(ident, def.did, current_hir).1;
849849
if !def.is_enum() && !field.vis.is_accessible_from(def_id, self.tcx) {
850850
struct_span_err!(self.tcx.sess, span, E0451, "field `{}` of {} `{}` is private",
851851
field.ident, def.variant_descr(), self.tcx.def_path_str(def.did))

src/librustc_resolve/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
762762
ItemKind::Use(..) => {
763763
// don't suggest placing a use before the prelude
764764
// import or other generated ones
765-
if item.span.ctxt().outer().expn_info().is_none() {
765+
if item.span.ctxt().outer_expn_info().is_none() {
766766
self.span = Some(item.span.shrink_to_lo());
767767
self.found_use = true;
768768
return;
@@ -772,7 +772,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
772772
ItemKind::ExternCrate(_) => {}
773773
// but place them before the first other item
774774
_ => if self.span.map_or(true, |span| item.span < span ) {
775-
if item.span.ctxt().outer().expn_info().is_none() {
775+
if item.span.ctxt().outer_expn_info().is_none() {
776776
// don't insert between attributes and an item
777777
if item.attrs.is_empty() {
778778
self.span = Some(item.span.shrink_to_lo());
@@ -2308,7 +2308,7 @@ impl<'a> Resolver<'a> {
23082308

23092309
fn hygienic_lexical_parent(&mut self, module: Module<'a>, span: &mut Span)
23102310
-> Option<Module<'a>> {
2311-
if !module.expansion.is_descendant_of(span.ctxt().outer()) {
2311+
if !module.expansion.outer_is_descendant_of(span.ctxt()) {
23122312
return Some(self.macro_def_scope(span.remove_mark()));
23132313
}
23142314

@@ -2344,7 +2344,7 @@ impl<'a> Resolver<'a> {
23442344
module.expansion.is_descendant_of(parent.expansion) {
23452345
// The macro is a proc macro derive
23462346
if module.expansion.looks_like_proc_macro_derive() {
2347-
if parent.expansion.is_descendant_of(span.ctxt().outer()) {
2347+
if parent.expansion.outer_is_descendant_of(span.ctxt()) {
23482348
*poisoned = Some(node_id);
23492349
return module.parent;
23502350
}

src/librustc_resolve/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl<'a> Resolver<'a> {
413413

414414
// Possibly apply the macro helper hack
415415
if kind == MacroKind::Bang && path.len() == 1 &&
416-
path[0].ident.span.ctxt().outer().expn_info()
416+
path[0].ident.span.ctxt().outer_expn_info()
417417
.map_or(false, |info| info.local_inner_macros) {
418418
let root = Ident::new(kw::DollarCrate, path[0].ident.span);
419419
path.insert(0, Segment::from_ident(root));

0 commit comments

Comments
 (0)