Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 5 pull requests #99948

Merged
merged 14 commits into from
Jul 30, 2022
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
3 changes: 1 addition & 2 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {

// We use the statements were the binding was initialized, and inspect the HIR to look
// for the branching codepaths that aren't covered, to point at them.
let hir_id = self.mir_hir_id();
let map = self.infcx.tcx.hir();
let body_id = map.body_owned_by(hir_id);
let body_id = map.body_owned_by(self.mir_def_id());
let body = map.body(body_id);

let mut visitor = ConditionVisitor { spans: &spans, name: &name, errors: vec![] };
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let closure_id = self.mir_hir_id();
let fn_call_id = hir.get_parent_node(closure_id);
let node = hir.get(fn_call_id);
let item_id = hir.enclosing_body_owner(fn_call_id);
let def_id = hir.enclosing_body_owner(fn_call_id);
let mut look_at_return = true;
// If we can detect the expression to be an `fn` call where the closure was an argument,
// we point at the `fn` definition argument...
Expand All @@ -864,7 +864,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
.filter(|(_, arg)| arg.hir_id == closure_id)
.map(|(pos, _)| pos)
.next();
let def_id = hir.local_def_id(item_id);
let tables = self.infcx.tcx.typeck(def_id);
if let Some(ty::FnDef(def_id, _)) =
tables.node_type_opt(func.hir_id).as_ref().map(|ty| ty.kind())
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_data_structures/src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rustc_index::bit_set;
use rustc_index::vec;
use smallvec::SmallVec;
use std::hash::{BuildHasher, Hash, Hasher};
use std::marker::PhantomData;
use std::mem;

#[cfg(test)]
Expand Down Expand Up @@ -261,6 +262,10 @@ impl<CTX> HashStable<CTX> for ! {
}
}

impl<CTX, T> HashStable<CTX> for PhantomData<T> {
fn hash_stable(&self, _ctx: &mut CTX, _hasher: &mut StableHasher) {}
}

impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
#[inline]
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_driver/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
let typeck_results = self.maybe_typeck_results.get().or_else(|| {
self.tcx
.hir()
.maybe_body_owned_by(self.tcx.hir().local_def_id_to_hir_id(expr.hir_id.owner))
.maybe_body_owned_by(expr.hir_id.owner)
.map(|body_id| self.tcx.typeck_body(body_id))
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ pub fn find_param_with_region<'tcx>(
};

let hir = &tcx.hir();
let hir_id = hir.local_def_id_to_hir_id(id.as_local()?);
let body_id = hir.maybe_body_owned_by(hir_id)?;
let body = hir.body(body_id);
let def_id = id.as_local()?;
let hir_id = hir.local_def_id_to_hir_id(def_id);

// FIXME: use def_kind
// Don't perform this on closures
match hir.get(hir_id) {
hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
Expand All @@ -61,11 +61,14 @@ pub fn find_param_with_region<'tcx>(
_ => {}
}

let body_id = hir.maybe_body_owned_by(def_id)?;

let owner_id = hir.body_owner(body_id);
let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap();
let poly_fn_sig = tcx.fn_sig(id);

let fn_sig = tcx.liberate_late_bound_regions(id, poly_fn_sig);
let body = hir.body(body_id);
body.params
.iter()
.take(if fn_sig.c_variadic {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
fn encode_info_for_anon_const(&mut self, id: hir::HirId) {
let def_id = self.tcx.hir().local_def_id(id);
debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id);
let body_id = self.tcx.hir().body_owned_by(id);
let body_id = self.tcx.hir().body_owned_by(def_id);
let const_data = self.encode_rendered_const_for_body(body_id);
let qualifs = self.tcx.mir_const_qualif(def_id);

Expand Down
21 changes: 11 additions & 10 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,10 @@ impl<'hir> Map<'hir> {
}
}

pub fn enclosing_body_owner(self, hir_id: HirId) -> HirId {
pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId {
for (parent, _) in self.parent_iter(hir_id) {
if let Some(body) = self.maybe_body_owned_by(parent) {
return self.body_owner(body);
if let Some(body) = self.find(parent).map(associated_body).flatten() {
return self.body_owner_def_id(body);
}
}

Expand All @@ -419,19 +419,20 @@ impl<'hir> Map<'hir> {
self.local_def_id(self.body_owner(id))
}

/// Given a `HirId`, returns the `BodyId` associated with it,
/// 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, hir_id: HirId) -> Option<BodyId> {
self.find(hir_id).map(associated_body).flatten()
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<BodyId> {
self.get_if_local(id.to_def_id()).map(associated_body).flatten()
}

/// Given a body owner's id, returns the `BodyId` associated with it.
pub fn body_owned_by(self, id: HirId) -> BodyId {
pub fn body_owned_by(self, id: LocalDefId) -> BodyId {
self.maybe_body_owned_by(id).unwrap_or_else(|| {
let hir_id = self.local_def_id_to_hir_id(id);
span_bug!(
self.span(id),
self.span(hir_id),
"body_owned_by: {} has no associated body",
self.node_to_string(id)
self.node_to_string(hir_id)
);
})
}
Expand Down Expand Up @@ -670,7 +671,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 {
self.body_const_context(self.local_def_id(self.enclosing_body_owner(hir_id))).is_some()
self.body_const_context(self.enclosing_body_owner(hir_id)).is_some()
}

/// Retrieves the `HirId` for `id`'s enclosing method, unless there's a
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ pub fn provide(providers: &mut Providers) {
};
providers.fn_arg_names = |tcx, id| {
let hir = tcx.hir();
let hir_id = hir.local_def_id_to_hir_id(id.expect_local());
if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
let def_id = id.expect_local();
let hir_id = hir.local_def_id_to_hir_id(def_id);
if let Some(body_id) = hir.maybe_body_owned_by(def_id) {
tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
} else if let Node::TraitItem(&TraitItem {
kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
Expand Down
26 changes: 1 addition & 25 deletions compiler/rustc_middle/src/traits/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ use crate::infer::canonical::{Canonical, QueryResponse};
use crate::ty::error::TypeError;
use crate::ty::subst::GenericArg;
use crate::ty::{self, Ty, TyCtxt};

use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_errors::struct_span_err;
use rustc_query_system::ich::StableHashingContext;
use rustc_span::source_map::Span;
use std::iter::FromIterator;
use std::mem;

pub mod type_op {
use crate::ty::fold::TypeFoldable;
Expand Down Expand Up @@ -226,29 +222,9 @@ pub struct NormalizationResult<'tcx> {
/// case they are called implied bounds). They are fed to the
/// `OutlivesEnv` which in turn is supplied to the region checker and
/// other parts of the inference system.
#[derive(Clone, Debug, TypeFoldable, TypeVisitable, Lift)]
#[derive(Clone, Debug, TypeFoldable, TypeVisitable, Lift, HashStable)]
pub enum OutlivesBound<'tcx> {
RegionSubRegion(ty::Region<'tcx>, ty::Region<'tcx>),
RegionSubParam(ty::Region<'tcx>, ty::ParamTy),
RegionSubProjection(ty::Region<'tcx>, ty::ProjectionTy<'tcx>),
}

impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for OutlivesBound<'tcx> {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
OutlivesBound::RegionSubRegion(ref a, ref b) => {
a.hash_stable(hcx, hasher);
b.hash_stable(hcx, hasher);
}
OutlivesBound::RegionSubParam(ref a, ref b) => {
a.hash_stable(hcx, hasher);
b.hash_stable(hcx, hasher);
}
OutlivesBound::RegionSubProjection(ref a, ref b) => {
a.hash_stable(hcx, hasher);
b.hash_stable(hcx, hasher);
}
}
}
}
40 changes: 0 additions & 40 deletions compiler/rustc_middle/src/ty/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,46 +101,6 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::subst::GenericArgKin
}
}

impl<'a> HashStable<StableHashingContext<'a>> for ty::EarlyBoundRegion {
#[inline]
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
self.def_id.hash_stable(hcx, hasher);
self.index.hash_stable(hcx, hasher);
self.name.hash_stable(hcx, hasher);
}
}

impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionVid {
#[inline]
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
self.index().hash_stable(hcx, hasher);
}
}

impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::ConstVid<'tcx> {
#[inline]
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
self.index.hash_stable(hcx, hasher);
}
}

impl<'tcx> HashStable<StableHashingContext<'tcx>> for ty::BoundVar {
#[inline]
fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
self.index().hash_stable(hcx, hasher);
}
}

impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for ty::Binder<'tcx, T>
where
T: HashStable<StableHashingContext<'a>>,
{
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
self.as_ref().skip_binder().hash_stable(hcx, hasher);
self.bound_vars().hash_stable(hcx, hasher);
}
}

// AllocIds get resolved to whatever they point to (to be stable)
impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::AllocId {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
Expand Down
26 changes: 3 additions & 23 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,22 +1182,13 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
/// identified by both a universe, as well as a name residing within that universe. Distinct bound
/// regions/types/consts within the same universe simply have an unknown relationship to one
/// another.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(HashStable, TyEncodable, TyDecodable)]
pub struct Placeholder<T> {
pub universe: UniverseIndex,
pub name: T,
}

impl<'a, T> HashStable<StableHashingContext<'a>> for Placeholder<T>
where
T: HashStable<StableHashingContext<'a>>,
{
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
self.universe.hash_stable(hcx, hasher);
self.name.hash_stable(hcx, hasher);
}
}

pub type PlaceholderRegion = Placeholder<BoundRegionKind>;

pub type PlaceholderType = Placeholder<BoundVar>;
Expand Down Expand Up @@ -1581,6 +1572,7 @@ impl<'tcx> PolyTraitRef<'tcx> {
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TypeFoldable, TypeVisitable)]
#[derive(HashStable)]
pub struct ParamEnvAnd<'tcx, T> {
pub param_env: ParamEnv<'tcx>,
pub value: T,
Expand All @@ -1598,18 +1590,6 @@ impl<'tcx, T> ParamEnvAnd<'tcx, T> {
}
}

impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for ParamEnvAnd<'tcx, T>
where
T: HashStable<StableHashingContext<'a>>,
{
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
let ParamEnvAnd { ref param_env, ref value } = *self;

param_env.hash_stable(hcx, hasher);
value.hash_stable(hcx, hasher);
}
}

#[derive(Copy, Clone, Debug, HashStable, Encodable, Decodable)]
pub struct Destructor {
/// The `DefId` of the destructor method
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,7 @@ impl BoundVariableKind {
///
/// `Decodable` and `Encodable` are implemented for `Binder<T>` using the `impl_binder_encode_decode!` macro.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(HashStable)]
pub struct Binder<'tcx, T>(T, &'tcx List<BoundVariableKind>);

impl<'tcx, T> Binder<'tcx, T>
Expand Down Expand Up @@ -1355,6 +1356,7 @@ impl<'tcx> fmt::Debug for Region<'tcx> {
}

#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, PartialOrd, Ord)]
#[derive(HashStable)]
pub struct EarlyBoundRegion {
pub def_id: DefId,
pub index: u32,
Expand All @@ -1368,14 +1370,16 @@ impl fmt::Debug for EarlyBoundRegion {
}

/// A **`const`** **v**ariable **ID**.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(HashStable, TyEncodable, TyDecodable)]
pub struct ConstVid<'tcx> {
pub index: u32,
pub phantom: PhantomData<&'tcx ()>,
}

rustc_index::newtype_index! {
/// A **region** (lifetime) **v**ariable **ID**.
#[derive(HashStable)]
pub struct RegionVid {
DEBUG_FORMAT = custom,
}
Expand All @@ -1388,6 +1392,7 @@ impl Atom for RegionVid {
}

rustc_index::newtype_index! {
#[derive(HashStable)]
pub struct BoundVar { .. }
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalD
if tcx.is_closure(def.did.to_def_id()) {
let hir = tcx.hir();
let owner = hir.enclosing_body_owner(hir.local_def_id_to_hir_id(def.did));
tcx.ensure().thir_check_unsafety(hir.local_def_id(owner));
tcx.ensure().thir_check_unsafety(owner);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/thir/cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) fn thir_body<'tcx>(
owner_def: ty::WithOptConstParam<LocalDefId>,
) -> Result<(&'tcx Steal<Thir<'tcx>>, ExprId), ErrorGuaranteed> {
let hir = tcx.hir();
let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(owner_def.did)));
let body = hir.body(hir.body_owned_by(owner_def.did));
let mut cx = Cx::new(tcx, owner_def);
if let Some(reported) = cx.typeck_results.tainted_by_errors {
return Err(reported);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use rustc_span::{BytePos, Span};
pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: DefId) {
let body_id = match def_id.as_local() {
None => return,
Some(id) => tcx.hir().body_owned_by(tcx.hir().local_def_id_to_hir_id(id)),
Some(def_id) => tcx.hir().body_owned_by(def_id),
};

let pattern_arena = TypedArena::default();
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_mir_transform/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,15 +464,15 @@ fn check_unused_unsafe(
def_id: LocalDefId,
used_unsafe_blocks: &FxHashMap<HirId, UsedUnsafeBlockData>,
) -> Vec<(HirId, UnusedUnsafe)> {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let body_id = tcx.hir().maybe_body_owned_by(hir_id);
let body_id = tcx.hir().maybe_body_owned_by(def_id);

let Some(body_id) = body_id else {
debug!("check_unused_unsafe({:?}) - no body found", def_id);
return vec![];
};
let body = tcx.hir().body(body_id);

let body = tcx.hir().body(body_id);
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let context = match tcx.hir().fn_sig_by_hir_id(hir_id) {
Some(sig) if sig.header.unsafety == hir::Unsafety::Unsafe => Context::UnsafeFn(hir_id),
_ => Context::Safe,
Expand Down
Loading