Rollup merge of #116543 - ouz-a:crate_return_vec, r=oli-obk
In smir `find_crates` returns `Vec<Crate>` instead of `Option<Crate>` Addresses https://github.com/rust-lang/project-stable-mir/issues/40 r? `@oli-obk`
This commit is contained in:
commit
2266e79421
@ -31,11 +31,18 @@ impl<'tcx> Context for Tables<'tcx> {
|
|||||||
self.tcx.crates(()).iter().map(|crate_num| smir_crate(self.tcx, *crate_num)).collect()
|
self.tcx.crates(()).iter().map(|crate_num| smir_crate(self.tcx, *crate_num)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_crate(&self, name: &str) -> Option<stable_mir::Crate> {
|
fn find_crates(&self, name: &str) -> Vec<stable_mir::Crate> {
|
||||||
[LOCAL_CRATE].iter().chain(self.tcx.crates(()).iter()).find_map(|crate_num| {
|
let crates: Vec<stable_mir::Crate> = [LOCAL_CRATE]
|
||||||
let crate_name = self.tcx.crate_name(*crate_num).to_string();
|
.iter()
|
||||||
(name == crate_name).then(|| smir_crate(self.tcx, *crate_num))
|
.chain(self.tcx.crates(()).iter())
|
||||||
})
|
.map(|crate_num| {
|
||||||
|
let crate_name = self.tcx.crate_name(*crate_num).to_string();
|
||||||
|
(name == crate_name).then(|| smir_crate(self.tcx, *crate_num))
|
||||||
|
})
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|c| c)
|
||||||
|
.collect();
|
||||||
|
crates
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name_of_def_id(&self, def_id: stable_mir::DefId) -> String {
|
fn name_of_def_id(&self, def_id: stable_mir::DefId) -> String {
|
||||||
|
@ -125,9 +125,9 @@ pub fn local_crate() -> Crate {
|
|||||||
with(|cx| cx.local_crate())
|
with(|cx| cx.local_crate())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Try to find a crate with the given name.
|
/// Try to find a crate or crates if multiple crates exist from given name.
|
||||||
pub fn find_crate(name: &str) -> Option<Crate> {
|
pub fn find_crates(name: &str) -> Vec<Crate> {
|
||||||
with(|cx| cx.find_crate(name))
|
with(|cx| cx.find_crates(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Try to find a crate with the given name.
|
/// Try to find a crate with the given name.
|
||||||
@ -174,7 +174,7 @@ pub trait Context {
|
|||||||
fn external_crates(&self) -> Vec<Crate>;
|
fn external_crates(&self) -> Vec<Crate>;
|
||||||
|
|
||||||
/// Find a crate with the given name.
|
/// Find a crate with the given name.
|
||||||
fn find_crate(&self, name: &str) -> Option<Crate>;
|
fn find_crates(&self, name: &str) -> Vec<Crate>;
|
||||||
|
|
||||||
/// Prints the name of given `DefId`
|
/// Prints the name of given `DefId`
|
||||||
fn name_of_def_id(&self, def_id: DefId) -> String;
|
fn name_of_def_id(&self, def_id: DefId) -> String;
|
||||||
|
@ -38,8 +38,8 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
|
|||||||
let items = stable_mir::all_local_items();
|
let items = stable_mir::all_local_items();
|
||||||
assert!(get_item(&items, (DefKind::Fn, "foo::bar")).is_some());
|
assert!(get_item(&items, (DefKind::Fn, "foo::bar")).is_some());
|
||||||
|
|
||||||
// Find the `std` crate.
|
// Find the `std` crate and assert that there is only one of it.
|
||||||
assert!(stable_mir::find_crate("std").is_some());
|
assert!(stable_mir::find_crates("std").len() == 1);
|
||||||
|
|
||||||
let bar = get_item(&items, (DefKind::Fn, "bar")).unwrap();
|
let bar = get_item(&items, (DefKind::Fn, "bar")).unwrap();
|
||||||
let body = bar.body();
|
let body = bar.body();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user