Skip to content

Make traits_in_crate and impls_in_crate proper queries #100601

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8925fcc
hashmap compiles
Robert-Cunningham Aug 13, 2022
d84ecf2
write all_local_trait_impls in terms of new query
Robert-Cunningham Aug 13, 2022
8acd145
remove fom resolver
Robert-Cunningham Aug 13, 2022
2d54ab7
convert hashmap to indexmap
Robert-Cunningham Aug 14, 2022
ab7c2ba
add eval_always
Robert-Cunningham Aug 15, 2022
d3426dd
stop prinnting dep graph
Robert-Cunningham Aug 15, 2022
251fcbc
clean up
Robert-Cunningham Aug 15, 2022
83ca511
revert derive formatting changes
Robert-Cunningham Aug 15, 2022
31222de
remove comments
Robert-Cunningham Aug 15, 2022
5a7d801
remove last robert-trait annotation
Robert-Cunningham Aug 15, 2022
058b8dc
implement cjg first suggestion
Robert-Cunningham Sep 12, 2022
c2a2636
Merge branch 'rust-lang:master' into impls_in_crate
Robert-Cunningham Sep 12, 2022
687280d
Merge branch 'impls_in_crate' of github.com:Robert-Cunningham/rust in…
Robert-Cunningham Sep 12, 2022
6b0ab18
temporary changed found january
Robert-Cunningham Jan 13, 2023
44364ae
resolve merge comfinclits
Robert-Cunningham Jan 13, 2023
11b2ade
make compile after merge
Robert-Cunningham Jan 13, 2023
3b486b2
fix git rename misses
Robert-Cunningham Jan 13, 2023
621ba62
debug checks
Robert-Cunningham Jan 14, 2023
bd35683
change type to option
Robert-Cunningham Jan 15, 2023
58d1577
remove local_impls_in_crate, find cycle
Robert-Cunningham Jan 15, 2023
90e12e8
passing tests
Robert-Cunningham Jan 15, 2023
0655439
revert everything
Robert-Cunningham Jan 16, 2023
ecc2c08
clean and clarify
Robert-Cunningham Jan 16, 2023
66179b9
passes tests
Robert-Cunningham Jan 16, 2023
6e61761
merge upstream changes
Robert-Cunningham Jan 17, 2023
a1bee1c
finish merge
Robert-Cunningham Jan 17, 2023
d313486
remove comment churn
Robert-Cunningham Jan 17, 2023
5c07123
remove last comment
Robert-Cunningham Jan 17, 2023
37aba45
add comment to address review
Robert-Cunningham Jan 21, 2023
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
10 changes: 5 additions & 5 deletions compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//! `tcx.inherent_impls(def_id)`). That value, however,
//! is computed by selecting an idea from this table.

use hir::def_id::LOCAL_CRATE;
use hir::{ItemId, OwnerId};
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
Expand All @@ -19,8 +21,8 @@ use rustc_span::Span;
/// On-demand query: yields a map containing all types mapped to their inherent impls.
pub fn crate_inherent_impls(tcx: TyCtxt<'_>, (): ()) -> CrateInherentImpls {
let mut collect = InherentCollect { tcx, impls_map: Default::default() };
for id in tcx.hir().items() {
collect.check_item(id);
for id in tcx.impls_in_crate(LOCAL_CRATE) {
collect.check_item(ItemId { owner_id: OwnerId { def_id: id.expect_local() } });
}
collect.impls_map
}
Expand Down Expand Up @@ -177,9 +179,7 @@ impl<'tcx> InherentCollect<'tcx> {
}

fn check_item(&mut self, id: hir::ItemId) {
if !matches!(self.tcx.def_kind(id.owner_id), DefKind::Impl) {
return;
}
debug_assert!(matches!(self.tcx.def_kind(id.owner_id), DefKind::Impl));

let item = self.tcx.hir().item(id);
let hir::ItemKind::Impl(hir::Impl { of_trait: None, self_ty: ty, ref items, .. }) = item.kind else {
Expand Down
78 changes: 32 additions & 46 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use crate::rmeta::table::TableBuilder;
use crate::rmeta::*;

use rustc_ast::Attribute;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
use rustc_data_structures::memmap::{Mmap, MmapMut};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::{join, par_iter, Lrc, ParallelIterator};
use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_hir as hir;
Expand Down Expand Up @@ -1896,42 +1895,27 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let mut fx_hash_map: FxHashMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>> =
FxHashMap::default();

for id in tcx.hir().items() {
if matches!(tcx.def_kind(id.owner_id), DefKind::Impl) {
if let Some(trait_ref) = tcx.impl_trait_ref(id.owner_id) {
let trait_ref = trait_ref.subst_identity();

let simplified_self_ty = fast_reject::simplify_type(
self.tcx,
trait_ref.self_ty(),
TreatParams::AsInfer,
);

fx_hash_map
.entry(trait_ref.def_id)
.or_default()
.push((id.owner_id.def_id.local_def_index, simplified_self_ty));
}
for id in tcx.impls_in_crate(LOCAL_CRATE) {
if let Some(trait_ref) = tcx.impl_trait_ref(id) {
let trait_ref = trait_ref.subst_identity();

let simplified_self_ty =
fast_reject::simplify_type(self.tcx, trait_ref.self_ty(), TreatParams::AsInfer);

fx_hash_map
.entry(trait_ref.def_id)
.or_default()
.push((id.expect_local().local_def_index, simplified_self_ty));
}
}

let mut all_impls: Vec<_> = fx_hash_map.into_iter().collect();

// Bring everything into deterministic order for hashing
all_impls.sort_by_cached_key(|&(trait_def_id, _)| tcx.def_path_hash(trait_def_id));
let all_impls: Vec<_> = fx_hash_map.into_iter().collect();

let all_impls: Vec<_> = all_impls
.into_iter()
.map(|(trait_def_id, mut impls)| {
// Bring everything into deterministic order for hashing
impls.sort_by_cached_key(|&(index, _)| {
tcx.hir().def_path_hash(LocalDefId { local_def_index: index })
});

TraitImpls {
trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
impls: self.lazy_array(&impls),
}
.map(|(trait_def_id, impls)| TraitImpls {
trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
impls: self.lazy_array(&impls),
})
.collect();

Expand All @@ -1942,22 +1926,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
debug!("EncodeContext::encode_traits_and_impls()");
empty_proc_macro!(self);
let tcx = self.tcx;
let mut all_impls: Vec<_> = tcx.crate_inherent_impls(()).incoherent_impls.iter().collect();
tcx.with_stable_hashing_context(|mut ctx| {
all_impls.sort_by_cached_key(|&(&simp, _)| {
let mut hasher = StableHasher::new();
simp.hash_stable(&mut ctx, &mut hasher);
hasher.finish::<Fingerprint>()
})
});
let all_impls: Vec<_> = tcx.crate_inherent_impls(()).incoherent_impls.iter().collect();
let all_impls: Vec<_> = all_impls
.into_iter()
.map(|(&simp, impls)| {
let mut impls: Vec<_> =
let impls: Vec<_> =
impls.into_iter().map(|def_id| def_id.local_def_index).collect();
impls.sort_by_cached_key(|&local_def_index| {
tcx.hir().def_path_hash(LocalDefId { local_def_index })
});

IncoherentImpls { self_ty: simp, impls: self.lazy_array(impls) }
})
Expand Down Expand Up @@ -2279,10 +2253,22 @@ pub fn provide(providers: &mut Providers) {
}
}

// Bring everything into deterministic order.
traits.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id));
// We don't need to sort, since the default order is source-code order.
tcx.arena.alloc_slice(&traits)
},
impls_in_crate: |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);

let mut impls = Vec::new();
for id in tcx.hir().items() {
if matches!(tcx.def_kind(id.owner_id), DefKind::Impl) {
impls.push(id.owner_id.to_def_id())
}
}

// We don't need to sort, since the default order is source-code order.
tcx.arena.alloc_slice(&impls)
},

..*providers
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,13 @@ rustc_queries! {
separate_provide_extern
}

/// A list of all impls in a crate.
query impls_in_crate(_: CrateNum) -> &'tcx [DefId] {
eval_always
desc { "fetching trait to impl map in a crate" }
separate_provide_extern
}

/// The list of symbols exported from the given crate.
///
/// - All names contained in `exported_symbols(cnum)` are guaranteed to
Expand Down
15 changes: 10 additions & 5 deletions src/tools/clippy/clippy_lints/src/same_name_method.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use clippy_utils::diagnostics::span_lint_hir_and_then;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{HirId, Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_hir::{HirId, Impl, ItemId, ItemKind, Node, OwnerId, Path, QPath, TraitRef, TyKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::AssocKind;
use rustc_session::{declare_lint_pass, declare_tool_lint};
Expand Down Expand Up @@ -51,10 +52,14 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
let mut map = FxHashMap::<Res, ExistingName>::default();

for id in cx.tcx.hir().items() {
if matches!(cx.tcx.def_kind(id.owner_id), DefKind::Impl)
&& let item = cx.tcx.hir().item(id)
&& let ItemKind::Impl(Impl {
for id in cx.tcx.impls_in_crate(LOCAL_CRATE) {
let id = ItemId { owner_id: OwnerId { def_id: id.expect_local() } };
Comment on lines +55 to +56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use a helper method in rustc_middle::hir::map.


debug_assert!(matches!(cx.tcx.def_kind(id.owner_id), DefKind::Impl));
// impls_in_crate returns only items with DefKind::Impl, so this will always be true.

let item = cx.tcx.hir().item(id);
if let ItemKind::Impl(Impl {
items,
of_trait,
self_ty,
Expand Down