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

Add trait_upcasting related languages changes #1622

Merged
merged 4 commits into from
Feb 8, 2025

Conversation

WaffleLapkin
Copy link
Member

@WaffleLapkin WaffleLapkin commented Sep 20, 2024

I applied review comments from #1259 and added updates from rust-lang/rust#120248.

Tracking issue: rust-lang/rust#65991
Stabilization proposal: rust-lang/rust#134367

cc @crlf0710 @compiler-errors @RalfJung

@WaffleLapkin
Copy link
Member Author

rust-lang/rust#101336 is still open, did we actually end up committing to something?

@compiler-errors
Copy link
Member

compiler-errors commented Sep 21, 2024

@WaffleLapkin: Yes, it's just pending documentation rust-lang/rust#101336 (comment) -- presumably this is (A.) on Niko's list.

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

You may also want to edit https://doc.rust-lang.org/reference/behavior-considered-undefined.html as Niko mentioned in the issue you linked; you may want to re-read the FCP, but I believe it's UB to conjure up a vtable now since it must be valid for upcasting.

src/type-coercions.md Show resolved Hide resolved
src/expressions/operator-expr.md Outdated Show resolved Hide resolved
@RalfJung
Copy link
Member

You may also want to edit https://doc.rust-lang.org/reference/behavior-considered-undefined.html as Niko mentioned in the issue you linked; you may want to re-read the FCP, but I believe it's UB to conjure up a vtable now since it must be valid for upcasting.

What exactly are you referring to here? I assume the "issue" is rust-lang/rust#101336 (a link would have been good, it took clicking on 4 links to track this down :D ). Niko says a lot of things there, though. :)

@WaffleLapkin
Copy link
Member Author

@compiler-errors doc.rust-lang.org/reference/behavior-considered-undefined.html already mentions invalid metadata in any kind of pointer makes an invalid value, so I don't think we need to add anything on top of that?

  • Invalid metadata in a wide reference, Box<T>, or raw pointer. The requirement for the metadata is determined by the type of the unsized tail:
    • dyn Trait metadata is invalid if it is not a pointer to a vtable for Trait.
    • Slice ([T]) metadata is invalid if the length is not a valid usize (i.e., it must not be read from uninitialized memory). Furthermore, for wide references and Box<T>, slice metadata is invalid if it makes the total size of the pointed-to value bigger than isize::MAX.

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

I believe this is ready and consistent with what is implemented on master. I think all that needs to be done w.r.t. trait upcasting is a fresh stabilization PR?

@traviscross traviscross added the S-waiting-on-stabilization Waiting for a stabilization PR to be merged in the main Rust repository label Oct 22, 2024
@rustbot

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. label Dec 3, 2024
@PoignardAzur
Copy link

Any movement on this?

@WaffleLapkin
Copy link
Member Author

@PoignardAzur I'm working on a stabilization PR (after which this can be merged)

@WaffleLapkin
Copy link
Member Author

@compiler-errors could you take another look, do the changes still make sense?

@WaffleLapkin
Copy link
Member Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: The marked PR is awaiting review from a maintainer and removed S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. labels Dec 16, 2024
src/type-coercions.md Outdated Show resolved Hide resolved
@traviscross
Copy link
Contributor

In the original PR for this...

...there's a discussion about adding a line to the behavior considered undefined of:

  • Performing non-nop coercion on a dangling or unaligned raw pointer.

There was some back and forth and it's not clear where that landed. Thoughts on that?

@compiler-errors @RalfJung @WaffleLapkin

@compiler-errors
Copy link
Member

The comment here is the answer https://github.com/rust-lang/reference/pull/1259/files#r1404804424. I don't think this needs further changing. It doesn't really matter if the pointer is bad; upcasting only cares about the vtable metadata.

src/type-coercions.md Outdated Show resolved Hide resolved
src/type-coercions.md Show resolved Hide resolved
src/expressions/operator-expr.md Outdated Show resolved Hide resolved
@nikomatsakis
Copy link
Contributor

@compiler-errors doc.rust-lang.org/reference/behavior-considered-undefined.html already mentions invalid metadata in any kind of pointer makes an invalid value, so I don't think we need to add anything on top of that?

  • Invalid metadata in a wide reference, Box<T>, or raw pointer. The requirement for the metadata is determined by the type of the unsized tail:

    • dyn Trait metadata is invalid if it is not a pointer to a vtable for Trait.
    • Slice ([T]) metadata is invalid if the length is not a valid usize (i.e., it must not be read from uninitialized memory). Furthermore, for wide references and Box<T>, slice metadata is invalid if it makes the total size of the pointed-to value bigger than isize::MAX.

@WaffleLapkin I think this isn't quite right. How I read the Behaviors Considered Undefined section, it says...

Producing an invalid value. “Producing” a value happens any time a value is assigned to or read from a place, passed to a function/primitive operation or returned from a function/primitive operation.

where "producing an invalid value is hence immediate UB". But the consensus from rust-lang/rust#101336 was that having an invalid vtable is not a failure of the validity invariant. Rather, the UB occurs when a dyn pointer (raw, reference, whatever) with a malformed vtable is upcast or has a method invoked on it. Because safe code can perform those operations (including on raw pointers, in the case of upcast and potentially method calls in the future), this implies that the safety invariant (the conditions required to release such a value to safe code) requires a valid vtable. But if you can keep the *const dyn Foo confined to unsafe code and be absolutely sure nobody upcasts it, it doesn't require a valid vtable. Quoting rust-lang/rust#101336:

The proposal is as follows:

  • Vtable-adjusting upcasts are safe operations. The upcast is UB if performed on a value without a valid vtable
  • As such, the "safety invariant" requires a fully valid vtable.
  • The "validity invariant" requires *dyn metadata to be word-aligned and non-null.

Vtable-adjusting upcasts are defined as:

  • Trait upcasts that alter the set of methods that can be invoked on the resulting value at runtime (e.g., dyn Bar to dyn Foo from the introduction). In particular, upcasts that simply add or remove auto-traits are not vtable-adjusting (e.g., dyn Debug + Send to dyn Debug).

@RalfJung
Copy link
Member

But the consensus from rust-lang/rust#101336 was that having an invalid vtable is not a failure of the validity invariant.

To be clear, consensus was that we are not ruling on the validity invariant at this point. The validity invariant for vtable metadata is an open question, discussed at rust-lang/unsafe-code-guidelines#516. Meanwhile, the reference says that the vtable pointer must point to a vtable of the right trait and Miri enforces this; this is following our usual strategy of having a bit more UB for now and possibly relaxing this in the future.

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Dec 20, 2024 via email

@RalfJung
Copy link
Member

We currently say this above the UB list

The following list is not exhaustive; it may grow or shrink.

So the entire thing should be considered as approximating. Maybe we should start slowly widdling this down to just a few items in the list, though I think we should always reserve the right to remove UB.

Anyway this is getting off-topic for this PR. The validity invariant for vtables doesn't have to be settled for this one.

@traviscross traviscross removed the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Feb 5, 2025
@WaffleLapkin
Copy link
Member Author

@rustbot label -I-lang-nominated

I've also prepared a refactor of the unsizing section: de3ffc3. I'll open a PR once this is merged.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 7, 2025
…treat, r=compiler-errors

Stabilize `feature(trait_upcasting)`

This feature was "done" for a while now, I think it's finally time to stabilize it! Stabilization report: rust-lang#134367 (comment).
cc reference PR: rust-lang/reference#1622.

Closes rust-lang#65991 (tracking issue), closes rust-lang#89460 (the lint is no longer future incompat).

r? compiler-errors
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 7, 2025
…treat, r=compiler-errors

Stabilize `feature(trait_upcasting)`

This feature was "done" for a while now, I think it's finally time to stabilize it! Stabilization report: rust-lang#134367 (comment).
cc reference PR: rust-lang/reference#1622.

Closes rust-lang#65991 (tracking issue), closes rust-lang#89460 (the lint is no longer future incompat).

r? compiler-errors
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 7, 2025
Rollup merge of rust-lang#134367 - WaffleLapkin:trait_upcasting_as_a_treat, r=compiler-errors

Stabilize `feature(trait_upcasting)`

This feature was "done" for a while now, I think it's finally time to stabilize it! Stabilization report: rust-lang#134367 (comment).
cc reference PR: rust-lang/reference#1622.

Closes rust-lang#65991 (tracking issue), closes rust-lang#89460 (the lint is no longer future incompat).

r? compiler-errors
@WaffleLapkin
Copy link
Member Author

rust-lang/rust#134367 (comment) has been merged, so this can be merged as well :)

github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Feb 8, 2025
…compiler-errors

Stabilize `feature(trait_upcasting)`

This feature was "done" for a while now, I think it's finally time to stabilize it! Stabilization report: rust-lang/rust#134367 (comment).
cc reference PR: rust-lang/reference#1622.

Closes #65991 (tracking issue), closes #89460 (the lint is no longer future incompat).

r? compiler-errors
WaffleLapkin and others added 3 commits February 8, 2025 21:48
This aligns the reference with the results of r-l/r/120248.
Co-authored-by: Michael Goulet <[email protected]>
Co-authored-by: Charles Lew <[email protected]>
I.e., document the behavior after r-l/r/119338.
@traviscross
Copy link
Contributor

traviscross commented Feb 8, 2025

I rebased to collapse some back and forth, to fix an author name, to make some minor stylistic tweaks (capitalization and line unwrapping), and to remove the note section mentioned above.

We're anticipating further work on this to address some things raised in the threads here in a follow-up PR that Waffle has in progress.

@traviscross traviscross added this pull request to the merge queue Feb 8, 2025
@traviscross traviscross removed the S-waiting-on-stabilization Waiting for a stabilization PR to be merged in the main Rust repository label Feb 8, 2025
Merged via the queue into rust-lang:master with commit de2d528 Feb 8, 2025
5 checks passed
@WaffleLapkin WaffleLapkin deleted the trait_upcast branch February 9, 2025 13:53
@WaffleLapkin
Copy link
Member Author

WaffleLapkin commented Feb 9, 2025

Follow-up: #1731

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 10, 2025
Update books

## rust-lang/edition-guide

1 commits in f56aecc3b036dff16404b525a83b00f911b9bbea..8dbdda7cae4fa030f09f8f5b63994d4d1dde74b9
2025-02-06 20:06:58 UTC to 2025-02-06 20:06:58 UTC

- Correct spelling error in `static-mut-references.md` (rust-lang/edition-guide#358)

## rust-embedded/book

1 commits in ddbf1b4e2858fedb71b7c42eb15c4576517dc125..0b8219ac23a3e09464e4e0166c768cf1c4bba0d5
2025-02-07 08:26:59 UTC to 2025-02-07 08:26:59 UTC

- Update gcc toolchain download link for windows (rust-embedded/book#384)

## rust-lang/reference

3 commits in 4249fb411dd27f945e2881eb0378044b94cee06f..de2d5289e45506b11dd652bef4f99de64be70e1c
2025-02-08 22:12:20 UTC to 2025-02-06 19:02:01 UTC

- Add trait_upcasting related languages changes (rust-lang/reference#1622)
- fixup `»` insertion for rules (rust-lang/reference#1730)
- doc `cenum_impl_drop_cast` (rust-lang/reference#1713)

## rust-lang/rust-by-example

2 commits in 743766929f1e53e72fab74394ae259bbfb4a7619..66543bbc5b7dbd4e679092c07ae06ba6c73fd912
2025-02-06 12:38:08 UTC to 2025-02-04 01:46:09 UTC

- Add more examples @ drop.md (rust-lang/rust-by-example#1912)
- es translation to 3100 (rust-lang/rust-by-example#1911)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 10, 2025
Update books

## rust-lang/edition-guide

1 commits in f56aecc3b036dff16404b525a83b00f911b9bbea..8dbdda7cae4fa030f09f8f5b63994d4d1dde74b9
2025-02-06 20:06:58 UTC to 2025-02-06 20:06:58 UTC

- Correct spelling error in `static-mut-references.md` (rust-lang/edition-guide#358)

## rust-embedded/book

1 commits in ddbf1b4e2858fedb71b7c42eb15c4576517dc125..0b8219ac23a3e09464e4e0166c768cf1c4bba0d5
2025-02-07 08:26:59 UTC to 2025-02-07 08:26:59 UTC

- Update gcc toolchain download link for windows (rust-embedded/book#384)

## rust-lang/reference

3 commits in 4249fb411dd27f945e2881eb0378044b94cee06f..de2d5289e45506b11dd652bef4f99de64be70e1c
2025-02-08 22:12:20 UTC to 2025-02-06 19:02:01 UTC

- Add trait_upcasting related languages changes (rust-lang/reference#1622)
- fixup `»` insertion for rules (rust-lang/reference#1730)
- doc `cenum_impl_drop_cast` (rust-lang/reference#1713)

## rust-lang/rust-by-example

2 commits in 743766929f1e53e72fab74394ae259bbfb4a7619..66543bbc5b7dbd4e679092c07ae06ba6c73fd912
2025-02-06 12:38:08 UTC to 2025-02-04 01:46:09 UTC

- Add more examples @ drop.md (rust-lang/rust-by-example#1912)
- es translation to 3100 (rust-lang/rust-by-example#1911)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 11, 2025
Update books

## rust-lang/edition-guide

1 commits in f56aecc3b036dff16404b525a83b00f911b9bbea..8dbdda7cae4fa030f09f8f5b63994d4d1dde74b9
2025-02-06 20:06:58 UTC to 2025-02-06 20:06:58 UTC

- Correct spelling error in `static-mut-references.md` (rust-lang/edition-guide#358)

## rust-embedded/book

1 commits in ddbf1b4e2858fedb71b7c42eb15c4576517dc125..0b8219ac23a3e09464e4e0166c768cf1c4bba0d5
2025-02-07 08:26:59 UTC to 2025-02-07 08:26:59 UTC

- Update gcc toolchain download link for windows (rust-embedded/book#384)

## rust-lang/reference

3 commits in 4249fb411dd27f945e2881eb0378044b94cee06f..de2d5289e45506b11dd652bef4f99de64be70e1c
2025-02-08 22:12:20 UTC to 2025-02-06 19:02:01 UTC

- Add trait_upcasting related languages changes (rust-lang/reference#1622)
- fixup `»` insertion for rules (rust-lang/reference#1730)
- doc `cenum_impl_drop_cast` (rust-lang/reference#1713)

## rust-lang/rust-by-example

2 commits in 743766929f1e53e72fab74394ae259bbfb4a7619..66543bbc5b7dbd4e679092c07ae06ba6c73fd912
2025-02-06 12:38:08 UTC to 2025-02-04 01:46:09 UTC

- Add more examples @ drop.md (rust-lang/rust-by-example#1912)
- es translation to 3100 (rust-lang/rust-by-example#1911)
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 11, 2025
Rollup merge of rust-lang#136825 - rustbot:docs-update, r=ehuss

Update books

## rust-lang/edition-guide

1 commits in f56aecc3b036dff16404b525a83b00f911b9bbea..8dbdda7cae4fa030f09f8f5b63994d4d1dde74b9
2025-02-06 20:06:58 UTC to 2025-02-06 20:06:58 UTC

- Correct spelling error in `static-mut-references.md` (rust-lang/edition-guide#358)

## rust-embedded/book

1 commits in ddbf1b4e2858fedb71b7c42eb15c4576517dc125..0b8219ac23a3e09464e4e0166c768cf1c4bba0d5
2025-02-07 08:26:59 UTC to 2025-02-07 08:26:59 UTC

- Update gcc toolchain download link for windows (rust-embedded/book#384)

## rust-lang/reference

3 commits in 4249fb411dd27f945e2881eb0378044b94cee06f..de2d5289e45506b11dd652bef4f99de64be70e1c
2025-02-08 22:12:20 UTC to 2025-02-06 19:02:01 UTC

- Add trait_upcasting related languages changes (rust-lang/reference#1622)
- fixup `»` insertion for rules (rust-lang/reference#1730)
- doc `cenum_impl_drop_cast` (rust-lang/reference#1713)

## rust-lang/rust-by-example

2 commits in 743766929f1e53e72fab74394ae259bbfb4a7619..66543bbc5b7dbd4e679092c07ae06ba6c73fd912
2025-02-06 12:38:08 UTC to 2025-02-04 01:46:09 UTC

- Add more examples @ drop.md (rust-lang/rust-by-example#1912)
- es translation to 3100 (rust-lang/rust-by-example#1911)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants