summaryrefslogtreecommitdiffstats
path: root/tests/ui/borrowck/return-local-binding-from-desugaring.rs
blob: c6643edf5a9547f2f6393e4f3df30ccde3142b02 (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
25
26
27
28
29
30
31
32
33
// To avoid leaking the names of local bindings from expressions like for loops, #60984
// explicitly ignored them, but an assertion that `LocalKind::Var` *must* have a name would
// trigger an ICE. Before this change, this file's output would be:
// ```
// error[E0515]: cannot return value referencing local variable `__next`
//   --> return-local-binding-from-desugaring.rs:LL:CC
//    |
// LL |     for ref x in xs {
//    |         ----- `__next` is borrowed here
// ...
// LL |     result
//    |     ^^^^^^ returns a value referencing data owned by the current function
// ```
// FIXME: ideally `LocalKind` would carry more information to more accurately explain the problem.

use std::collections::HashMap;
use std::hash::Hash;

fn group_by<I, F, T>(xs: &mut I, f: F) -> HashMap<T, Vec<&I::Item>>
where
    I: Iterator,
    F: Fn(&I::Item) -> T,
    T: Eq + Hash,
{
    let mut result = HashMap::new();
    for ref x in xs {
        let key = f(x);
        result.entry(key).or_insert(Vec::new()).push(x);
    }
    result //~ ERROR cannot return value referencing temporary value
}

fn main() {}