Auto merge of #125918 - oli-obk:const_block_ice, r=compiler-errors
Revert: create const block bodies in typeck via query feeding as per the discussion in https://github.com/rust-lang/rust/pull/125806#discussion_r1622563948 It was a mistake to try to shoehorn const blocks and some specific anon consts into the same box and feed them during typeck. It turned out not simplifying anything (my hope was that we could feed `type_of` to start avoiding the huge HIR matcher, but that didn't work out), but instead making a few things more fragile. reverts the const-block-specific parts of https://github.com/rust-lang/rust/pull/124650 `@bors` rollup=never had a small perf impact previously fixes https://github.com/rust-lang/rust/issues/125846 r? `@compiler-errors`
This commit is contained in:
commit
1be24d70ce
44 changed files with 211 additions and 189 deletions
|
@ -1,5 +1,3 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use crate::hir::ModuleItems;
|
||||
use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
|
||||
use crate::query::LocalCrate;
|
||||
|
@ -256,26 +254,13 @@ impl<'hir> Map<'hir> {
|
|||
|
||||
/// Given a `LocalDefId`, returns the `BodyId` associated with it,
|
||||
/// if the node is a body owner, otherwise returns `None`.
|
||||
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<Cow<'hir, Body<'hir>>> {
|
||||
Some(match self.tcx.def_kind(id) {
|
||||
// Inline consts do not have bodies of their own, so create one to make the follow-up logic simpler.
|
||||
DefKind::InlineConst => {
|
||||
let e = self.expect_expr(self.tcx.local_def_id_to_hir_id(id));
|
||||
Cow::Owned(Body {
|
||||
params: &[],
|
||||
value: match e.kind {
|
||||
ExprKind::ConstBlock(body) => body,
|
||||
_ => span_bug!(e.span, "InlineConst was not a ConstBlock: {e:#?}"),
|
||||
},
|
||||
})
|
||||
}
|
||||
_ => Cow::Borrowed(self.body(self.tcx.hir_node_by_def_id(id).body_id()?)),
|
||||
})
|
||||
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<&'hir Body<'hir>> {
|
||||
Some(self.body(self.tcx.hir_node_by_def_id(id).body_id()?))
|
||||
}
|
||||
|
||||
/// Given a body owner's id, returns the `BodyId` associated with it.
|
||||
#[track_caller]
|
||||
pub fn body_owned_by(self, id: LocalDefId) -> Cow<'hir, Body<'hir>> {
|
||||
pub fn body_owned_by(self, id: LocalDefId) -> &'hir Body<'hir> {
|
||||
self.maybe_body_owned_by(id).unwrap_or_else(|| {
|
||||
let hir_id = self.tcx.local_def_id_to_hir_id(id);
|
||||
span_bug!(
|
||||
|
@ -338,7 +323,7 @@ impl<'hir> Map<'hir> {
|
|||
|
||||
/// Returns an iterator of the `DefId`s for all body-owners in this
|
||||
/// crate. If you would prefer to iterate over the bodies
|
||||
/// themselves, you can do `self.hir().krate().owners.iter()`.
|
||||
/// themselves, you can do `self.hir().krate().body_ids.iter()`.
|
||||
#[inline]
|
||||
pub fn body_owners(self) -> impl Iterator<Item = LocalDefId> + 'hir {
|
||||
self.tcx.hir_crate_items(()).body_owners.iter().copied()
|
||||
|
@ -525,17 +510,7 @@ impl<'hir> Map<'hir> {
|
|||
/// Whether the expression pointed at by `hir_id` belongs to a `const` evaluation context.
|
||||
/// Used exclusively for diagnostics, to avoid suggestion function calls.
|
||||
pub fn is_inside_const_context(self, hir_id: HirId) -> bool {
|
||||
for (_, node) in self.parent_iter(hir_id) {
|
||||
if let Some((def_id, _)) = node.associated_body() {
|
||||
return self.body_const_context(def_id).is_some();
|
||||
}
|
||||
if let Node::Expr(e) = node {
|
||||
if let ExprKind::ConstBlock(_) = e.kind {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
self.body_const_context(self.enclosing_body_owner(hir_id)).is_some()
|
||||
}
|
||||
|
||||
/// Retrieves the `HirId` for `id`'s enclosing function *if* the `id` block or return is
|
||||
|
@ -918,6 +893,7 @@ impl<'hir> Map<'hir> {
|
|||
Node::Variant(variant) => variant.span,
|
||||
Node::Field(field) => field.span,
|
||||
Node::AnonConst(constant) => constant.span,
|
||||
Node::ConstBlock(constant) => self.body(constant.body).value.span,
|
||||
Node::Expr(expr) => expr.span,
|
||||
Node::ExprField(field) => field.span,
|
||||
Node::Stmt(stmt) => stmt.span,
|
||||
|
@ -1187,6 +1163,7 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
|
|||
format!("{id} (field `{}` in {})", field.ident, path_str(field.def_id))
|
||||
}
|
||||
Node::AnonConst(_) => node_str("const"),
|
||||
Node::ConstBlock(_) => node_str("const"),
|
||||
Node::Expr(_) => node_str("expr"),
|
||||
Node::ExprField(_) => node_str("expr field"),
|
||||
Node::Stmt(_) => node_str("stmt"),
|
||||
|
@ -1336,6 +1313,11 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
|
|||
intravisit::walk_anon_const(self, c)
|
||||
}
|
||||
|
||||
fn visit_inline_const(&mut self, c: &'hir ConstBlock) {
|
||||
self.body_owners.push(c.def_id);
|
||||
intravisit::walk_inline_const(self, c)
|
||||
}
|
||||
|
||||
fn visit_expr(&mut self, ex: &'hir Expr<'hir>) {
|
||||
if let ExprKind::Closure(closure) = ex.kind {
|
||||
self.body_owners.push(closure.def_id);
|
||||
|
|
|
@ -774,7 +774,6 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
|
|||
1,
|
||||
),
|
||||
bodies,
|
||||
has_inline_consts: false,
|
||||
})));
|
||||
self.feed_owner_id().hir_attrs(attrs);
|
||||
}
|
||||
|
|
|
@ -217,10 +217,6 @@ pub struct TypeckResults<'tcx> {
|
|||
|
||||
/// Container types and field indices of `offset_of!` expressions
|
||||
offset_of_data: ItemLocalMap<(Ty<'tcx>, Vec<(VariantIdx, FieldIdx)>)>,
|
||||
|
||||
/// Maps from `HirId`s of const blocks (the `ExprKind::ConstBlock`, not the inner expression's)
|
||||
/// to the `DefId` of the corresponding inline const.
|
||||
pub inline_consts: FxIndexMap<ItemLocalId, LocalDefId>,
|
||||
}
|
||||
|
||||
impl<'tcx> TypeckResults<'tcx> {
|
||||
|
@ -253,7 +249,6 @@ impl<'tcx> TypeckResults<'tcx> {
|
|||
treat_byte_string_as_slice: Default::default(),
|
||||
closure_size_eval: Default::default(),
|
||||
offset_of_data: Default::default(),
|
||||
inline_consts: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue