Skip to content

Conversation

@BoxyUwU
Copy link
Member

@BoxyUwU BoxyUwU commented Feb 9, 2025

Fixes #136702

Reference PR:

Background reading about VTable calls/dyn compatibility: https://hackmd.io/zUp-sgZ0RFuFgsNfD4JqYw

This PR causes us to start enforcing that lifetimes of dyn types are constrained through pointer casts. Currently on stable casting *mut dyn Trait + 'a to *mut dyn Trait + 'b passes with no requirements on 'a or 'b. Under this PR we now require 'a to outlive 'b.

Even though the pointee of *mut pointers is considered to be invariant, we still use subtyping rather than equality. This mirrors how we support coercing &mut dyn Trait + 'a to &mut dyn Trait + 'b while requiring only 'a: 'b. I believe this coercion is sound as there is no way for safe code to mem::swap two dyn Trait's, and the same is definitely true of raw pointers.

See the changes to this test: https://github.com/rust-lang/rust/pull/136776/files#diff-5523f20a800287a89c9f3e92646c887f3f7599be006b29dd9315f734a2137764

We also do not enforce any constraints on the lifetime of the dyn types if there are multiple pointer indirections. For example *mut *mut dyn Trait + 'a is allowed to be casted to *mut *mut dyn Trait + 'b with no requirements on 'a or 'b`. This case is just a normal thin pointer cast where we do not care about the pointee type as there is no VTable in play.

Test: https://github.com/rust-lang/rust/pull/136776/files#diff-3b6c8da342bb6530524158d686455a545bb8fd6f59cf5ff50d1d991ce74c9649

Finally, this is about any cast where the pointee is unsized with dyn-type metadata, not just literally the pointee type being a dyn-type. E.g. casting *mut Wrapper<dyn Trait + 'a> to *mut Wrapper<dyn Trait + 'b> requires 'a: 'b under this PR.

Test: https://github.com/rust-lang/rust/pull/136776/files#diff-ca0c44df62ae1ad1be70f892f01a59714336c7baf78602a5887ac1cf81145c96

Breakage

This is a breaking change.
Crater Report Comment: #136776 (comment)
Generated Report: https://crater-reports.s3.amazonaws.com/pr-136776/index.html

The majority of the breakage is caused by the metrics crate with 142 of the regressions, and the may crate with 14 of the regressions. The metrics crate has been fixed and has backported the fix to previous versions of the crate that were also affected. Themay crate has also been fixed.

PRs against affected crates have been opened and can be seen here:

There were three regressions I've not filed PRs against:

r? @ghost

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 9, 2025
@BoxyUwU
Copy link
Member Author

BoxyUwU commented Feb 9, 2025

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 9, 2025
…, r=<try>

[WIP] Forbid object lifetime changing pointer casts

Fixes rust-lang#136702

r? `@ghost`
@bors
Copy link
Collaborator

bors commented Feb 9, 2025

⌛ Trying commit d5ebeac with merge 44f3504...

@bors
Copy link
Collaborator

bors commented Feb 9, 2025

☀️ Try build successful - checks-actions
Build commit: 44f3504 (44f3504e96c944ae54fc72b5f5008f53f7eda001)

@BoxyUwU
Copy link
Member Author

BoxyUwU commented Feb 9, 2025

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-136776 created and queued.
🤖 Automatically detected try build 44f3504
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 9, 2025
@craterbot
Copy link
Collaborator

🚧 Experiment pr-136776 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-136776 is completed!
📊 169 regressed and 4 fixed (580506 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Feb 11, 2025
@RalfJung
Copy link
Member

RalfJung commented Feb 11, 2025

Most of these are on github; in terms of crates.io regressions all we have is:

  • may
  • a bunch of crates using metrics, see e.g. this (for metrics-0.23) or this (for metrics-0.24, the latest version)

Overall, 142 regressions are caused by metrics and 14 by may; if we ca get fixed versions of those crates out that seems to mostly cover it.

EDIT: Ah, there's also cogo.

@traviscross traviscross added the I-lang-nominated Nominated for discussion during a lang team meeting. label Feb 12, 2025
@traviscross
Copy link
Contributor

We discussed this in the lang triage call today. We wanted to think more about it, so we're leaving it nominated to discuss again.

@tmandry
Copy link
Member

tmandry commented Feb 19, 2025

@BoxyUwU Do you think it would be possible to implement this as an FCW? We talked about this in lang triage today and would prefer to start with that if we can. If it's not feasible, a hard error can also work (I would say though that we should upstream PRs to any crates we break).

Another small thing I noticed is that the error message links to the Nomicon section on variance, but it would be ideal to link to a tracking issue or something describing this issue in particular.

@traviscross
Copy link
Contributor

traviscross commented Feb 19, 2025

To add on to what tmandry, said, in our discussions we did feel that the approach taken in this PR is generally the right way forward, and we're happy to see this progress so as to help clear the way for arbitrary_self_types and derive_coerce_pointee.

cc @rust-lang/lang

@BoxyUwU
Copy link
Member Author

BoxyUwU commented Feb 26, 2025

@tmandry I do expect it to be possible to FCW this. We can likely do something hacky around to fully emulate the fix (but as a lint), but if that doesn't work out all the regression we found were relatively "simple" cases that can probably be taken advantage of (if need be) to lint a subset of the actual cases we'd break with this PR

edit: see compiler-errors' comment, I'm not so convinced this will be possible to FCW anymore and will likely investigate improving the diagnostics here. I've already filed PRs to the affected crates to migrate them over to a transmute to avoid the breakage if this lands

@compiler-errors
Copy link
Member

I was thinking earlier that it may be possible to implement a lint to detect, but it seems to me that MIR borrowck is not equipped to implement such a lint.

Specifically, it seems near impossible to answer whether a region outlives constraint (like, 'a: 'b) would not hold in a way that doesn't actually commit to that constraint, at least not without tons of false positives based on how NLL computes lower bounds for all of the regions it deals with in the MIR.

To fix this would require some significant engineering effort to refactor how NLL processes its region graph to make it easier to clone and reprocess with new constraints.

workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Mar 4, 2025
…uto_to_object-hard-error, r=oli-obk

Make `ptr_cast_add_auto_to_object` lint into hard error

In Rust 1.81, we added a FCW lint (including linting in dependencies) against pointer casts that add an auto trait to dyn bounds.  This was part of work making casts of pointers involving trait objects stricter, and was part of the work needed to restabilize trait upcasting.

We considered just making this a hard error, but opted against it at that time due to breakage found by crater.  This breakage was mostly due to the `anymap` crate which has been a persistent problem for us.

It's now a year later, and the fact that this is not yet a hard error is giving us pause about stabilizing arbitrary self types and `derive(CoercePointee)`.  So let's see about making a hard error of this.

r? ghost

cc `@adetaylor` `@Darksonn` `@BoxyUwU` `@RalfJung` `@compiler-errors` `@oli-obk` `@WaffleLapkin`

Related:

- rust-lang#135881
- rust-lang#136702
- rust-lang#136776

Tracking:

- rust-lang#127323
- rust-lang#44874
- rust-lang#123430
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Mar 5, 2025
…uto_to_object-hard-error, r=oli-obk

Make `ptr_cast_add_auto_to_object` lint into hard error

In Rust 1.81, we added a FCW lint (including linting in dependencies) against pointer casts that add an auto trait to dyn bounds.  This was part of work making casts of pointers involving trait objects stricter, and was part of the work needed to restabilize trait upcasting.

We considered just making this a hard error, but opted against it at that time due to breakage found by crater.  This breakage was mostly due to the `anymap` crate which has been a persistent problem for us.

It's now a year later, and the fact that this is not yet a hard error is giving us pause about stabilizing arbitrary self types and `derive(CoercePointee)`.  So let's see about making a hard error of this.

r? ghost

cc ``@adetaylor`` ``@Darksonn`` ``@BoxyUwU`` ``@RalfJung`` ``@compiler-errors`` ``@oli-obk`` ``@WaffleLapkin``

Related:

- rust-lang#135881
- rust-lang#136702
- rust-lang#136776

Tracking:

- rust-lang#127323
- rust-lang#44874
- rust-lang#123430
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Mar 5, 2025
…uto_to_object-hard-error, r=oli-obk

Make `ptr_cast_add_auto_to_object` lint into hard error

In Rust 1.81, we added a FCW lint (including linting in dependencies) against pointer casts that add an auto trait to dyn bounds.  This was part of work making casts of pointers involving trait objects stricter, and was part of the work needed to restabilize trait upcasting.

We considered just making this a hard error, but opted against it at that time due to breakage found by crater.  This breakage was mostly due to the `anymap` crate which has been a persistent problem for us.

It's now a year later, and the fact that this is not yet a hard error is giving us pause about stabilizing arbitrary self types and `derive(CoercePointee)`.  So let's see about making a hard error of this.

r? ghost

cc ```@adetaylor``` ```@Darksonn``` ```@BoxyUwU``` ```@RalfJung``` ```@compiler-errors``` ```@oli-obk``` ```@WaffleLapkin```

Related:

- rust-lang#135881
- rust-lang#136702
- rust-lang#136776

Tracking:

- rust-lang#127323
- rust-lang#44874
- rust-lang#123430
@lcnr lcnr added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 17, 2025
@BoxyUwU
Copy link
Member Author

BoxyUwU commented Sep 29, 2025

@rfcbot resolve can we do a FCW

I get the impression that people don't seem too concerned about landing this without an FCW and I think it'll be a while before I can get around to looking into if we even can

@rust-rfcbot rust-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 29, 2025
@rust-rfcbot
Copy link
Collaborator

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

@rust-rfcbot rust-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 Oct 9, 2025
@rust-rfcbot
Copy link
Collaborator

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.

@lcnr
Copy link
Contributor

lcnr commented Oct 13, 2025

Given the breakage, I would try to merge this right after the next beta cutoff to have the full 6 weeks on nightly

{
diag.span_note(
constraint.span,
format!("raw pointer casts of trait objects do not cast away lifetimes"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
format!("raw pointer casts of trait objects do not cast away lifetimes"),
format!("raw pointer casts of trait objects must not extend lifetimes"),

maybe? "cast away" sounds weird

Copy link
Contributor

Choose a reason for hiding this comment

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

"must not" sounds like "you can do it but you shouldn't". How about "can't extend lifetimes"?

Copy link
Member

Choose a reason for hiding this comment

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

I learned in school that "must not" means "is not allowed to". But maybe it doesn't universally have that meaning?

Copy link
Contributor

Choose a reason for hiding this comment

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

Well... It does mean that it's not allowed to do it. But to me "not allowed to" brings forward the wrong connotations compared to "can't"/"not able to"/"not possible" in the specific case of a compiler error message.

Comment on lines 1529 to 1544
// Trait parameters are Invariant, the only part that actually has subtyping
// here is the lifetime bound of the dyn-type.
//
// For example in `dyn Trait<'a> + 'b <: dyn Trait<'c> + 'd` we would require
// that `'a == 'c` but only that `'b: 'd`.
//
// We must not allow freely casting lifetime bounds of dyn-types as it may allow
// for inaccessible VTable methods being callable: #136702
//
// We don't enforce this for casts of principal-less dyn types as their VTables do
// not contain any functions with `Self: 'a` bounds that could start holding after
// a pointer cast.
//
// We also don't enforce this for casts of pointers to pointers to dyn types. E.g.
// `*mut *mut dyn Trait + 'a -> *mut *mut dyn Trait + 'static` is allowed. This is
// fine because there is no actual VTable in play.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Trait parameters are Invariant, the only part that actually has subtyping
// here is the lifetime bound of the dyn-type.
//
// For example in `dyn Trait<'a> + 'b <: dyn Trait<'c> + 'd` we would require
// that `'a == 'c` but only that `'b: 'd`.
//
// We must not allow freely casting lifetime bounds of dyn-types as it may allow
// for inaccessible VTable methods being callable: #136702
//
// We don't enforce this for casts of principal-less dyn types as their VTables do
// not contain any functions with `Self: 'a` bounds that could start holding after
// a pointer cast.
//
// We also don't enforce this for casts of pointers to pointers to dyn types. E.g.
// `*mut *mut dyn Trait + 'a -> *mut *mut dyn Trait + 'static` is allowed. This is
// fine because there is no actual VTable in play.
// Trait parameters are invariant, the only part that actually has subtyping
// here is the lifetime bound of the dyn-type.
//
// For example in `dyn Trait<'a> + 'b <: dyn Trait<'c> + 'd` we would require
// that `'a == 'c` but only that `'b: 'd`.
//
// We must not allow freely casting lifetime bounds of dyn-types as it may allow
// for inaccessible VTable methods being callable: #136702
//
// We don't enforce this for casts of principal-less dyn types as their VTables do
// not contain any functions with `Self: 'a` bounds that could start holding after
// a pointer cast.
//
// We also don't enforce this for casts of pointers to pointers to dyn types. E.g.
// `*mut *mut dyn Trait + 'a -> *mut *mut dyn Trait + 'static` is allowed. This is
// fine because there is no actual VTable in play.

Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like allowing this for principal-less trait objects is quite inconsistent :< what's the additional breakage when always checking this, even if there are only auto traits

Copy link
Contributor

Choose a reason for hiding this comment

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

What is meant by "principal-less" here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

So this is about casting dyn Send + 'short to dyn Send + 'long? I'm not convinced there's any reason to have a special case in the language for that particular case.

Copy link
Member Author

@BoxyUwU BoxyUwU Oct 13, 2025

Choose a reason for hiding this comment

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

I feel like allowing this for principal-less trait objects is quite inconsistent

I agree with u 🤔 I guess this would imply that we never have unconditional vtable methods that depend on lifetimes. E.g. sticking a type_id fn in all vtables like we do for size_of. I've no idea what the additional breakage is like. I can check this for auto-trait only dyn-types and do a second crater run (which will also detect any additional cases in the wild since the last one...)

Copy link
Contributor

Choose a reason for hiding this comment

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

please do ❤️

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there anything I can do to help here?

Copy link
Member Author

@BoxyUwU BoxyUwU Nov 21, 2025

Choose a reason for hiding this comment

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

If you'd be able to checkout this PR and maybe rebase it then make a commit to check for the case where the target type has no principal trait and if so push an outlives requirement that the lifetime of the source trait object outlives the lifetime of the target trait object, that would be helpful 🤔 then its just a matter of queueing a crater run and checking it's not got more regressions than the previous one

Copy link
Contributor

Choose a reason for hiding this comment

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

Done.

@BoxyUwU BoxyUwU added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. labels Oct 21, 2025
@Darksonn Darksonn force-pushed the forbid_object_lifetime_casts branch from a3d0dac to d525233 Compare November 21, 2025 14:48
@rustbot
Copy link
Collaborator

rustbot commented Nov 21, 2025

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Darksonn
Copy link
Contributor

@bors try

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Nov 21, 2025
Forbid freely casting lifetime bounds of dyn-types
@rust-bors
Copy link

rust-bors bot commented Nov 21, 2025

☀️ Try build successful (CI)
Build commit: 7424ce9 (7424ce9ff9baac3e9d9f5dd3c85828cddea0134d, parent: e22dab387f6b4f6a87dfc54ac2f6013dddb41e68)

@Darksonn
Copy link
Contributor

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-136776-1 created and queued.
🤖 Automatically detected try build 7424ce9
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 21, 2025
Comment on lines +1581 to +1582
&& src_tty.principal().is_none()
&& dst_tty.principal().is_none()
Copy link
Member Author

Choose a reason for hiding this comment

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

@Darksonn I think this currently means that casting something like: *mut dyn Trait + Send + 'a to *mut Send + 'b won't check 'a: 'b. We ought to drop the src_tty.principal().is_none() check here, unless I'm misreading this code :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. S-waiting-on-crater Status: Waiting on a crater run to be completed. T-lang Relevant to the language team T-types Relevant to the types team, which will review and decide on the PR/issue. to-announce Announce this issue on triage meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

arbitrary_self_types + derive_coerce_pointee allows calling methods whose where clauses are violated