-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AST] Introduce ArgumentList #38836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[AST] Introduce ArgumentList #38836
Conversation
@nathawes Mind taking a look at the changes specifically in Formatting.cpp? |
Nice work! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Formatting.cpp changes LGTM. It's so great this is finally happening – thanks a lot!
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! I have left a small comment about ArgumentLists
that could be addressed separately.
/// that locator. | ||
llvm::DenseMap<ConstraintLocator *, ArgumentInfo> ArgumentInfos; | ||
/// constructions) to the argument lists for the call to that locator. | ||
llvm::DenseMap<ConstraintLocator *, ArgumentList *> ArgumentLists; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we are at it I think we need to do change this to behave like addNodeTypes
and record ArgumentLists
in the solution as well. Since constraint generation for result builders happens during solving this list would effectively get rewritten multiple times with the out-of-scope ArgumentList
objects just to keep them around until constraint system lifetime ends.
return getAsExpr(simplifyLocatorToAnchor(argListLoc)); | ||
ArgumentList * | ||
FailureDiagnostic::getArgumentListFor(ConstraintLocator *locator) const { | ||
return getConstraintSystem().getArgumentList(locator); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment about ArgumentLists
- It'd be great if we could extract this information for Solution
instead because we can't reply on constraint system having solution applied always e.g. ambiguity cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome 😍! The refactoring changes look good to me (two comments but it's fine as is).
This comment has been minimized.
This comment has been minimized.
Compiler performance numbers are just noise, Jenkins is sporadically crashing during some of the source compat suite runs. Previous performance runs didn't show any meaningful differences though, only a slim decrease in AST memory allocation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 lib/Index, lib/IDE, ASTWalker, and SourceKit all LGTM. I made one suggestion in SyntaxModel, but it doesn't need to block the PR, I think. I love how much horrible code this removed.
4174484
to
406d321
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lib/Parse, lib/IDE, SourceKit changes LGTM!
lpLoc, yieldElts, rpLoc, SyntaxKind::ExprList); | ||
for (auto &elt : yieldElts) { | ||
assert(elt.Label.empty()); | ||
assert(elt.LabelLoc.isInvalid()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Unrelated to this PR)
Hmm, I just noticed the following code hits these assertion even with current main
.
struct S {
var property: String {
_read {
yield(x: "test")
}
}
}
Instead of skipping parsing labels in parseExprList()
, we probably should diagnose parsed labels here. Or, we should not use parseExprList
. (Filed https://bugs.swift.org/browse/SR-15066)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Looks like the logic in parseExprList
is trying to handle this case, but isn't working as SyntaxKind::ExprList
is being passed (though looking at the git history, this seems to have been changed to fix the syntax tree). I can take a look at this in a follow-up.
406d321
to
9ecbcc2
Compare
Stress tester run uncovered a nasty bug in the ASTWalker logic, now fixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to be great! I've only looked at ArgumentList.h so far, but here are some initial thoughts. Sorry if you've already addressed some of this previously.
include/swift/AST/ArgumentList.h
Outdated
/// Note that in type-checked AST, this may return \c true for an index of a | ||
/// variadic expansion which contains a trailing closure (and it may not | ||
/// necessarily be the first argument of the expansion). | ||
bool isRawTrailingClosureIndex(unsigned i) const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isTrailingClosureArgument()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I think calling out the fact that it's a "raw" index is important, as it's not what you may expect in type-checked AST, in particular it might return true for default arguments or arguments that have been re-ordered after the trailing closure.
Alternatively we may just want to completely ban calling this on a type-checked argument list, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the original arguments update this becomes isTrailingClosureIndex
. Personally I prefer that over isTrailingClosureArgument
, as that sounds like it would expect an Argument
parameter, but I'm happy to change it over
Introduce the ArgumentList type, which represents a set of call arguments for a function or subscript. This will supersede the use of tuple and paren exprs as argument lists.
Switch out the representation of argument lists in various AST nodes with ArgumentList.
Split up the expr list parsing members such that there are separate entry points for tuple and argument list parsing, and start using the argument list parsing member for call and subscripts.
With the removal of TupleExpr and ParenExpr argument lists, it's no longer sufficient to check whether a given parent expr has a possible chain sub-expr, as it might be a child that's unrelated to the chain under consideration. For example, for a chain that's an argument to a function call, we don't want to walk up and consider the call expr to be an extension of the chain just because it has a function expr. Tighten up the check by comparing whether the given sub-expr of a possible chain parent matches the current expr in the chain.
- Explicitly limit favoring logic to only handle unary args, this seems to have always been the case, but needs to be handled explicitly now that argument lists aren't exprs - Update the ConstraintLocator simplification to handle argument lists - Store a mapping of locators to argument lists in the constraint system - Abstract more logic into a getArgumentLocator method which retrieves an argument-to-param locator from an argument anchor expr
Most of this should be fairly mechanical, the changes in PlaygroundTransform are a little more involved though.
Track the depth and clear out the map when it reaches 0 to save memory.
We were accidentally continuing to walk the old argument list even if the caller had returned a new one.
- Remove OriginalArguments in favor of storing the pre-rewritten argument list, which simplifies things nicely - Adopt llvm::indexed_accessor_iterator
9ecbcc2
to
3e28bbb
Compare
@swift-ci please test |
@swift-ci please test source compatibility |
@swift-ci please test compiler performance |
@swift-ci please SourceKit stress test |
Stress tester result is a UPASS:
The source compat failures are already known |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a1f127f LGTM.
This comment has been minimized.
This comment has been minimized.
🚢 |
Introduce the
ArgumentList
type, which represents a set of call arguments for a function or subscript. This supersedes the use of tuple and paren exprs as argument lists, a relic from when tuple splatting was a language feature.For now, paren and tuple types retain their
ParameterTypeFlags
, as both the constraint system and a small amount of diagnostic logic is still relying on converting argument lists into tuple or paren types, this will be removed in a follow-up. This then sets the stage for the removal ofInOutExpr
andInOutType
.I've split the commits up based on the part of the compiler they touch, please feel free to review the bits of the compiler you're most familiar with.