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

Do not fetch HIR to compute variances. #98487

Merged
merged 1 commit into from
Jul 7, 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
9 changes: 9 additions & 0 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ impl ModuleItems {
self.foreign_items.iter().copied()
}

pub fn definitions(&self) -> impl Iterator<Item = LocalDefId> + '_ {
self.items
.iter()
.map(|id| id.def_id)
.chain(self.trait_items.iter().map(|id| id.def_id))
.chain(self.impl_items.iter().map(|id| id.def_id))
.chain(self.foreign_items.iter().map(|id| id.def_id))
}

pub fn par_items(&self, f: impl Fn(ItemId) + Send + Sync) {
par_for_each_in(&self.items[..], |&id| f(id))
}
Expand Down
76 changes: 14 additions & 62 deletions compiler/rustc_typeck/src/variance/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,74 +64,28 @@ pub fn add_constraints_from_crate<'a, 'tcx>(

let crate_items = tcx.hir_crate_items(());

for id in crate_items.items() {
constraint_cx.check_item(id);
}

for id in crate_items.trait_items() {
if let DefKind::AssocFn = tcx.def_kind(id.def_id) {
constraint_cx.check_node_helper(id.hir_id());
}
}

for id in crate_items.impl_items() {
if let DefKind::AssocFn = tcx.def_kind(id.def_id) {
constraint_cx.check_node_helper(id.hir_id());
}
}

for id in crate_items.foreign_items() {
if let DefKind::Fn = tcx.def_kind(id.def_id) {
constraint_cx.check_node_helper(id.hir_id());
}
}

constraint_cx
}

impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
fn check_item(&mut self, id: hir::ItemId) {
let def_kind = self.tcx().def_kind(id.def_id);
for def_id in crate_items.definitions() {
let def_kind = tcx.def_kind(def_id);
match def_kind {
DefKind::Struct | DefKind::Union => {
let item = self.tcx().hir().item(id);

if let hir::ItemKind::Struct(ref struct_def, _)
| hir::ItemKind::Union(ref struct_def, _) = item.kind
{
self.check_node_helper(item.hir_id());
DefKind::Struct | DefKind::Union | DefKind::Enum => {
constraint_cx.build_constraints_for_item(def_id);

if let hir::VariantData::Tuple(..) = *struct_def {
self.check_node_helper(struct_def.ctor_hir_id().unwrap());
let adt = tcx.adt_def(def_id);
for variant in adt.variants() {
if let Some(ctor) = variant.ctor_def_id {
constraint_cx.build_constraints_for_item(ctor.expect_local());
}
}
}
DefKind::Enum => {
let item = self.tcx().hir().item(id);

if let hir::ItemKind::Enum(ref enum_def, _) = item.kind {
self.check_node_helper(item.hir_id());

for variant in enum_def.variants {
if let hir::VariantData::Tuple(..) = variant.data {
self.check_node_helper(variant.data.ctor_hir_id().unwrap());
}
}
}
}
DefKind::Fn => {
self.check_node_helper(id.hir_id());
}
DefKind::Fn | DefKind::AssocFn => constraint_cx.build_constraints_for_item(def_id),
_ => {}
}
}

fn check_node_helper(&mut self, id: hir::HirId) {
let tcx = self.terms_cx.tcx;
let def_id = tcx.hir().local_def_id(id);
self.build_constraints_for_item(def_id);
}
constraint_cx
}

impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.terms_cx.tcx
}
Expand All @@ -145,8 +99,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
return;
}

let id = tcx.hir().local_def_id_to_hir_id(def_id);
let inferred_start = self.terms_cx.inferred_starts[&id];
let inferred_start = self.terms_cx.inferred_starts[&def_id];
let current_item = &CurrentItem { inferred_start };
match tcx.type_of(def_id).kind() {
ty::Adt(def, _) => {
Expand Down Expand Up @@ -372,8 +325,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
}

let (local, remote) = if let Some(def_id) = def_id.as_local() {
let id = self.tcx().hir().local_def_id_to_hir_id(def_id);
(Some(self.terms_cx.inferred_starts[&id]), None)
(Some(self.terms_cx.inferred_starts[&def_id]), None)
} else {
(None, Some(self.tcx().variances_of(def_id)))
};
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_typeck/src/variance/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> {
self.terms_cx
.inferred_starts
.iter()
.map(|(&id, &InferredIndex(start))| {
let def_id = tcx.hir().local_def_id(id);
.map(|(&def_id, &InferredIndex(start))| {
let generics = tcx.generics_of(def_id);
let count = generics.count();

Expand Down
90 changes: 24 additions & 66 deletions compiler/rustc_typeck/src/variance/terms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
// a variable.

use rustc_arena::DroplessArena;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::HirIdMap;
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap};
use rustc_middle::ty::{self, TyCtxt};
use std::fmt;

Expand Down Expand Up @@ -52,11 +51,11 @@ pub struct TermsContext<'a, 'tcx> {
// For marker types, UnsafeCell, and other lang items where
// variance is hardcoded, records the item-id and the hardcoded
// variance.
pub lang_items: Vec<(hir::HirId, Vec<ty::Variance>)>,
pub lang_items: Vec<(LocalDefId, Vec<ty::Variance>)>,

// Maps from the node id of an item to the first inferred index
// used for its type & region parameters.
pub inferred_starts: HirIdMap<InferredIndex>,
pub inferred_starts: LocalDefIdMap<InferredIndex>,

// Maps from an InferredIndex to the term for that variable.
pub inferred_terms: Vec<VarianceTermPtr<'a>>,
Expand All @@ -81,51 +80,48 @@ pub fn determine_parameters_to_be_inferred<'a, 'tcx>(
// - https://rustc-dev-guide.rust-lang.org/variance.html
let crate_items = tcx.hir_crate_items(());

for id in crate_items.items() {
terms_cx.check_item(id);
}
for def_id in crate_items.definitions() {
debug!("add_inferreds for item {:?}", def_id);

for id in crate_items.trait_items() {
if let DefKind::AssocFn = tcx.def_kind(id.def_id) {
terms_cx.add_inferreds_for_item(id.hir_id());
}
}
let def_kind = tcx.def_kind(def_id);

for id in crate_items.impl_items() {
if let DefKind::AssocFn = tcx.def_kind(id.def_id) {
terms_cx.add_inferreds_for_item(id.hir_id());
}
}
match def_kind {
DefKind::Struct | DefKind::Union | DefKind::Enum => {
terms_cx.add_inferreds_for_item(def_id);

for id in crate_items.foreign_items() {
if let DefKind::Fn = tcx.def_kind(id.def_id) {
terms_cx.add_inferreds_for_item(id.hir_id());
let adt = tcx.adt_def(def_id);
for variant in adt.variants() {
if let Some(ctor) = variant.ctor_def_id {
terms_cx.add_inferreds_for_item(ctor.expect_local());
}
}
}
DefKind::Fn | DefKind::AssocFn => terms_cx.add_inferreds_for_item(def_id),
_ => {}
}
}

terms_cx
}

fn lang_items(tcx: TyCtxt<'_>) -> Vec<(hir::HirId, Vec<ty::Variance>)> {
fn lang_items(tcx: TyCtxt<'_>) -> Vec<(LocalDefId, Vec<ty::Variance>)> {
let lang_items = tcx.lang_items();
let all = [
(lang_items.phantom_data(), vec![ty::Covariant]),
(lang_items.unsafe_cell_type(), vec![ty::Invariant]),
];

all.into_iter() // iterating over (Option<DefId>, Variance)
.filter(|&(ref d, _)| d.is_some())
.map(|(d, v)| (d.unwrap(), v)) // (DefId, Variance)
.filter_map(|(d, v)| {
d.as_local().map(|d| tcx.hir().local_def_id_to_hir_id(d)).map(|n| (n, v))
}) // (HirId, Variance)
let def_id = d?.as_local()?; // LocalDefId
Some((def_id, v))
})
.collect()
}

impl<'a, 'tcx> TermsContext<'a, 'tcx> {
fn add_inferreds_for_item(&mut self, id: hir::HirId) {
fn add_inferreds_for_item(&mut self, def_id: LocalDefId) {
let tcx = self.tcx;
let def_id = tcx.hir().local_def_id(id);
let count = tcx.generics_of(def_id).count();

if count == 0 {
Expand All @@ -134,7 +130,7 @@ impl<'a, 'tcx> TermsContext<'a, 'tcx> {

// Record the start of this item's inferreds.
let start = self.inferred_terms.len();
let newly_added = self.inferred_starts.insert(id, InferredIndex(start)).is_none();
let newly_added = self.inferred_starts.insert(def_id, InferredIndex(start)).is_none();
assert!(newly_added);

// N.B., in the code below for writing the results back into the
Expand All @@ -146,42 +142,4 @@ impl<'a, 'tcx> TermsContext<'a, 'tcx> {
(start..(start + count)).map(|i| &*arena.alloc(InferredTerm(InferredIndex(i)))),
);
}

fn check_item(&mut self, id: hir::ItemId) {
debug!("add_inferreds for item {}", self.tcx.hir().node_to_string(id.hir_id()));

let def_kind = self.tcx.def_kind(id.def_id);
match def_kind {
DefKind::Struct | DefKind::Union => {
let item = self.tcx.hir().item(id);

if let hir::ItemKind::Struct(ref struct_def, _)
| hir::ItemKind::Union(ref struct_def, _) = item.kind
{
self.add_inferreds_for_item(item.hir_id());

if let hir::VariantData::Tuple(..) = *struct_def {
self.add_inferreds_for_item(struct_def.ctor_hir_id().unwrap());
}
}
}
DefKind::Enum => {
let item = self.tcx.hir().item(id);

if let hir::ItemKind::Enum(ref enum_def, _) = item.kind {
self.add_inferreds_for_item(item.hir_id());

for variant in enum_def.variants {
if let hir::VariantData::Tuple(..) = variant.data {
self.add_inferreds_for_item(variant.data.ctor_hir_id().unwrap());
}
}
}
}
DefKind::Fn => {
self.add_inferreds_for_item(id.hir_id());
}
_ => {}
}
}
}