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

Turn type_id into a constant intrinsic #47892

Merged
merged 1 commit into from
Feb 5, 2018

Conversation

Badel2
Copy link
Contributor

@Badel2 Badel2 commented Jan 30, 2018

#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
Collaborator

r? @dtolnay

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

@Badel2
Copy link
Contributor Author

Badel2 commented Jan 30, 2018

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
Member

eddyb commented Jan 30, 2018

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

@oli-obk
Copy link
Contributor

oli-obk commented Jan 31, 2018

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
Copy link
Contributor

durka commented Jan 31, 2018 via email

@durka
Copy link
Contributor

durka commented Jan 31, 2018 via email

@oli-obk
Copy link
Contributor

oli-obk commented Jan 31, 2018

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
Copy link
Contributor

durka commented Jan 31, 2018 via email

@oli-obk
Copy link
Contributor

oli-obk commented Jan 31, 2018

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
Member

eddyb commented Jan 31, 2018

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.

@kennytm kennytm 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
Contributor Author

Badel2 commented Jan 31, 2018

#[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.

@@ -91,4 +92,11 @@ pub fn main() {
// Check fn pointer against collisions
assert!(TypeId::of::<fn(fn(A) -> A) -> A>() !=
TypeId::of::<fn(fn() -> A, A) -> A>());

// Check const
const T1: TypeId = TypeId::of::<A>();
Copy link
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
Contributor Author

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 Badel2 force-pushed the const_type_id_of branch 2 times, most recently from 2da171f to 80cb8b3 Compare January 31, 2018 16:40
@dtolnay
Copy link
Member

dtolnay commented Jan 31, 2018

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

@bors delegate=oli-obk

@bors
Copy link
Contributor

bors commented Jan 31, 2018

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

@Badel2
Copy link
Contributor Author

Badel2 commented Feb 1, 2018

ping @oli-obk

@kennytm kennytm 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
Copy link
Contributor

@oli-obk oli-obk left a comment

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


fn main() {
const A_ID: TypeId = TypeId::of::<A>();
//~^ ERROR `std::any::TypeId::of` is not yet stable as a const fn
Copy link
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

let llval = C_u64(self.cx,
self.cx.tcx.type_id_hash(substs.type_at(0)));
Ok(Const::new(llval, tcx.types.u64))

Copy link
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
Member

dtolnay commented Feb 4, 2018

@bors r=oli-obk

@bors
Copy link
Contributor

bors commented Feb 4, 2018

📌 Commit 196fad0 has been approved by oli-obk

@kennytm kennytm 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`.
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:master Feb 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: 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