6
6
// pointer.
7
7
fn write_to_ret (condition : bool ) -> Data {
8
8
let f = get_data ();
9
- if condition { abort (); }
9
+ XXX
10
10
f
11
11
}
12
12
13
- fn write_to_ref1 (condition : bool , result : & mut Data ) {
13
+ fn write_to_ref (condition : bool , result : & mut Data ) {
14
14
let f = get_data ();
15
- if condition { abort (); }
16
- * result = f ;
17
- }
18
-
19
- fn write_to_ref2 (condition : bool , result : & mut Data ) {
20
- let f = get_data ();
21
- if condition {
22
- /* something that does not abort but does not panic */
23
- }
15
+ XXX
24
16
* result = f ;
25
17
}
26
18
27
19
fn write_to_raw1 (condition : bool , result : * mut Data ) {
28
20
let f = get_data ();
29
- if condition { abort (); }
21
+ XXX
30
22
unsafe { * result = f ; } // maybe not, did not observe a write
31
23
}
32
24
33
25
fn write_to_raw1a (condition : bool , result : & mut Data ) {
34
26
let f = get_data ();
35
- if condition { abort (); }
27
+ XXX
36
28
* result = f ; // but not this
37
29
38
30
let f = get_data ();
39
- if condition { abort (); }
31
+ XXX
40
32
* result = f ; // can NRVO this one
41
33
}
42
34
43
35
fn write_to_raw2 (condition : bool , result : * mut Data ) {
44
36
let result = unsafe { & mut * result };
45
37
let f = get_data ();
46
- if condition { abort (); }
38
+ XXX
47
39
* result = f ;
48
40
}
49
41
50
42
fn write_to_raw3 (condition : bool , result : * mut Data ) {
51
43
let f = get_data ();
52
- if condition { abort (); }
44
+ XXX
53
45
unsafe { * result = f ; }
54
46
55
47
let result = & mut * result ; // this cannot "be moved"
56
48
let f = get_data ();
57
- if condition { abort (); }
49
+ XXX
58
50
* result = f ; // can only be promoted as high as the `&mut *result`
59
51
}
60
52
```
@@ -68,13 +60,18 @@ eventual location. The danger typically arises because that write
68
60
would be visible (potentially) earlier than it was "supposed" to
69
61
happen -- when is that ok?
70
62
71
- These examples mostly include a conditional abort in between: the
72
- assumption is that the compiler understands that abort * aborts* and
73
- does not return.
74
-
75
- Some factors that are relevant:
76
-
77
- - could other threads observe?
63
+ These examples include some unknown behavior XXX in between. Typically
64
+ speaking, whether the optimization can be performed will depend (in
65
+ each case) on what this XXX is:
66
+
67
+ - For example, it may contain a * conditional abort* . In that case, if
68
+ we optimize the write to occur early, we don't know that the write
69
+ would even ever execute!
70
+ - Similarly, if XXX might unwind, that could lead to potential reads
71
+ of the ultimate destination, in some cases.
72
+ - Finally, the XXX might read from the ultimate destination through an
73
+ alias (in some cases, that read may be considered undefined
74
+ behavior, however).
78
75
79
76
80
77
### Source
0 commit comments