Skip to content

Commit beaebe5

Browse files
authored
Extend is_fn_call to find assoc reft bodies for FnOnce::call_once, FnMut::call_mut (#1525)
1 parent 2df5417 commit beaebe5

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

crates/flux-middle/src/global_env.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,22 @@ impl<'genv, 'tcx> GlobalEnv<'genv, 'tcx> {
497497
== def_id
498498
}
499499

500-
/// Returns whether `def_id` is the `call` method in the `Fn` trait.
500+
/// Returns whether `def_id` is the `call` method in the `Fn` trait,
501+
/// the `call_mut` method in the `FnMut` trait,
502+
/// or the `call_once` method in the `FnOnce` trait.
501503
pub fn is_fn_call(&self, def_id: DefId) -> bool {
504+
let methods_and_names = [
505+
(LangItem::Fn, sym::call),
506+
(LangItem::FnMut, sym::call_mut),
507+
(LangItem::FnOnce, sym::call_once),
508+
];
502509
let tcx = self.tcx();
503510
let Some(assoc_item) = tcx.opt_associated_item(def_id) else { return false };
504511
let Some(trait_id) = assoc_item.trait_container(tcx) else { return false };
505-
assoc_item.name() == sym::call && tcx.is_lang_item(trait_id, LangItem::Fn)
512+
513+
methods_and_names.iter().any(|(lang_item, method_name)| {
514+
assoc_item.name() == *method_name && tcx.is_lang_item(trait_id, *lang_item)
515+
})
506516
}
507517

508518
/// Iterator over all local def ids that are not a extern spec
File renamed without changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#[flux::sig(fn(f: F) -> i32)]
2+
#[flux::no_panic_if(true)]
3+
fn foo<F: Fn(i32) -> i32>(f: F) -> i32 {
4+
f(3) //~ ERROR may panic
5+
}
6+
7+
#[flux::sig(fn(f: F) -> i32)]
8+
#[flux::no_panic_if(true)]
9+
fn foo2<F: FnOnce(i32) -> i32>(f: F) -> i32 {
10+
f(3) //~ ERROR may panic
11+
}
12+
13+
#[flux::sig(fn(f: F) -> i32)]
14+
#[flux::no_panic_if(true)]
15+
fn foo3<F: FnMut(i32) -> i32>(mut f: F) -> i32 {
16+
f(3) //~ ERROR may panic
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#[flux::sig(fn(f: F) -> i32)]
2+
#[flux::no_panic_if(F::no_panic())]
3+
fn foo<F: Fn(i32) -> i32>(f: F) -> i32 {
4+
f(3)
5+
}
6+
7+
#[flux::sig(fn(f: F) -> i32)]
8+
#[flux::no_panic_if(F::no_panic())]
9+
fn foo2<F: FnOnce(i32) -> i32>(f: F) -> i32 {
10+
f(3)
11+
}
12+
13+
#[flux::sig(fn(f: F) -> i32)]
14+
#[flux::no_panic_if(F::no_panic())]
15+
fn foo3<F: FnMut(i32) -> i32>(mut f: F) -> i32 {
16+
f(3)
17+
}

0 commit comments

Comments
 (0)