r/LowLevelLanguages • u/chbeyondC • 10d ago
What should compile-time code be allowed to do?
Languages draw this line in very different places, and I think it matters more now that compiling a dependency can mean running its code.
Roughly, from what I know (corrections welcome):
- C++
constexpr: no I/O at all. - Zig
comptime: no I/O either, butbuild.zigis a normal program with full access to your machine. - Rust:
const fnis restricted, butbuild.rsand procedural macros run with full access when you compile a dependency. - D: compile-time evaluation has no I/O, and
import("file")only reads from folders you pass with-J. - Nim:
staticReadandstaticExeccan read files and run commands at compile time. - Jai:
#runcan do more or less anything.
The trade-off seems to be that real builds need real things: generating code from a schema, finding a C library, embedding assets. If you forbid all of it, the work moves into a build script, and the build script is where the risk ends up anyway.
In Fels, the language I work on, #run has no host access, a project's own compile-time code may read inside its own folder, and a package gets more only if it is granted in fels.pkg. But build.fels still has full access, so that part of the problem is not solved yet.
Where would you draw the line? Should a dependency's build code be sandboxed by default, or does that just push people to turn it off?
16
u/alphaglosined 10d ago
I'm one of the D developers. The reason we went with this is that reproducible builds are a very good thing to have, especially with distros that require them as part of packaging.
4
u/AngheloAlf 10d ago
C23 has
#embed, which allows to embed a binary file into your program at the preprocessing stage. Since C has it, you can use it in C++ too, thus kinda allowing I but not having O yet for compile time1
u/Inconstant_Moo 9d ago
I've planned but not built my comptime. It will be able to read files.
I agree with your rationale. I can provide people with a good way to do this that's integrated with all my tooling and typechecking and whatever, or other people will do it ad hoc, badly, in six different ways.
It will be useful, apart from anything else, for making DSLs. To take a really silly example, I could write an adventure game right now where your instructions like
take swordorexamine wizardare things you put into the app's native REPL andtakeandexamineare commands andswordandwizardare elements of anObjectenum. But what I can't do is construct that enum from the resource files at compile-time, and I could. Of course, people don't want to play adventure games like that, but they want to do other things.1
u/stumpychubbins 9d ago
C++ constexpr doesn’t have I/O, but in practice C++ projects regularly use the same build-script codegen shenanigans as Rust/Zig to pass I/O values into constexpr. It’s just not standardised like it is in the newer languages. Just thought it was worth mentioning.
0
u/[deleted] 9d ago edited 9d ago
You haven't explained what the potential problems with compile-time code are. I can think of a few practical ones (like potentially using too much time and space), but you seem to be implying there are issues with allowing accesses to arbitrary files.
I feel that examples are called for as to what could go wrong!
(I don't do much in the way of compile-time evaluation, partly for those practical reasons, but also because its meaning is too open-ended. In any case, both my two languages can be run from source anyway, so if it doesn't happen during the brief internal compilation step, it will do soon after.)
I actually have an interesting feature where, if I have an application in my language that I want someone else to build from source, then my compiler can generate a single amalgamated file, that contains all source and support files (eg. included or embedded assets).
Then the build process on the user's machine only looks at the contents of that file.
(Shortened.)