r/rust • miri • Jul 19 '18

Thoughts on Compile-Time Function Evaluation and Type Systems

https://www.ralfj.de/blog/2018/07/19/const.html
73 Upvotes

63 comments sorted by

View all comments

Show parent comments

12

u/eddyb Jul 19 '18 edited Jul 20 '18

That's what "huge cache" means. People keep using array lengths directly as a problem but remember you can compute arbitrary types from such constants with associated types and those are not cached across crates, but non-deterministic CTFE could break the typesystem if any query ever executed isn't cached and reused

If you have a conflict between sibling crates you must deny linking them, otherwise they could interpret the same associated type as different types.

EDIT: please refer to https://www.reddit.com/r/rust/comments/907a6d/thoughts_on_compiletime_function_evaluation_and/e2pdqnt/ instead

2

u/zokier Jul 19 '18

That's what "huge cache" means

I don't think "huge cache" would be exactly the same as what /u/tending suggested. "Huge cache", if I understand correctly, would imply that the following would have the same type signatures:

fn f(t: &[T; random()]) 

fn g(t: &[T; random()]) 

Whereas I would interpret /u/tending such as they would have different type signatures.

I think the disconnect here comes from that people (including me) would have expected that each type declaration is evaluated exactly once during compilation, but apparently that is not the case?

4

u/eddyb Jul 20 '18 edited Jul 20 '18

That's not the kind of function we're worried about (we can actually make those cases work, they're not much worse than include_str! etc.), think more:

fn f<T>(t: &[T; random() % size_of::<T>()])

That is, the call is evaluated from application of generics. There's an even worse one based on associated types, but I don't have the link on hand.

EDIT: please refer to https://www.reddit.com/r/rust/comments/907a6d/thoughts_on_compiletime_function_evaluation_and/e2pdqnt/ instead

1

u/zokier Jul 20 '18

Thanks for the examples, they are good food for thought