forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#136107 - dingxiangfei2009:coerce-pointee-wellformed, r=compiler-errors Introduce CoercePointeeWellformed for coherence checks at typeck stage Fix rust-lang#135206 This is the first PR to introduce the "wellformedness" check for `derive(CoercePointee)`. This patch introduces a new error code to cover all the prerequisites of the said macro. The checks that is enforced with this patch is whether the data is indeed `struct` and whether the layout is set to `repr(transparent)`. A following series of patch will arrive later to address the following concern. 1. rust-lang#135217 so that we would only admit one single coercion on one type parameter, and leave the rest for future consideration in tandem of development of other coercion rules. 1. Enforcement of data field requirements. **An open question** is whether there is a good schema to encode the `#[pointee]` as well, so that we could also check if the `#[pointee]` type parameter is indeed `?Sized`. ``@rustbot`` label F-derive_coerce_pointee
- Loading branch information
Showing
15 changed files
with
385 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
The target of `derive(CoercePointee)` macro has inadmissible specification for | ||
a meaningful use. | ||
|
||
Erroneous code examples: | ||
|
||
The target data is not a `struct`. | ||
|
||
```compile_fail,E0802 | ||
#![feature(coerce_pointee)] | ||
use std::marker::CoercePointee; | ||
#[derive(CoercePointee)] | ||
enum NotStruct<'a, T: ?Sized> { | ||
Variant(&'a T), | ||
} | ||
``` | ||
|
||
The target data has a layout that is not transparent, or `repr(transparent)` | ||
in other words. | ||
|
||
```compile_fail,E0802 | ||
#![feature(coerce_pointee)] | ||
use std::marker::CoercePointee; | ||
#[derive(CoercePointee)] | ||
struct NotTransparent<'a, #[pointee] T: ?Sized> { | ||
ptr: &'a T, | ||
} | ||
``` | ||
|
||
The target data has no data field. | ||
|
||
```compile_fail,E0802 | ||
#![feature(coerce_pointee)] | ||
use std::marker::CoercePointee; | ||
#[derive(CoercePointee)] | ||
#[repr(transparent)] | ||
struct NoField<'a, #[pointee] T: ?Sized> {} | ||
``` | ||
|
||
The target data is not generic over any data, or has no generic type parameter. | ||
|
||
```compile_fail,E0802 | ||
#![feature(coerce_pointee)] | ||
use std::marker::CoercePointee; | ||
#[derive(CoercePointee)] | ||
#[repr(transparent)] | ||
struct NoGeneric<'a>(&'a u8); | ||
``` | ||
|
||
The target data has multiple generic type parameters, but none is designated as | ||
a pointee for coercion. | ||
|
||
```compile_fail,E0802 | ||
#![feature(coerce_pointee)] | ||
use std::marker::CoercePointee; | ||
#[derive(CoercePointee)] | ||
#[repr(transparent)] | ||
struct AmbiguousPointee<'a, T1: ?Sized, T2: ?Sized> { | ||
a: (&'a T1, &'a T2), | ||
} | ||
``` | ||
|
||
The target data has multiple generic type parameters that are designated as | ||
pointees for coercion. | ||
|
||
```compile_fail,E0802 | ||
#![feature(coerce_pointee)] | ||
use std::marker::CoercePointee; | ||
#[derive(CoercePointee)] | ||
#[repr(transparent)] | ||
struct TooManyPointees< | ||
'a, | ||
#[pointee] A: ?Sized, | ||
#[pointee] B: ?Sized> | ||
((&'a A, &'a B)); | ||
``` | ||
|
||
The type parameter that is designated as a pointee is not marked `?Sized`. | ||
|
||
```compile_fail,E0802 | ||
#![feature(coerce_pointee)] | ||
use std::marker::CoercePointee; | ||
#[derive(CoercePointee)] | ||
#[repr(transparent)] | ||
struct NoMaybeSized<'a, #[pointee] T> { | ||
ptr: &'a T, | ||
} | ||
``` | ||
|
||
In summary, the `CoercePointee` macro demands the type to be a `struct` that is | ||
generic over at least one type or over more types, one of which is marked with | ||
`#[pointee]`, and has at least one data field and adopts a `repr(transparent)` | ||
layout. | ||
The only generic type or the type marked with `#[pointee]` has to be also | ||
marked as `?Sized`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -545,6 +545,7 @@ E0798: 0798, | |
E0799: 0799, | ||
E0800: 0800, | ||
E0801: 0801, | ||
E0802: 0802, | ||
); | ||
) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.