From 507afdaaa2ef411511a50c2c6c6e1a900475fcb6 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 11 Dec 2022 19:33:41 +0000 Subject: [PATCH] Don't consider sized type variables in coerce_unsized --- compiler/rustc_hir_typeck/src/coercion.rs | 30 +---- .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 12 -- ...ce-issue-49593-box-never.nofallback.stderr | 123 ++++++++++++++++-- 3 files changed, 114 insertions(+), 51 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index f0b349f0c98dd..0302a1162dcf7 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -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` where we know that `$0: Sized`, try going - // for unsizing. - } - _ => { - // Some other case for `$0: Unsize`. 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); } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 952d272625918..13d11591b367d 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -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> { vec![self.tcx.ty_error(); len] } diff --git a/src/test/ui/coercion/coerce-issue-49593-box-never.nofallback.stderr b/src/test/ui/coercion/coerce-issue-49593-box-never.nofallback.stderr index 322681b97bccb..73ed96691dd9f 100644 --- a/src/test/ui/coercion/coerce-issue-49593-box-never.nofallback.stderr +++ b/src/test/ui/coercion/coerce-issue-49593-box-never.nofallback.stderr @@ -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 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::::new` + --> $SRC_DIR/alloc/src/boxed.rs:LL:COL + | +LL | impl Box { + | ^ required by this bound in `Box::::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) -> *mut T { + | ^ required by this bound in `raw_ptr_box` +help: consider relaxing the implicit `Sized` restriction + | +LL | fn raw_ptr_box(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 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::::new` + --> $SRC_DIR/alloc/src/boxed.rs:LL:COL + | +LL | impl Box { + | ^ required by this bound in `Box::::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 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::::unwrap` + --> $SRC_DIR/core/src/option.rs:LL:COL + | +LL | impl Option { + | ^ required by this bound in `Option::::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 */ = 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 { + | ^ required by this bound in `None` + +error[E0308]: mismatched types + --> $DIR/coerce-issue-49593-box-never.rs:48:13 + | +LL | let mut x /* : Option */ = None; + | ---- expected due to this value +... +LL | x = Some(S); + | ^^^^^^^ expected trait object `dyn Xyz`, found struct `S` + | + = note: expected enum `Option` + found enum `Option` + +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 { + | ^ 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` + found mutable reference `&mut Option` +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(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`.