From 1d47c76ae69bfe5f9ecce4fae100d93bac87ccff Mon Sep 17 00:00:00 2001 From: Kivooeo Date: Sat, 24 May 2025 00:31:18 +0500 Subject: [PATCH 1/3] fixed unsafe suggestion --- .../rustc_mir_build/src/check_unsafety.rs | 22 +++++++++++++++++-- tests/ui/union/union-unsafe.rs | 8 +++++++ tests/ui/union/union-unsafe.stderr | 12 +++++----- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 9d0681b19b9d5..9aed6c5ebceb2 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -45,6 +45,9 @@ struct UnsafetyVisitor<'a, 'tcx> { /// Flag to ensure that we only suggest wrapping the entire function body in /// an unsafe block once. suggest_unsafe_block: bool, + /// Track whether we're currently inside a `&raw const/mut` expression. + /// Used to allow safe access to union fields when only taking their address. + in_raw_borrow: bool, } impl<'tcx> UnsafetyVisitor<'_, 'tcx> { @@ -218,6 +221,7 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> { inside_adt: false, warnings: self.warnings, suggest_unsafe_block: self.suggest_unsafe_block, + in_raw_borrow: false, }; // params in THIR may be unsafe, e.g. a union pattern. for param in &inner_thir.params { @@ -538,14 +542,24 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> { } } ExprKind::RawBorrow { arg, .. } => { + // Set flag when entering raw borrow context + let old_in_raw_borrow = self.in_raw_borrow; + self.in_raw_borrow = true; + if let ExprKind::Scope { value: arg, .. } = self.thir[arg].kind && let ExprKind::Deref { arg } = self.thir[arg].kind { // Taking a raw ref to a deref place expr is always safe. // Make sure the expression we're deref'ing is safe, though. visit::walk_expr(self, &self.thir[arg]); - return; + } else { + // Handle other raw borrow cases (including field access) + visit::walk_expr(self, &self.thir[arg]); } + + // Restore previous state + self.in_raw_borrow = old_in_raw_borrow; + return; } ExprKind::Deref { arg } => { if let ExprKind::StaticRef { def_id, .. } | ExprKind::ThreadLocalRef(def_id) = @@ -651,7 +665,10 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> { "union fields that need dropping should be impossible: {assigned_ty}" ); } - } else { + } else if !self.in_raw_borrow { + // Union field access is unsafe because the field may be uninitialized. + // However, `&raw const/mut union.field` is safe since it only computes + // the field's address without reading the potentially uninitialized value. self.requires_unsafe(expr.span, AccessToUnionField); } } @@ -1187,6 +1204,7 @@ pub(crate) fn check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) { inside_adt: false, warnings: &mut warnings, suggest_unsafe_block: true, + in_raw_borrow: false, }; // params in THIR may be unsafe, e.g. a union pattern. for param in &thir.params { diff --git a/tests/ui/union/union-unsafe.rs b/tests/ui/union/union-unsafe.rs index bd3946686be36..23f2e9a99d761 100644 --- a/tests/ui/union/union-unsafe.rs +++ b/tests/ui/union/union-unsafe.rs @@ -57,6 +57,14 @@ fn main() { let a = u1.a; //~ ERROR access to union field is unsafe u1.a = 11; // OK + let mut u2 = U1 { a: 10 }; + let a = &raw mut u2.a; // OK + unsafe { *a = 3 }; + + let mut u3 = U1 { a: 10 }; + let a = std::ptr::addr_of_mut!(u3.a); // OK + unsafe { *a = 14 }; + let U1 { a } = u1; //~ ERROR access to union field is unsafe if let U1 { a: 12 } = u1 {} //~ ERROR access to union field is unsafe if let Some(U1 { a: 13 }) = Some(u1) {} //~ ERROR access to union field is unsafe diff --git a/tests/ui/union/union-unsafe.stderr b/tests/ui/union/union-unsafe.stderr index 82b3f897167c7..05e539fa9d310 100644 --- a/tests/ui/union/union-unsafe.stderr +++ b/tests/ui/union/union-unsafe.stderr @@ -31,7 +31,7 @@ LL | let a = u1.a; = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:60:14 + --> $DIR/union-unsafe.rs:68:14 | LL | let U1 { a } = u1; | ^ access to union field @@ -39,7 +39,7 @@ LL | let U1 { a } = u1; = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:61:20 + --> $DIR/union-unsafe.rs:69:20 | LL | if let U1 { a: 12 } = u1 {} | ^^ access to union field @@ -47,7 +47,7 @@ LL | if let U1 { a: 12 } = u1 {} = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:62:25 + --> $DIR/union-unsafe.rs:70:25 | LL | if let Some(U1 { a: 13 }) = Some(u1) {} | ^^ access to union field @@ -55,7 +55,7 @@ LL | if let Some(U1 { a: 13 }) = Some(u1) {} = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:67:6 + --> $DIR/union-unsafe.rs:75:6 | LL | *u2.a = String::from("new"); | ^^^^ access to union field @@ -63,7 +63,7 @@ LL | *u2.a = String::from("new"); = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:71:6 + --> $DIR/union-unsafe.rs:79:6 | LL | *u3.a = 1; | ^^^^ access to union field @@ -71,7 +71,7 @@ LL | *u3.a = 1; = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:75:6 + --> $DIR/union-unsafe.rs:83:6 | LL | *u3.a = String::from("new"); | ^^^^ access to union field From 87fb4addda943349a4fc5abfda93ca6cd5aedc71 Mon Sep 17 00:00:00 2001 From: Kivooeo Date: Sat, 24 May 2025 03:00:33 +0500 Subject: [PATCH 2/3] not allow using &raw in other than plain field context --- .../rustc_mir_build/src/check_unsafety.rs | 19 +++++-- tests/ui/union/union-unsafe.rs | 15 +++++ tests/ui/union/union-unsafe.stderr | 56 +++++++++++++++---- 3 files changed, 75 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 9aed6c5ebceb2..4b9024578fca3 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -544,7 +544,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> { ExprKind::RawBorrow { arg, .. } => { // Set flag when entering raw borrow context let old_in_raw_borrow = self.in_raw_borrow; - self.in_raw_borrow = true; + self.in_raw_borrow = is_direct_place_expr(self.thir, arg); if let ExprKind::Scope { value: arg, .. } = self.thir[arg].kind && let ExprKind::Deref { arg } = self.thir[arg].kind @@ -552,11 +552,9 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> { // Taking a raw ref to a deref place expr is always safe. // Make sure the expression we're deref'ing is safe, though. visit::walk_expr(self, &self.thir[arg]); - } else { - // Handle other raw borrow cases (including field access) - visit::walk_expr(self, &self.thir[arg]); } + visit::walk_expr(self, &self.thir[arg]); // Restore previous state self.in_raw_borrow = old_in_raw_borrow; return; @@ -722,6 +720,19 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> { } } +fn is_direct_place_expr<'a, 'tcx>(thir: &'a Thir<'tcx>, expr_id: ExprId) -> bool { + match thir[expr_id].kind { + // Direct place expressions that don't read values + ExprKind::Field { .. } | ExprKind::VarRef { .. } | ExprKind::UpvarRef { .. } => true, + + // Scope is transparent for place expressions + ExprKind::Scope { value, .. } => is_direct_place_expr(thir, value), + + // Any other expression (including Deref, method calls, etc.) reads values + _ => false, + } +} + #[derive(Clone)] enum SafetyContext { Safe, diff --git a/tests/ui/union/union-unsafe.rs b/tests/ui/union/union-unsafe.rs index 23f2e9a99d761..93dbc6ef63bf2 100644 --- a/tests/ui/union/union-unsafe.rs +++ b/tests/ui/union/union-unsafe.rs @@ -17,6 +17,10 @@ union U4 { a: T, } +union U5 { + a: usize, +} + union URef { p: &'static mut i32, } @@ -31,6 +35,12 @@ fn deref_union_field(mut u: URef) { *(u.p) = 13; //~ ERROR access to union field is unsafe } +fn raw_deref_union_field(mut u: URef) { + let _p = &raw const *(u.p); + //~^ ERROR access to union field is unsafe + //~| ERROR access to union field is unsafe +} + fn assign_noncopy_union_field(mut u: URefCell) { u.a = (ManuallyDrop::new(RefCell::new(0)), 1); // OK (assignment does not drop) u.a.0 = ManuallyDrop::new(RefCell::new(0)); // OK (assignment does not drop) @@ -65,6 +75,11 @@ fn main() { let a = std::ptr::addr_of_mut!(u3.a); // OK unsafe { *a = 14 }; + let u4 = U5 { a: 2 }; + let vec = vec![1, 2, 3]; + let _a = &raw const vec[u4.a]; //~ ERROR access to union field is unsafe + //~^ ERROR access to union field is unsafe + let U1 { a } = u1; //~ ERROR access to union field is unsafe if let U1 { a: 12 } = u1 {} //~ ERROR access to union field is unsafe if let Some(U1 { a: 13 }) = Some(u1) {} //~ ERROR access to union field is unsafe diff --git a/tests/ui/union/union-unsafe.stderr b/tests/ui/union/union-unsafe.stderr index 05e539fa9d310..4ef8102fb1c38 100644 --- a/tests/ui/union/union-unsafe.stderr +++ b/tests/ui/union/union-unsafe.stderr @@ -1,5 +1,5 @@ error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:31:6 + --> $DIR/union-unsafe.rs:35:6 | LL | *(u.p) = 13; | ^^^^^ access to union field @@ -7,7 +7,24 @@ LL | *(u.p) = 13; = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:43:6 + --> $DIR/union-unsafe.rs:39:26 + | +LL | let _p = &raw const *(u.p); + | ^^^^^ access to union field + | + = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior + +error[E0133]: access to union field is unsafe and requires unsafe function or block + --> $DIR/union-unsafe.rs:39:26 + | +LL | let _p = &raw const *(u.p); + | ^^^^^ access to union field + | + = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +error[E0133]: access to union field is unsafe and requires unsafe function or block + --> $DIR/union-unsafe.rs:53:6 | LL | *u3.a = T::default(); | ^^^^ access to union field @@ -15,7 +32,7 @@ LL | *u3.a = T::default(); = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:49:6 + --> $DIR/union-unsafe.rs:59:6 | LL | *u3.a = T::default(); | ^^^^ access to union field @@ -23,7 +40,7 @@ LL | *u3.a = T::default(); = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:57:13 + --> $DIR/union-unsafe.rs:67:13 | LL | let a = u1.a; | ^^^^ access to union field @@ -31,7 +48,24 @@ LL | let a = u1.a; = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:68:14 + --> $DIR/union-unsafe.rs:80:29 + | +LL | let _a = &raw const vec[u4.a]; + | ^^^^ access to union field + | + = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior + +error[E0133]: access to union field is unsafe and requires unsafe function or block + --> $DIR/union-unsafe.rs:80:29 + | +LL | let _a = &raw const vec[u4.a]; + | ^^^^ access to union field + | + = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +error[E0133]: access to union field is unsafe and requires unsafe function or block + --> $DIR/union-unsafe.rs:83:14 | LL | let U1 { a } = u1; | ^ access to union field @@ -39,7 +73,7 @@ LL | let U1 { a } = u1; = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:69:20 + --> $DIR/union-unsafe.rs:84:20 | LL | if let U1 { a: 12 } = u1 {} | ^^ access to union field @@ -47,7 +81,7 @@ LL | if let U1 { a: 12 } = u1 {} = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:70:25 + --> $DIR/union-unsafe.rs:85:25 | LL | if let Some(U1 { a: 13 }) = Some(u1) {} | ^^ access to union field @@ -55,7 +89,7 @@ LL | if let Some(U1 { a: 13 }) = Some(u1) {} = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:75:6 + --> $DIR/union-unsafe.rs:90:6 | LL | *u2.a = String::from("new"); | ^^^^ access to union field @@ -63,7 +97,7 @@ LL | *u2.a = String::from("new"); = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:79:6 + --> $DIR/union-unsafe.rs:94:6 | LL | *u3.a = 1; | ^^^^ access to union field @@ -71,13 +105,13 @@ LL | *u3.a = 1; = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:83:6 + --> $DIR/union-unsafe.rs:98:6 | LL | *u3.a = String::from("new"); | ^^^^ access to union field | = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior -error: aborting due to 10 previous errors +error: aborting due to 14 previous errors For more information about this error, try `rustc --explain E0133`. From cde9fa00e95339888a3ce523daa7b644a83f7f20 Mon Sep 17 00:00:00 2001 From: Kivooeo Date: Sat, 24 May 2025 16:37:17 +0500 Subject: [PATCH 3/3] completly revised logic --- .../rustc_mir_build/src/check_unsafety.rs | 80 +++++++++++++------ tests/ui/union/union-unsafe.rs | 8 +- tests/ui/union/union-unsafe.stderr | 30 ++----- 3 files changed, 64 insertions(+), 54 deletions(-) diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 4b9024578fca3..bfda080444c2e 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -45,9 +45,6 @@ struct UnsafetyVisitor<'a, 'tcx> { /// Flag to ensure that we only suggest wrapping the entire function body in /// an unsafe block once. suggest_unsafe_block: bool, - /// Track whether we're currently inside a `&raw const/mut` expression. - /// Used to allow safe access to union fields when only taking their address. - in_raw_borrow: bool, } impl<'tcx> UnsafetyVisitor<'_, 'tcx> { @@ -221,7 +218,6 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> { inside_adt: false, warnings: self.warnings, suggest_unsafe_block: self.suggest_unsafe_block, - in_raw_borrow: false, }; // params in THIR may be unsafe, e.g. a union pattern. for param in &inner_thir.params { @@ -542,9 +538,19 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> { } } ExprKind::RawBorrow { arg, .. } => { - // Set flag when entering raw borrow context - let old_in_raw_borrow = self.in_raw_borrow; - self.in_raw_borrow = is_direct_place_expr(self.thir, arg); + // Handle the case where we're taking a raw pointer to a union field + if let ExprKind::Scope { value: arg, .. } = self.thir[arg].kind { + if self.is_union_field_access(arg) { + // Taking a raw pointer to a union field is safe - just check the base expression + // but skip the union field safety check + self.visit_union_field_for_raw_borrow(arg); + return; + } + } else if self.is_union_field_access(arg) { + // Direct raw borrow of union field + self.visit_union_field_for_raw_borrow(arg); + return; + } if let ExprKind::Scope { value: arg, .. } = self.thir[arg].kind && let ExprKind::Deref { arg } = self.thir[arg].kind @@ -552,12 +558,8 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> { // Taking a raw ref to a deref place expr is always safe. // Make sure the expression we're deref'ing is safe, though. visit::walk_expr(self, &self.thir[arg]); + return; } - - visit::walk_expr(self, &self.thir[arg]); - // Restore previous state - self.in_raw_borrow = old_in_raw_borrow; - return; } ExprKind::Deref { arg } => { if let ExprKind::StaticRef { def_id, .. } | ExprKind::ThreadLocalRef(def_id) = @@ -654,6 +656,8 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> { if adt_def.variant(variant_index).fields[name].safety.is_unsafe() { self.requires_unsafe(expr.span, UseOfUnsafeField); } else if adt_def.is_union() { + // Check if this field access is part of a raw borrow operation + // If so, we've already handled it above and shouldn't reach here if let Some(assigned_ty) = self.assignment_info { if assigned_ty.needs_drop(self.tcx, self.typing_env) { // This would be unsafe, but should be outright impossible since we @@ -663,10 +667,8 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> { "union fields that need dropping should be impossible: {assigned_ty}" ); } - } else if !self.in_raw_borrow { - // Union field access is unsafe because the field may be uninitialized. - // However, `&raw const/mut union.field` is safe since it only computes - // the field's address without reading the potentially uninitialized value. + } else { + // Only require unsafe if this is not a raw borrow operation self.requires_unsafe(expr.span, AccessToUnionField); } } @@ -720,16 +722,43 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> { } } -fn is_direct_place_expr<'a, 'tcx>(thir: &'a Thir<'tcx>, expr_id: ExprId) -> bool { - match thir[expr_id].kind { - // Direct place expressions that don't read values - ExprKind::Field { .. } | ExprKind::VarRef { .. } | ExprKind::UpvarRef { .. } => true, - - // Scope is transparent for place expressions - ExprKind::Scope { value, .. } => is_direct_place_expr(thir, value), +impl<'a, 'tcx> UnsafetyVisitor<'a, 'tcx> { + /// Check if an expression is a union field access + fn is_union_field_access(&self, expr_id: ExprId) -> bool { + match self.thir[expr_id].kind { + ExprKind::Field { lhs, .. } => { + let lhs = &self.thir[lhs]; + if let ty::Adt(adt_def, _) = lhs.ty.kind() { adt_def.is_union() } else { false } + } + _ => false, + } + } - // Any other expression (including Deref, method calls, etc.) reads values - _ => false, + /// Visit a union field access in the context of a raw borrow operation + /// This ensures we still check safety of nested operations while allowing + /// the raw pointer creation itself + fn visit_union_field_for_raw_borrow(&mut self, expr_id: ExprId) { + match self.thir[expr_id].kind { + ExprKind::Field { lhs, variant_index, name } => { + let lhs_expr = &self.thir[lhs]; + if let ty::Adt(adt_def, _) = lhs_expr.ty.kind() { + // Check for unsafe fields but skip the union access check + if adt_def.variant(variant_index).fields[name].safety.is_unsafe() { + self.requires_unsafe(self.thir[expr_id].span, UseOfUnsafeField); + } + // For unions, we don't require unsafe for raw pointer creation + // But we still need to check the LHS for safety + self.visit_expr(lhs_expr); + } else { + // Not a union, use normal visiting + visit::walk_expr(self, &self.thir[expr_id]); + } + } + _ => { + // Not a field access, use normal visiting + visit::walk_expr(self, &self.thir[expr_id]); + } + } } } @@ -1215,7 +1244,6 @@ pub(crate) fn check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) { inside_adt: false, warnings: &mut warnings, suggest_unsafe_block: true, - in_raw_borrow: false, }; // params in THIR may be unsafe, e.g. a union pattern. for param in &thir.params { diff --git a/tests/ui/union/union-unsafe.rs b/tests/ui/union/union-unsafe.rs index 93dbc6ef63bf2..b94325667805b 100644 --- a/tests/ui/union/union-unsafe.rs +++ b/tests/ui/union/union-unsafe.rs @@ -36,9 +36,8 @@ fn deref_union_field(mut u: URef) { } fn raw_deref_union_field(mut u: URef) { - let _p = &raw const *(u.p); - //~^ ERROR access to union field is unsafe - //~| ERROR access to union field is unsafe + // This is unsafe because we first dereference u.p (reading uninitialized memory) + let _p = &raw const *(u.p); //~ ERROR access to union field is unsafe } fn assign_noncopy_union_field(mut u: URefCell) { @@ -77,8 +76,9 @@ fn main() { let u4 = U5 { a: 2 }; let vec = vec![1, 2, 3]; + // This is unsafe because we read u4.a (potentially uninitialized memory) + // to use as an array index let _a = &raw const vec[u4.a]; //~ ERROR access to union field is unsafe - //~^ ERROR access to union field is unsafe let U1 { a } = u1; //~ ERROR access to union field is unsafe if let U1 { a: 12 } = u1 {} //~ ERROR access to union field is unsafe diff --git a/tests/ui/union/union-unsafe.stderr b/tests/ui/union/union-unsafe.stderr index 4ef8102fb1c38..ced9be73022c1 100644 --- a/tests/ui/union/union-unsafe.stderr +++ b/tests/ui/union/union-unsafe.stderr @@ -7,7 +7,7 @@ LL | *(u.p) = 13; = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:39:26 + --> $DIR/union-unsafe.rs:40:26 | LL | let _p = &raw const *(u.p); | ^^^^^ access to union field @@ -15,16 +15,7 @@ LL | let _p = &raw const *(u.p); = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:39:26 - | -LL | let _p = &raw const *(u.p); - | ^^^^^ access to union field - | - = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior - = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` - -error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:53:6 + --> $DIR/union-unsafe.rs:52:6 | LL | *u3.a = T::default(); | ^^^^ access to union field @@ -32,7 +23,7 @@ LL | *u3.a = T::default(); = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:59:6 + --> $DIR/union-unsafe.rs:58:6 | LL | *u3.a = T::default(); | ^^^^ access to union field @@ -40,7 +31,7 @@ LL | *u3.a = T::default(); = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:67:13 + --> $DIR/union-unsafe.rs:66:13 | LL | let a = u1.a; | ^^^^ access to union field @@ -48,21 +39,12 @@ LL | let a = u1.a; = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:80:29 - | -LL | let _a = &raw const vec[u4.a]; - | ^^^^ access to union field - | - = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior - -error[E0133]: access to union field is unsafe and requires unsafe function or block - --> $DIR/union-unsafe.rs:80:29 + --> $DIR/union-unsafe.rs:81:29 | LL | let _a = &raw const vec[u4.a]; | ^^^^ access to union field | = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior - = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` error[E0133]: access to union field is unsafe and requires unsafe function or block --> $DIR/union-unsafe.rs:83:14 @@ -112,6 +94,6 @@ LL | *u3.a = String::from("new"); | = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior -error: aborting due to 14 previous errors +error: aborting due to 12 previous errors For more information about this error, try `rustc --explain E0133`.