-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Compile Time Constant Extraction] Add extraction of Dictionary values. #62752
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
[Compile Time Constant Extraction] Add extraction of Dictionary values. #62752
Conversation
@swift-ci test |
include/swift/AST/ConstTypeInfo.h
Outdated
|
||
static bool classof(const CompileTimeValue *T) { | ||
return T->getKind() == ValueKind::Dictionary; | ||
} | ||
|
||
std::vector<std::shared_ptr<CompileTimeValue>> getElements() const { | ||
return elements; |
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.
I have two small thoughts on this.
- It may be safer to constrain this to
TupleElement
instead ofCompileTimeValue
. We are doing a cast during JSON rendering, so this would move the cast into the vector insertion instead of rendering. Maybe check thekind
of the extracted compile time value before inserting into the vector? - We may want to use capitalized
Elements
to be consistent within the file.
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.
Great points. Since we know Dictionary
elements get extracted as TupleValue
, we can definitely narrow the type earlier. Also, I'm fine with switching to that capitalization.
lib/ConstExtract/ConstExtract.cpp
Outdated
case ExprKind::Dictionary: { | ||
auto dictionaryExpr = cast<DictionaryExpr>(expr); | ||
std::vector<std::shared_ptr<CompileTimeValue>> elementValues; | ||
for (unsigned n = dictionaryExpr->getNumElements(), i = 0; i != n; i++) { |
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.
[nit] I think you could use auto elementExpr : dictionaryExpr->getElements()
here if you didn't want to do the indexing logic.
…alue` to `TupleValue`.
@swift-ci test |
lib/ConstExtract/ConstExtract.cpp
Outdated
auto elementValue = extractCompileTimeValue(elementExpr); | ||
tuples.push_back(std::static_pointer_cast<TupleValue>(elementValue)); |
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.
Thanks for making the change to store tuples. Seems more straightforward now.
My only other question: Do you think we need to check the elementValue->getKind()
here? On one hand, we know it is a tuple, but if that was ever not the case I think this may crash. I think this is fine as-is, but wanted to raise the thought.
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 thought, I'll wrap it in a guard and only cast if it's the correct type.
@swift-ci test |
Handle extraction of
ExprKind::Dictionary
type.Update
DictionaryValue
to acceptTupleValue
elements for key-value pairs.Add JSON output as an array of objects with
"key"
and"value"
attributes.Update ExtractGroups.swift with expected JSON output and new test cases.