Skip to content

Commit 3ff866e

Browse files
committed
resolve: Scope visiting doesn't need an Ident
1 parent bf5f306 commit 3ff866e

File tree

6 files changed

+90
-57
lines changed

6 files changed

+90
-57
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,8 @@ impl<'a> Resolver<'a> {
595595
filter_fn: &impl Fn(Res) -> bool,
596596
) -> Option<TypoSuggestion> {
597597
let mut suggestions = Vec::new();
598-
self.visit_scopes(scope_set, parent_scope, ident, |this, scope, use_prelude, _| {
598+
let ctxt = ident.span.ctxt();
599+
self.visit_scopes(scope_set, parent_scope, ctxt, |this, scope, use_prelude, _| {
599600
match scope {
600601
Scope::DeriveHelpers(expn_id) => {
601602
let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);

compiler/rustc_resolve/src/late.rs

+47-35
Original file line numberDiff line numberDiff line change
@@ -262,52 +262,60 @@ impl<'a> PathSource<'a> {
262262

263263
crate fn is_expected(self, res: Res) -> bool {
264264
match self {
265-
PathSource::Type => matches!(res, Res::Def(
265+
PathSource::Type => matches!(
266+
res,
267+
Res::Def(
266268
DefKind::Struct
267-
| DefKind::Union
268-
| DefKind::Enum
269-
| DefKind::Trait
270-
| DefKind::TraitAlias
271-
| DefKind::TyAlias
272-
| DefKind::AssocTy
273-
| DefKind::TyParam
274-
| DefKind::OpaqueTy
275-
| DefKind::ForeignTy,
269+
| DefKind::Union
270+
| DefKind::Enum
271+
| DefKind::Trait
272+
| DefKind::TraitAlias
273+
| DefKind::TyAlias
274+
| DefKind::AssocTy
275+
| DefKind::TyParam
276+
| DefKind::OpaqueTy
277+
| DefKind::ForeignTy,
276278
_,
277-
)
278-
| Res::PrimTy(..)
279-
| Res::SelfTy(..)),
279+
) | Res::PrimTy(..)
280+
| Res::SelfTy(..)
281+
),
280282
PathSource::Trait(AliasPossibility::No) => matches!(res, Res::Def(DefKind::Trait, _)),
281283
PathSource::Trait(AliasPossibility::Maybe) => {
282284
matches!(res, Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
283285
}
284-
PathSource::Expr(..) => matches!(res, Res::Def(
286+
PathSource::Expr(..) => matches!(
287+
res,
288+
Res::Def(
285289
DefKind::Ctor(_, CtorKind::Const | CtorKind::Fn)
286-
| DefKind::Const
287-
| DefKind::Static
288-
| DefKind::Fn
289-
| DefKind::AssocFn
290-
| DefKind::AssocConst
291-
| DefKind::ConstParam,
290+
| DefKind::Const
291+
| DefKind::Static
292+
| DefKind::Fn
293+
| DefKind::AssocFn
294+
| DefKind::AssocConst
295+
| DefKind::ConstParam,
292296
_,
293-
)
294-
| Res::Local(..)
295-
| Res::SelfCtor(..)),
296-
PathSource::Pat => matches!(res, Res::Def(
297+
) | Res::Local(..)
298+
| Res::SelfCtor(..)
299+
),
300+
PathSource::Pat => matches!(
301+
res,
302+
Res::Def(
297303
DefKind::Ctor(_, CtorKind::Const) | DefKind::Const | DefKind::AssocConst,
298304
_,
299-
)
300-
| Res::SelfCtor(..)),
305+
) | Res::SelfCtor(..)
306+
),
301307
PathSource::TupleStruct(..) => res.expected_in_tuple_struct_pat(),
302-
PathSource::Struct => matches!(res, Res::Def(
308+
PathSource::Struct => matches!(
309+
res,
310+
Res::Def(
303311
DefKind::Struct
304-
| DefKind::Union
305-
| DefKind::Variant
306-
| DefKind::TyAlias
307-
| DefKind::AssocTy,
312+
| DefKind::Union
313+
| DefKind::Variant
314+
| DefKind::TyAlias
315+
| DefKind::AssocTy,
308316
_,
309-
)
310-
| Res::SelfTy(..)),
317+
) | Res::SelfTy(..)
318+
),
311319
PathSource::TraitItem(ns) => match res {
312320
Res::Def(DefKind::AssocConst | DefKind::AssocFn, _) if ns == ValueNS => true,
313321
Res::Def(DefKind::AssocTy, _) if ns == TypeNS => true,
@@ -2397,8 +2405,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
23972405
&mut found_traits,
23982406
&self.parent_scope,
23992407
);
2400-
search_module =
2401-
unwrap_or!(self.r.hygienic_lexical_parent(search_module, &mut ident.span), break);
2408+
let mut span_data = ident.span.data();
2409+
search_module = unwrap_or!(
2410+
self.r.hygienic_lexical_parent(search_module, &mut span_data.ctxt),
2411+
break
2412+
);
2413+
ident.span = span_data.span();
24022414
}
24032415

24042416
if let Some(prelude) = self.r.prelude {

compiler/rustc_resolve/src/lib.rs

+29-18
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use rustc_middle::{bug, span_bug};
5050
use rustc_session::lint;
5151
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
5252
use rustc_session::Session;
53+
use rustc_span::edition::Edition;
5354
use rustc_span::hygiene::{ExpnId, ExpnKind, MacroKind, SyntaxContext, Transparency};
5455
use rustc_span::source_map::Spanned;
5556
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -759,10 +760,13 @@ impl<'a> NameBinding<'a> {
759760
}
760761

761762
fn is_variant(&self) -> bool {
762-
matches!(self.kind, NameBindingKind::Res(
763+
matches!(
764+
self.kind,
765+
NameBindingKind::Res(
763766
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Variant, ..), _),
764767
_,
765-
))
768+
)
769+
)
766770
}
767771

768772
fn is_extern_crate(&self) -> bool {
@@ -1626,8 +1630,13 @@ impl<'a> Resolver<'a> {
16261630
&mut self,
16271631
scope_set: ScopeSet,
16281632
parent_scope: &ParentScope<'a>,
1629-
ident: Ident,
1630-
mut visitor: impl FnMut(&mut Self, Scope<'a>, /*use_prelude*/ bool, Ident) -> Option<T>,
1633+
ctxt: SyntaxContext,
1634+
mut visitor: impl FnMut(
1635+
&mut Self,
1636+
Scope<'a>,
1637+
/*use_prelude*/ bool,
1638+
SyntaxContext,
1639+
) -> Option<T>,
16311640
) -> Option<T> {
16321641
// General principles:
16331642
// 1. Not controlled (user-defined) names should have higher priority than controlled names
@@ -1670,7 +1679,7 @@ impl<'a> Resolver<'a> {
16701679
// 4c. Standard library prelude (de-facto closed, controlled).
16711680
// 6. Language prelude: builtin attributes (closed, controlled).
16721681

1673-
let rust_2015 = ident.span.rust_2015();
1682+
let rust_2015 = ctxt.edition() == Edition::Edition2015;
16741683
let (ns, macro_kind, is_absolute_path) = match scope_set {
16751684
ScopeSet::All(ns, _) => (ns, None, false),
16761685
ScopeSet::AbsolutePath(ns) => (ns, None, true),
@@ -1683,7 +1692,7 @@ impl<'a> Resolver<'a> {
16831692
TypeNS | ValueNS => Scope::Module(module),
16841693
MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
16851694
};
1686-
let mut ident = ident.normalize_to_macros_2_0();
1695+
let mut ctxt = ctxt.normalize_to_macros_2_0();
16871696
let mut use_prelude = !module.no_implicit_prelude;
16881697

16891698
loop {
@@ -1719,7 +1728,7 @@ impl<'a> Resolver<'a> {
17191728
};
17201729

17211730
if visit {
1722-
if let break_result @ Some(..) = visitor(self, scope, use_prelude, ident) {
1731+
if let break_result @ Some(..) = visitor(self, scope, use_prelude, ctxt) {
17231732
return break_result;
17241733
}
17251734
}
@@ -1749,17 +1758,17 @@ impl<'a> Resolver<'a> {
17491758
},
17501759
Scope::CrateRoot => match ns {
17511760
TypeNS => {
1752-
ident.span.adjust(ExpnId::root());
1761+
ctxt.adjust(ExpnId::root());
17531762
Scope::ExternPrelude
17541763
}
17551764
ValueNS | MacroNS => break,
17561765
},
17571766
Scope::Module(module) => {
17581767
use_prelude = !module.no_implicit_prelude;
1759-
match self.hygienic_lexical_parent(module, &mut ident.span) {
1768+
match self.hygienic_lexical_parent(module, &mut ctxt) {
17601769
Some(parent_module) => Scope::Module(parent_module),
17611770
None => {
1762-
ident.span.adjust(ExpnId::root());
1771+
ctxt.adjust(ExpnId::root());
17631772
match ns {
17641773
TypeNS => Scope::ExternPrelude,
17651774
ValueNS => Scope::StdLibPrelude,
@@ -1882,16 +1891,18 @@ impl<'a> Resolver<'a> {
18821891
ident = normalized_ident;
18831892
let mut poisoned = None;
18841893
loop {
1894+
let mut span_data = ident.span.data();
18851895
let opt_module = if let Some(node_id) = record_used_id {
18861896
self.hygienic_lexical_parent_with_compatibility_fallback(
18871897
module,
1888-
&mut ident.span,
1898+
&mut span_data.ctxt,
18891899
node_id,
18901900
&mut poisoned,
18911901
)
18921902
} else {
1893-
self.hygienic_lexical_parent(module, &mut ident.span)
1903+
self.hygienic_lexical_parent(module, &mut span_data.ctxt)
18941904
};
1905+
ident.span = span_data.span();
18951906
module = unwrap_or!(opt_module, break);
18961907
let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
18971908
let result = self.resolve_ident_in_module_unadjusted(
@@ -1965,10 +1976,10 @@ impl<'a> Resolver<'a> {
19651976
fn hygienic_lexical_parent(
19661977
&mut self,
19671978
module: Module<'a>,
1968-
span: &mut Span,
1979+
ctxt: &mut SyntaxContext,
19691980
) -> Option<Module<'a>> {
1970-
if !module.expansion.outer_expn_is_descendant_of(span.ctxt()) {
1971-
return Some(self.macro_def_scope(span.remove_mark()));
1981+
if !module.expansion.outer_expn_is_descendant_of(*ctxt) {
1982+
return Some(self.macro_def_scope(ctxt.remove_mark()));
19721983
}
19731984

19741985
if let ModuleKind::Block(..) = module.kind {
@@ -1981,11 +1992,11 @@ impl<'a> Resolver<'a> {
19811992
fn hygienic_lexical_parent_with_compatibility_fallback(
19821993
&mut self,
19831994
module: Module<'a>,
1984-
span: &mut Span,
1995+
ctxt: &mut SyntaxContext,
19851996
node_id: NodeId,
19861997
poisoned: &mut Option<NodeId>,
19871998
) -> Option<Module<'a>> {
1988-
if let module @ Some(..) = self.hygienic_lexical_parent(module, span) {
1999+
if let module @ Some(..) = self.hygienic_lexical_parent(module, ctxt) {
19892000
return module;
19902001
}
19912002

@@ -2010,7 +2021,7 @@ impl<'a> Resolver<'a> {
20102021
let ext = self.get_macro_by_def_id(def_id);
20112022
if !ext.is_builtin
20122023
&& ext.macro_kind() == MacroKind::Derive
2013-
&& parent.expansion.outer_expn_is_descendant_of(span.ctxt())
2024+
&& parent.expansion.outer_expn_is_descendant_of(*ctxt)
20142025
{
20152026
*poisoned = Some(node_id);
20162027
return module.parent;

compiler/rustc_resolve/src/macros.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,9 @@ impl<'a> Resolver<'a> {
618618
let break_result = self.visit_scopes(
619619
scope_set,
620620
parent_scope,
621-
orig_ident,
622-
|this, scope, use_prelude, ident| {
621+
orig_ident.span.ctxt(),
622+
|this, scope, use_prelude, ctxt| {
623+
let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
623624
let ok = |res, span, arenas| {
624625
Ok((
625626
(res, ty::Visibility::Public, span, ExpnId::root()).to_name_binding(arenas),

compiler/rustc_span/src/hygiene.rs

+4
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,10 @@ impl SyntaxContext {
622622
pub fn dollar_crate_name(self) -> Symbol {
623623
HygieneData::with(|data| data.syntax_context_data[self.0 as usize].dollar_crate_name)
624624
}
625+
626+
pub fn edition(self) -> Edition {
627+
self.outer_expn_data().edition
628+
}
625629
}
626630

627631
impl fmt::Debug for SyntaxContext {

compiler/rustc_span/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ pub struct SpanData {
300300
}
301301

302302
impl SpanData {
303+
#[inline]
304+
pub fn span(&self) -> Span {
305+
Span::new(self.lo, self.hi, self.ctxt)
306+
}
303307
#[inline]
304308
pub fn with_lo(&self, lo: BytePos) -> Span {
305309
Span::new(lo, self.hi, self.ctxt)
@@ -468,7 +472,7 @@ impl Span {
468472

469473
/// Edition of the crate from which this span came.
470474
pub fn edition(self) -> edition::Edition {
471-
self.ctxt().outer_expn_data().edition
475+
self.ctxt().edition()
472476
}
473477

474478
#[inline]

0 commit comments

Comments
 (0)