Add test for accessing static mut

This commit is contained in:
Paul Daniel Faria 2020-08-06 19:58:37 -04:00
parent f1d507270c
commit 6be528da0d
2 changed files with 14 additions and 0 deletions

View File

@ -281,6 +281,12 @@ impl HasUnsafeFn {
unsafe fn unsafe_method(&self) {}
}
struct TypeForStaticMut {
a: u8
}
static mut global_mut: TypeForStaticMut = TypeForStaticMut { a: 0 };
fn main() {
let x = &5 as *const usize;
unsafe {
@ -288,6 +294,7 @@ fn main() {
HasUnsafeFn.unsafe_method();
let y = *(x);
let z = -x;
let a = global_mut.a;
}
}
"#

View File

@ -43,6 +43,12 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="keyword unsafe">unsafe</span> <span class="keyword">fn</span> <span class="function declaration unsafe">unsafe_method</span><span class="punctuation">(</span><span class="operator">&</span><span class="self_keyword">self</span><span class="punctuation">)</span> <span class="punctuation">{</span><span class="punctuation">}</span>
<span class="punctuation">}</span>
<span class="keyword">struct</span> <span class="struct declaration">TypeForStaticMut</span> <span class="punctuation">{</span>
<span class="field declaration">a</span><span class="punctuation">:</span> <span class="builtin_type">u8</span>
<span class="punctuation">}</span>
<span class="keyword">static</span> <span class="keyword">mut</span> <span class="static declaration mutable">global_mut</span><span class="punctuation">:</span> <span class="struct">TypeForStaticMut</span> <span class="operator">=</span> <span class="struct">TypeForStaticMut</span> <span class="punctuation">{</span> <span class="field">a</span><span class="punctuation">:</span> <span class="numeric_literal">0</span> <span class="punctuation">}</span><span class="punctuation">;</span>
<span class="keyword">fn</span> <span class="function declaration">main</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span>
<span class="keyword">let</span> <span class="variable declaration">x</span> <span class="operator">=</span> <span class="operator">&</span><span class="numeric_literal">5</span> <span class="keyword">as</span> <span class="keyword">*</span><span class="keyword">const</span> <span class="builtin_type">usize</span><span class="punctuation">;</span>
<span class="keyword unsafe">unsafe</span> <span class="punctuation">{</span>
@ -50,5 +56,6 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="struct">HasUnsafeFn</span><span class="punctuation">.</span><span class="function unsafe">unsafe_method</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
<span class="keyword">let</span> <span class="variable declaration">y</span> <span class="operator">=</span> <span class="operator unsafe">*</span><span class="punctuation">(</span><span class="variable">x</span><span class="punctuation">)</span><span class="punctuation">;</span>
<span class="keyword">let</span> <span class="variable declaration">z</span> <span class="operator">=</span> <span class="numeric_literal">-</span><span class="variable">x</span><span class="punctuation">;</span>
<span class="keyword">let</span> <span class="variable declaration">a</span> <span class="operator">=</span> <span class="static mutable">global_mut</span><span class="punctuation">.</span><span class="field">a</span><span class="punctuation">;</span>
<span class="punctuation">}</span>
<span class="punctuation">}</span></code></pre>