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

RPITIT forces Self: Sized upon all other methods in a given trait. #115464

Closed
AhoyISki opened this issue Sep 2, 2023 · 2 comments · Fixed by #115467
Closed

RPITIT forces Self: Sized upon all other methods in a given trait. #115464

AhoyISki opened this issue Sep 2, 2023 · 2 comments · Fixed by #115467
Assignees
Labels
C-bug Category: This is a bug. F-return_position_impl_trait_in_trait `#![feature(return_position_impl_trait_in_trait)]` requires-nightly This issue requires a nightly compiler in some way. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@AhoyISki
Copy link

AhoyISki commented Sep 2, 2023

I tried this code:

#![feature(return_position_impl_trait_in_trait)]

fn main() {
    let vec: Vec<Box<dyn Trait>> = Vec::new();

    for i in vec {
        i.fn_2();
    }
}

trait OtherTrait {}

trait Trait {
    fn fn_1(&self) -> impl OtherTrait
    where
        Self: Sized;

    fn fn_2(&self) -> bool;
}

I expected to see this happen: Successful compilation, since fn_2 does not require that Self: Sized, which is the case in vec.

Instead, this happened: I got the following compiler error:

error[E0277]: the size for values of type `dyn Trait<bool>` cannot be known at compilation time
 --> src/main.rs:7:11
  |
7 |         i.fn_2();
  |           ^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `dyn Trait<bool>`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `rust-testing` (bin "rust-testing") due to previous error

Even though fn_2 does not require that Self: Sized. I'm not really sure that this is the intended behavior of this feature, since changing impl OtherTrait to any concrete type would allow this to compile just fine.

Meta

rustc --version --verbose:

rustc 1.74.0-nightly (35e416303 2023-09-01)
binary: rustc
commit-hash: 35e416303e6591a71ef6a91e006c602d2def3968
commit-date: 2023-09-01
host: x86_64-unknown-linux-gnu
release: 1.74.0-nightly
LLVM version: 17.0.0
@AhoyISki AhoyISki added the C-bug Category: This is a bug. label Sep 2, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 2, 2023
@compiler-errors compiler-errors added requires-nightly This issue requires a nightly compiler in some way. T-types Relevant to the types team, which will review and decide on the PR/issue. F-return_position_impl_trait_in_trait `#![feature(return_position_impl_trait_in_trait)]` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 2, 2023
@compiler-errors compiler-errors self-assigned this Sep 2, 2023
@compiler-errors
Copy link
Member

compiler-errors commented Sep 2, 2023

Ooh, lovely. Same problem with associated types, even after #112319:

trait Foo {
    type Bar<'a> where Self: Sized;

    fn test(&self) {}
}

fn test(x: &dyn Foo) {
    x.test();
    //~^ ERROR the trait `Foo` cannot be made into an object
}

@AhoyISki
Copy link
Author

AhoyISki commented Sep 2, 2023

Yeah, it kinda prevents any use of RPITIT in any trait that is meant to be used as a trait object 😕

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 4, 2023
…fety, r=oli-obk

Do not require associated types with Self: Sized to uphold bounds when confirming object candidate

RPITITs and associated types that have `Self: Sized` bounds are opted out of the `dyn Trait` well-formedness check that happens during confirmation. This ensures that we can actually *use* `dyn Trait`s that have associated types that, e.g., have GATs and RPITITs and other naughty things as long as those are opted-out of object safety via a `Self: Sized` bound.

Fixes rust-lang#115464

This seems like a natural part of rust-lang#112319 (comment), and I don't think needs re-litigation.

r? `@oli-obk`
@bors bors closed this as completed in 9c609ae Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-return_position_impl_trait_in_trait `#![feature(return_position_impl_trait_in_trait)]` requires-nightly This issue requires a nightly compiler in some way. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
3 participants