tests: PointeeSized bounds with extern types

These tests necessarily need to change now that `?Sized` is not
sufficient to accept extern types and `PointeeSized` is now necessary. In
addition, the `size_of_val`/`align_of_val` test can now be changed to
expect an error.
This commit is contained in:
David Wood 2025-01-22 18:07:04 +00:00
parent 118d4e62c3
commit 3c3ba37ba5
No known key found for this signature in database
20 changed files with 131 additions and 69 deletions

View file

@ -3,8 +3,9 @@
//@ compile-flags: -C no-prepopulate-passes -Copt-level=0
#![crate_type = "lib"]
#![feature(extern_types)]
#![feature(extern_types, sized_hierarchy)]
use std::marker::PointeeSized;
use std::ptr::addr_of;
// Hack to get the correct type for usize
@ -12,7 +13,7 @@ use std::ptr::addr_of;
#[no_mangle]
pub fn helper(_: usize) {}
struct Dst<T: ?Sized> {
struct Dst<T: PointeeSized> {
x: u32,
y: u8,
z: T,

View file

@ -7,7 +7,9 @@ extern "C" {
type Opaque;
}
const _SIZE: usize = unsafe { size_of_val(&4 as *const i32 as *const Opaque) }; //~ ERROR layout
const _ALIGN: usize = unsafe { align_of_val(&4 as *const i32 as *const Opaque) }; //~ ERROR layout
const _SIZE: usize = unsafe { size_of_val(&4 as *const i32 as *const Opaque) };
//~^ ERROR the size for values of type `Opaque` cannot be known
const _ALIGN: usize = unsafe { align_of_val(&4 as *const i32 as *const Opaque) };
//~^ ERROR the size for values of type `Opaque` cannot be known
fn main() {}

View file

@ -1,15 +1,27 @@
error[E0080]: `extern type` does not have known layout
--> $DIR/const-size_of_val-align_of_val-extern-type.rs:10:31
error[E0277]: the size for values of type `Opaque` cannot be known
--> $DIR/const-size_of_val-align_of_val-extern-type.rs:10:43
|
LL | const _SIZE: usize = unsafe { size_of_val(&4 as *const i32 as *const Opaque) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `_SIZE` failed here
| ----------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a known size
| |
| required by a bound introduced by this call
|
= help: the trait `MetaSized` is not implemented for `Opaque`
note: required by a bound in `std::intrinsics::size_of_val`
--> $SRC_DIR/core/src/intrinsics/mod.rs:LL:COL
error[E0080]: `extern type` does not have known layout
--> $DIR/const-size_of_val-align_of_val-extern-type.rs:11:32
error[E0277]: the size for values of type `Opaque` cannot be known
--> $DIR/const-size_of_val-align_of_val-extern-type.rs:12:45
|
LL | const _ALIGN: usize = unsafe { align_of_val(&4 as *const i32 as *const Opaque) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `_ALIGN` failed here
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a known size
| |
| required by a bound introduced by this call
|
= help: the trait `MetaSized` is not implemented for `Opaque`
note: required by a bound in `std::intrinsics::align_of_val`
--> $SRC_DIR/core/src/intrinsics/mod.rs:LL:COL
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0080`.
For more information about this error, try `rustc --explain E0277`.

View file

@ -3,7 +3,10 @@
// Two extern types shouldn't really be considered similar just
// because they are both extern types.
#![feature(extern_types)]
#![feature(extern_types, sized_hierarchy)]
use std::marker::PointeeSized;
extern "C" {
type ShouldNotBeMentioned;
}
@ -14,7 +17,7 @@ extern "C" {
unsafe impl Send for ShouldNotBeMentioned {}
fn assert_send<T: Send + ?Sized>() {}
fn assert_send<T: Send + PointeeSized>() {}
fn main() {
assert_send::<Foo>()

View file

@ -1,14 +1,14 @@
error[E0277]: `Foo` cannot be sent between threads safely
--> $DIR/extern-type-diag-not-similar.rs:20:19
--> $DIR/extern-type-diag-not-similar.rs:23:19
|
LL | assert_send::<Foo>()
| ^^^ `Foo` cannot be sent between threads safely
|
= help: the trait `Send` is not implemented for `Foo`
note: required by a bound in `assert_send`
--> $DIR/extern-type-diag-not-similar.rs:17:19
--> $DIR/extern-type-diag-not-similar.rs:20:19
|
LL | fn assert_send<T: Send + ?Sized>() {}
LL | fn assert_send<T: Send + PointeeSized>() {}
| ^^^^ required by this bound in `assert_send`
error: aborting due to 1 previous error

View file

@ -1,7 +1,9 @@
//@ run-pass
// Test that unsafe impl for Sync/Send can be provided for extern types.
#![feature(extern_types)]
#![feature(extern_types, sized_hierarchy)]
use std::marker::PointeeSized;
extern "C" {
type A;
@ -10,8 +12,8 @@ extern "C" {
unsafe impl Sync for A {}
unsafe impl Send for A {}
fn assert_sync<T: ?Sized + Sync>() {}
fn assert_send<T: ?Sized + Send>() {}
fn assert_sync<T: PointeeSized + Sync>() {}
fn assert_send<T: PointeeSized + Send>() {}
fn main() {
assert_sync::<A>();

View file

@ -1,13 +1,15 @@
// Make sure extern types are !Sync and !Send.
#![feature(extern_types)]
#![feature(extern_types, sized_hierarchy)]
use std::marker::PointeeSized;
extern "C" {
type A;
}
fn assert_sync<T: ?Sized + Sync>() {}
fn assert_send<T: ?Sized + Send>() {}
fn assert_sync<T: PointeeSized + Sync>() {}
fn assert_send<T: PointeeSized + Send>() {}
fn main() {
assert_sync::<A>();

View file

@ -1,28 +1,28 @@
error[E0277]: `A` cannot be shared between threads safely
--> $DIR/extern-types-not-sync-send.rs:13:19
--> $DIR/extern-types-not-sync-send.rs:15:19
|
LL | assert_sync::<A>();
| ^ `A` cannot be shared between threads safely
|
= help: the trait `Sync` is not implemented for `A`
note: required by a bound in `assert_sync`
--> $DIR/extern-types-not-sync-send.rs:9:28
--> $DIR/extern-types-not-sync-send.rs:11:34
|
LL | fn assert_sync<T: ?Sized + Sync>() {}
| ^^^^ required by this bound in `assert_sync`
LL | fn assert_sync<T: PointeeSized + Sync>() {}
| ^^^^ required by this bound in `assert_sync`
error[E0277]: `A` cannot be sent between threads safely
--> $DIR/extern-types-not-sync-send.rs:16:19
--> $DIR/extern-types-not-sync-send.rs:18:19
|
LL | assert_send::<A>();
| ^ `A` cannot be sent between threads safely
|
= help: the trait `Send` is not implemented for `A`
note: required by a bound in `assert_send`
--> $DIR/extern-types-not-sync-send.rs:10:28
--> $DIR/extern-types-not-sync-send.rs:12:34
|
LL | fn assert_send<T: ?Sized + Send>() {}
| ^^^^ required by this bound in `assert_send`
LL | fn assert_send<T: PointeeSized + Send>() {}
| ^^^^ required by this bound in `assert_send`
error: aborting due to 2 previous errors

View file

@ -2,7 +2,8 @@
#![allow(dead_code)]
// Test that pointers to extern types can be cast from/to usize,
// despite being !Sized.
#![feature(extern_types)]
#![feature(extern_types, sized_hierarchy)]
use std::marker::PointeeSized;
extern "C" {
type A;
@ -13,7 +14,7 @@ struct Foo {
tail: A,
}
struct Bar<T: ?Sized> {
struct Bar<T: PointeeSized> {
x: u8,
tail: T,
}

View file

@ -1,8 +1,4 @@
//@ run-fail
//@ check-run-results
//@ exec-env:RUST_BACKTRACE=0
//@ normalize-stderr: "(core/src/panicking\.rs):[0-9]+:[0-9]+" -> "$1:$$LINE:$$COL"
//@ revisions: size align
//@ check-fail
#![feature(extern_types)]
use std::mem::{align_of_val, size_of_val};
@ -14,10 +10,8 @@ extern "C" {
fn main() {
let x: &A = unsafe { &*(1usize as *const A) };
// These don't have a dynamic size, so this should panic.
if cfg!(size) {
assert_eq!(size_of_val(x), 0);
} else {
assert_eq!(align_of_val(x), 1);
}
size_of_val(x);
//~^ ERROR the size for values of type `A` cannot be known
align_of_val(x);
//~^ ERROR the size for values of type `A` cannot be known
}

View file

@ -0,0 +1,39 @@
error[E0277]: the size for values of type `A` cannot be known
--> $DIR/extern-types-size_of_val.rs:13:17
|
LL | size_of_val(x);
| ----------- ^ the trait `MetaSized` is not implemented for `A`
| |
| required by a bound introduced by this call
|
= note: the trait bound `A: MetaSized` is not satisfied
note: required by a bound in `std::mem::size_of_val`
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
help: consider borrowing here
|
LL | size_of_val(&x);
| +
LL | size_of_val(&mut x);
| ++++
error[E0277]: the size for values of type `A` cannot be known
--> $DIR/extern-types-size_of_val.rs:15:18
|
LL | align_of_val(x);
| ------------ ^ the trait `MetaSized` is not implemented for `A`
| |
| required by a bound introduced by this call
|
= note: the trait bound `A: MetaSized` is not satisfied
note: required by a bound in `std::mem::align_of_val`
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
help: consider borrowing here
|
LL | align_of_val(&x);
| +
LL | align_of_val(&mut x);
| ++++
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0277`.

View file

@ -2,8 +2,9 @@
#![allow(dead_code)]
// Test that pointers and references to extern types are thin, ie they have the same size and
// alignment as a pointer to ().
#![feature(extern_types)]
#![feature(extern_types, sized_hierarchy)]
use std::marker::PointeeSized;
use std::mem::{align_of, size_of};
extern "C" {
@ -15,12 +16,12 @@ struct Foo {
tail: A,
}
struct Bar<T: ?Sized> {
struct Bar<T: PointeeSized> {
x: u8,
tail: T,
}
fn assert_thin<T: ?Sized>() {
fn assert_thin<T: PointeeSized>() {
assert_eq!(size_of::<*const T>(), size_of::<*const ()>());
assert_eq!(align_of::<*const T>(), align_of::<*const ()>());

View file

@ -1,13 +1,14 @@
//@ run-pass
#![allow(dead_code)]
// Test that traits can be implemented for extern types.
#![feature(extern_types)]
#![feature(extern_types, sized_hierarchy)]
use std::marker::PointeeSized;
extern "C" {
type A;
}
trait Foo {
trait Foo: PointeeSized {
fn foo(&self) {}
}
@ -15,9 +16,9 @@ impl Foo for A {
fn foo(&self) {}
}
fn assert_foo<T: ?Sized + Foo>() {}
fn assert_foo<T: PointeeSized + Foo>() {}
fn use_foo<T: ?Sized + Foo>(x: &dyn Foo) {
fn use_foo<T: PointeeSized + Foo>(x: &dyn Foo) {
x.foo();
}

View file

@ -1,5 +1,6 @@
#![feature(extern_types)]
#![feature(extern_types, sized_hierarchy)]
use std::marker::PointeeSized;
use std::mem::offset_of;
struct Alpha {
@ -26,7 +27,7 @@ struct Gamma {
z: Extern,
}
struct Delta<T: ?Sized> {
struct Delta<T: PointeeSized> {
x: u8,
y: u16,
z: T,

View file

@ -1,5 +1,5 @@
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:36:5
--> $DIR/offset-of-dst-field.rs:37:5
|
LL | offset_of!(Alpha, z);
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
@ -8,7 +8,7 @@ LL | offset_of!(Alpha, z);
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:37:5
--> $DIR/offset-of-dst-field.rs:38:5
|
LL | offset_of!(Beta, z);
| ^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
@ -17,7 +17,7 @@ LL | offset_of!(Beta, z);
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the size for values of type `Extern` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:38:5
--> $DIR/offset-of-dst-field.rs:39:5
|
LL | offset_of!(Gamma, z);
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
@ -26,7 +26,7 @@ LL | offset_of!(Gamma, z);
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:40:5
--> $DIR/offset-of-dst-field.rs:41:5
|
LL | offset_of!((u8, dyn Trait), 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
@ -35,7 +35,7 @@ LL | offset_of!((u8, dyn Trait), 1);
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the size for values of type `Extern` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:45:5
--> $DIR/offset-of-dst-field.rs:46:5
|
LL | offset_of!(Delta<Extern>, z);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
@ -44,7 +44,7 @@ LL | offset_of!(Delta<Extern>, z);
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:46:5
--> $DIR/offset-of-dst-field.rs:47:5
|
LL | offset_of!(Delta<dyn Trait>, z);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
@ -53,21 +53,21 @@ LL | offset_of!(Delta<dyn Trait>, z);
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:44:5
--> $DIR/offset-of-dst-field.rs:45:5
|
LL | offset_of!(Delta<Alpha>, z);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `Alpha`, the trait `Sized` is not implemented for `[u8]`
note: required because it appears within the type `Alpha`
--> $DIR/offset-of-dst-field.rs:5:8
--> $DIR/offset-of-dst-field.rs:6:8
|
LL | struct Alpha {
| ^^^^^
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the size for values of type `T` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:50:5
--> $DIR/offset-of-dst-field.rs:51:5
|
LL | fn generic_with_maybe_sized<T: ?Sized>() -> usize {
| - this type parameter needs to be `Sized`
@ -82,7 +82,7 @@ LL + fn generic_with_maybe_sized<T>() -> usize {
|
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:54:16
--> $DIR/offset-of-dst-field.rs:55:16
|
LL | offset_of!(([u8], u8), 1);
| ^^^^^^^^^^ doesn't have a size known at compile-time

View file

@ -2,13 +2,16 @@
//@ compile-flags: -C symbol-mangling-version=v0
#![feature(extern_types)]
#![feature(sized_hierarchy)]
#![feature(rustc_attrs)]
use std::marker::PointeeSized;
extern "C" {
type ForeignType;
}
struct Check<T: ?Sized>(T);
struct Check<T: PointeeSized>(T);
#[rustc_symbol_name]
//~^ ERROR symbol-name(_RMCs

View file

@ -1,17 +1,17 @@
error: symbol-name(_RMCsCRATE_HASH_13foreign_typesINtB<REF>_5CheckNtB<REF>_11ForeignTypeE)
--> $DIR/foreign-types.rs:13:1
--> $DIR/foreign-types.rs:16:1
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^
error: demangling(<foreign_types[fcdd87e190ad88e3]::Check<foreign_types[fcdd87e190ad88e3]::ForeignType>>)
--> $DIR/foreign-types.rs:13:1
--> $DIR/foreign-types.rs:16:1
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^
error: demangling-alt(<foreign_types::Check<foreign_types::ForeignType>>)
--> $DIR/foreign-types.rs:13:1
--> $DIR/foreign-types.rs:16:1
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

View file

@ -9,7 +9,7 @@ LL | foo(x);
note: required by a bound in `foo`
--> $DIR/extern-types.rs:26:8
|
LL | fn foo<T: ?Sized>(_: &T) {}
LL | fn foo<T: PointeeSized>(_: &T) {}
| ^ required by this bound in `foo`
error: aborting due to 1 previous error

View file

@ -9,7 +9,7 @@ LL | foo(x);
note: required by a bound in `foo`
--> $DIR/extern-types.rs:26:8
|
LL | fn foo<T: ?Sized>(_: &T) {}
LL | fn foo<T: PointeeSized>(_: &T) {}
| ^ required by this bound in `foo`
error: aborting due to 1 previous error

View file

@ -23,7 +23,7 @@ pub trait Copy {}
auto trait Leak {}
// implicit T: Leak here
fn foo<T: ?Sized>(_: &T) {}
fn foo<T: PointeeSized>(_: &T) {}
mod extern_leak {
use crate::*;