Skip to content

Commit 4feb1b7

Browse files
committed
Move expansion creation methods to TyCtxt.
1 parent 2560b80 commit 4feb1b7

File tree

10 files changed

+106
-90
lines changed

10 files changed

+106
-90
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
160160
self.lower_expr_if(cond, then, else_opt.as_deref())
161161
}
162162
ExprKind::While(cond, body, opt_label) => self.with_loop_scope(e.id, |this| {
163-
let span = this.mark_span_with_reason(DesugaringKind::WhileLoop, e.span, None);
163+
let span =
164+
this.tcx.mark_span_with_reason(DesugaringKind::WhileLoop, e.span, None);
164165
this.lower_expr_while_in_loop_scope(span, cond, body, *opt_label)
165166
}),
166167
ExprKind::Loop(body, opt_label, span) => self.with_loop_scope(e.id, |this| {
@@ -448,7 +449,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
448449
_ => {
449450
let cond = self.lower_expr(cond);
450451
let reason = DesugaringKind::CondTemporary;
451-
let span_block = self.mark_span_with_reason(reason, cond.span, None);
452+
let span_block = self.tcx.mark_span_with_reason(reason, cond.span, None);
452453
self.expr_drop_temps(span_block, cond)
453454
}
454455
}
@@ -501,15 +502,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
501502
// Final expression of the block (if present) or `()` with span at the end of block
502503
let (try_span, tail_expr) = if let Some(expr) = block.expr.take() {
503504
(
504-
this.mark_span_with_reason(
505+
this.tcx.mark_span_with_reason(
505506
DesugaringKind::TryBlock,
506507
expr.span,
507508
this.allow_try_trait.clone(),
508509
),
509510
expr,
510511
)
511512
} else {
512-
let try_span = this.mark_span_with_reason(
513+
let try_span = this.tcx.mark_span_with_reason(
513514
DesugaringKind::TryBlock,
514515
this.tcx.sess.source_map().end_point(body.span),
515516
this.allow_try_trait.clone(),
@@ -519,7 +520,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
519520
};
520521

521522
let ok_wrapped_span =
522-
this.mark_span_with_reason(DesugaringKind::TryBlock, tail_expr.span, None);
523+
this.tcx.mark_span_with_reason(DesugaringKind::TryBlock, tail_expr.span, None);
523524

524525
// `::std::ops::Try::from_output($tail_expr)`
525526
block.expr = Some(this.wrap_in_try_constructor(
@@ -591,8 +592,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
591592
let output = ret_ty.unwrap_or_else(|| hir::FnRetTy::DefaultReturn(self.lower_span(span)));
592593

593594
// Resume argument type: `ResumeTy`
594-
let unstable_span =
595-
self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone());
595+
let unstable_span = self.tcx.mark_span_with_reason(
596+
DesugaringKind::Async,
597+
span,
598+
self.allow_gen_future.clone(),
599+
);
596600
let resume_ty = hir::QPath::LangItem(hir::LangItem::ResumeTy, unstable_span, None);
597601
let input_ty = hir::Ty {
598602
hir_id: self.next_id(),
@@ -661,7 +665,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
661665
&& attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))
662666
{
663667
let unstable_span =
664-
self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone());
668+
self.tcx.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone());
665669
self.lower_attrs(
666670
inner_hir_id,
667671
&[Attribute {
@@ -707,8 +711,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
707711
});
708712
}
709713
}
710-
let span = self.mark_span_with_reason(DesugaringKind::Await, await_kw_span, None);
711-
let gen_future_span = self.mark_span_with_reason(
714+
let span = self.tcx.mark_span_with_reason(DesugaringKind::Await, await_kw_span, None);
715+
let gen_future_span = self.tcx.mark_span_with_reason(
712716
DesugaringKind::Await,
713717
full_span,
714718
self.allow_gen_future.clone(),
@@ -1466,9 +1470,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
14661470
let head = self.lower_expr_mut(head);
14671471
let pat = self.lower_pat(pat);
14681472
let for_span =
1469-
self.mark_span_with_reason(DesugaringKind::ForLoop, self.lower_span(e.span), None);
1470-
let head_span = self.mark_span_with_reason(DesugaringKind::ForLoop, head.span, None);
1471-
let pat_span = self.mark_span_with_reason(DesugaringKind::ForLoop, pat.span, None);
1473+
self.tcx.mark_span_with_reason(DesugaringKind::ForLoop, self.lower_span(e.span), None);
1474+
let head_span = self.tcx.mark_span_with_reason(DesugaringKind::ForLoop, head.span, None);
1475+
let pat_span = self.tcx.mark_span_with_reason(DesugaringKind::ForLoop, pat.span, None);
14721476

14731477
// `None => break`
14741478
let none_arm = {
@@ -1562,13 +1566,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
15621566
/// }
15631567
/// ```
15641568
fn lower_expr_try(&mut self, span: Span, sub_expr: &Expr) -> hir::ExprKind<'hir> {
1565-
let unstable_span = self.mark_span_with_reason(
1569+
let unstable_span = self.tcx.mark_span_with_reason(
15661570
DesugaringKind::QuestionMark,
15671571
span,
15681572
self.allow_try_trait.clone(),
15691573
);
15701574
let try_span = self.tcx.sess.source_map().end_point(span);
1571-
let try_span = self.mark_span_with_reason(
1575+
let try_span = self.tcx.mark_span_with_reason(
15721576
DesugaringKind::QuestionMark,
15731577
try_span,
15741578
self.allow_try_trait.clone(),
@@ -1659,10 +1663,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
16591663
let (yeeted_span, yeeted_expr) = if let Some(sub_expr) = sub_expr {
16601664
(sub_expr.span, self.lower_expr(sub_expr))
16611665
} else {
1662-
(self.mark_span_with_reason(DesugaringKind::YeetExpr, span, None), self.expr_unit(span))
1666+
(
1667+
self.tcx.mark_span_with_reason(DesugaringKind::YeetExpr, span, None),
1668+
self.expr_unit(span),
1669+
)
16631670
};
16641671

1665-
let unstable_span = self.mark_span_with_reason(
1672+
let unstable_span = self.tcx.mark_span_with_reason(
16661673
DesugaringKind::YeetExpr,
16671674
span,
16681675
self.allow_try_trait.clone(),

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
10711071
}
10721072
};
10731073

1074-
let desugared_span = this.mark_span_with_reason(DesugaringKind::Async, span, None);
1074+
let desugared_span =
1075+
this.tcx.mark_span_with_reason(DesugaringKind::Async, span, None);
10751076

10761077
// Construct a parameter representing `__argN: <ty>` to replace the parameter of the
10771078
// async function.
@@ -1160,7 +1161,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11601161

11611162
// Transform into `drop-temps { <user-body> }`, an expression:
11621163
let desugared_span =
1163-
this.mark_span_with_reason(DesugaringKind::Async, user_body.span, None);
1164+
this.tcx.mark_span_with_reason(DesugaringKind::Async, user_body.span, None);
11641165
let user_body =
11651166
this.expr_drop_temps(desugared_span, this.arena.alloc(user_body));
11661167

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -762,19 +762,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
762762
self.tcx.sess.diagnostic()
763763
}
764764

765-
/// Reuses the span but adds information like the kind of the desugaring and features that are
766-
/// allowed inside this span.
767-
fn mark_span_with_reason(
768-
&self,
769-
reason: DesugaringKind,
770-
span: Span,
771-
allow_internal_unstable: Option<Lrc<[Symbol]>>,
772-
) -> Span {
773-
self.tcx.with_stable_hashing_context(|hcx| {
774-
span.mark_with_reason(allow_internal_unstable, reason, self.tcx.sess.edition(), hcx)
775-
})
776-
}
777-
778765
/// Intercept all spans entering HIR.
779766
/// Mark a span as relative to the current owning item.
780767
fn lower_span(&self, span: Span) -> Span {
@@ -1529,7 +1516,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15291516
// desugaring that explicitly states that we don't want to track that.
15301517
// Not tracking it makes lints in rustc and clippy very fragile, as
15311518
// frequently opened issues show.
1532-
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);
1519+
let opaque_ty_span = self.tcx.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);
15331520

15341521
let opaque_ty_def_id = self.create_def(
15351522
self.current_hir_id_owner.def_id,
@@ -1893,7 +1880,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18931880
) -> hir::FnRetTy<'hir> {
18941881
let span = output.span();
18951882

1896-
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::Async, span, None);
1883+
let opaque_ty_span = self.tcx.mark_span_with_reason(DesugaringKind::Async, span, None);
18971884

18981885
let fn_def_id = self.local_def_id(fn_node_id);
18991886

compiler/rustc_expand/src/expand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
486486
.map(|(path, item, _exts, is_const)| {
487487
// FIXME: Consider using the derive resolutions (`_exts`)
488488
// instead of enqueuing the derives to be resolved again later.
489-
let expn_id = LocalExpnId::fresh_empty();
489+
let expn_id = LocalExpnId::reserve_expansion_id();
490490
derive_invocations.push((
491491
Invocation {
492492
kind: InvocationKind::Derive { path, item, is_const },
@@ -1548,7 +1548,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
15481548
}
15491549

15501550
fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
1551-
let expn_id = LocalExpnId::fresh_empty();
1551+
let expn_id = LocalExpnId::reserve_expansion_id();
15521552
let vis = kind.placeholder_visibility();
15531553
self.invocations.push((
15541554
Invocation {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use rustc_session::Limit;
6565
use rustc_session::Session;
6666
use rustc_span::def_id::{DefPathHash, StableCrateId};
6767
use rustc_span::symbol::{kw, sym, Ident, Symbol};
68+
use rustc_span::{DesugaringKind, ExpnData, ExpnKind, LocalExpnId};
6869
use rustc_span::{Span, DUMMY_SP};
6970
use rustc_target::abi::{FieldIdx, Layout, LayoutS, TargetDataLayout, VariantIdx};
7071
use rustc_target::spec::abi;
@@ -901,6 +902,38 @@ impl<'tcx> TyCtxt<'tcx> {
901902
}
902903
}
903904

905+
impl<'tcx> TyCtxt<'tcx> {
906+
#[instrument(level = "trace", skip(self), ret)]
907+
pub fn create_expansion(self, expn_data: ExpnData) -> LocalExpnId {
908+
self.with_stable_hashing_context(|ctx| {
909+
LocalExpnId::create_untracked_expansion(expn_data, ctx)
910+
})
911+
}
912+
913+
/// Fill an empty expansion. This method must not be used outside of the resolver.
914+
#[inline]
915+
#[instrument(level = "trace", skip(self))]
916+
pub fn finalize_expansion(self, expn_id: LocalExpnId, expn_data: ExpnData) {
917+
self.with_stable_hashing_context(|ctx| expn_id.set_untracked_expn_data(expn_data, ctx));
918+
}
919+
920+
/// Reuses the span but adds information like the kind of the desugaring and features that are
921+
/// allowed inside this span.
922+
pub fn mark_span_with_reason(
923+
self,
924+
reason: DesugaringKind,
925+
span: Span,
926+
allow_internal_unstable: Option<Lrc<[Symbol]>>,
927+
) -> Span {
928+
let edition = self.sess.edition();
929+
let mut expn_data =
930+
ExpnData::default(ExpnKind::Desugaring(reason), span, edition, None, None);
931+
expn_data.allow_internal_unstable = allow_internal_unstable;
932+
let expn_id = self.create_expansion(expn_data);
933+
span.fresh_expansion(expn_id)
934+
}
935+
}
936+
904937
impl<'tcx> TyCtxtAt<'tcx> {
905938
/// Create a new definition within the incr. comp. engine.
906939
pub fn create_def(

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,10 @@ pub(crate) fn incremental_verify_ich<Tcx, V>(
676676
});
677677

678678
let old_hash = dep_graph_data.prev_fingerprint_of(prev_index);
679+
debug!(
680+
dep_node = ?tcx.dep_graph().data().unwrap().prev_node_of(prev_index),
681+
?new_hash, ?old_hash,
682+
);
679683

680684
if new_hash != old_hash {
681685
incremental_verify_ich_failed(tcx, prev_index, &|| format_value(&result));

compiler/rustc_resolve/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ use rustc_middle::query::Providers;
5151
use rustc_middle::span_bug;
5252
use rustc_middle::ty::{self, MainDefinition, RegisteredTools, TyCtxt};
5353
use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs};
54-
use rustc_query_system::ich::StableHashingContext;
5554
use rustc_session::lint::LintBuffer;
5655
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency};
5756
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -1445,10 +1444,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14451444
ResolverOutputs { global_ctxt, ast_lowering }
14461445
}
14471446

1448-
fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
1449-
StableHashingContext::new(self.tcx.sess, self.tcx.untracked())
1450-
}
1451-
14521447
fn crate_loader<T>(&mut self, f: impl FnOnce(&mut CrateLoader<'_, '_>) -> T) -> T {
14531448
f(&mut CrateLoader::new(
14541449
self.tcx,

compiler/rustc_resolve/src/macros.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,14 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
217217
) -> LocalExpnId {
218218
let parent_module =
219219
parent_module_id.map(|module_id| self.local_def_id(module_id).to_def_id());
220-
let expn_id = LocalExpnId::fresh(
221-
ExpnData::allow_unstable(
222-
ExpnKind::AstPass(pass),
223-
call_site,
224-
self.tcx.sess.edition(),
225-
features.into(),
226-
None,
227-
parent_module,
228-
),
229-
self.create_stable_hashing_context(),
230-
);
220+
let expn_id = self.tcx.create_expansion(ExpnData::allow_unstable(
221+
ExpnKind::AstPass(pass),
222+
call_site,
223+
self.tcx.sess.edition(),
224+
features.into(),
225+
None,
226+
parent_module,
227+
));
231228

232229
let parent_scope =
233230
parent_module.map_or(self.empty_module, |def_id| self.expect_module(def_id));
@@ -290,15 +287,15 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
290287

291288
let span = invoc.span();
292289
let def_id = res.opt_def_id();
293-
invoc_id.set_expn_data(
290+
self.tcx.finalize_expansion(
291+
invoc_id,
294292
ext.expn_data(
295293
parent_scope.expansion,
296294
span,
297295
fast_print_path(path),
298296
def_id,
299297
def_id.map(|def_id| self.macro_def_scope(def_id).nearest_parent_mod()),
300298
),
301-
self.create_stable_hashing_context(),
302299
);
303300

304301
Ok(ext)

0 commit comments

Comments
 (0)