Add missing tests and adress concerns

This commit is contained in:
Pieter-Louis Schoeman
2026-06-08 21:41:50 +02:00
parent f3ef3bd882
commit e789c33edb
39 changed files with 1137 additions and 0 deletions

View File

@@ -0,0 +1,110 @@
//@ known-bug: unknown
#![feature(reborrow)]
#![allow(dead_code)]
use std::marker::{CoerceShared, Reborrow};
// This test combines alias/projection normalization with the leaf `&mut T` to `&T`
// shared reborrow path.
struct InnerMut<'a, T> {
value: &'a mut T,
}
impl<'a, T> Reborrow for InnerMut<'a, T> {}
struct InnerRef<'a, T> {
value: &'a T,
}
impl<'a, T> Clone for InnerRef<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Copy for InnerRef<'a, T> {}
impl<'a, T> CoerceShared<InnerRef<'a, T>> for InnerMut<'a, T> {}
type DirectInnerRef<'a, T> = InnerRef<'a, T>;
trait RefFamily<'a, T> {
type Ref;
}
struct Projected;
impl<'a, T: 'a> RefFamily<'a, T> for Projected {
type Ref = InnerRef<'a, T>;
}
type ProjectedInnerRef<'a, T> = <Projected as RefFamily<'a, T>>::Ref;
struct OuterMut<'a, T> {
inner: InnerMut<'a, T>,
tag: usize,
}
impl<'a, T> Reborrow for OuterMut<'a, T> {}
struct OuterAliasRef<'a, T> {
inner: DirectInnerRef<'a, T>,
tag: usize,
}
impl<'a, T> Clone for OuterAliasRef<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Copy for OuterAliasRef<'a, T> {}
impl<'a, T> CoerceShared<OuterAliasRef<'a, T>> for OuterMut<'a, T> {}
struct OuterProjectionRef<'a, T: 'a> {
inner: ProjectedInnerRef<'a, T>,
tag: usize,
}
impl<'a, T: 'a> Clone for OuterProjectionRef<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T: 'a> Copy for OuterProjectionRef<'a, T> {}
impl<'a, T: 'a> CoerceShared<OuterProjectionRef<'a, T>> for OuterMut<'a, T> {}
fn read_alias<'a>(outer: OuterAliasRef<'a, u32>) -> (&'a u32, usize) {
(outer.inner.value, outer.tag)
}
fn read_projection<'a>(outer: OuterProjectionRef<'a, u32>) -> (&'a u32, usize) {
(outer.inner.value, outer.tag)
}
const fn const_accept_projection(_outer: OuterProjectionRef<'_, u32>) {}
const fn consteval_projection_reborrow() {
let mut value = 11;
const_accept_projection(OuterMut {
inner: InnerMut { value: &mut value },
tag: 5,
});
}
fn main() {
const { consteval_projection_reborrow(); }
let mut value = 22;
let outer = OuterMut { inner: InnerMut { value: &mut value }, tag: 7 };
let (alias_value, alias_tag) = read_alias(outer);
assert_eq!((*alias_value, alias_tag), (22, 7));
let (projection_value, projection_tag) = read_projection(outer);
assert_eq!((*projection_value, projection_tag), (22, 7));
}

View File

@@ -0,0 +1,41 @@
//@ known-bug: unknown
#![feature(reborrow)]
#![allow(dead_code)]
use std::marker::{CoerceShared, PhantomData, Reborrow};
use std::ptr::NonNull;
struct ImbrisMut<'a, T> {
ptr: NonNull<T>,
metadata: usize,
marker: PhantomData<&'a mut T>,
}
impl<'a, T> Reborrow for ImbrisMut<'a, T> {}
struct ImbrisRef<'a, T> {
ptr: NonNull<T>,
marker: PhantomData<&'a T>,
}
impl<'a, T> Clone for ImbrisRef<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Copy for ImbrisRef<'a, T> {}
impl<'a, T> CoerceShared<ImbrisRef<'a, T>> for ImbrisMut<'a, T> {}
fn ptr(value: ImbrisRef<'_, i32>) -> NonNull<i32> {
value.ptr
}
fn main() {
let mut value = 1;
let raw = NonNull::from(&mut value);
let wrapped = ImbrisMut { ptr: raw, metadata: 32, marker: PhantomData };
assert_eq!(ptr(wrapped), raw);
}

View File

@@ -0,0 +1,21 @@
//@ known-bug: unknown
//@ edition: 2024
#![feature(reborrow)]
use std::marker::{CoerceShared, PhantomData, Reborrow};
struct CustomMarker<'a>(PhantomData<&'a ()>);
struct CustomMarkerRef;
impl<'a> Reborrow for CustomMarker<'a> {}
impl<'a> CoerceShared<CustomMarkerRef> for CustomMarker<'a> {}
//~^ ERROR
fn method(_a: CustomMarkerRef) {}
fn main() {
let a = CustomMarker(PhantomData);
method(a);
//~^ ERROR
}

View File

@@ -0,0 +1,58 @@
//@ known-bug: unknown
#![feature(reborrow)]
#![allow(dead_code)]
use std::marker::{CoerceShared, PhantomData, Reborrow};
use std::ptr::NonNull;
struct MatMut<'a, T> {
ptr: NonNull<T>,
rows: usize,
cols: usize,
row_stride: usize,
col_stride: usize,
marker: PhantomData<&'a mut T>,
}
impl<'a, T> Reborrow for MatMut<'a, T> {}
struct MatRef<'a, T> {
ptr: NonNull<T>,
rows: usize,
cols: usize,
row_stride: usize,
col_stride: usize,
marker: PhantomData<&'a T>,
}
impl<'a, T> Clone for MatRef<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Copy for MatRef<'a, T> {}
impl<'a, T> CoerceShared<MatRef<'a, T>> for MatMut<'a, T> {}
fn dims<T>(mat: MatRef<'_, T>) -> (usize, usize, usize, usize) {
let _ = mat.ptr;
(mat.rows, mat.cols, mat.row_stride, mat.col_stride)
}
fn main() {
let mut value = 0;
let mat = MatMut {
ptr: NonNull::from(&mut value),
rows: 2,
cols: 3,
row_stride: 4,
col_stride: 5,
marker: PhantomData,
};
assert_eq!(dims(mat), (2, 3, 4, 5));
// Reusing the same source proves repeated shared reborrows keep source-only data protected
// without consuming the reborrowable value.
assert_eq!(dims(mat), (2, 3, 4, 5));
}

View File

@@ -0,0 +1,62 @@
//@ known-bug: unknown
#![feature(reborrow)]
#![allow(dead_code)]
use std::marker::{CoerceShared, Reborrow};
struct InnerMut<'a, T> {
value: &'a mut T,
}
impl<'a, T> Reborrow for InnerMut<'a, T> {}
struct InnerRef<'a, T> {
value: &'a T,
}
impl<'a, T> Clone for InnerRef<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Copy for InnerRef<'a, T> {}
impl<'a, T> CoerceShared<InnerRef<'a, T>> for InnerMut<'a, T> {}
struct OuterMut<'a, T> {
inner: InnerMut<'a, T>,
tag: usize,
}
impl<'a, T> Reborrow for OuterMut<'a, T> {}
struct OuterRef<'a, T> {
inner: InnerRef<'a, T>,
tag: usize,
}
impl<'a, T> Clone for OuterRef<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Copy for OuterRef<'a, T> {}
impl<'a, T> CoerceShared<OuterRef<'a, T>> for OuterMut<'a, T> {}
fn get<'a>(outer: OuterRef<'a, i32>) -> (&'a i32, usize) {
(outer.inner.value, outer.tag)
}
fn main() {
let mut value = 22;
let outer = OuterMut { inner: InnerMut { value: &mut value }, tag: 7 };
let (first, tag) = get(outer);
assert_eq!((*first, tag), (22, 7));
let (second, tag) = get(outer);
assert_eq!((*second, tag), (22, 7));
}

View File

@@ -0,0 +1,86 @@
//@ known-bug: unknown
#![feature(reborrow)]
#![allow(dead_code)]
use std::marker::{CoerceShared, PhantomData, Reborrow};
struct SourceLeadingMut<'a, T>(PhantomData<&'a mut T>, &'a mut T);
impl<'a, T> Reborrow for SourceLeadingMut<'a, T> {}
struct SourceLeadingRef<'a, T>(&'a T);
impl<'a, T> Clone for SourceLeadingRef<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Copy for SourceLeadingRef<'a, T> {}
impl<'a, T> CoerceShared<SourceLeadingRef<'a, T>> for SourceLeadingMut<'a, T> {}
struct TargetLeadingMut<'a, T>(&'a mut T);
impl<'a, T> Reborrow for TargetLeadingMut<'a, T> {}
struct TargetLeadingRef<'a, T>(PhantomData<&'a T>, &'a T);
impl<'a, T> Clone for TargetLeadingRef<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Copy for TargetLeadingRef<'a, T> {}
impl<'a, T> CoerceShared<TargetLeadingRef<'a, T>> for TargetLeadingMut<'a, T> {}
struct InterleavedMut<'a, T, U>(
PhantomData<&'a mut T>,
&'a mut T,
PhantomData<&'a mut U>,
&'a mut U,
);
impl<'a, T, U> Reborrow for InterleavedMut<'a, T, U> {}
struct InterleavedRef<'a, T, U>(&'a T, PhantomData<&'a T>, &'a U, PhantomData<&'a U>);
impl<'a, T, U> Clone for InterleavedRef<'a, T, U> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T, U> Copy for InterleavedRef<'a, T, U> {}
impl<'a, T, U> CoerceShared<InterleavedRef<'a, T, U>> for InterleavedMut<'a, T, U> {}
fn read_source_leading<'a>(value: SourceLeadingRef<'a, i32>) -> &'a i32 {
value.0
}
fn read_target_leading<'a>(value: TargetLeadingRef<'a, i32>) -> &'a i32 {
value.1
}
fn read_interleaved<'a>(value: InterleavedRef<'a, i32, i64>) -> (&'a i32, &'a i64) {
(value.0, value.2)
}
fn main() {
let mut source_leading = 10;
let wrapped = SourceLeadingMut(PhantomData, &mut source_leading);
assert_eq!(*read_source_leading(wrapped), 10);
let mut target_leading = 20;
let wrapped = TargetLeadingMut(&mut target_leading);
assert_eq!(*read_target_leading(wrapped), 20);
let mut first = 30;
let mut second = 40_i64;
let wrapped = InterleavedMut(PhantomData, &mut first, PhantomData, &mut second);
let (first, second) = read_interleaved(wrapped);
assert_eq!((*first, *second), (30, 40));
}

View File

@@ -0,0 +1,22 @@
//@ known-bug: unknown
//@ normalize-stderr: "\n\n\z" -> "\n"
#![feature(reborrow)]
use std::marker::{CoerceShared, Reborrow};
struct CustomMut<'a, T>(&'a mut T);
impl<'a, T> Reborrow for CustomMut<'a, T> {}
struct CustomRef<'a, T>(&'a CustomMut<'a, T>);
//~^ ERROR
impl<'a, T> CoerceShared<CustomRef<'a, T>> for CustomMut<'a, T> {}
fn method(_a: CustomRef<'_, ()>) {}
fn main() {
let a = CustomMut(&mut ());
method(a);
}

View File

@@ -0,0 +1,30 @@
//@ known-bug: unknown
//@ edition: 2024
#![feature(reborrow)]
// Malformed no-ICE regression: the unrelated name and signature errors are intentional because the
// original issue combined them with CoerceShared validation.
use std::marker::{CoerceShared, PhantomData, Reborrow};
struct CustomMarker<'a>(PhantomData<&'a ()>);
struct CustomMarkerRef<'a>(PhantomData<(Debug, Clone, Copy)>);
//~^ ERROR
//~| ERROR
//~| ERROR
impl<'a> Reborrow for CustomMarker<'a> {}
impl<'a> CoerceShared<CustomMarkerRef<'a>> for CustomMarker<'a> {}
//~^ ERROR
fn method<'a>(_a: CustomMarkerRef<'a>) -> 'a () {
//~^ ERROR
&()
}
fn main() {
let a = CustomMarker(PhantomData);
let b = method(a);
}

View File

@@ -0,0 +1,24 @@
//@ known-bug: unknown
#![feature(reborrow)]
// Malformed no-ICE regression: this intentionally keeps the missing generic arguments from the
// original reproducer while the corrected test isolates the CoerceShared field error.
use std::marker::{CoerceShared, Reborrow};
struct CustomMut<'a, T>(&'a mut T);
impl<'a, T> Reborrow for CustomMut<'a, T> {}
impl<'a, T> CoerceShared<CustomRef<'a, T>> for CustomMut<'a, T> {}
struct CustomRef<'a, T>(&'a CustomMut);
//~^ ERROR
//~| ERROR
//~| ERROR
fn method(_a: CustomRef<'_, ()>) {}
fn main() {
let a = CustomMut(&mut ());
method(a);
}

View File

@@ -0,0 +1,6 @@
#![allow(dead_code)]
#[derive(Clone, Copy)]
pub struct ForeignRef<'a> {
value: &'a i32,
}

View File

@@ -0,0 +1,30 @@
#![feature(reborrow)]
#![allow(dead_code)]
use std::marker::{CoerceShared, Reborrow};
trait Trait {
type Assoc;
}
impl Trait for i32 {
type Assoc = i64;
}
struct MyMut<'a> {
x: &'a (),
y: i64,
}
#[derive(Copy, Clone)]
struct MyRef<'a> {
x: &'a (),
y: <i32 as Trait>::Assoc,
}
impl Reborrow for MyMut<'_> {}
impl<'a> CoerceShared<MyRef<'a>> for MyMut<'a> {}
//~^ ERROR
fn main() {}

View File

@@ -0,0 +1,8 @@
error: implementing `CoerceShared` does not allow multiple lifetimes or fields to be coerced
--> $DIR/coerce-shared-associated-type-field.rs:27:10
|
LL | impl<'a> CoerceShared<MyRef<'a>> for MyMut<'a> {}
| ^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error

View File

@@ -0,0 +1,26 @@
#![feature(reborrow, decl_macro)]
#![allow(incomplete_features)]
use std::marker::{CoerceShared, Reborrow};
macro my_macro($field:ident) {
pub struct MyMut<'a> {
$field: &'a i32,
field: &'a i64,
}
#[derive(Clone, Copy)]
pub struct MyRef<'a> {
$field: &'a i32,
field: &'a i64,
}
impl Reborrow for MyMut<'_> {}
impl<'a> CoerceShared<MyRef<'a>> for MyMut<'a> {}
//~^ ERROR
}
my_macro!(field);
fn main() {}

View File

@@ -0,0 +1,13 @@
error: implementing `CoerceShared` does not allow multiple lifetimes or fields to be coerced
--> $DIR/coerce-shared-decl-macro-hygiene.rs:20:14
|
LL | impl<'a> CoerceShared<MyRef<'a>> for MyMut<'a> {}
| ^^^^^^^^^^^^^^^^^^^^^^^
...
LL | my_macro!(field);
| ---------------- in this macro invocation
|
= note: this error originates in the macro `my_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 1 previous error

View File

@@ -0,0 +1,37 @@
//@ run-pass
#![feature(reborrow)]
#![allow(dead_code)]
use std::marker::{CoerceShared, PhantomData, Reborrow};
struct MarkerExtraMut<'a, T> {
value: &'a mut T,
marker: PhantomData<&'a mut T>,
}
impl<'a, T> Reborrow for MarkerExtraMut<'a, T> {}
struct MarkerExtraRef<'a, T> {
value: &'a T,
}
impl<'a, T> Clone for MarkerExtraRef<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Copy for MarkerExtraRef<'a, T> {}
impl<'a, T> CoerceShared<MarkerExtraRef<'a, T>> for MarkerExtraMut<'a, T> {}
fn get<'a>(value: MarkerExtraRef<'a, i32>) -> &'a i32 {
value.value
}
fn main() {
let mut value = 1;
let wrapped = MarkerExtraMut { value: &mut value, marker: PhantomData };
assert_eq!(*get(wrapped), 1);
}

View File

@@ -0,0 +1,21 @@
#![feature(reborrow)]
use std::marker::{CoerceShared, Reborrow};
struct MyMut<'a> {
x: &'static (),
y: &'a (),
}
impl Reborrow for MyMut<'_> {}
#[derive(Copy, Clone)]
struct MyRef<'a> {
x: &'a (),
y: &'static (),
}
impl<'a> CoerceShared<MyRef<'a>> for MyMut<'a> {}
//~^ ERROR
fn main() {}

View File

@@ -0,0 +1,8 @@
error: implementing `CoerceShared` does not allow multiple lifetimes or fields to be coerced
--> $DIR/coerce-shared-field-lifetime-swap.rs:18:10
|
LL | impl<'a> CoerceShared<MyRef<'a>> for MyMut<'a> {}
| ^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error

View File

@@ -0,0 +1,53 @@
//@ normalize-stderr: "\n\n\z" -> "\n"
#![feature(reborrow)]
use std::marker::{CoerceShared, Reborrow};
struct CustomMut<'a, T> {
value: &'a mut T,
}
impl<'a, T> Reborrow for CustomMut<'a, T> {}
#[derive(Clone, Copy)]
struct CustomRef<'a, T> {
value: &'a T,
}
impl<'a, T> CoerceShared<CustomRef<'a, T>> for CustomMut<'a, T> {}
struct RenamedMut<'a, T> {
source: &'a mut T,
}
impl<'a, T> Reborrow for RenamedMut<'a, T> {}
#[derive(Clone, Copy)]
struct RenamedRef<'a, T> {
target: &'a T,
}
impl<'a, T> CoerceShared<RenamedRef<'a, T>> for RenamedMut<'a, T> {}
struct BadMut<'a, T> {
value: &'a mut T,
}
impl<'a, T> Reborrow for BadMut<'a, T> {}
#[derive(Clone, Copy)]
struct BadRef<'a, T> {
value: &'a u32,
_marker: std::marker::PhantomData<T>,
}
impl<'a, T> CoerceShared<BadRef<'a, T>> for BadMut<'a, T> {}
//~^ ERROR
fn good(_value: CustomRef<'_, u32>) {}
fn main() {
let mut value = 1;
good(CustomMut { value: &mut value });
}

View File

@@ -0,0 +1,9 @@
error[E0277]: the trait bound `&'a mut T: CoerceShared<&'a u32>` is not satisfied
--> $DIR/coerce-shared-field-relations.rs:45:1
|
LL | impl<'a, T> CoerceShared<BadRef<'a, T>> for BadMut<'a, T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the nightly-only, unstable trait `CoerceShared<&'a u32>` is not implemented for `&'a mut T`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.

View File

@@ -0,0 +1,20 @@
//@ check-pass
//@ aux-build: reborrow_foreign_private.rs
#![feature(reborrow)]
extern crate reborrow_foreign_private;
use reborrow_foreign_private::ForeignRef;
use std::marker::{CoerceShared, Reborrow};
struct LocalMut<'a> {
value: &'a mut i32,
}
impl<'a> Reborrow for LocalMut<'a> {}
impl<'a> CoerceShared<ForeignRef<'a>> for LocalMut<'a> {}
fn main() {}

View File

@@ -0,0 +1,22 @@
//@ check-pass
#![feature(reborrow)]
use std::marker::{CoerceShared, PhantomData, Reborrow};
mod foreign_ptr {
use std::marker::PhantomData;
#[derive(Clone, Copy)]
pub struct ForeignPtrRef<'a>(*const i32, PhantomData<&'a ()>);
}
use foreign_ptr::ForeignPtrRef;
struct LocalPtrMut<'a>(*const i32, PhantomData<&'a ()>);
impl<'a> Reborrow for LocalPtrMut<'a> {}
impl<'a> CoerceShared<ForeignPtrRef<'a>> for LocalPtrMut<'a> {}
fn main() {}

View File

@@ -0,0 +1,41 @@
#![feature(reborrow)]
#![allow(dead_code)]
use std::marker::{CoerceShared, Reborrow};
struct BufferMut<'a, T, U, const N: usize> {
data: &'a mut [T; N],
meta: U,
}
impl<'a, T, U: Copy, const N: usize> Reborrow for BufferMut<'a, T, U, N> {}
struct BufferRef<'a, T, U, const N: usize> {
data: &'a [T; N],
meta: U,
}
impl<'a, T, U: Copy, const N: usize> Clone for BufferRef<'a, T, U, N> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T, U: Copy, const N: usize> Copy for BufferRef<'a, T, U, N> {}
impl<'a, T, U: Copy, const N: usize> CoerceShared<BufferRef<'a, T, U, N>>
//~^ ERROR
for BufferMut<'a, T, U, N>
{
}
fn inspect<const N: usize>(buffer: BufferRef<'_, u8, u16, N>) -> (usize, u16) {
(buffer.data.len(), buffer.meta)
}
fn main() {
let mut data = [1, 2, 3, 4];
let buffer = BufferMut { data: &mut data, meta: 9_u16 };
assert_eq!(inspect(buffer), (4, 9));
}

View File

@@ -0,0 +1,8 @@
error: implementing `CoerceShared` does not allow multiple lifetimes or fields to be coerced
--> $DIR/coerce-shared-generics.rs:26:38
|
LL | impl<'a, T, U: Copy, const N: usize> CoerceShared<BufferRef<'a, T, U, N>>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error

View File

@@ -0,0 +1,23 @@
#![feature(reborrow)]
// The impl is accepted, but using it to coerce a local marker into a `'static`
// target still requires the local borrow to live for `'static`.
use std::marker::{CoerceShared, PhantomData, Reborrow};
struct CustomMarker<'a>(PhantomData<&'a ()>);
impl<'a> Reborrow for CustomMarker<'a> {}
#[derive(Clone, Copy)]
struct StaticMarkerRef<'a>(PhantomData<&'a ()>);
impl<'a> CoerceShared<StaticMarkerRef<'static>> for CustomMarker<'a> {}
fn method(_a: StaticMarkerRef<'static>) {}
fn main() {
let a = CustomMarker(PhantomData);
method(a);
//~^ ERROR
}

View File

@@ -0,0 +1,17 @@
error[E0597]: `a` does not live long enough
--> $DIR/coerce-shared-lifetime-mismatch.rs:21:12
|
LL | let a = CustomMarker(PhantomData);
| - binding `a` declared here
LL | method(a);
| -------^-
| | |
| | borrowed value does not live long enough
| argument requires that `a` is borrowed for `'static`
LL |
LL | }
| - `a` dropped here while still borrowed
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0597`.

View File

@@ -0,0 +1,22 @@
//@ normalize-stderr: "\n\n\z" -> "\n"
#![feature(reborrow)]
use std::marker::{CoerceShared, Reborrow};
struct MissingSourceMut<'a, T> {
value: &'a mut T,
}
impl<'a, T> Reborrow for MissingSourceMut<'a, T> {}
#[derive(Clone, Copy)]
struct MissingSourceRef<'a, T> {
value: &'a T,
len: usize,
}
impl<'a, T> CoerceShared<MissingSourceRef<'a, T>> for MissingSourceMut<'a, T> {}
//~^ ERROR
fn main() {}

View File

@@ -0,0 +1,7 @@
error: implementing `CoerceShared` does not allow multiple lifetimes or fields to be coerced
--> $DIR/coerce-shared-missing-target-field.rs:19:13
|
LL | impl<'a, T> CoerceShared<MissingSourceRef<'a, T>> for MissingSourceMut<'a, T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error

View File

@@ -0,0 +1,45 @@
//@ normalize-stderr: "\n\n\z" -> "\n"
#![feature(reborrow)]
use std::marker::{CoerceShared, Reborrow};
trait Trait {
type Assoc;
}
impl Trait for i32 {
type Assoc = u32;
}
type AliasMutField<'a> = &'a mut <i32 as Trait>::Assoc;
type AliasRefField<'a> = &'a u32;
struct AliasMut<'a> {
value: AliasMutField<'a>,
}
impl Reborrow for AliasMut<'_> {}
#[derive(Copy, Clone)]
struct AliasRef<'a> {
value: AliasRefField<'a>,
}
impl<'a> CoerceShared<AliasRef<'a>> for AliasMut<'a> {}
//~^ ERROR
struct InnerLifetimeMut<'a> {
value: &'a mut &'static (),
}
impl Reborrow for InnerLifetimeMut<'_> {}
#[derive(Copy, Clone)]
struct InnerLifetimeRef<'a> {
value: &'a &'a (),
}
impl<'a> CoerceShared<InnerLifetimeRef<'a>> for InnerLifetimeMut<'a> {}
fn main() {}

View File

@@ -0,0 +1,9 @@
error[E0277]: the trait bound `&'a mut u32: CoerceShared<&'a u32>` is not satisfied
--> $DIR/coerce-shared-mut-ref-field-validation.rs:29:1
|
LL | impl<'a> CoerceShared<AliasRef<'a>> for AliasMut<'a> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the nightly-only, unstable trait `CoerceShared<&'a u32>` is not implemented for `&'a mut u32`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.

View File

@@ -0,0 +1,51 @@
#![feature(reborrow)]
#![allow(dead_code)]
use std::marker::{CoerceShared, Reborrow};
struct ExtraMut<'a, T> {
value: &'a mut T,
}
impl<'a, T> Reborrow for ExtraMut<'a, T> {}
struct OmitMut<'a, T> {
value: &'a mut T,
extra: ExtraMut<'a, T>,
}
impl<'a, T> Reborrow for OmitMut<'a, T> {}
struct OmitRef<'a, T> {
value: &'a T,
}
impl<'a, T> Clone for OmitRef<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Copy for OmitRef<'a, T> {}
impl<'a, T> CoerceShared<OmitRef<'a, T>> for OmitMut<'a, T> {}
//~^ ERROR
fn read(value: OmitRef<'_, i32>) {
assert_eq!(*value.value, 1);
}
fn main() {
let mut value = 1;
let mut extra_value = 2;
{
let extra = ExtraMut { value: &mut extra_value };
let wrapped = OmitMut { value: &mut value, extra };
read(wrapped);
}
extra_value = 3;
assert_eq!(extra_value, 3);
}

View File

@@ -0,0 +1,8 @@
error: implementing `CoerceShared` does not allow multiple lifetimes or fields to be coerced
--> $DIR/coerce-shared-omitted-reborrow-field-after-dead.rs:31:13
|
LL | impl<'a, T> CoerceShared<OmitRef<'a, T>> for OmitMut<'a, T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error

View File

@@ -0,0 +1,53 @@
#![feature(reborrow)]
use std::marker::{CoerceShared, Reborrow};
struct ExtraMut<'a, T> {
value: &'a mut T,
}
impl<'a, T> Reborrow for ExtraMut<'a, T> {}
struct OmitMut<'a, T> {
value: &'a mut T,
extra: ExtraMut<'a, T>,
}
impl<'a, T> Reborrow for OmitMut<'a, T> {}
struct OmitRef<'a, T> {
value: &'a T,
}
impl<'a, T> Clone for OmitRef<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Copy for OmitRef<'a, T> {}
impl<'a, T> CoerceShared<OmitRef<'a, T>> for OmitMut<'a, T> {}
//~^ ERROR
fn get<'a>(value: OmitRef<'a, i32>) -> &'a i32 {
value.value
}
fn main() {
let mut value = 1;
let mut extra_value = 2;
let extra = ExtraMut { value: &mut extra_value };
let mut wrapped = OmitMut {
value: &mut value,
extra,
};
let shared = get(wrapped);
*wrapped.extra.value = 3;
//~^ ERROR cannot assign to `*wrapped.extra.value` because it is borrowed
let _ = shared;
}

View File

@@ -0,0 +1,21 @@
error: implementing `CoerceShared` does not allow multiple lifetimes or fields to be coerced
--> $DIR/coerce-shared-omitted-reborrow-field-locked.rs:30:13
|
LL | impl<'a, T> CoerceShared<OmitRef<'a, T>> for OmitMut<'a, T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0506]: cannot assign to `*wrapped.extra.value` because it is borrowed
--> $DIR/coerce-shared-omitted-reborrow-field-locked.rs:49:5
|
LL | let shared = get(wrapped);
| ------- `*wrapped.extra.value` is borrowed here
LL |
LL | *wrapped.extra.value = 3;
| ^^^^^^^^^^^^^^^^^^^^^^^^
| |
| `*wrapped.extra.value` is assigned to here but it was already borrowed
| borrow later used here
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0506`.

View File

@@ -0,0 +1,45 @@
#![feature(reborrow)]
#![allow(dead_code)]
use std::marker::{CoerceShared, Reborrow};
struct ExtraMut<'a, T> {
value: &'a mut T,
}
impl<'a, T> Reborrow for ExtraMut<'a, T> {}
struct OmitMut<'a, T> {
value: &'a mut T,
extra: ExtraMut<'a, T>,
}
impl<'a, T> Reborrow for OmitMut<'a, T> {}
struct OmitRef<'a, T> {
value: &'a T,
}
impl<'a, T> Clone for OmitRef<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Copy for OmitRef<'a, T> {}
impl<'a, T> CoerceShared<OmitRef<'a, T>> for OmitMut<'a, T> {}
//~^ ERROR
fn get<'a>(value: OmitRef<'a, i32>) -> &'a i32 {
value.value
}
fn main() {
let mut value = 1;
let mut extra = 2;
let extra = ExtraMut { value: &mut extra };
let wrapped = OmitMut { value: &mut value, extra };
assert_eq!(*get(wrapped), 1);
}

View File

@@ -0,0 +1,8 @@
error: implementing `CoerceShared` does not allow multiple lifetimes or fields to be coerced
--> $DIR/coerce-shared-omitted-reborrow-field.rs:31:13
|
LL | impl<'a, T> CoerceShared<OmitRef<'a, T>> for OmitMut<'a, T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error

View File

@@ -0,0 +1,32 @@
#![feature(reborrow)]
#![allow(dead_code)]
use std::marker::{CoerceShared, Reborrow};
struct ReorderMut<'a> {
a: &'a mut u8,
b: &'a mut u16,
}
impl<'a> Reborrow for ReorderMut<'a> {}
#[derive(Clone, Copy)]
struct ReorderRef<'a> {
b: &'a u16,
a: &'a u8,
}
impl<'a> CoerceShared<ReorderRef<'a>> for ReorderMut<'a> {}
//~^ ERROR
fn read(value: ReorderRef<'_>) -> (u16, u8) {
(*value.b, *value.a)
}
fn main() {
let mut a = 1;
let mut b = 2;
let wrapped = ReorderMut { a: &mut a, b: &mut b };
assert_eq!(read(wrapped), (2, 1));
}

View File

@@ -0,0 +1,8 @@
error: implementing `CoerceShared` does not allow multiple lifetimes or fields to be coerced
--> $DIR/coerce-shared-reordered-field.rs:19:10
|
LL | impl<'a> CoerceShared<ReorderRef<'a>> for ReorderMut<'a> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error

View File

@@ -0,0 +1,23 @@
//@ normalize-stderr: "\n\n\z" -> "\n"
#![feature(reborrow)]
use std::marker::{CoerceShared, PhantomData, Reborrow};
struct GenericMut<'a, T, U> {
value: &'a mut T,
marker: PhantomData<U>,
}
impl<'a, T, U> Reborrow for GenericMut<'a, T, U> {}
#[derive(Clone, Copy)]
struct GenericRef<'a, T, U> {
value: &'a U,
marker: PhantomData<T>,
}
impl<'a, T, U> CoerceShared<GenericRef<'a, T, U>> for GenericMut<'a, T, U> {}
//~^ ERROR
fn main() {}

View File

@@ -0,0 +1,9 @@
error[E0277]: the trait bound `&'a mut T: CoerceShared<&'a U>` is not satisfied
--> $DIR/coerce-shared-wrong-generic.rs:20:1
|
LL | impl<'a, T, U> CoerceShared<GenericRef<'a, T, U>> for GenericMut<'a, T, U> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the nightly-only, unstable trait `CoerceShared<&'a U>` is not implemented for `&'a mut T`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.