Skip to content

[experiment] Don't consider sized type variables in coerce_unsized #105568

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
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
30 changes: 1 addition & 29 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,35 +639,7 @@ 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 trait_pred = self.resolve_vars_if_possible(trait_pred);
let self_ty = trait_pred.skip_binder().self_ty();
let unsize_ty = trait_pred.skip_binder().trait_ref.substs[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);
}
}
Err(traits::Unimplemented) => {
Ok(None) | Err(traits::Unimplemented) => {
debug!("coerce_unsized: early return - can't prove obligation");
return Err(TypeError::Mismatch);
}
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 @@ -669,18 +669,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)
}

pub(in super::super) 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).any(|obligation| {
match obligation.predicate.kind().skip_binder() {
ty::PredicateKind::Clause(ty::Clause::Trait(data)) => {
Some(data.def_id()) == sized_did
}
_ => false,
}
})
}

pub(in super::super) fn err_args(&self, len: usize) -> Vec<Ty<'tcx>> {
vec![self.tcx.ty_error(); len]
}
Expand Down
123 changes: 113 additions & 10 deletions src/test/ui/coercion/coerce-issue-49593-box-never.nofallback.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,122 @@
error[E0277]: the trait bound `(): std::error::Error` is not satisfied
--> $DIR/coerce-issue-49593-box-never.rs:18:53
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:18:75
|
LL | /* *mut $0 is coerced to Box<dyn Error> here */ 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 `()` to the object type `dyn std::error::Error`
= 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
|
LL | impl<T> Box<T> {
| ^ required by this bound in `Box::<T>::new`

error[E0277]: the trait bound `(): std::error::Error` is not satisfied
--> $DIR/coerce-issue-49593-box-never.rs:23:49
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:23:74
|
LL | /* *mut $0 is coerced to *mut Error here */ raw_ptr_box::<_ /* ! */>(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
|
= help: the trait `Sized` is not implemented for `(dyn std::error::Error + 'static)`
note: required by a bound in `raw_ptr_box`
--> $DIR/coerce-issue-49593-box-never.rs:13:16
|
LL | fn raw_ptr_box<T>(t: T) -> *mut T {
| ^ required by this bound 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 Xyz` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:45: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
|
LL | impl<T> Box<T> {
| ^ required by this bound in `Box::<T>::new`

error[E0277]: the size for values of type `dyn Xyz` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:45:70
|
LL | = /* Box<$0> is coerced to Box<Xyz> here */ Box::new(x.unwrap());
| ^ ------ required by a bound introduced by this call
| |
| 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
|
LL | impl<T> Option<T> {
| ^ required by this bound in `Option::<T>::unwrap`

error[E0277]: the size for values of type `dyn Xyz` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:40: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
|
LL | pub enum Option<T> {
| ^ required by this bound in `None`

error[E0308]: mismatched types
--> $DIR/coerce-issue-49593-box-never.rs:48:13
|
LL | let mut x /* : Option<S> */ = None;
| ---- expected due to this value
...
LL | x = Some(S);
| ^^^^^^^ expected trait object `dyn Xyz`, found struct `S`
|
= note: expected enum `Option<dyn Xyz>`
found enum `Option<S>`

error[E0277]: the size for values of type `dyn Xyz` cannot be known at compilation time
--> $DIR/coerce-issue-49593-box-never.rs:54: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 a bound in `Option`
--> $SRC_DIR/core/src/option.rs:LL:COL
|
LL | pub enum Option<T> {
| ^ required by this bound in `Option`

error[E0308]: mismatched types
--> $DIR/coerce-issue-49593-box-never.rs:54:23
|
LL | mem::swap(&mut x, &mut y);
| --------- ^^^^^^ expected trait object `dyn Xyz`, found struct `S`
| |
| arguments to this function are incorrect
|
= note: expected mutable reference `&mut Option<dyn Xyz>`
found mutable reference `&mut Option<S>`
note: function defined here
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
|
= note: required for the cast from `()` to the object type `(dyn std::error::Error + 'static)`
LL | pub const fn swap<T>(x: &mut T, y: &mut T) {
| ^^^^

error: aborting due to 2 previous errors
error: aborting due to 8 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`.