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

Allow dropping dyn Trait principal #126660

Closed

Conversation

Jules-Bertholet
Copy link
Contributor

@Jules-Bertholet Jules-Bertholet commented Jun 19, 2024

Redux of #114679, fixes #126313

This allows the following examples to compile:

trait Trait: Send {}
fn foo(x: &dyn Trait) -> &dyn Send { x }
trait Trait {}
fn foo(x: &(dyn Trait + Send)) -> &dyn Send { x }

This makes the language more consistent, as we already allow:

trait Trait {}
fn foo(x: &(dyn Trait + Send)) -> &dyn Trait { x }

The PR includes a test case, in tests/ui/traits/dyn-drop-principal.rs.

Documentation

dyn upcasting coercions, of any kind, are currently entirely undocumented in the reference. A future reference PR should address this.

@rustbot label T-lang T-types needs-fcp A-coercions A-trait-objects

@rustbot
Copy link
Collaborator

rustbot commented Jun 19, 2024

r? @fmease

rustbot has assigned @fmease.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jun 19, 2024
@rustbot
Copy link
Collaborator

rustbot commented Jun 19, 2024

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@rustbot rustbot added A-coercions Area: implicit and explicit `expr as Type` coercions A-trait-objects Area: trait objects, vtable layout needs-fcp This change is insta-stable, so needs a completed FCP to proceed. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. labels Jun 19, 2024
@fmease
Copy link
Member

fmease commented Jun 20, 2024

cc @compiler-errors
r? @lcnr

@rustbot rustbot assigned lcnr and unassigned fmease Jun 20, 2024
@compiler-errors
Copy link
Member

I'll let @lcnr do the judgement call of whether this needs a lang FCP or if types suffices. Thanks for picking this back up!

@compiler-errors compiler-errors removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 20, 2024
@lcnr
Copy link
Contributor

lcnr commented Jul 10, 2024

sorry for the delay!

nominating for lang, to let them decide whether they mind delegating this to t-types. We simply reuse the vtable of the parent trait when dropping the principal. This relies on all vtables to have the same header used for dropping and size_of_val/align_of_val. I don't know whether that's currently guaranteed anywhere, cc @rust-lang/opsem i guess?

Also, please extend the test to also check size_of_val and align_of_val 😁

@lcnr lcnr added the I-lang-nominated Nominated for discussion during a lang team meeting. label Jul 10, 2024
@RalfJung
Copy link
Member

Is there a Miri test that does this? This may run afoul of Miri's vtable validity check, which is supposed to ensure that the vtable has the same principal as the dyn trait type indicates.

This relies on all vtables to have the same header which is used for dropping and size_of_val/align_of_val. I don't know whether that's currently guaranteed anywhere, cc @rust-lang/opsem i guess?

vtables are entirely an implementation detail. In the op.sem they don't even exist in-memory, just as abstract opaque concepts (similar to function pointers). So we're free to do whatever here.

But it does mean we have to define the validity of wide pointers such that it's okay for the vtable to have a principal even if the type does not.

@rustbot
Copy link
Collaborator

rustbot commented Jul 10, 2024

The Miri subtree was changed

cc @rust-lang/miri

@Jules-Bertholet Jules-Bertholet requested a review from RalfJung July 10, 2024 17:01
@Jules-Bertholet
Copy link
Contributor Author

Also, please extend the test to also check size_of_val and align_of_val

Done

@RalfJung
Copy link
Member

I don't understand how this new test can pass, it seems like it should fail this check...

@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member

Miri test looks good, thanks!

@lcnr
Copy link
Contributor

lcnr commented Jul 16, 2024

This relies on all vtables to have the same header which is used for dropping and size_of_val/align_of_val. I don't know whether that's currently guaranteed anywhere, cc @rust-lang/opsem i guess?

vtables are entirely an implementation detail. In the op.sem they don't even exist in-memory, just as abstract opaque concepts (similar to function pointers). So we're free to do whatever here.

But it does mean we have to define the validity of wide pointers such that it's okay for the vtable to have a principal even if the type does not.

does that mean this also requires an opsem FCP? I would expect that this sort of type pruning is definitely sound. I would expect that shrinking the size of the pointee is totally safe and we can't assume that all vtables for the same type are equal.

@lcnr lcnr added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 14, 2024
@lcnr
Copy link
Contributor

lcnr commented Aug 14, 2024

cc @rust-lang/lang the nomination is for

nominating for lang, to let them decide whether they mind delegating this to t-types. We simply reuse the vtable of the parent trait when dropping the principal. This relies on all vtables to have the same header used for dropping and size_of_val/align_of_val.

@RalfJung
Copy link
Member

RalfJung commented Aug 14, 2024 via email

@traviscross
Copy link
Contributor

@rustbot labels -I-lang-nominated
@rfcbot fcp merge

We discussed this today in lang triage. This sounded right to us, in terms of making the language more consistent. We discussed whether we were closing any meaningful doors here and couldn't really find any. This seems in particular consistent with the direction that we set with dyn trait upcasting. We'll of course do this as a dual FCP with T-types.

@rfcbot
Copy link

rfcbot commented Aug 14, 2024

Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rustbot rustbot removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Aug 14, 2024
@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Aug 14, 2024
@scottmcm
Copy link
Member

This seems entirely reasonable to me -- it's basically like every trait has the supertrait ε and you can go from dyn Foo + ε to just dyn ε.

@rfcbot reviewed

@bors
Copy link
Contributor

bors commented Sep 17, 2024

☔ The latest upstream changes (presumably #130473) made this pull request unmergeable. Please resolve the merge conflicts.

@joshtriplett
Copy link
Member

Ping @nikomatsakis @pnkfelix @tmandry for remaining checkboxes.

@tmandry
Copy link
Member

tmandry commented Sep 18, 2024

@rfcbot reviewed

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Sep 18, 2024
@rfcbot
Copy link

rfcbot commented Sep 18, 2024

🔔 This is now entering its final comment period, as per the review above. 🔔

@nikomatsakis
Copy link
Contributor

@rfcbot reviewed

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Sep 28, 2024
@rfcbot
Copy link

rfcbot commented Sep 28, 2024

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

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

we still use data_a.principal_def_id() == data_b.principal_def_id() in coerce_dyn_star, can you add a test for that

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 18, 2024
… r=compiler-errors

Allow dropping dyn principal

Revival of rust-lang#126660, which was a revival of rust-lang#114679. Fixes rust-lang#126313.

Allows dropping principal when coercing trait objects, e.g. `dyn Debug + Send` -> `dyn Send`.

cc `@compiler-errors` `@Jules-Bertholet`
r? `@lcnr`
@RalfJung
Copy link
Member

Superseded by #131857 -- should this PR be closed?

rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 18, 2024
Rollup merge of rust-lang#131857 - WaffleLapkin:dyn-drop-principal-3, r=compiler-errors

Allow dropping dyn principal

Revival of rust-lang#126660, which was a revival of rust-lang#114679. Fixes rust-lang#126313.

Allows dropping principal when coercing trait objects, e.g. `dyn Debug + Send` -> `dyn Send`.

cc `@compiler-errors` `@Jules-Bertholet`
r? `@lcnr`
@Jules-Bertholet Jules-Bertholet deleted the dyn-drop-principal-2 branch December 14, 2024 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-trait-objects Area: trait objects, vtable layout disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. to-announce Announce this issue on triage meeting WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

trait Trait: Send: upcasting from dyn Trait to dyn Send does not work