]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/return-local-binding-from-desugaring.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / return-local-binding-from-desugaring.rs
1 // To avoid leaking the names of local bindings from expressions like for loops, #60984
2 // explicitly ignored them, but an assertion that `LocalKind::Var` *must* have a name would
3 // trigger an ICE. Before this change, this file's output would be:
4 // ```
5 // error[E0515]: cannot return value referencing local variable `__next`
6 //   --> return-local-binding-from-desugaring.rs:LL:CC
7 //    |
8 // LL |     for ref x in xs {
9 //    |         ----- `__next` is borrowed here
10 // ...
11 // LL |     result
12 //    |     ^^^^^^ returns a value referencing data owned by the current function
13 // ```
14 // FIXME: ideally `LocalKind` would carry more information to more accurately explain the problem.
15
16 use std::collections::HashMap;
17 use std::hash::Hash;
18
19 fn group_by<I, F, T>(xs: &mut I, f: F) -> HashMap<T, Vec<&I::Item>>
20 where
21     I: Iterator,
22     F: Fn(&I::Item) -> T,
23     T: Eq + Hash,
24 {
25     let mut result = HashMap::new();
26     for ref x in xs {
27         let key = f(x);
28         result.entry(key).or_insert(Vec::new()).push(x);
29     }
30     result //~ ERROR cannot return value referencing temporary value
31 }
32
33 fn main() {}