Allow constraining opaque types during unsizing

This commit is contained in:
Oli Scherer 2024-05-24 13:41:20 +00:00
parent 9889a6f5d3
commit 4dcb70b8cf
7 changed files with 12 additions and 60 deletions

View File

@ -1228,7 +1228,7 @@ fn confirm_builtin_unsize_candidate(
let InferOk { obligations, .. } = self
.infcx
.at(&obligation.cause, obligation.param_env)
.eq(DefineOpaqueTypes::No, b, a)
.eq(DefineOpaqueTypes::Yes, b, a)
.map_err(|_| Unimplemented)?;
ImplSource::Builtin(BuiltinImplSource::Misc, obligations)
@ -1276,7 +1276,7 @@ fn confirm_builtin_unsize_candidate(
let InferOk { obligations, .. } = self
.infcx
.at(&obligation.cause, obligation.param_env)
.eq(DefineOpaqueTypes::No, target, new_struct)
.eq(DefineOpaqueTypes::Yes, target, new_struct)
.map_err(|_| Unimplemented)?;
nested.extend(obligations);
@ -1309,7 +1309,7 @@ fn confirm_builtin_unsize_candidate(
let InferOk { mut obligations, .. } = self
.infcx
.at(&obligation.cause, obligation.param_env)
.eq(DefineOpaqueTypes::No, target, new_tuple)
.eq(DefineOpaqueTypes::Yes, target, new_tuple)
.map_err(|_| Unimplemented)?;
// Add a nested `T: Unsize<U>` predicate.

View File

@ -1,4 +1,6 @@
//! Test that we do not allow unsizing `Foo<[Opaque; N]>` to `Foo<[Concrete]>`.
//! Test that we allow unsizing `Foo<[Opaque; N]>` to `Foo<[Concrete]>`.
//@check-pass
struct Foo<T: ?Sized>(T);
@ -6,7 +8,6 @@
if false {
let x = hello();
let _: &Foo<[i32]> = &x;
//~^ ERROR: mismatched types
}
todo!()
}

View File

@ -1,17 +0,0 @@
error[E0308]: mismatched types
--> $DIR/unsize_adt.rs:8:30
|
LL | fn hello() -> Foo<[impl Sized; 2]> {
| ---------- the found opaque type
...
LL | let _: &Foo<[i32]> = &x;
| ----------- ^^ expected `&Foo<[i32]>`, found `&Foo<[impl Sized; 2]>`
| |
| expected due to this
|
= note: expected reference `&Foo<[i32]>`
found reference `&Foo<[impl Sized; 2]>`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.

View File

@ -1,10 +1,11 @@
//! Test that we do not allow unsizing `[Opaque; N]` to `[Concrete]`.
//! Test that we allow unsizing `[Opaque; N]` to `[Concrete]`.
//@check-pass
fn hello() -> [impl Sized; 2] {
if false {
let x = hello();
let _: &[i32] = &x;
//~^ ERROR: mismatched types
}
todo!()
}

View File

@ -1,17 +0,0 @@
error[E0308]: mismatched types
--> $DIR/unsize_slice.rs:6:25
|
LL | fn hello() -> [impl Sized; 2] {
| ---------- the found opaque type
...
LL | let _: &[i32] = &x;
| ------ ^^ expected `&[i32]`, found `&[impl Sized; 2]`
| |
| expected due to this
|
= note: expected reference `&[i32]`
found reference `&[impl Sized; 2]`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.

View File

@ -1,4 +1,6 @@
//! Test that we do not allow unsizing `([Opaque; N],)` to `([Concrete],)`.
//! Test that we allow unsizing `([Opaque; N],)` to `([Concrete],)`.
//@check-pass
#![feature(unsized_tuple_coercion)]
@ -6,7 +8,6 @@
if false {
let x = hello();
let _: &([i32],) = &x;
//~^ ERROR: mismatched types
}
todo!()
}

View File

@ -1,17 +0,0 @@
error[E0308]: mismatched types
--> $DIR/unsize_tuple.rs:8:28
|
LL | fn hello() -> ([impl Sized; 2],) {
| ---------- the found opaque type
...
LL | let _: &([i32],) = &x;
| --------- ^^ expected `&([i32],)`, found `&([impl Sized; 2],)`
| |
| expected due to this
|
= note: expected reference `&([i32],)`
found reference `&([impl Sized; 2],)`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.