summaryrefslogtreecommitdiffstats
path: root/src/test/ui/return/issue-82612-return-mutable-reference.rs
blob: db0d08ddb915b1a9be64f3038b5aff9e1f4850b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Regression test for #82612.

use std::marker::PhantomData;

pub trait SparseSetIndex {
    fn sparse_set_index(&self) -> usize;
}
pub struct SparseArray<I, V = I> {
    values: Vec<Option<V>>,
    marker: PhantomData<I>,
}

impl<I: SparseSetIndex, V> SparseArray<I, V> {
    pub fn get_or_insert_with(&mut self, index: I, func: impl FnOnce() -> V) -> &mut V {
        let index = index.sparse_set_index();
        if index < self.values.len() {
            let value = unsafe { self.values.get_unchecked_mut(index) };
            value.get_or_insert_with(func) //~ ERROR mismatched types
        }
        unsafe { self.values.get_unchecked_mut(index).as_mut().unwrap() }
    }
}

fn main() {}