]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/mir_augmented_assignments.rs
New upstream version 1.19.0+dfsg1
[rustc.git] / src / test / run-pass / mir_augmented_assignments.rs
CommitLineData
7453a54e
SL
1// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
7453a54e
SL
11use std::mem;
12use std::ops::{
13 AddAssign, BitAndAssign, BitOrAssign, BitXorAssign, DivAssign, MulAssign, RemAssign,
14 ShlAssign, ShrAssign, SubAssign,
15};
16
17#[derive(Debug, PartialEq)]
18struct Int(i32);
19
20struct Slice([i32]);
21
22impl Slice {
23 fn new(slice: &mut [i32]) -> &mut Slice {
24 unsafe {
25 mem::transmute(slice)
26 }
27 }
28}
29
30fn main() {
31 main_mir();
32}
33
7453a54e
SL
34fn main_mir() {
35 let mut x = Int(1);
36
37 x += Int(2);
38 assert_eq!(x, Int(0b11));
39
40 x &= Int(0b01);
41 assert_eq!(x, Int(0b01));
42
43 x |= Int(0b10);
44 assert_eq!(x, Int(0b11));
45
46 x ^= Int(0b01);
47 assert_eq!(x, Int(0b10));
48
49 x /= Int(2);
50 assert_eq!(x, Int(1));
51
52 x *= Int(3);
53 assert_eq!(x, Int(3));
54
55 x %= Int(2);
56 assert_eq!(x, Int(1));
57
58 // overloaded RHS
59 x <<= 1u8;
60 assert_eq!(x, Int(2));
61
62 x <<= 1u16;
63 assert_eq!(x, Int(4));
64
65 x >>= 1u8;
66 assert_eq!(x, Int(2));
67
68 x >>= 1u16;
69 assert_eq!(x, Int(1));
70
71 x -= Int(1);
72 assert_eq!(x, Int(0));
73
74 // indexed LHS
75 // FIXME(mir-drop): use the vec![..] macro
76 let mut v = Vec::new();
77 v.push(Int(1));
78 v.push(Int(2));
79 v[0] += Int(2);
80 assert_eq!(v[0], Int(3));
81
82 // unsized RHS
83 let mut array = [0, 1, 2];
84 *Slice::new(&mut array) += 1;
85 assert_eq!(array[0], 1);
86 assert_eq!(array[1], 2);
87 assert_eq!(array[2], 3);
88
89}
90
91impl AddAssign for Int {
7453a54e
SL
92 fn add_assign(&mut self, rhs: Int) {
93 self.0 += rhs.0;
94 }
95}
96
97impl BitAndAssign for Int {
7453a54e
SL
98 fn bitand_assign(&mut self, rhs: Int) {
99 self.0 &= rhs.0;
100 }
101}
102
103impl BitOrAssign for Int {
7453a54e
SL
104 fn bitor_assign(&mut self, rhs: Int) {
105 self.0 |= rhs.0;
106 }
107}
108
109impl BitXorAssign for Int {
7453a54e
SL
110 fn bitxor_assign(&mut self, rhs: Int) {
111 self.0 ^= rhs.0;
112 }
113}
114
115impl DivAssign for Int {
7453a54e
SL
116 fn div_assign(&mut self, rhs: Int) {
117 self.0 /= rhs.0;
118 }
119}
120
121impl MulAssign for Int {
7453a54e
SL
122 fn mul_assign(&mut self, rhs: Int) {
123 self.0 *= rhs.0;
124 }
125}
126
127impl RemAssign for Int {
7453a54e
SL
128 fn rem_assign(&mut self, rhs: Int) {
129 self.0 %= rhs.0;
130 }
131}
132
133impl ShlAssign<u8> for Int {
7453a54e
SL
134 fn shl_assign(&mut self, rhs: u8) {
135 self.0 <<= rhs;
136 }
137}
138
139impl ShlAssign<u16> for Int {
7453a54e
SL
140 fn shl_assign(&mut self, rhs: u16) {
141 self.0 <<= rhs;
142 }
143}
144
145impl ShrAssign<u8> for Int {
7453a54e
SL
146 fn shr_assign(&mut self, rhs: u8) {
147 self.0 >>= rhs;
148 }
149}
150
151impl ShrAssign<u16> for Int {
7453a54e
SL
152 fn shr_assign(&mut self, rhs: u16) {
153 self.0 >>= rhs;
154 }
155}
156
157impl SubAssign for Int {
7453a54e
SL
158 fn sub_assign(&mut self, rhs: Int) {
159 self.0 -= rhs.0;
160 }
161}
162
163impl AddAssign<i32> for Slice {
7453a54e
SL
164 fn add_assign(&mut self, rhs: i32) {
165 for lhs in &mut self.0 {
166 *lhs += rhs;
167 }
168 }
169}