From 7b1187968ce5385758996ec7765d886d3a659d07 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 27 May 2020 20:31:17 +0200 Subject: [PATCH] expand unaligned_references test --- src/test/ui/lint/unaligned_references.rs | 15 +++++++--- src/test/ui/lint/unaligned_references.stderr | 30 +++++++++++++++----- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/test/ui/lint/unaligned_references.rs b/src/test/ui/lint/unaligned_references.rs index 1d9f4c3db2eb5..c4e5d065643c8 100644 --- a/src/test/ui/lint/unaligned_references.rs +++ b/src/test/ui/lint/unaligned_references.rs @@ -2,20 +2,27 @@ #[repr(packed)] pub struct Good { - data: &'static u32, - data2: [&'static u32; 2], + data: u64, + ptr: &'static u64, + data2: [u64; 2], aligned: [u8; 32], } fn main() { unsafe { - let good = Good { data: &0, data2: [&0, &0], aligned: [0; 32] }; + let good = Good { data: 0, ptr: &0, data2: [0, 0], aligned: [0; 32] }; + let _ = &good.ptr; //~ ERROR reference to packed field let _ = &good.data; //~ ERROR reference to packed field + // Error even when turned into raw pointer immediately. let _ = &good.data as *const _; //~ ERROR reference to packed field let _: *const _ = &good.data; //~ ERROR reference to packed field + // Error on method call. + let _ = good.data.clone(); //~ ERROR reference to packed field + // Error for nested fields. let _ = &good.data2[0]; //~ ERROR reference to packed field - let _ = &*good.data; // ok, behind a pointer + + let _ = &*good.ptr; // ok, behind a pointer let _ = &good.aligned; // ok, has align 1 let _ = &good.aligned[2]; // ok, has align 1 } diff --git a/src/test/ui/lint/unaligned_references.stderr b/src/test/ui/lint/unaligned_references.stderr index 0c594cdb30a3c..8786b9c05db27 100644 --- a/src/test/ui/lint/unaligned_references.stderr +++ b/src/test/ui/lint/unaligned_references.stderr @@ -1,8 +1,8 @@ error: reference to packed field is unaligned - --> $DIR/unaligned_references.rs:14:17 + --> $DIR/unaligned_references.rs:15:17 | -LL | let _ = &good.data; - | ^^^^^^^^^^ +LL | let _ = &good.ptr; + | ^^^^^^^^^ | note: the lint level is defined here --> $DIR/unaligned_references.rs:1:9 @@ -12,7 +12,15 @@ LL | #![deny(unaligned_references)] = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) error: reference to packed field is unaligned - --> $DIR/unaligned_references.rs:15:17 + --> $DIR/unaligned_references.rs:16:17 + | +LL | let _ = &good.data; + | ^^^^^^^^^^ + | + = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) + +error: reference to packed field is unaligned + --> $DIR/unaligned_references.rs:18:17 | LL | let _ = &good.data as *const _; | ^^^^^^^^^^ @@ -20,7 +28,7 @@ LL | let _ = &good.data as *const _; = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) error: reference to packed field is unaligned - --> $DIR/unaligned_references.rs:16:27 + --> $DIR/unaligned_references.rs:19:27 | LL | let _: *const _ = &good.data; | ^^^^^^^^^^ @@ -28,12 +36,20 @@ LL | let _: *const _ = &good.data; = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) error: reference to packed field is unaligned - --> $DIR/unaligned_references.rs:17:17 + --> $DIR/unaligned_references.rs:21:17 + | +LL | let _ = good.data.clone(); + | ^^^^^^^^^ + | + = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) + +error: reference to packed field is unaligned + --> $DIR/unaligned_references.rs:23:17 | LL | let _ = &good.data2[0]; | ^^^^^^^^^^^^^^ | = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) -error: aborting due to 4 previous errors +error: aborting due to 6 previous errors