Type/Value/Sema bugfix compendium #25244
Closed
+727
−436
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.
Revival of #25222, where I bungled so bad with git that I had to create a new branch, not just for the operation of the code, but for my honor.
Addresses multiple bugs in Sema/Value/Type.
To be specific:
Peer
@TypeOf
in a@cImport
blockIn a unary
@TypeOf
block, the current behavior regarding@cImport
statements is that operands are treated as if they are outside of a@cImport
block; If you use@TypeOf(@cInclude(...))
even inside of a@cImport
block, this is a compile error. It seems that peer@TypeOf
did not agree with this implementation. Or any implementation for that matter. It crashed the compiler by attempting to write to a@cImport
buffer that didn't exist. Now the behavior is consistent with unary@TypeOf
.In practice, this turns the following code from a crash into a compile error:
Pass an optional sema parameter to
print_value
andType.print
Some API changes were made to
Value
,Type
, andprint_value
to allow for semantic analysis during the formatting ofValue
andType
. Resolves the TODO aboveType.print
.print_value
now take anopt_sema: ?*Sema
parameterValue
Value.fmtValueOptSema
Value.fmtSema
, except thesema
parameter is now optional.Sema
may or may not be availableValue.fmtDepth
Value.fmt
, but takes an additionaldepth
parameterValue.fmtSemaDepth
Value.fmtSema
, but takes an additionaldepth
parameterValue.fmtOptSemaDepth
Value.fmtOptSema
, but takes an additionaldepth
parameterType.print
now takes an?*Sema
parameterType
Type.fmt
remains unchangedValue.printValue
Type.fmtSema
Type.fmt
, but accepts an additional*Sema
parameterSema
is availableValue.printValueSema
Type.fmtOptSema
Type.fmt
, but accepts an additional?*Sema
parameter?*Sema
parameter is nonnullSema
may or may not be availableValue.printValueOptSema
These changes fix the following compiler crash (and many similar crashes in the same vain):
And speaking of capturing comptime variables as default field values in anonymous struct literals...
Better checks for capturing comptime-mutable references
Previously, it was relatively simple to "hide" a comptime-mutable reference inside of a type. If you stored a comptime-mutable reference in the sentinel of an array or the default field of a struct, it was possible to create a type which contained a comptime-mutable reference. Building on that, it was possible to create structs containing these types, pointers pointing to these types, arrays with these types as children, and so on. After a thorough rework of the relevant code, this is no more!
In practice, the following code contains multiple parts that used to crash the compiler:
And now, as ridiculous as that code was, we get a proper error message: