stage2: fix extern fn decl parsing and astgen #10040
Merged
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.
AstGen's
fnDecl
skips the call todetectLocalShadowing
for parameter names of external functions. However,LocalVal
scopes are still added for each parameter, so shadowing can change the type of later parameters. For example, inthe emitted param block looks like
which makes the type of
b
invalid.These changes preserve the permissive shadowing by not adding any extern function parameter names to the current scope. This makes a test for extern functions with bodies in compile_errors.zig fail because parse.zig does not check for this like parser.cpp does, so I added that as well.
Note that this change prevents any reference to earlier parameters in an extern function, so
extern fn f(a: u32, b: @TypeOf(a)) void
becomes invalid. I think this is probably the right choice when shadowing is allowed, but one alternative would be to add just the parameter names that don't shadow anything to the current scope. Or perhaps the best choice is to simply apply the usual rules for shadowing to extern functions as well. Both are easy changes.