Skip to content

Commit 0361bd0

Browse files
committed
resolve: In visit_scopes do not extract ctxt out of span unless necessary
1 parent 2fd6efc commit 0361bd0

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ use rustc_span::edit_distance::find_best_match_for_name;
3232
use rustc_span::edition::Edition;
3333
use rustc_span::hygiene::MacroKind;
3434
use rustc_span::source_map::{SourceMap, Spanned};
35-
use rustc_span::{BytePos, Ident, Macros20NormalizedIdent, Span, Symbol, SyntaxContext, kw, sym};
35+
use rustc_span::{
36+
BytePos, DUMMY_SP, Ident, Macros20NormalizedIdent, Span, Symbol, SyntaxContext, kw, sym,
37+
};
3638
use thin_vec::{ThinVec, thin_vec};
3739
use tracing::{debug, instrument};
3840

@@ -1179,6 +1181,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11791181
ctxt: SyntaxContext,
11801182
filter_fn: &impl Fn(Res) -> bool,
11811183
) {
1184+
let ctxt = DUMMY_SP.with_ctxt(ctxt);
11821185
self.cm().visit_scopes(scope_set, ps, ctxt, None, |this, scope, use_prelude, _| {
11831186
match scope {
11841187
Scope::DeriveHelpers(expn_id) => {

compiler/rustc_resolve/src/ident.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
5353
mut self: CmResolver<'r, 'ra, 'tcx>,
5454
scope_set: ScopeSet<'ra>,
5555
parent_scope: &ParentScope<'ra>,
56-
orig_ctxt: SyntaxContext,
56+
// Location of the span is not significant, but pass a `Span` instead of `SyntaxContext`
57+
// to avoid extracting and re-packaging the syntax context unnecessarily.
58+
orig_ctxt: Span,
5759
derive_fallback_lint_id: Option<NodeId>,
5860
mut visitor: impl FnMut(
5961
&mut CmResolver<'r, 'ra, 'tcx>,
6062
Scope<'ra>,
6163
UsePrelude,
62-
SyntaxContext,
64+
Span,
6365
) -> ControlFlow<T>,
6466
) -> Option<T> {
6567
// General principles:
@@ -238,11 +240,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
238240
fn hygienic_lexical_parent(
239241
&self,
240242
module: Module<'ra>,
241-
ctxt: &mut SyntaxContext,
243+
span: &mut Span,
242244
derive_fallback_lint_id: Option<NodeId>,
243245
) -> Option<(Module<'ra>, Option<NodeId>)> {
244-
if !module.expansion.outer_expn_is_descendant_of(*ctxt) {
245-
return Some((self.expn_def_scope(ctxt.remove_mark()), None));
246+
let ctxt = span.ctxt();
247+
if !module.expansion.outer_expn_is_descendant_of(ctxt) {
248+
return Some((self.expn_def_scope(span.remove_mark()), None));
246249
}
247250

248251
if let ModuleKind::Block = module.kind {
@@ -272,7 +275,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
272275
let ext = &self.get_macro_by_def_id(def_id).ext;
273276
if ext.builtin_name.is_none()
274277
&& ext.macro_kinds() == MacroKinds::DERIVE
275-
&& parent.expansion.outer_expn_is_descendant_of(*ctxt)
278+
&& parent.expansion.outer_expn_is_descendant_of(ctxt)
276279
{
277280
return Some((parent, derive_fallback_lint_id));
278281
}
@@ -433,10 +436,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
433436
let break_result = self.visit_scopes(
434437
scope_set,
435438
parent_scope,
436-
orig_ident.span.ctxt(),
439+
orig_ident.span,
437440
derive_fallback_lint_id,
438441
|this, scope, use_prelude, ctxt| {
439-
let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
442+
let ident = Ident::new(orig_ident.name, ctxt);
440443
// The passed `ctxt` is already normalized, so avoid expensive double normalization.
441444
let ident = Macros20NormalizedIdent(ident);
442445
let res = match this.reborrow().resolve_ident_in_scope(

compiler/rustc_resolve/src/late.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ use rustc_session::config::{CrateType, ResolveDocLinks};
3737
use rustc_session::lint;
3838
use rustc_session::parse::feature_err;
3939
use rustc_span::source_map::{Spanned, respan};
40-
use rustc_span::{
41-
BytePos, DUMMY_SP, Ident, Macros20NormalizedIdent, Span, Symbol, SyntaxContext, kw, sym,
42-
};
40+
use rustc_span::{BytePos, DUMMY_SP, Ident, Macros20NormalizedIdent, Span, Symbol, kw, sym};
4341
use smallvec::{SmallVec, smallvec};
4442
use thin_vec::ThinVec;
4543
use tracing::{debug, instrument, trace};
@@ -5224,7 +5222,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
52245222
self.r.traits_in_scope(
52255223
self.current_trait_ref.as_ref().map(|(module, _)| *module),
52265224
&self.parent_scope,
5227-
ident.span.ctxt(),
5225+
ident.span,
52285226
Some((ident.name, ns)),
52295227
)
52305228
}
@@ -5323,7 +5321,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
53235321
.entry(self.parent_scope.module.nearest_parent_mod().expect_local())
53245322
.or_insert_with(|| {
53255323
self.r
5326-
.traits_in_scope(None, &self.parent_scope, SyntaxContext::root(), None)
5324+
.traits_in_scope(None, &self.parent_scope, DUMMY_SP, None)
53275325
.into_iter()
53285326
.filter_map(|tr| {
53295327
if self.is_invalid_proc_macro_item_for_doc(tr.def_id) {

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1918,7 +1918,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19181918
&mut self,
19191919
current_trait: Option<Module<'ra>>,
19201920
parent_scope: &ParentScope<'ra>,
1921-
ctxt: SyntaxContext,
1921+
ctxt: Span,
19221922
assoc_item: Option<(Symbol, Namespace)>,
19231923
) -> Vec<TraitCandidate> {
19241924
let mut found_traits = Vec::new();

0 commit comments

Comments
 (0)