Skip to content

Turn type_id into a constant intrinsic - #47892

Merged
bors merged 1 commit into
rust-lang:masterfrom
Badel2:const_type_id_of
Feb 5, 2018
Merged

Turn type_id into a constant intrinsic#47892
bors merged 1 commit into
rust-lang:masterfrom
Badel2:const_type_id_of

Conversation

@Badel2

Copy link
Copy Markdown
Contributor

#27745

The method get_type_id in Any is intended to support reflection. It's currently unstable in favor of using an associated constant instead. This PR makes the type_id intrinsic a constant intrinsic, the same as size_of and align_of, allowing TypeId::of to be a const fn, which will allow using an associated constant in Any.

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @dtolnay

(rust_highfive has picked a reviewer for you, use r? to override)

@Badel2

Copy link
Copy Markdown
ContributorAuthor

I basically copied @eddyb 's #42859, as it's my first time dealing with compiler instrinsics.

In miri/const_eval.rs there's an error called "NeedsRfc", which would suggest that adding intrinsics to const_eval needs some discussion, so I'm not sure if my PR would be allowed.

I added a few lines of code to an existing test because I don't know what exactly to test, so any suggestions welcome.

As for the Any trait, I tried adding a simple const TYPE_ID: TypeId = TypeId::of::<Self>(); but it didn't compile, I get an error (E0038) that says "the trait cannot contain associated consts" and I'm not sure why. Since that is the goal of this PR, I would like to make sure that adding an associated constant to Any is possible, and maybe do it in this PR.

@eddyb

Copy link
Copy Markdown
Contributor

cc @oli-obk@durka This probably needs to indicate that it's not stably const fn.

@oli-obk

oli-obk commented Jan 31, 2018

Copy link
Copy Markdown
Contributor

Do the type IDs change between compilations?

According to the docs they'll change between releases and platforms. I guess this could be said of type_size, too, but that one is at least predictable in some manner.

Should definitely be unstable const fn until it can actually be added to Any

@durka

durka commented Jan 31, 2018 via email

Copy link
Copy Markdown
Contributor

@durka

durka commented Jan 31, 2018 via email

Copy link
Copy Markdown
Contributor

@oli-obk

Copy link
Copy Markdown
Contributor

So completely unstable between compiler versions. Like enum discriminants but worse.

With miri we could make sure that no decisions are made on that value.

@durka

durka commented Jan 31, 2018 via email

Copy link
Copy Markdown
Contributor

@oli-obk

Copy link
Copy Markdown
Contributor

You can't use it as an index to arrays, you can't compare it to anything but other values derived from type_id, ...

@eddyb

Copy link
Copy Markdown
Contributor

I'd rather not go crazy about it - it's your fault if you misuse this random (but deterministic within one compilation) number that the compiler gave you.

@kennytmkennytm added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jan 31, 2018
@Badel2

Copy link
Copy Markdown
ContributorAuthor

#[rustc_const_unstable(feature="const_type_id")]

Thanks @durka, but it was really hard to find documentation about it. I assume that when you add it to a stable function, and that function is called in a non-const context, it works the same as before? Because TypeId::of is already stable so it should keep working. Or maybe do I need to add rustc_const_unstable to the intrinsic itself? I have run rg rustc_const_unstable src/ and there are zero uses of this feature outside of tests, so I don't know what to do.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be in a separate file to ensure that TypeId::of without the feature enabled still works

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I agree, this test is only temporal as I didn't know what to test. (It still works btw).

@Badel2
Badel2force-pushed the const_type_id_of branch 2 times, most recently from 2da171f to 80cb8b3CompareJanuary 31, 2018 16:40
@dtolnay

Copy link
Copy Markdown
Member

Seems reasonable to me, @oli-obk please merge when ready. Thanks!

@bors delegate=oli-obk

@bors

bors commented Jan 31, 2018

Copy link
Copy Markdown
Collaborator

✌️ @oli-obk can now approve this pull request

@Badel2

Copy link
Copy Markdown
ContributorAuthor

ping @oli-obk

@kennytmkennytm 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 Feb 1, 2018

@oli-obkoli-obk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just a style thing, Lgtm otherwise

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, this is a good test that I forgot about. What I meant was an extra file that uses TypeId::of without the feature gate in a non const context.

....

And a quick search shows there are already files testing that. Don't mind me

Comment threadsrc/librustc_trans/mir/constant.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: remove this empty line

Add rustc_const_unstable attribute for `any::TypeId::of`
Add test for `const fn TypeId::of`
@dtolnay

Copy link
Copy Markdown
Member

@bors r=oli-obk

@bors

bors commented Feb 4, 2018

Copy link
Copy Markdown
Collaborator

📌 Commit 196fad0 has been approved by oli-obk

@kennytmkennytm 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 Feb 4, 2018
kennytm added a commit to kennytm/rust that referenced this pull request Feb 4, 2018
Turn `type_id` into a constant intrinsic
rust-lang#27745
The method `get_type_id` in `Any` is intended to support reflection. It's currently unstable in favor of using an associated constant instead. This PR makes the `type_id` intrinsic a constant intrinsic, the same as `size_of` and `align_of`, allowing `TypeId::of` to be a `const fn`, which will allow using an associated constant in `Any`.
@kennytmkennytm mentioned this pull request Feb 4, 2018
bors added a commit that referenced this pull request Feb 4, 2018
Rollup of 10 pull requests
- Successful merges: #47862, #47877, #47896, #47912, #47947, #47958, #47978, #47996, #47999, #47892
- Failed merges:
bors added a commit that referenced this pull request Feb 4, 2018
Rollup of 10 pull requests
- Successful merges: #47862, #47877, #47896, #47912, #47947, #47958, #47978, #47996, #47999, #47892
- Failed merges:
@bors
bors merged commit 196fad0 into rust-lang:masterFeb 5, 2018
@KodrAusKodrAus mentioned this pull request Sep 23, 2020
4 tasks
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@Badel2@rust-highfive@eddyb@oli-obk@durka@dtolnay@bors@kennytm