std.Build: accept comptime string as option value #22135
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Small quality of life improvement for
std.Build
interface: accept comptime string as option value without the need to cast it to[]const u8
.Problem
On one project I have a build option declared as such:
Then on another project I want to set that option:
I'm forced to cast it to
[]const u8
.If I try this:
I get the error:
Expectation
Zig compiler automatically casts comptime strings to
[]const u8
if they are, for example, declared as the type of a function argument. I'd expect the same here.Diagnosis
std.Build.userInputOptionsFromArgs
is a comptime function that evaluates its args using@TypeOf
information. So the compiler has no way to know whether that requires an auto-cast or not.Solution
This PR tries to address the issue by forcing the
[]const u8
type when it encounters a value that looks like a comptime string. As the innerUserValue.value
uses a[]const u8
, I suppose the behavior does not change.I'm not sure if that's the correct way to infer if a value can be considered a "comptime string". But it works for my use case.