diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 31685a012ca2f..4a5024b003c80 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -529,6 +529,30 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligation.predicate.to_poly_trait_ref(), trait_ref, )?); + + let lang_items = self.tcx().lang_items(); + if [lang_items.fn_once_trait(), lang_items.fn_trait(), lang_items.fn_mut_trait()] + .contains(&Some(obligation.predicate.def_id())) + { + // Do not allow `foo:: A>();` for `A: !Sized` (#82633) + let fn_sig = obligation.predicate.self_ty().skip_binder().fn_sig(self.tcx()); + let ty = fn_sig.output().skip_binder(); + let trait_ref = ty::TraitRef { + def_id: lang_items.sized_trait().unwrap(), + substs: self.tcx().mk_substs_trait(ty, &[]), + }; + if !matches!(ty.kind(), ty::Projection(_) | ty::Opaque(..)) { + let pred = crate::traits::util::predicate_for_trait_ref( + self.tcx(), + obligation.cause.clone(), + obligation.param_env, + trait_ref, + obligation.recursion_depth, + ); + obligations.push(pred); + } + } + Ok(ImplSourceFnPointerData { fn_ty: self_ty, nested: obligations }) } diff --git a/src/test/ui/closures/closure-return-type-must-be-sized.rs b/src/test/ui/closures/closure-return-type-must-be-sized.rs new file mode 100644 index 0000000000000..8c483f2439b51 --- /dev/null +++ b/src/test/ui/closures/closure-return-type-must-be-sized.rs @@ -0,0 +1,72 @@ +#![feature(unboxed_closures)] + +trait A { + fn a() where Self: Sized; +} + +mod a { + use A; + + pub fn foo>() where F::Output: A { + F::Output::a() + } + + pub fn bar R, R: ?Sized>() {} + + pub fn baz>() where F::Output: A, F::Output: Sized { + F::Output::a() + } +} + +mod b { + use A; + + pub fn foo>() where F::Output: A { + F::Output::a() + } + + pub fn bar R, R: ?Sized>() {} + + pub fn baz>() where F::Output: A, F::Output: Sized { + F::Output::a() + } +} + +mod c { + use A; + + pub fn foo>() where F::Output: A { + F::Output::a() + } + + pub fn bar R, R: ?Sized>() {} + + pub fn baz>() where F::Output: A, F::Output: Sized { + F::Output::a() + } +} + +impl A for Box { + fn a() {} +} + +fn main() { + a::foo:: dyn A>(); //~ERROR E0277 + a::bar:: dyn A, _>(); //~ERROR E0277 + a::baz:: dyn A>(); //~ERROR E0277 + a::foo:: Box>(); // ok + a::bar:: Box, _>(); // ok + a::baz:: Box>(); // ok + b::foo:: dyn A>(); //~ERROR E0277 + b::bar:: dyn A, _>(); //~ERROR E0277 + b::baz:: dyn A>(); //~ERROR E0277 + b::foo:: Box>(); // ok + b::bar:: Box, _>(); // ok + b::baz:: Box>(); // ok + c::foo:: dyn A>(); //~ERROR E0277 + c::bar:: dyn A, _>(); //~ERROR E0277 + c::baz:: dyn A>(); //~ERROR E0277 + c::foo:: Box>(); // ok + c::bar:: Box, _>(); // ok + c::baz:: Box>(); // ok +} diff --git a/src/test/ui/closures/closure-return-type-must-be-sized.stderr b/src/test/ui/closures/closure-return-type-must-be-sized.stderr new file mode 100644 index 0000000000000..f013e1b3c6528 --- /dev/null +++ b/src/test/ui/closures/closure-return-type-must-be-sized.stderr @@ -0,0 +1,99 @@ +error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time + --> $DIR/closure-return-type-must-be-sized.rs:54:5 + | +LL | a::foo:: dyn A>(); + | ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `(dyn A + 'static)` + +error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time + --> $DIR/closure-return-type-must-be-sized.rs:55:5 + | +LL | pub fn bar R, R: ?Sized>() {} + | ------------- required by this bound in `a::bar` +... +LL | a::bar:: dyn A, _>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `(dyn A + 'static)` + +error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time + --> $DIR/closure-return-type-must-be-sized.rs:56:5 + | +LL | pub fn baz>() where F::Output: A, F::Output: Sized { + | ----- required by this bound in `a::baz` +... +LL | a::baz:: dyn A>(); + | ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `(dyn A + 'static)` + +error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time + --> $DIR/closure-return-type-must-be-sized.rs:60:5 + | +LL | pub fn foo>() where F::Output: A { + | ------ required by this bound in `b::foo` +... +LL | b::foo:: dyn A>(); + | ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `(dyn A + 'static)` + +error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time + --> $DIR/closure-return-type-must-be-sized.rs:61:5 + | +LL | pub fn bar R, R: ?Sized>() {} + | --------- required by this bound in `b::bar` +... +LL | b::bar:: dyn A, _>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `(dyn A + 'static)` + +error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time + --> $DIR/closure-return-type-must-be-sized.rs:62:5 + | +LL | pub fn baz>() where F::Output: A, F::Output: Sized { + | ----- required by this bound in `b::baz` +... +LL | b::baz:: dyn A>(); + | ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `(dyn A + 'static)` + +error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time + --> $DIR/closure-return-type-must-be-sized.rs:66:5 + | +LL | pub fn foo>() where F::Output: A { + | --------- required by this bound in `c::foo` +... +LL | c::foo:: dyn A>(); + | ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `(dyn A + 'static)` + +error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time + --> $DIR/closure-return-type-must-be-sized.rs:67:5 + | +LL | pub fn bar R, R: ?Sized>() {} + | ------------ required by this bound in `c::bar` +... +LL | c::bar:: dyn A, _>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `(dyn A + 'static)` + +error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time + --> $DIR/closure-return-type-must-be-sized.rs:68:5 + | +LL | pub fn baz>() where F::Output: A, F::Output: Sized { + | ----- required by this bound in `c::baz` +... +LL | c::baz:: dyn A>(); + | ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `(dyn A + 'static)` + +error: aborting due to 9 previous errors + +For more information about this error, try `rustc --explain E0277`.