Skip to content

Commit 987e1e0

Browse files
committed
Add trait implementations to the list of items
The only thing missing is incoherent implementations for primitive types
1 parent e4a47ad commit 987e1e0

File tree

2 files changed

+31
-42
lines changed

2 files changed

+31
-42
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::mir;
1414
use rustc_middle::mir::interpret::AllocId;
1515
use rustc_middle::ty::data_structures::HashSet;
1616
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
17-
use rustc_span::def_id::{CRATE_DEF_INDEX, CrateNum, DefId, LOCAL_CRATE};
17+
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1818
use stable_mir::abi::Layout;
1919
use stable_mir::mir::mono::{InstanceDef, StaticDef};
2020
use stable_mir::ty::{FnDef, MirConstId, Span, TyConstId};
@@ -97,34 +97,25 @@ impl<'tcx> Tables<'tcx> {
9797
where
9898
F: Fn(&mut Tables<'tcx>, TyCtxt<'tcx>, DefId) -> Option<T>,
9999
{
100-
let crate_def = rustc_span::def_id::DefId { index: CRATE_DEF_INDEX, krate };
100+
let crate_def = krate.as_def_id();
101101
let mut queue = vec![crate_def];
102+
queue.extend_from_slice(tcx.trait_impls_in_crate(krate));
102103
let mut visited = HashSet::default();
103104
let mut result = vec![];
104-
while let Some(next) = queue.pop() {
105-
for def_id in tcx.module_children(next).iter().filter_map(|item| item.res.opt_def_id())
106-
{
107-
if !visited.contains(&def_id) {
108-
visited.insert(def_id);
109-
result.extend(func(self, tcx, def_id));
110-
match tcx.def_kind(def_id) {
111-
DefKind::Mod | DefKind::ForeignMod => queue.push(def_id),
112-
DefKind::Struct | DefKind::Enum | DefKind::Union => {
113-
for associated_item in tcx
114-
.inherent_impls(def_id)
115-
.iter()
116-
.flat_map(|impl_id| tcx.associated_item_def_ids(impl_id))
117-
{
118-
result.extend(func(self, tcx, *associated_item));
119-
}
120-
}
121-
DefKind::Trait => {
122-
for associated_item in tcx.associated_item_def_ids(def_id) {
123-
result.extend(func(self, tcx, *associated_item));
124-
}
125-
}
126-
_ => {}
105+
while let Some(def_id) = queue.pop() {
106+
if visited.insert(def_id) {
107+
result.extend(func(self, tcx, def_id));
108+
match tcx.def_kind(def_id) {
109+
DefKind::Mod | DefKind::ForeignMod | DefKind::Trait => queue.extend(
110+
tcx.module_children(def_id).iter().filter_map(|item| item.res.opt_def_id()),
111+
),
112+
DefKind::Impl { .. } => queue.extend(
113+
tcx.associated_items(def_id).in_definition_order().map(|item| item.def_id),
114+
),
115+
DefKind::Struct | DefKind::Enum | DefKind::Union => {
116+
queue.extend(tcx.inherent_impls(def_id));
127117
}
118+
_ => {}
128119
}
129120
}
130121
}

tests/ui-fulldeps/stable-mir/check_crate_defs.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern crate stable_mir;
1818

1919
use rustc_smir::rustc_internal;
2020
use stable_mir::CrateDef;
21+
use std::collections::HashSet;
2122
use std::io::Write;
2223
use std::ops::ControlFlow;
2324

@@ -47,7 +48,12 @@ fn test_stable_mir() -> ControlFlow<()> {
4748
let core = stable_mir::find_crates("core").pop().expect("Cannot find `core` crate");
4849
contains(
4950
&core.fn_defs(),
50-
&["std::fmt::Debug::fmt", "std::option::Option::<T>::is_some", "std::ptr::swap"],
51+
&[
52+
"std::fmt::Debug::fmt",
53+
"std::option::Option::<T>::is_some",
54+
"std::ptr::swap",
55+
"<std::slice::Iter<'a, T> as std::iter::Iterator>::next",
56+
],
5157
);
5258
// Ensure nothing crashes. There is no public static in core that we can test here.
5359
let _ = core.statics();
@@ -58,25 +64,17 @@ fn test_stable_mir() -> ControlFlow<()> {
5864
/// Check if the list of definitions matches the expected list.
5965
/// Note that order doesn't matter.
6066
fn check_items<T: CrateDef>(items: &[T], expected: &[&str]) {
61-
let item_names: Vec<_> = items.iter().map(|item| item.name()).collect();
62-
let unexpected: Vec<_> =
63-
item_names.iter().filter(|item| !expected.contains(&item.as_str())).collect();
64-
assert!(unexpected.is_empty(), "Unexpected items: {:?}", unexpected);
65-
assert_eq!(items.len(), expected.len(), "Expected items: {expected:?}\nFound: {item_names:?}");
67+
let expected: HashSet<_> = expected.iter().map(|s| s.to_string()).collect();
68+
let item_names: HashSet<_> = items.iter().map(|item| item.name()).collect();
69+
assert_eq!(item_names, expected);
6670
}
6771

6872
/// Check that the list contains the expected items.
69-
fn contains<T: CrateDef>(items: &[T], expected: &[&str]) {
70-
let item_names: Vec<_> = items.iter().map(|item| item.name()).collect();
71-
let found: Vec<_> =
72-
item_names.iter().filter(|item| expected.contains(&item.as_str())).collect();
73-
assert!(items.len() >= expected.len(), "Missing items: {:?}", item_names);
74-
assert_eq!(
75-
found.len(),
76-
expected.len(),
77-
"Expected items: {:?}\nFound: {found:?}",
78-
&item_names[0..10]
79-
);
73+
fn contains<T: CrateDef + std::fmt::Debug>(items: &[T], expected: &[&str]) {
74+
let expected: HashSet<_> = expected.iter().map(|s| s.to_string()).collect();
75+
let item_names = items.iter().map(|item| item.name()).collect();
76+
let not_found: Vec<_> = expected.difference(&item_names).collect();
77+
assert!(not_found.is_empty(), "Missing items: {:?}", not_found);
8078
}
8179

8280
/// This test will generate and analyze a dummy crate using the stable mir.

0 commit comments

Comments
 (0)