summaryrefslogtreecommitdiff
path: root/tests/control-flow/bug761267-2.vala
blob: 0bafe04d4e907aa8a1c1d344f67f6bbb2143b220 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
[Compact]
class Bar {
	public int k = 17;
}

class Foo {
	public int i = 42;

	public int j { get ; set ; default = 23; }

	public Bar b = new Bar ();

	public unowned int foo () {
		return i;
	}

	public Bar bar () {
		return new Bar ();
	}

	public Bar[] bars (int n) {
		Bar[] a = new Bar[n];
		for (int i = 0; i < n; i++) {
			a[i] = new Bar () { k = i };
		}
		return a;
	}

	public int[] seq (int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = i;
		}
		return a;
	}
}

void bar (Foo? f) {
	{
		// null method call
		Bar? b = f?.bar ();
		assert (b == null);
	}
	{
		// null element access
		int[]? a = null;
		int? i = a?[3];
		assert (i == null);
	}
	{
		// null slice expression
		int[]? a = null;
		int[]? s = a?[1:3];
		assert (s == null);
	}
}

void baz (Foo? f) {
	{
		// non-null element access
		int[]? a = f.seq (10);
		int? i = a?[3];
		assert (i == 3);
	}
	{
		// non-null slice access
		int[]? a = f.seq (10);
		int[]? s = a?[1:3];
		assert (s[0] == 1);
	}
	{
		// ownership transfer through member access
		Bar? b = (owned) f?.b;
		assert (b.k == 17);
	}
	{
		// ownership transfer through method call
		Bar? b = f?.bar ();
		assert (b.k == 17);
	}
	{
		// ownership transfer through element access
		Bar[]? a = f?.bars (10);
		Bar? b = (owned) a?[3];
		assert (b.k == 3);
	}
	{
		// member access to non-nullable unowned value type
		int? j = f?.j;
		assert (j == 23);
	}
	{
		// method call returns non-nullable unowned value type
		int? i = f?.foo ();
		assert (i == 42);
	}
}

void main () {
	{
		bar (null);
		baz (new Foo ());
	}
	{
		// owned inner expression
		int? i = new Foo ()?.i;
		assert (i == 42);
	}
}