Skip to content

Allow &raw [mut | const] for union field #19867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions crates/hir-ty/src/diagnostics/unsafe_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ impl<'db> UnsafeVisitor<'db> {

return;
}
Expr::Field { .. } => {
if self.contains_union_field_access(*expr) {
// Walk the entire field access chain without triggering union field errors
self.walk_field_chain_for_raw_ptr(*expr);
return;
}
}
_ => (),
}
}
Expand Down Expand Up @@ -401,4 +408,43 @@ impl<'db> UnsafeVisitor<'db> {
}
}
}

fn contains_union_field_access(&mut self, expr: ExprId) -> bool {
match &self.body.exprs[expr] {
Expr::Field { expr: base_expr, .. } => {
// Check if this field access is from a union
if matches!(
self.infer.field_resolution(expr),
Some(Either::Left(FieldId { parent: VariantId::UnionId(_), .. }))
) {
true
} else {
// Recursively check the base expression
self.contains_union_field_access(*base_expr)
}
}
_ => false,
}
}

/// Walks a field access chain for raw pointer creation, avoiding union field access errors
fn walk_field_chain_for_raw_ptr(&mut self, expr: ExprId) {
match &self.body.exprs[expr] {
Expr::Field { expr: base_expr, .. } => {
// First, recursively handle the base expression
self.walk_field_chain_for_raw_ptr(*base_expr);

// Then handle any non-field child expressions of this field access
self.body.walk_child_exprs_without_pats(expr, |child| {
if child != *base_expr {
self.walk_expr(child);
}
});
}
_ => {
// We've reached the base expression (not a field access), walk it normally
self.walk_expr(expr);
}
}
}
}
70 changes: 70 additions & 0 deletions crates/ide-diagnostics/src/handlers/missing_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,76 @@ fn bar(mut v: Union2) {
)
}

#[test]
fn raw_deref_on_union_field() {
check_diagnostics(
r#"
fn main() {
union U1 {
a: u8
}
let x = U1 { a: 3 };

let a = x.a;
// ^^^ 💡 error: access to union field is unsafe and requires an unsafe function or block


let b = &raw const x.a;

let tmp = Vec::from([1, 2, 3]);

let c = &raw const tmp[x.a];
// ^^^ 💡 error: access to union field is unsafe and requires an unsafe function or block

union URef {
p: &'static mut i32,
}

fn deref_union_field(u: URef) {
// Not an assignment but an access to the union field!
*(u.p) = 13;
// ^^^ 💡 error: access to union field is unsafe and requires an unsafe function or block
}
}
"#,
)
}

#[test]
fn union_fields_chain_is_allowed() {
check_diagnostics(
r#"
union Inner {
a: u8,
}

union MoreInner {
moreinner: ManuallyDrop<Inner>,
}

union LessOuter {
lessouter: ManuallyDrop<MoreInner>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ManuallyDrop is not resolved, just include the type without it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it mean the test is not correct in it's current state? i will look into it little bit later today

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as in "it does not test the thing it should".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, thanks for this catch, but, can i add this import into test somehow? as far as i remember it wont work without ManuallyDrop

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you can add //- minicore: manually_drop, deref at the top of the test and also use core::mem::ManuallyDrop.

}

union Outer {
outer: ManuallyDrop<LessOuter>,
}

fn main() {
let super_outer = Outer {
outer: ManuallyDrop::new(LessOuter {
lessouter: ManuallyDrop::new(MoreInner {
moreinner: ManuallyDrop::new(Inner { a: 42 }),
}),
}),
};

let ptr = &raw const super_outer.outer.lessouter.moreinner.a;
}
"#,
);
}

#[test]
fn raw_ref_reborrow_is_safe() {
check_diagnostics(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@

<span class="variable">u</span><span class="operator">.</span><span class="field unsafe">field</span><span class="semicolon">;</span>
<span class="operator">&</span><span class="variable">u</span><span class="operator">.</span><span class="field unsafe">field</span><span class="semicolon">;</span>
<span class="operator">&</span><span class="keyword">raw</span> <span class="keyword const">const</span> <span class="variable">u</span><span class="operator">.</span><span class="field unsafe">field</span><span class="semicolon">;</span>
<span class="operator">&</span><span class="keyword">raw</span> <span class="keyword const">const</span> <span class="variable">u</span><span class="operator">.</span><span class="field">field</span><span class="semicolon">;</span>
<span class="comment">// this should be safe!</span>
<span class="keyword">let</span> <span class="union">Union</span> <span class="brace">{</span> <span class="field">field</span><span class="colon">:</span> <span class="punctuation">_</span> <span class="brace">}</span><span class="semicolon">;</span>
<span class="comment">// but not these</span>
Expand Down