mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-24 12:03:31 +00:00
Add test for unsafe union field access highlighting
This commit is contained in:
parent
f1d507270c
commit
a6532905a9
2 changed files with 22 additions and 0 deletions
|
@ -275,6 +275,11 @@ fn test_unsafe_highlighting() {
|
|||
r#"
|
||||
unsafe fn unsafe_fn() {}
|
||||
|
||||
union Union {
|
||||
a: u32,
|
||||
b: f32,
|
||||
}
|
||||
|
||||
struct HasUnsafeFn;
|
||||
|
||||
impl HasUnsafeFn {
|
||||
|
@ -283,8 +288,14 @@ impl HasUnsafeFn {
|
|||
|
||||
fn main() {
|
||||
let x = &5 as *const usize;
|
||||
let u = Union { b: 0 };
|
||||
unsafe {
|
||||
unsafe_fn();
|
||||
let b = u.b;
|
||||
match u {
|
||||
Union { b: 0 } => (),
|
||||
Union { a } => (),
|
||||
}
|
||||
HasUnsafeFn.unsafe_method();
|
||||
let y = *(x);
|
||||
let z = -x;
|
||||
|
|
|
@ -37,6 +37,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
|||
</style>
|
||||
<pre><code><span class="keyword unsafe">unsafe</span> <span class="keyword">fn</span> <span class="function declaration unsafe">unsafe_fn</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span><span class="punctuation">}</span>
|
||||
|
||||
<span class="keyword">union</span> <span class="union declaration">Union</span> <span class="punctuation">{</span>
|
||||
<span class="field declaration">a</span><span class="punctuation">:</span> <span class="builtin_type">u32</span><span class="punctuation">,</span>
|
||||
<span class="field declaration">b</span><span class="punctuation">:</span> <span class="builtin_type">f32</span><span class="punctuation">,</span>
|
||||
<span class="punctuation">}</span>
|
||||
|
||||
<span class="keyword">struct</span> <span class="struct declaration">HasUnsafeFn</span><span class="punctuation">;</span>
|
||||
|
||||
<span class="keyword">impl</span> <span class="struct">HasUnsafeFn</span> <span class="punctuation">{</span>
|
||||
|
@ -45,8 +50,14 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
|||
|
||||
<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">let</span> <span class="variable declaration">u</span> <span class="operator">=</span> <span class="union">Union</span> <span class="punctuation">{</span> <span class="unresolved_reference">b</span><span class="punctuation">:</span> <span class="numeric_literal">0</span> <span class="punctuation">}</span><span class="punctuation">;</span>
|
||||
<span class="keyword unsafe">unsafe</span> <span class="punctuation">{</span>
|
||||
<span class="function unsafe">unsafe_fn</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
|
||||
<span class="keyword">let</span> <span class="variable declaration">b</span> <span class="operator">=</span> <span class="variable">u</span><span class="punctuation">.</span><span class="unresolved_reference">b</span><span class="punctuation">;</span>
|
||||
<span class="keyword control">match</span> <span class="variable">u</span> <span class="punctuation">{</span>
|
||||
<span class="union">Union</span> <span class="punctuation">{</span> <span class="unresolved_reference">b</span><span class="punctuation">:</span> <span class="numeric_literal">0</span> <span class="punctuation">}</span> <span class="operator">=></span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">,</span>
|
||||
<span class="union">Union</span> <span class="punctuation">{</span> <span class="variable declaration">a</span> <span class="punctuation">}</span> <span class="operator">=></span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">,</span>
|
||||
<span class="punctuation">}</span>
|
||||
<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>
|
||||
|
|
Loading…
Reference in a new issue