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 that dyn* coercions are actually pointer-sized #104338

Merged
merged 3 commits into from
Nov 19, 2022

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Nov 12, 2022

Implement a perma-unstable, rudimentary PointerSized trait to enforce dyn* casts are usize-sized for now, at least to prevent ICEs and weird codegen issues from cropping up after monomorphization since currently we enforce nothing.

This probably can/should be removed in favor of a more sophisticated trait for handling dyn* conversions when we decide on one, but I just want to get something up for discussion and experimentation for now.

r? @eholk cc @tmandry (though feel free to claim/reassign)

Fixes #102141
Fixes #102173

@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 Nov 12, 2022
@rustbot
Copy link
Collaborator

rustbot commented Nov 12, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@workingjubilee
Copy link
Member

what busts where Pointee::Metadata = ()?

@compiler-errors
Copy link
Member Author

compiler-errors commented Nov 12, 2022

@workingjubilee -- are you asking why this is enforcing something that's stricter than just T: Pointee<Metadata = ()>? Well, the currently implementation of dyn* treats dyn* Trait as being usize-sized, so we need to enforce that the thing that's being coerced into a dyn* is that size as well.

We could probably relax this to check size <= sizeof(usize), but I wanted to keep it simple. Also I need to do an alignment check I guess.

If that isn't what you're asking, then could you maybe re-explain?

@compiler-errors
Copy link
Member Author

compiler-errors commented Nov 12, 2022

Unless you were asking what happens in the case like this:

#![feature(ptr_metadata)]
#![feature(dyn_star)]

use std::fmt::Debug;
use std::ptr::Thin;

fn polymorphic<T: Debug + ?Sized + Thin>(t: &T) {
    let _ = t as dyn* Debug;
}

In which case:

error[E0277]: `&T` needs to be a pointer-sized type
 --> /home/gh-compiler-errors/test.rs:9:13
  |
9 |     let _ = t as dyn* Debug;
  |             ^ `&T` needs to be a pointer-sized type
  |
  = help: the trait `PointerSized` is not implemented for `&T`
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
  |
8 | fn polymorphic<T: Debug + ?Sized + Thin>(t: &T) where &T: PointerSized {
  |                                                 ++++++++++++++++++++++

This happens. Layout computation is not smart enough to take in account that T: Thin implies sizeof(&T) = sizeof(usize) 🤔

@workingjubilee
Copy link
Member

Hmm, I'm not sure which I was asking, honestly, but I am satisfied with that answer, aside from the question of whether or not layout computation could simply be taught that instead I suppose. But that can be tabled for now.

@compiler-errors
Copy link
Member Author

Yeah, I have a follow-up commit to teach layout computation exactly that, but I want to separate that from this PR since it maybe needs a bit more inspection.

@bors
Copy link
Contributor

bors commented Nov 15, 2022

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

@eholk
Copy link
Contributor

eholk commented Nov 15, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Nov 15, 2022

📌 Commit 5edbf2c has been approved by eholk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 15, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 16, 2022
…holk

Enforce that `dyn*` coercions are actually pointer-sized

Implement a perma-unstable, rudimentary `PointerSized` trait to enforce `dyn*` casts are `usize`-sized for now, at least to prevent ICEs and weird codegen issues from cropping up after monomorphization since currently we enforce *nothing*.

This probably can/should be removed in favor of a more sophisticated trait for handling `dyn*` conversions when we decide on one, but I just want to get something up for discussion and experimentation for now.

r? `@eholk` cc `@tmandry` (though feel free to claim/reassign)

Fixes rust-lang#102141
Fixes rust-lang#102173
@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member Author

@bors r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 16, 2022
@crlf0710 crlf0710 added the F-dyn_star `#![feature(dyn_star)]` label Nov 16, 2022
@compiler-errors
Copy link
Member Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 16, 2022
@eholk
Copy link
Contributor

eholk commented Nov 17, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Nov 17, 2022

📌 Commit 4e20fa5209c868c48a6f044e76447bef004a87a7 has been approved by eholk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 17, 2022
@bors
Copy link
Contributor

bors commented Nov 18, 2022

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

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 18, 2022
@compiler-errors
Copy link
Member Author

Rebased past oli's Obligation constructor PR.

@bors r=eholk

@bors
Copy link
Contributor

bors commented Nov 18, 2022

📌 Commit 39e076a has been approved by eholk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 18, 2022
Manishearth added a commit to Manishearth/rust that referenced this pull request Nov 18, 2022
…holk

Enforce that `dyn*` coercions are actually pointer-sized

Implement a perma-unstable, rudimentary `PointerSized` trait to enforce `dyn*` casts are `usize`-sized for now, at least to prevent ICEs and weird codegen issues from cropping up after monomorphization since currently we enforce *nothing*.

This probably can/should be removed in favor of a more sophisticated trait for handling `dyn*` conversions when we decide on one, but I just want to get something up for discussion and experimentation for now.

r? `@eholk` cc `@tmandry` (though feel free to claim/reassign)

Fixes rust-lang#102141
Fixes rust-lang#102173
Manishearth added a commit to Manishearth/rust that referenced this pull request Nov 18, 2022
…holk

Enforce that `dyn*` coercions are actually pointer-sized

Implement a perma-unstable, rudimentary `PointerSized` trait to enforce `dyn*` casts are `usize`-sized for now, at least to prevent ICEs and weird codegen issues from cropping up after monomorphization since currently we enforce *nothing*.

This probably can/should be removed in favor of a more sophisticated trait for handling `dyn*` conversions when we decide on one, but I just want to get something up for discussion and experimentation for now.

r? ``@eholk`` cc ``@tmandry`` (though feel free to claim/reassign)

Fixes rust-lang#102141
Fixes rust-lang#102173
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 18, 2022
…earth

Rollup of 8 pull requests

Successful merges:

 - rust-lang#102977 (remove HRTB from `[T]::is_sorted_by{,_key}`)
 - rust-lang#103378 (Fix mod_inv termination for the last iteration)
 - rust-lang#103456 (`unchecked_{shl|shr}` should use `u32` as the RHS)
 - rust-lang#103701 (Simplify some pointer method implementations)
 - rust-lang#104047 (Diagnostics `icu4x` based list formatting.)
 - rust-lang#104338 (Enforce that `dyn*` coercions are actually pointer-sized)
 - rust-lang#104498 (Edit docs for `rustc_errors::Handler::stash_diagnostic`)
 - rust-lang#104556 (rustdoc: use `code-header` class to format enum variants)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 24ee599 into rust-lang:master Nov 19, 2022
@rustbot rustbot added this to the 1.67.0 milestone Nov 19, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 4, 2023
…s-thin-ptr, r=wesleywiser

layout_of: `T: Thin` implies `sizeof(&T) == sizeof(usize)`

Use the `<T as Pointee>::Metadata` associated type to calculate the layout of a pointee's metadata, instead of hard-coding rules about certain types.

Maybe this approach is overkill -- we could instead hard-code this approach as a fallback, with the matching on `Slice`/`Dynamic`/etc. happening first

Fixes this issue here rust-lang#104338 (comment) .. But is also useful with transmutes, for example, given the UI test I added below.
@compiler-errors compiler-errors deleted the pointer-sized branch August 11, 2023 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-dyn_star `#![feature(dyn_star)]` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dyn* ICE primitive read not possible for type: [i32; 4] Broken LLVM function when pushing dyn* item to Vec.
7 participants