]> git.lizzy.rs Git - rust.git/blob - tests/ui/return/issue-82612-return-mutable-reference.rs
Rollup merge of #106692 - eggyal:mv-binary_heap.rs-binary_heap/mod.rs, r=Mark-Simulacrum
[rust.git] / tests / ui / return / issue-82612-return-mutable-reference.rs
1 // Regression test for #82612.
2
3 use std::marker::PhantomData;
4
5 pub trait SparseSetIndex {
6     fn sparse_set_index(&self) -> usize;
7 }
8 pub struct SparseArray<I, V = I> {
9     values: Vec<Option<V>>,
10     marker: PhantomData<I>,
11 }
12
13 impl<I: SparseSetIndex, V> SparseArray<I, V> {
14     pub fn get_or_insert_with(&mut self, index: I, func: impl FnOnce() -> V) -> &mut V {
15         let index = index.sparse_set_index();
16         if index < self.values.len() {
17             let value = unsafe { self.values.get_unchecked_mut(index) };
18             value.get_or_insert_with(func) //~ ERROR mismatched types
19         }
20         unsafe { self.values.get_unchecked_mut(index).as_mut().unwrap() }
21     }
22 }
23
24 fn main() {}