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

12

u/sociopath_in_me Jul 19 '18

I don't understand the reasoning why CTFE cannot read files. The compiler can already include arbitrary files compile time with the include macros. What's the difference? I think this limitation is just way too forced and does not really make sense.

2

u/Ar-Curunir Jul 19 '18

Well what if the file changes between two invocations of the compiler, while the source code is unchanged? Now your binary behaves differently. More to the point, the binary now depends on which computer it was compiled on.

And the include! macro adds files to the "source code" of your project, which is different from reading a random file.

1

u/sociopath_in_me Jul 19 '18

You really did not make it clearer:/ I still feel like that there is no real difference and it is just a semi random rule from the past. I'm sure eventually it will be changed because it makes no sense..

1

u/Ar-Curunir Jul 19 '18

Ok, let's simplify. There should be no fundamental reason that you can't invoke include! inside a const context (if the file doesn't exist, then you should get a compile error). However, what you can't do is something like this:

File::open("path_to_file");

This is a dynamic operation that depends on the file-system contents, and the compiler has no way of statically enforcing the result of this.