Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let req_span = self.mark_span_with_reason(
rustc_span::DesugaringKind::Contract,
lowered_req.span,
Some(Arc::clone(&self.allow_contracts)),
Some(Arc::clone(&crate::ALLOW_CONTRACTS)),
);
let precond = self.expr_call_lang_item_fn_mut(
req_span,
Expand All @@ -161,7 +161,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let ens_span = self.mark_span_with_reason(
rustc_span::DesugaringKind::Contract,
ens_span,
Some(Arc::clone(&self.allow_contracts)),
Some(Arc::clone(&crate::ALLOW_CONTRACTS)),
);
let lowered_ens = self.lower_expr_mut(&ens);
self.expr_call_lang_item_fn(
Expand Down
22 changes: 11 additions & 11 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,15 +742,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
this.mark_span_with_reason(
DesugaringKind::TryBlock,
expr.span,
Some(Arc::clone(&this.allow_try_trait)),
Some(Arc::clone(&crate::ALLOW_TRY_TRAIT)),
),
expr,
)
} else {
let try_span = this.mark_span_with_reason(
DesugaringKind::TryBlock,
this.tcx.sess.source_map().end_point(body.span),
Some(Arc::clone(&this.allow_try_trait)),
Some(Arc::clone(&crate::ALLOW_TRY_TRAIT)),
);

(try_span, this.expr_unit(try_span))
Expand Down Expand Up @@ -878,7 +878,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let unstable_span = self.mark_span_with_reason(
DesugaringKind::Async,
self.lower_span(span),
Some(Arc::clone(&self.allow_gen_future)),
Some(Arc::clone(self.allow_gen_future())),
);
let resume_ty = self.make_lang_item_qpath(LangItem::ResumeTy, unstable_span, None);
let input_ty = hir::Ty {
Expand Down Expand Up @@ -1037,15 +1037,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
};

let features = match await_kind {
FutureKind::Future if is_async_gen => Some(Arc::clone(&self.allow_async_gen)),
FutureKind::Future if is_async_gen => Some(Arc::clone(&crate::ALLOW_ASYNC_GEN)),
FutureKind::Future => None,
FutureKind::AsyncIterator => Some(Arc::clone(&self.allow_for_await)),
FutureKind::AsyncIterator => Some(Arc::clone(&crate::ALLOW_FOR_AWAIT)),
};
let span = self.mark_span_with_reason(DesugaringKind::Await, await_kw_span, features);
let gen_future_span = self.mark_span_with_reason(
DesugaringKind::Await,
full_span,
Some(Arc::clone(&self.allow_gen_future)),
Some(Arc::clone(self.allow_gen_future())),
);
let expr_hir_id = expr.hir_id;

Expand Down Expand Up @@ -1594,7 +1594,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
Some((id, _)) => {
if let Some(loop_id) = self.curr_owner.owner.get_label_res(id) {
let local_id = self.curr_owner.ident_and_label_to_local_id[&loop_id];
let loop_hir_id = HirId { owner: self.curr_owner.owner_id, local_id };
let loop_hir_id = HirId { owner: self.curr_owner.owner_id(), local_id };
Ok(loop_hir_id)
} else {
Err(hir::LoopIdError::UnresolvedLabel)
Expand Down Expand Up @@ -1727,7 +1727,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let desugar_span = self.mark_span_with_reason(
DesugaringKind::Async,
span,
Some(Arc::clone(&self.allow_async_gen)),
Some(Arc::clone(&crate::ALLOW_ASYNC_GEN)),
);
let wrapped_yielded = self.expr_call_lang_item_fn(
desugar_span,
Expand Down Expand Up @@ -1946,13 +1946,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
let unstable_span = self.mark_span_with_reason(
DesugaringKind::QuestionMark,
span,
Some(Arc::clone(&self.allow_try_trait)),
Some(Arc::clone(&crate::ALLOW_TRY_TRAIT)),
);
let try_span = self.tcx.sess.source_map().end_point(span);
let try_span = self.mark_span_with_reason(
DesugaringKind::QuestionMark,
try_span,
Some(Arc::clone(&self.allow_try_trait)),
Some(Arc::clone(&crate::ALLOW_TRY_TRAIT)),
);

// `Try::branch(<expr>)`
Expand Down Expand Up @@ -2049,7 +2049,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let unstable_span = self.mark_span_with_reason(
DesugaringKind::YeetExpr,
span,
Some(Arc::clone(&self.allow_try_trait)),
Some(Arc::clone(&crate::ALLOW_TRY_TRAIT)),
);

let from_yeet_expr = self.wrap_in_try_constructor(
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'hir> ItemLowerer<'_, 'hir> {
#[instrument(level = "debug", skip(self, c))]
pub(super) fn lower_crate(&mut self, c: &Crate) -> hir::MaybeOwner<'hir> {
self.with_lctx(CRATE_NODE_ID, |lctx| {
debug_assert_eq!(lctx.curr_owner.owner_id, CRATE_OWNER_ID);
debug_assert_eq!(lctx.curr_owner.owner_id(), CRATE_OWNER_ID);
let module = lctx.lower_mod(&c.items, &c.spans);
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, c.spans.inner_span, Target::Crate);
hir::OwnerNode::Crate(module)
Expand Down Expand Up @@ -204,7 +204,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
let owner_id = self.curr_owner.owner_id;
let owner_id = self.curr_owner.owner_id();
let hir_id: HirId = owner_id.into();
let vis_span = self.lower_span(i.vis.span);

Expand Down Expand Up @@ -731,7 +731,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
let owner_id = self.curr_owner.owner_id;
let owner_id = self.curr_owner.owner_id();
let hir_id: HirId = owner_id.into();
let attrs =
self.lower_attrs(hir_id, &i.attrs, i.span, Target::from_foreign_item_kind(&i.kind));
Expand Down Expand Up @@ -912,7 +912,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
let trait_item_def_id = self.curr_owner.owner_id;
let trait_item_def_id = self.curr_owner.owner_id();
let hir_id: HirId = trait_item_def_id.into();
let attrs = self.lower_attrs(
hir_id,
Expand Down Expand Up @@ -1161,7 +1161,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
let owner_id = self.curr_owner.owner_id;
let owner_id = self.curr_owner.owner_id();
let hir_id: HirId = owner_id.into();
let parent_id = self.tcx.local_parent(owner_id.def_id);
let is_in_trait_impl =
Expand Down Expand Up @@ -1322,7 +1322,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
) -> hir::BodyId {
let body = hir::Body { params, value: self.arena.alloc(value) };
let id = body.id();
assert_eq!(id.hir_id.owner, self.curr_owner.owner_id);
assert_eq!(id.hir_id.owner, self.curr_owner.owner_id());
self.curr_owner.bodies.push((id.hir_id.local_id, self.arena.alloc(body)));
id
}
Expand Down
86 changes: 44 additions & 42 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
// tidy-alphabetical-end

use std::mem;
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use rustc_ast::mut_visit::{self, MutVisitor};
use rustc_ast::node_id::NodeMap;
Expand Down Expand Up @@ -154,7 +154,6 @@ pub(crate) mod re_lowering {
struct PerOwnerLoweringState<'a, 'hir> {
// -- Identity --
owner: &'a PerOwnerResolverData<'hir>,
owner_id: hir::OwnerId,
disambiguator: PerParentDisambiguatorState,

// -- HirId allocation --
Expand Down Expand Up @@ -196,7 +195,6 @@ impl<'a, 'hir> PerOwnerLoweringState<'a, 'hir> {

PerOwnerLoweringState {
owner,
owner_id: hir::OwnerId { def_id: owner.def_id },
disambiguator,
// 0 corresponds to `owner` lowered as `owner_id`, and we never call
// `lower_node_id(owner)`.
Expand All @@ -215,12 +213,16 @@ impl<'a, 'hir> PerOwnerLoweringState<'a, 'hir> {
}
}

fn owner_id(&self) -> hir::OwnerId {
hir::OwnerId { def_id: self.owner.def_id }
}

fn into_owner_info(
self,
tcx: TyCtxt<'hir>,
node: hir::OwnerNode<'hir>,
) -> &'hir hir::OwnerInfo<'hir> {
assert_eq!(self.owner_id, node.def_id());
assert_eq!(self.owner_id(), node.def_id());
assert!(self.impl_trait_defs.is_empty());
assert!(self.impl_trait_bounds.is_empty());

Expand Down Expand Up @@ -312,15 +314,6 @@ struct LoweringContext<'a, 'hir> {
/// so we only store `self_param_id`.
partial_res_overrides: NodeMap<NodeId>,

allow_contracts: Arc<[Symbol]>,
allow_try_trait: Arc<[Symbol]>,
allow_gen_future: Arc<[Symbol]>,
allow_pattern_type: Arc<[Symbol]>,
allow_async_gen: Arc<[Symbol]>,
allow_async_iterator: Arc<[Symbol]>,
allow_for_await: Arc<[Symbol]>,
allow_async_fn_traits: Arc<[Symbol]>,

/// Stack of `move(...)` collection states. A closure-like body pushes
/// `Some`, so `move(...)` expressions can record the generated locals they
/// should lower to. Nested bodies that cannot use `move(...)` push `None`.
Expand All @@ -332,6 +325,26 @@ struct LoweringContext<'a, 'hir> {
attribute_parser: AttributeParser<'hir>,
}

macro_rules! allow {
($($name:ident: $list:expr;)*) => {
$( static $name: LazyLock<Arc<[Symbol]>> = LazyLock::new(|| $list.into()); )*
}
}

allow! {
ALLOW_CONTRACTS: [sym::contracts_internals];
ALLOW_TRY_TRAIT: [sym::try_trait_v2, sym::try_trait_v2_residual, sym::yeet_desugar_details];
ALLOW_PATTERN_TYPE: [sym::pattern_types, sym::pattern_type_range_trait];
ALLOW_GEN_FUTURE: [sym::gen_future];
ALLOW_GEN_FUTURE_WITH_ASYNC_FN_TRACK_CALLER: [sym::gen_future, sym::closure_track_caller];
ALLOW_FOR_AWAIT: [sym::async_gen_internals, sym::async_iterator];
ALLOW_ASYNC_FN_TRAITS: [sym::async_fn_traits];
ALLOW_ASYNC_GEN: [sym::async_gen_internals];
// FIXME(gen_blocks): how does `closure_track_caller`/`async_fn_track_caller`
// interact with `gen`/`async gen` blocks
ALLOW_ASYNC_ITERATOR: [sym::gen_future, sym::async_iterator];
}

impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn new(tcx: TyCtxt<'hir>, resolver: &'a ResolverAstLowering<'hir>, owner: NodeId) -> Self {
Self {
Expand All @@ -354,25 +367,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
coroutine_kind: None,
task_context: None,
current_item: None,
allow_contracts: [sym::contracts_internals].into(),
allow_try_trait: [
sym::try_trait_v2,
sym::try_trait_v2_residual,
sym::yeet_desugar_details,
]
.into(),
allow_pattern_type: [sym::pattern_types, sym::pattern_type_range_trait].into(),
allow_gen_future: if tcx.features().async_fn_track_caller() {
[sym::gen_future, sym::closure_track_caller].into()
} else {
[sym::gen_future].into()
},
allow_for_await: [sym::async_gen_internals, sym::async_iterator].into(),
allow_async_fn_traits: [sym::async_fn_traits].into(),
allow_async_gen: [sym::async_gen_internals].into(),
// FIXME(gen_blocks): how does `closure_track_caller`/`async_fn_track_caller`
// interact with `gen`/`async gen` blocks
allow_async_iterator: [sym::gen_future, sym::async_iterator].into(),

move_expr_bindings: Vec::new(),
lowering_move_expr_initializer: false,
Expand All @@ -388,6 +382,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
pub(crate) fn dcx(&self) -> DiagCtxtHandle<'hir> {
self.tcx.dcx()
}

fn allow_gen_future(&self) -> &Arc<[Symbol]> {
if self.tcx.features().async_fn_track_caller() {
&ALLOW_GEN_FUTURE_WITH_ASYNC_FN_TRACK_CALLER
} else {
&ALLOW_GEN_FUTURE
}
}
}

struct SpanLowerer {
Expand Down Expand Up @@ -836,7 +838,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
def_kind: DefKind,
span: Span,
) -> LocalDefId {
let parent = self.curr_owner.owner_id.def_id;
let parent = self.curr_owner.owner_id().def_id;
assert_ne!(node_id, ast::DUMMY_NODE_ID);
assert!(
self.opt_local_def_id(node_id).is_none(),
Expand Down Expand Up @@ -928,7 +930,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

let item = f(self);
let completed_child_owner = mem::replace(&mut self.curr_owner, parent_owner);
let owner_id = completed_child_owner.owner_id;
let owner_id = completed_child_owner.owner_id();
let info = completed_child_owner.into_owner_info(self.tcx, item);

self.curr_owner
Expand All @@ -948,7 +950,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_node_id(&mut self, ast_node_id: NodeId) -> HirId {
assert_ne!(ast_node_id, DUMMY_NODE_ID);

let owner = self.curr_owner.owner_id;
let owner = self.curr_owner.owner_id();
let local_id = self.curr_owner.item_local_id_counter;
assert_ne!(local_id, hir::ItemLocalId::ZERO);
self.curr_owner.item_local_id_counter.increment_by(1);
Expand All @@ -972,7 +974,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
/// Generate a new `HirId` without a backing `NodeId`.
#[instrument(level = "debug", skip(self), ret)]
fn next_id(&mut self) -> HirId {
let owner = self.curr_owner.owner_id;
let owner = self.curr_owner.owner_id();
let local_id = self.curr_owner.item_local_id_counter;
assert_ne!(local_id, hir::ItemLocalId::ZERO);
self.curr_owner.item_local_id_counter.increment_by(1);
Expand All @@ -982,7 +984,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
#[instrument(level = "trace", skip(self))]
fn lower_res(&mut self, res: Res<NodeId>) -> Res {
let res: Result<Res, ()> = res.apply_id(|id| {
let owner = self.curr_owner.owner_id;
let owner = self.curr_owner.owner_id();
let local_id =
self.curr_owner.ident_and_label_to_local_id.get(&id).copied().ok_or(())?;
Ok(HirId { owner, local_id })
Expand Down Expand Up @@ -1061,7 +1063,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn span_lowerer(&self) -> SpanLowerer {
SpanLowerer {
is_incremental: self.tcx.sess.opts.incremental.is_some(),
def_id: self.curr_owner.owner_id.def_id,
def_id: self.curr_owner.owner_id().def_id,
}
}

Expand Down Expand Up @@ -1201,7 +1203,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.lower_attrs_vec(attrs, self.lower_span(target_span), id, target, target_item);
lowered_attrs.extend(extra_hir_attributes.iter().cloned());

assert_eq!(id.owner, self.curr_owner.owner_id);
assert_eq!(id.owner, self.curr_owner.owner_id());
let ret = self.arena.alloc_from_iter(lowered_attrs);

// this is possible if an item contained syntactical attribute,
Expand Down Expand Up @@ -1251,8 +1253,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn alias_attrs(&mut self, id: HirId, target_id: HirId) {
assert_eq!(id.owner, self.curr_owner.owner_id);
assert_eq!(target_id.owner, self.curr_owner.owner_id);
assert_eq!(id.owner, self.curr_owner.owner_id());
assert_eq!(target_id.owner, self.curr_owner.owner_id());
if let Some(&a) = self.curr_owner.attrs.get(&target_id.local_id) {
assert!(!a.is_empty());
self.curr_owner.attrs.insert(id.local_id, a);
Expand Down Expand Up @@ -2093,7 +2095,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let (opaque_ty_node_id, allowed_features) = match coro.kind {
CoroutineKind::Async | CoroutineKind::Gen => (coro.return_impl_trait_id, None),
CoroutineKind::AsyncGen => {
(coro.return_impl_trait_id, Some(Arc::clone(&self.allow_async_iterator)))
(coro.return_impl_trait_id, Some(Arc::clone(&ALLOW_ASYNC_ITERATOR)))
}
};

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast_lowering/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir_id
} else {
hir::HirId {
owner: self.curr_owner.owner_id,
owner: self.curr_owner.owner_id(),
local_id: self.curr_owner.ident_and_label_to_local_id[&id],
}
}
Expand Down Expand Up @@ -482,7 +482,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let unstable_span = self.mark_span_with_reason(
DesugaringKind::PatTyRange,
span,
Some(Arc::clone(&self.allow_pattern_type)),
Some(Arc::clone(&crate::ALLOW_PATTERN_TYPE)),
);
let anon_const = self.with_new_scopes(span, |this| {
let def_id = this.local_def_id(e.id);
Expand Down Expand Up @@ -535,7 +535,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let unstable_span = self.mark_span_with_reason(
DesugaringKind::PatTyRange,
self.lower_span(span),
Some(Arc::clone(&self.allow_pattern_type)),
Some(Arc::clone(&crate::ALLOW_PATTERN_TYPE)),
);
let span = self.lower_span(base_type);

Expand Down
Loading
Loading