Skip to content

Handle infer -> dyn unsizing directly in the trait solver #138598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 2 additions & 25 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,31 +694,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
match selcx.select(&obligation.with(selcx.tcx(), trait_pred)) {
// Uncertain or unimplemented.
Ok(None) => {
if trait_pred.def_id() == unsize_did {
let self_ty = trait_pred.self_ty();
let unsize_ty = trait_pred.trait_ref.args[1].expect_ty();
debug!("coerce_unsized: ambiguous unsize case for {:?}", trait_pred);
match (self_ty.kind(), unsize_ty.kind()) {
(&ty::Infer(ty::TyVar(v)), ty::Dynamic(..))
if self.type_var_is_sized(v) =>
{
debug!("coerce_unsized: have sized infer {:?}", v);
coercion.obligations.push(obligation);
// `$0: Unsize<dyn Trait>` where we know that `$0: Sized`, try going
// for unsizing.
}
_ => {
// Some other case for `$0: Unsize<Something>`. Note that we
// hit this case even if `Something` is a sized type, so just
// don't do the coercion.
debug!("coerce_unsized: ambiguous unsize");
return Err(TypeError::Mismatch);
}
}
} else {
debug!("coerce_unsized: early return - ambiguous");
return Err(TypeError::Mismatch);
}
debug!("coerce_unsized: early return - ambiguous");
return Err(TypeError::Mismatch);
}
Err(traits::Unimplemented) => {
debug!("coerce_unsized: early return - can't prove obligation");
Expand Down
12 changes: 0 additions & 12 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,18 +701,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ret_ty.builtin_deref(true).unwrap()
}

pub(crate) fn type_var_is_sized(&self, self_ty: ty::TyVid) -> bool {
let sized_did = self.tcx.lang_items().sized_trait();
self.obligations_for_self_ty(self_ty).into_iter().any(|obligation| {
match obligation.predicate.kind().skip_binder() {
ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) => {
Some(data.def_id()) == sized_did
}
_ => false,
}
})
}

pub(crate) fn err_args(&self, len: usize, guar: ErrorGuaranteed) -> Vec<Ty<'tcx>> {
let ty_error = Ty::new_error(self.tcx, guar);
vec![ty_error; len]
Expand Down
117 changes: 117 additions & 0 deletions tests/ui/coercion/coerce-issue-49593-box-never.fallback.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
error[E0277]: the size for values of type `dyn std::error::Error` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:26:27
|
LL | Box::<_ /* ! */>::new(x)
| --------------------- ^ doesn't have a size known at compile-time
| |
| required by a bound introduced by this call
|
= help: the trait `Sized` is not implemented for `dyn std::error::Error`
note: required by a bound in `Box::<T>::new`
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL

error[E0277]: the size for values of type `(dyn std::error::Error + 'static)` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:32:19
|
LL | raw_ptr_box::<_ /* ! */>(x)
| ^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn std::error::Error + 'static)`
note: required by an implicit `Sized` bound in `raw_ptr_box`
--> $DIR/coerce-issue-49593-box-never.rs:19:16
|
LL | fn raw_ptr_box<T>(t: T) -> *mut T {
| ^ required by the implicit `Sized` requirement on this type parameter in `raw_ptr_box`
help: consider relaxing the implicit `Sized` restriction
|
LL | fn raw_ptr_box<T: ?Sized>(t: T) -> *mut T {
| ++++++++

error[E0277]: the size for values of type `(dyn std::error::Error + 'static)` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:32:30
|
LL | raw_ptr_box::<_ /* ! */>(x)
| ^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn std::error::Error + 'static)`
= note: all function arguments must have a statically known size
= help: unsized fn params are gated as an unstable feature

error[E0277]: the size for values of type `dyn Xyz` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:57:70
|
LL | = /* Box<$0> is coerced to Box<Xyz> here */ Box::new(x.unwrap());
| -------- ^^^^^^^^^^ doesn't have a size known at compile-time
| |
| required by a bound introduced by this call
|
= help: the trait `Sized` is not implemented for `dyn Xyz`
note: required by a bound in `Box::<T>::new`
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
help: consider removing this method call, as the receiver has type `Option<_>` and `Option<_>: Sized` trivially holds
|
LL - = /* Box<$0> is coerced to Box<Xyz> here */ Box::new(x.unwrap());
LL + = /* Box<$0> is coerced to Box<Xyz> here */ Box::new(x);
|

error[E0277]: the size for values of type `dyn Xyz` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:57:72
|
LL | = /* Box<$0> is coerced to Box<Xyz> here */ Box::new(x.unwrap());
| ^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `dyn Xyz`
note: required by a bound in `Option::<T>::unwrap`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0277]: the size for values of type `dyn Xyz` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:50:35
|
LL | let mut x /* : Option<S> */ = None;
| ^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `dyn Xyz`
note: required by a bound in `None`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0308]: mismatched types
--> $DIR/coerce-issue-49593-box-never.rs:62:13
|
LL | let mut x /* : Option<S> */ = None;
| ---- expected due to this value
...
LL | x = Some(S);
| ^^^^^^^ expected `Option<dyn Xyz>`, found `Option<S>`
|
= note: expected enum `Option<dyn Xyz>`
found enum `Option<S>`
= help: `S` implements `Xyz` so you could box the found value and coerce it to the trait object `Box<dyn Xyz>`, you will have to change the expected type as well

error[E0277]: the size for values of type `dyn Xyz` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:69:5
|
LL | mem::swap(&mut x, &mut y);
| ^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `dyn Xyz`
note: required by an implicit `Sized` bound in `Option`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0308]: mismatched types
--> $DIR/coerce-issue-49593-box-never.rs:69:23
|
LL | mem::swap(&mut x, &mut y);
| --------- ^^^^^^ expected `&mut Option<dyn Xyz>`, found `&mut Option<S>`
| |
| arguments to this function are incorrect
|
= note: expected mutable reference `&mut Option<dyn Xyz>`
found mutable reference `&mut Option<S>`
= help: `S` implements `Xyz` so you could box the found value and coerce it to the trait object `Box<dyn Xyz>`, you will have to change the expected type as well
note: function defined here
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL

error: aborting due to 9 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
118 changes: 108 additions & 10 deletions tests/ui/coercion/coerce-issue-49593-box-never.nofallback.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,117 @@
error[E0277]: the trait bound `(): std::error::Error` is not satisfied
--> $DIR/coerce-issue-49593-box-never.rs:18:5
error[E0277]: the size for values of type `dyn std::error::Error` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:26:27
|
LL | Box::<_ /* ! */>::new(x)
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `()`
| --------------------- ^ doesn't have a size known at compile-time
| |
| required by a bound introduced by this call
|
= note: required for the cast from `Box<()>` to `Box<(dyn std::error::Error + 'static)>`
= help: the trait `Sized` is not implemented for `dyn std::error::Error`
note: required by a bound in `Box::<T>::new`
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL

error[E0277]: the trait bound `(): std::error::Error` is not satisfied
--> $DIR/coerce-issue-49593-box-never.rs:24:5
error[E0277]: the size for values of type `(dyn std::error::Error + 'static)` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:32:19
|
LL | raw_ptr_box::<_ /* ! */>(x)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `()`
| ^ doesn't have a size known at compile-time
|
= note: required for the cast from `*mut ()` to `*mut (dyn std::error::Error + 'static)`
= help: the trait `Sized` is not implemented for `(dyn std::error::Error + 'static)`
note: required by an implicit `Sized` bound in `raw_ptr_box`
--> $DIR/coerce-issue-49593-box-never.rs:19:16
|
LL | fn raw_ptr_box<T>(t: T) -> *mut T {
| ^ required by the implicit `Sized` requirement on this type parameter in `raw_ptr_box`
help: consider relaxing the implicit `Sized` restriction
|
LL | fn raw_ptr_box<T: ?Sized>(t: T) -> *mut T {
| ++++++++

error[E0277]: the size for values of type `(dyn std::error::Error + 'static)` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:32:30
|
LL | raw_ptr_box::<_ /* ! */>(x)
| ^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn std::error::Error + 'static)`
= note: all function arguments must have a statically known size
= help: unsized fn params are gated as an unstable feature

error[E0277]: the size for values of type `dyn Xyz` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:57:70
|
LL | = /* Box<$0> is coerced to Box<Xyz> here */ Box::new(x.unwrap());
| -------- ^^^^^^^^^^ doesn't have a size known at compile-time
| |
| required by a bound introduced by this call
|
= help: the trait `Sized` is not implemented for `dyn Xyz`
note: required by a bound in `Box::<T>::new`
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
help: consider removing this method call, as the receiver has type `Option<_>` and `Option<_>: Sized` trivially holds
|
LL - = /* Box<$0> is coerced to Box<Xyz> here */ Box::new(x.unwrap());
LL + = /* Box<$0> is coerced to Box<Xyz> here */ Box::new(x);
|

error[E0277]: the size for values of type `dyn Xyz` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:57:72
|
LL | = /* Box<$0> is coerced to Box<Xyz> here */ Box::new(x.unwrap());
| ^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `dyn Xyz`
note: required by a bound in `Option::<T>::unwrap`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0277]: the size for values of type `dyn Xyz` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:50:35
|
LL | let mut x /* : Option<S> */ = None;
| ^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `dyn Xyz`
note: required by a bound in `None`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0308]: mismatched types
--> $DIR/coerce-issue-49593-box-never.rs:62:13
|
LL | let mut x /* : Option<S> */ = None;
| ---- expected due to this value
...
LL | x = Some(S);
| ^^^^^^^ expected `Option<dyn Xyz>`, found `Option<S>`
|
= note: expected enum `Option<dyn Xyz>`
found enum `Option<S>`
= help: `S` implements `Xyz` so you could box the found value and coerce it to the trait object `Box<dyn Xyz>`, you will have to change the expected type as well

error[E0277]: the size for values of type `dyn Xyz` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:69:5
|
LL | mem::swap(&mut x, &mut y);
| ^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `dyn Xyz`
note: required by an implicit `Sized` bound in `Option`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0308]: mismatched types
--> $DIR/coerce-issue-49593-box-never.rs:69:23
|
LL | mem::swap(&mut x, &mut y);
| --------- ^^^^^^ expected `&mut Option<dyn Xyz>`, found `&mut Option<S>`
| |
| arguments to this function are incorrect
|
= note: expected mutable reference `&mut Option<dyn Xyz>`
found mutable reference `&mut Option<S>`
= help: `S` implements `Xyz` so you could box the found value and coerce it to the trait object `Box<dyn Xyz>`, you will have to change the expected type as well
note: function defined here
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL

error: aborting due to 2 previous errors
error: aborting due to 9 previous errors

For more information about this error, try `rustc --explain E0277`.
Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
22 changes: 19 additions & 3 deletions tests/ui/coercion/coerce-issue-49593-box-never.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
//! This test was supposed to test that unsizing an uninferred but
//! `Sized` type works and will let inference happen later.
//! The issue was that what instead happened was that the inference
//! variable got equated to the unsized type, and subsequently failed
//! the `Sized` check.
//!
//! This was a wart in our coercion code, so it was removed to be instead
//! implemented in the trait system.

//@ revisions: nofallback fallback
//@[fallback] check-pass

#![feature(never_type)]
#![cfg_attr(fallback, feature(never_type_fallback))]
Expand All @@ -16,13 +24,14 @@ fn foo(x: !) -> Box<dyn Error> {
// Method resolution will generate new inference vars and relate them.
// Thus fallback will not fall back to `!`, but `()` instead.
Box::<_ /* ! */>::new(x)
//[nofallback]~^ ERROR trait bound `(): std::error::Error` is not satisfied
//~^ ERROR cannot be known at compilation time
}

fn foo_raw_ptr(x: !) -> *mut dyn Error {
/* *mut $0 is coerced to *mut Error here */
raw_ptr_box::<_ /* ! */>(x)
//[nofallback]~^ ERROR trait bound `(): std::error::Error` is not satisfied
//~^ ERROR cannot be known at compilation time
//~| ERROR cannot be known at compilation time
}

fn no_coercion(d: *mut dyn Error) -> *mut dyn Error {
Expand All @@ -39,20 +48,27 @@ impl Xyz for T {}

fn foo_no_never() {
let mut x /* : Option<S> */ = None;
//~^ ERROR cannot be known at compilation time

let mut first_iter = false;
loop {
if !first_iter {
let y: Box<dyn Xyz>
= /* Box<$0> is coerced to Box<Xyz> here */ Box::new(x.unwrap());
//~^ ERROR cannot be known at compilation time
//~| ERROR cannot be known at compilation time
}

x = Some(S);
//~^ ERROR mismatched types
first_iter = true;
}

let mut y: Option<S> = None;
// assert types are equal
mem::swap(&mut x, &mut y);
//~^ ERROR cannot be known at compilation time
//~| ERROR mismatched types
}

fn main() {}
Loading