]> git.proxmox.com Git - rustc.git/blob - tests/ui/overloaded/overloaded-index-in-field.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / ui / overloaded / overloaded-index-in-field.rs
1 // run-pass
2 // Test using overloaded indexing when the "map" is stored in a
3 // field. This caused problems at some point.
4
5 use std::ops::Index;
6
7 struct Foo {
8 x: isize,
9 y: isize,
10 }
11
12 struct Bar {
13 foo: Foo
14 }
15
16 impl Index<isize> for Foo {
17 type Output = isize;
18
19 fn index(&self, z: isize) -> &isize {
20 if z == 0 {
21 &self.x
22 } else {
23 &self.y
24 }
25 }
26 }
27
28 trait Int {
29 fn get(self) -> isize;
30 fn get_from_ref(&self) -> isize;
31 fn inc(&mut self);
32 }
33
34 impl Int for isize {
35 fn get(self) -> isize { self }
36 fn get_from_ref(&self) -> isize { *self }
37 fn inc(&mut self) { *self += 1; }
38 }
39
40 fn main() {
41 let f = Bar { foo: Foo {
42 x: 1,
43 y: 2,
44 } };
45 assert_eq!(f.foo[1].get(), 2);
46 }