summary refs log tree commit diff
path: root/src/test/run-pass/operator-overloading-leaks.rs
blob: 20f6201bd6bc61a99bea8a9b272f29644199600c (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// The cases commented as "Leaks" need to not leak. Issue #2581

impl methods<T: copy> for ~[T] {
    fn -(x: &[T]) -> ~[T] {
        ~[x[0], x[0], x[0]]
    }

    fn foo(x: &[T]) -> ~[T] {
        ~[x[0], x[0], x[0]]
    }
}

impl methods<T: copy> for ~T {
    fn +(rhs: ~T) -> ~T {
        rhs
    }
}

impl methods for ~int {
    fn -(rhs: ~int) -> ~int {
        ~(*self - *rhs)
    }
}

impl methods for @int {
    fn +(rhs: @int) -> @int {
        @(*self + *rhs)
    }
}

fn main() {
    // leaks
    let mut bar = ~[1, 2, 3];
    bar -= ~[3, 2, 1];
    bar -= ~[4, 5, 6];
    
    io::println(#fmt("%?", bar));

    // okay
    let mut bar = ~[1, 2, 3];
    bar = bar.foo(~[3, 2, 1]);
    bar = bar.foo(~[4, 5, 6]);

    io::println(#fmt("%?", bar));

    // okay
    let mut bar = ~[1, 2, 3];
    bar = bar - ~[3, 2, 1];
    bar = bar - ~[4, 5, 6];

    io::println(#fmt("%?", bar));

    // Leaks
    let mut bar = ~1;
    bar += ~2;
    bar += ~3;
    
    io:: println(#fmt("%?", bar));

    // Leaks
    let mut bar = ~1;
    bar -= ~2;
    bar -= ~3;
    
    io:: println(#fmt("%?", bar));

    // Leaks
    let mut bar = @1;
    bar += @2;
    bar += @3;
    
    io:: println(#fmt("%?", bar));

}