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

TypeID of Box<Fn() -> ()> inconsistent across crates #33703

Closed
MasonRemaley opened this issue May 18, 2016 · 7 comments
Closed

TypeID of Box<Fn() -> ()> inconsistent across crates #33703

MasonRemaley opened this issue May 18, 2016 · 7 comments
Labels
I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness

Comments

@MasonRemaley
Copy link

The TypeID of Box<Fn() -> ()> seems to be inconsistent across crates in both nightly and stable, I found a similar closed issue from a couple years ago and wasn't sure whether I should comment on that or file a new bug. I can recreate it consistently as follows:

typeid_issue/lib.rs:

pub fn check() {
    println!("{:?}", std::any::TypeId::of::<Box<Fn() -> ()>>());
}

typeid_issue/examples/example.rs:

extern crate typeid_issue;

fn main() {
    println!("{:?}", std::any::TypeId::of::<Box<Fn() -> ()>>());
    typeid_issue::check();
}

On my machine, cargo run --example example with the above project outputs two different TypeIds. The same thing doesn't happen for all types, e.g. Box<i32>s seem to work fine, and Box<Fn() -> ()> wrapped in a struct seems to work fine giving me a nice work around for now.

@Manishearth
Copy link
Member

I don't think typeid is supposed to be consistent across compilations

@eddyb
Copy link
Member

eddyb commented May 18, 2016

It looks like this is part of the same compilation, which would make it a bug.

@Manishearth
Copy link
Member

Oh, my bad.

@MasonRemaley
Copy link
Author

Yup, I should've made that a little clearer--I'm getting two different ids during the same run.

@eddyb
Copy link
Member

eddyb commented Jul 11, 2016

On IRC @willcrichton pointed out that ProjectionPredicate (found in ExistentialBounds, which gets hashed wholesale) would hash differently in different crates, and that's indeed what's happening here: it contains Ty (which hashes as an arena pointer), DefId (which differs based on the crate you're seeing it from) and Name (which hashes as the index in the string interner).
The immediate solution here is to manually include those in, with Name hashed as its string form.

@Manishearth
Copy link
Member

Manishearth commented Jul 11, 2016

Sounds simple enough to fix then, @willcrichton are you working on this or should I pick it up?

@Manishearth Manishearth added the I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness label Jul 11, 2016
@willcrichton
Copy link
Contributor

@Manishearth I'll take a stab at it and post any troubles here.

bors added a commit that referenced this issue Aug 6, 2016
Rewrite TypeId computation to not miss anything and work cross-crate.

Fixes #33703 and also some soundness holes in `Any` due to `TypeId` ignoring most lifetimes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
Projects
None yet
Development

No branches or pull requests

4 participants