Skip to content

Commit

Permalink
Ensure nested statics have a HIR node to prevent various queries from…
Browse files Browse the repository at this point in the history
… ICEing
  • Loading branch information
oli-obk committed Mar 19, 2024
1 parent bdb682e commit fae64e1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_const_eval/src/interpret/intern.rs
Expand Up @@ -111,6 +111,8 @@ fn intern_as_new_static<'tcx>(
feed.generics_of(tcx.generics_of(static_id).clone());
feed.def_ident_span(tcx.def_ident_span(static_id));
feed.explicit_predicates_of(tcx.explicit_predicates_of(static_id));

feed.feed_hir()
}

/// How a constant value should be interned.
Expand Down
21 changes: 21 additions & 0 deletions compiler/rustc_middle/src/ty/context.rs
Expand Up @@ -596,6 +596,27 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
pub fn feed_owner_id(&self) -> TyCtxtFeed<'tcx, hir::OwnerId> {
TyCtxtFeed { tcx: self.tcx, key: hir::OwnerId { def_id: self.key } }
}

// Fills in all the important parts needed by HIR queries
pub fn feed_hir(&self) {
self.local_def_id_to_hir_id(HirId::make_owner(self.def_id()));

let node = hir::OwnerNode::Synthetic;
let bodies = Default::default();
let attrs = hir::AttributeMap::EMPTY;

let (opt_hash_including_bodies, _) = self.tcx.hash_owner_nodes(node, &bodies, &attrs.map);
let node = node.into();
self.opt_hir_owner_nodes(Some(self.tcx.arena.alloc(hir::OwnerNodes {
opt_hash_including_bodies,
nodes: IndexVec::from_elem_n(
hir::ParentedNode { parent: hir::ItemLocalId::INVALID, node },
1,
),
bodies,
})));
self.feed_owner_id().hir_attrs(attrs);
}
}

/// The central data structure of the compiler. It stores references
Expand Down
29 changes: 4 additions & 25 deletions compiler/rustc_ty_utils/src/assoc.rs
@@ -1,11 +1,10 @@
use rustc_data_structures::fx::FxIndexSet;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{self as hir, HirId};
use rustc_index::IndexVec;
use rustc_middle::query::Providers;
use rustc_middle::ty::{self, ImplTraitInTraitData, TyCtxt, TyCtxtFeed};
use rustc_middle::ty::{self, ImplTraitInTraitData, TyCtxt};
use rustc_span::symbol::kw;

pub(crate) fn provide(providers: &mut Providers) {
Expand Down Expand Up @@ -238,26 +237,6 @@ fn associated_types_for_impl_traits_in_associated_fn(
}
}

fn feed_hir(feed: &TyCtxtFeed<'_, LocalDefId>) {
feed.local_def_id_to_hir_id(HirId::make_owner(feed.def_id()));

let node = hir::OwnerNode::Synthetic;
let bodies = Default::default();
let attrs = hir::AttributeMap::EMPTY;

let (opt_hash_including_bodies, _) = feed.tcx.hash_owner_nodes(node, &bodies, &attrs.map);
let node = node.into();
feed.opt_hir_owner_nodes(Some(feed.tcx.arena.alloc(hir::OwnerNodes {
opt_hash_including_bodies,
nodes: IndexVec::from_elem_n(
hir::ParentedNode { parent: hir::ItemLocalId::INVALID, node },
1,
),
bodies,
})));
feed.feed_owner_id().hir_attrs(attrs);
}

/// Given an `opaque_ty_def_id` corresponding to an `impl Trait` in an associated
/// function from a trait, synthesize an associated type for that `impl Trait`
/// that inherits properties that we infer from the method and the opaque type.
Expand All @@ -279,7 +258,7 @@ fn associated_type_for_impl_trait_in_trait(
let local_def_id = trait_assoc_ty.def_id();
let def_id = local_def_id.to_def_id();

feed_hir(&trait_assoc_ty);
trait_assoc_ty.feed_hir();

// Copy span of the opaque.
trait_assoc_ty.def_ident_span(Some(span));
Expand Down Expand Up @@ -333,7 +312,7 @@ fn associated_type_for_impl_trait_in_impl(
let local_def_id = impl_assoc_ty.def_id();
let def_id = local_def_id.to_def_id();

feed_hir(&impl_assoc_ty);
impl_assoc_ty.feed_hir();

// Copy span of the opaque.
impl_assoc_ty.def_ident_span(Some(span));
Expand Down
14 changes: 14 additions & 0 deletions src/tools/miri/tests/pass/nested_static.rs
@@ -0,0 +1,14 @@
const OID_RSA_ENCRYPTION: &[u64] = &[1, 2, 840, 113549, 1, 1, 1];

pub struct SignatureAlgorithm {
pub oids: &'static [&'static [u64]],
}

pub static PKCS_RSA_SHA256: SignatureAlgorithm = SignatureAlgorithm {
// Does not repro with this constant inlined.
oids: &[&OID_RSA_ENCRYPTION],
};

fn main() {
assert_eq!(PKCS_RSA_SHA256.oids[0][2], 840);
}

0 comments on commit fae64e1

Please sign in to comment.