-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pointer] Fix Ptr[Inner] variance (#2351)
Previously, `Ptr<'a, T>` and `PtrInner<'a, T>` documented themselves to be covariant in both `'a` and `T`. This was true for `PtrInner`, but not for `Ptr`, which used GATs, which are invariant. This is also not the desired variance: for `Exclusive` aliasing, the desired variance matches that of `&mut` references - namely, covariant in `'a` but invariant in `T`. This commit fixes this by making `Ptr<'a, T>` and `PtrInner<'a, T>` unconditionally covariant in `'a` and invariant in `T`. gherrit-pr-id: I29f8429d9d7b14026313f030f8dc1e895a98ad56
- Loading branch information
Showing
9 changed files
with
124 additions
and
19 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
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 @@ | ||
../ui-nightly/ptr-is-invariant-over-v.rs |
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,31 @@ | ||
error: lifetime may not live long enough | ||
--> tests/ui-msrv/ptr-is-invariant-over-v.rs:10:5 | ||
| | ||
6 | fn _when_exclusive<'big: 'small, 'small>( | ||
| ---- ------ lifetime `'small` defined here | ||
| | | ||
| lifetime `'big` defined here | ||
... | ||
10 | _small = big; | ||
| ^^^^^^^^^^^^ assignment requires that `'small` must outlive `'big` | ||
| | ||
= help: consider adding the following bound: `'small: 'big` | ||
= note: requirement occurs because of the type `Ptr<'_, Valid<&u32>, (invariant::Exclusive, Aligned)>`, which makes the generic argument `Valid<&u32>` invariant | ||
= note: the struct `Ptr<'a, V, I>` is invariant over the parameter `V` | ||
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance | ||
|
||
error: lifetime may not live long enough | ||
--> tests/ui-msrv/ptr-is-invariant-over-v.rs:17:5 | ||
| | ||
13 | fn _when_shared<'big: 'small, 'small>( | ||
| ---- ------ lifetime `'small` defined here | ||
| | | ||
| lifetime `'big` defined here | ||
... | ||
17 | _small = big; | ||
| ^^^^^^^^^^^^ assignment requires that `'small` must outlive `'big` | ||
| | ||
= help: consider adding the following bound: `'small: 'big` | ||
= note: requirement occurs because of the type `Ptr<'_, Valid<&u32>, (Shared, Aligned)>`, which makes the generic argument `Valid<&u32>` invariant | ||
= note: the struct `Ptr<'a, V, I>` is invariant over the parameter `V` | ||
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance |
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,20 @@ | ||
use zerocopy::pointer::{ | ||
invariant::{Aligned, Exclusive, Shared, Valid}, | ||
Ptr, | ||
}; | ||
|
||
fn _when_exclusive<'big: 'small, 'small>( | ||
big: Ptr<'small, Valid<&'big u32>, (Exclusive, Aligned)>, | ||
mut _small: Ptr<'small, Valid<&'small u32>, (Exclusive, Aligned)>, | ||
) { | ||
_small = big; | ||
} | ||
|
||
fn _when_shared<'big: 'small, 'small>( | ||
big: Ptr<'small, Valid<&'big u32>, (Shared, Aligned)>, | ||
mut _small: Ptr<'small, Valid<&'small u32>, (Shared, Aligned)>, | ||
) { | ||
_small = big; | ||
} | ||
|
||
fn main() {} |
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,31 @@ | ||
error: lifetime may not live long enough | ||
--> tests/ui-nightly/ptr-is-invariant-over-v.rs:10:5 | ||
| | ||
6 | fn _when_exclusive<'big: 'small, 'small>( | ||
| ---- ------ lifetime `'small` defined here | ||
| | | ||
| lifetime `'big` defined here | ||
... | ||
10 | _small = big; | ||
| ^^^^^^^^^^^^ assignment requires that `'small` must outlive `'big` | ||
| | ||
= help: consider adding the following bound: `'small: 'big` | ||
= note: requirement occurs because of the type `Ptr<'_, Valid<&u32>, (invariant::Exclusive, Aligned)>`, which makes the generic argument `Valid<&u32>` invariant | ||
= note: the struct `Ptr<'a, V, I>` is invariant over the parameter `V` | ||
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance | ||
|
||
error: lifetime may not live long enough | ||
--> tests/ui-nightly/ptr-is-invariant-over-v.rs:17:5 | ||
| | ||
13 | fn _when_shared<'big: 'small, 'small>( | ||
| ---- ------ lifetime `'small` defined here | ||
| | | ||
| lifetime `'big` defined here | ||
... | ||
17 | _small = big; | ||
| ^^^^^^^^^^^^ assignment requires that `'small` must outlive `'big` | ||
| | ||
= help: consider adding the following bound: `'small: 'big` | ||
= note: requirement occurs because of the type `Ptr<'_, Valid<&u32>, (Shared, Aligned)>`, which makes the generic argument `Valid<&u32>` invariant | ||
= note: the struct `Ptr<'a, V, I>` is invariant over the parameter `V` | ||
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance |
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 @@ | ||
../ui-nightly/ptr-is-invariant-over-v.rs |
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,31 @@ | ||
error: lifetime may not live long enough | ||
--> tests/ui-stable/ptr-is-invariant-over-v.rs:10:5 | ||
| | ||
6 | fn _when_exclusive<'big: 'small, 'small>( | ||
| ---- ------ lifetime `'small` defined here | ||
| | | ||
| lifetime `'big` defined here | ||
... | ||
10 | _small = big; | ||
| ^^^^^^^^^^^^ assignment requires that `'small` must outlive `'big` | ||
| | ||
= help: consider adding the following bound: `'small: 'big` | ||
= note: requirement occurs because of the type `Ptr<'_, Valid<&u32>, (invariant::Exclusive, Aligned)>`, which makes the generic argument `Valid<&u32>` invariant | ||
= note: the struct `Ptr<'a, V, I>` is invariant over the parameter `V` | ||
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance | ||
|
||
error: lifetime may not live long enough | ||
--> tests/ui-stable/ptr-is-invariant-over-v.rs:17:5 | ||
| | ||
13 | fn _when_shared<'big: 'small, 'small>( | ||
| ---- ------ lifetime `'small` defined here | ||
| | | ||
| lifetime `'big` defined here | ||
... | ||
17 | _small = big; | ||
| ^^^^^^^^^^^^ assignment requires that `'small` must outlive `'big` | ||
| | ||
= help: consider adding the following bound: `'small: 'big` | ||
= note: requirement occurs because of the type `Ptr<'_, Valid<&u32>, (Shared, Aligned)>`, which makes the generic argument `Valid<&u32>` invariant | ||
= note: the struct `Ptr<'a, V, I>` is invariant over the parameter `V` | ||
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance |