Skip to content
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

Enforce Sized return types on Fn* bounds #83915

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
28 changes: 27 additions & 1 deletion compiler/rustc_trait_selection/src/traits/select/confirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::traits::{
ImplSourceUserDefinedData,
};
use crate::traits::{ObjectCastObligation, PredicateObligation, TraitObligation};
use crate::traits::{Obligation, ObligationCause};
use crate::traits::{Obligation, ObligationCause, ObligationCauseCode};
use crate::traits::{SelectionError, Unimplemented};

use super::BuiltinImplConditions;
Expand Down Expand Up @@ -529,6 +529,32 @@ 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()))
// Skip `MiscObligation`s for better output.
&& matches!(obligation.cause.code, ObligationCauseCode::BindingObligation(_, _))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can look at the diff between the two commits to see what this check changes.

{
// Do not allow `foo::<fn() -> A>();` for `A: !Sized` (#82633)
let fn_sig = obligation.predicate.self_ty().skip_binder().fn_sig(self.tcx());
let ty = self.infcx.replace_bound_vars_with_placeholders(fn_sig.output());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using replace_bound_vars_with_placeholders pacified CI, but I still don't understand why it wasn't triggering on my machine. Maybe the assert! wasn't being run locally?

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(..)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewer context, the filtering out of projections and opaque types is a hack and shouldn't be necessary long term if we find a better place to inject the Sized obligation.

If I don't keep this in, then we start failing stage 1 builds. This tells me there's a better place to inject this obligation, but I haven't been able to find it.

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 })
}

Expand Down
72 changes: 72 additions & 0 deletions src/test/ui/closures/closure-return-type-must-be-sized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#![feature(unboxed_closures)]

trait A {
fn a() where Self: Sized;
}

mod a {
use A;

pub fn foo<F: FnOnce<()>>() where F::Output: A {
F::Output::a()
}

pub fn bar<F: FnOnce() -> R, R: ?Sized>() {}

pub fn baz<F: FnOnce<()>>() where F::Output: A, F::Output: Sized {
F::Output::a()
}
}

mod b {
use A;

pub fn foo<F: Fn<()>>() where F::Output: A {
F::Output::a()
}

pub fn bar<F: Fn() -> R, R: ?Sized>() {}

pub fn baz<F: Fn<()>>() where F::Output: A, F::Output: Sized {
F::Output::a()
}
}

mod c {
use A;

pub fn foo<F: FnMut<()>>() where F::Output: A {
F::Output::a()
}

pub fn bar<F: FnMut() -> R, R: ?Sized>() {}

pub fn baz<F: FnMut<()>>() where F::Output: A, F::Output: Sized {
F::Output::a()
}
}

impl A for Box<dyn A> {
fn a() {}
}

fn main() {
a::foo::<fn() -> dyn A>(); //~ERROR E0277
a::bar::<fn() -> dyn A, _>(); //~ERROR E0277
a::baz::<fn() -> dyn A>(); //~ERROR E0277
a::foo::<fn() -> Box<dyn A>>(); // ok
a::bar::<fn() -> Box<dyn A>, _>(); // ok
a::baz::<fn() -> Box<dyn A>>(); // ok
b::foo::<fn() -> dyn A>(); //~ERROR E0277
b::bar::<fn() -> dyn A, _>(); //~ERROR E0277
b::baz::<fn() -> dyn A>(); //~ERROR E0277
b::foo::<fn() -> Box<dyn A>>(); // ok
b::bar::<fn() -> Box<dyn A>, _>(); // ok
b::baz::<fn() -> Box<dyn A>>(); // ok
c::foo::<fn() -> dyn A>(); //~ERROR E0277
c::bar::<fn() -> dyn A, _>(); //~ERROR E0277
c::baz::<fn() -> dyn A>(); //~ERROR E0277
c::foo::<fn() -> Box<dyn A>>(); // ok
c::bar::<fn() -> Box<dyn A>, _>(); // ok
c::baz::<fn() -> Box<dyn A>>(); // ok
}
102 changes: 102 additions & 0 deletions src/test/ui/closures/closure-return-type-must-be-sized.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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 | pub fn foo<F: FnOnce<()>>() where F::Output: A {
| ---------- required by this bound in `a::foo`
...
LL | a::foo::<fn() -> 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<F: FnOnce() -> R, R: ?Sized>() {}
| ------------- required by this bound in `a::bar`
...
LL | a::bar::<fn() -> 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<F: FnOnce<()>>() where F::Output: A, F::Output: Sized {
| ----- required by this bound in `a::baz`
...
LL | a::baz::<fn() -> 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<F: Fn<()>>() where F::Output: A {
| ------ required by this bound in `b::foo`
...
LL | b::foo::<fn() -> 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<F: Fn() -> R, R: ?Sized>() {}
| --------- required by this bound in `b::bar`
...
LL | b::bar::<fn() -> 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<F: Fn<()>>() where F::Output: A, F::Output: Sized {
| ----- required by this bound in `b::baz`
...
LL | b::baz::<fn() -> 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<F: FnMut<()>>() where F::Output: A {
| --------- required by this bound in `c::foo`
...
LL | c::foo::<fn() -> 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<F: FnMut() -> R, R: ?Sized>() {}
| ------------ required by this bound in `c::bar`
...
LL | c::bar::<fn() -> 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<F: FnMut<()>>() where F::Output: A, F::Output: Sized {
| ----- required by this bound in `c::baz`
...
LL | c::baz::<fn() -> 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`.