Skip to content

Commit

Permalink
Add modernize_and_adjust methods.
Browse files Browse the repository at this point in the history
These combine two `HygieneData::with` calls into one.
  • Loading branch information
nnethercote committed Jun 4, 2019
1 parent ab9bbf4 commit 4c9ecbf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3101,15 +3101,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

pub fn adjust_ident(self, mut ident: Ident, scope: DefId) -> Ident {
ident = ident.modern();
ident.span.adjust(self.expansion_that_defined(scope));
ident.span.modernize_and_adjust(self.expansion_that_defined(scope));
ident
}

pub fn adjust_ident_and_get_scope(self, mut ident: Ident, scope: DefId, block: hir::HirId)
-> (Ident, DefId) {
ident = ident.modern();
let scope = match ident.span.adjust(self.expansion_that_defined(scope)) {
let scope = match ident.span.modernize_and_adjust(self.expansion_that_defined(scope)) {
Some(actual_expansion) =>
self.hir().definitions().parent_module_of_macro_def(actual_expansion),
None => self.hir().get_module_parent_by_hir_id(block),
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2330,14 +2330,12 @@ impl<'a> Resolver<'a> {
let orig_current_module = self.current_module;
match module {
ModuleOrUniformRoot::Module(module) => {
ident.span = ident.span.modern();
if let Some(def) = ident.span.adjust(module.expansion) {
if let Some(def) = ident.span.modernize_and_adjust(module.expansion) {
self.current_module = self.macro_def_scope(def);
}
}
ModuleOrUniformRoot::ExternPrelude => {
ident.span = ident.span.modern();
ident.span.adjust(Mark::root());
ident.span.modernize_and_adjust(Mark::root());
}
ModuleOrUniformRoot::CrateRootAndExternPrelude |
ModuleOrUniformRoot::CurrentScope => {
Expand Down
8 changes: 8 additions & 0 deletions src/libsyntax_pos/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,14 @@ impl SyntaxContext {
HygieneData::with(|data| data.adjust(self, expansion))
}

/// Like `SyntaxContext::adjust`, but also modernizes `self`.
pub fn modernize_and_adjust(&mut self, expansion: Mark) -> Option<Mark> {
HygieneData::with(|data| {
*self = data.modern(*self);
data.adjust(self, expansion)
})
}

/// Adjust this context for resolution in a scope created by the given expansion
/// via a glob import with the given `SyntaxContext`.
/// For example:
Expand Down
8 changes: 8 additions & 0 deletions src/libsyntax_pos/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,14 @@ impl Span {
mark
}

#[inline]
pub fn modernize_and_adjust(&mut self, expansion: Mark) -> Option<Mark> {
let mut span = self.data();
let mark = span.ctxt.modernize_and_adjust(expansion);
*self = Span::new(span.lo, span.hi, span.ctxt);
mark
}

#[inline]
pub fn glob_adjust(&mut self, expansion: Mark, glob_span: Span) -> Option<Option<Mark>> {
let mut span = self.data();
Expand Down

0 comments on commit 4c9ecbf

Please sign in to comment.