Skip to content

[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

Merged
merged 3 commits into from
Jan 5, 2023

Conversation

quinntaylor
Copy link
Contributor

Handle extraction of ExprKind::Dictionary type.
Update DictionaryValue to accept TupleValue 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.

@quinntaylor
Copy link
Contributor Author

@swift-ci test


static bool classof(const CompileTimeValue *T) {
return T->getKind() == ValueKind::Dictionary;
}

std::vector<std::shared_ptr<CompileTimeValue>> getElements() const {
return elements;
Copy link
Contributor

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.

  1. It may be safer to constrain this to TupleElement instead of CompileTimeValue. We are doing a cast during JSON rendering, so this would move the cast into the vector insertion instead of rendering. Maybe check the kind of the extracted compile time value before inserting into the vector?
  2. We may want to use capitalized Elements to be consistent within the file.

Copy link
Contributor Author

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.

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++) {
Copy link
Contributor

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.

@quinntaylor
Copy link
Contributor Author

@swift-ci test

Comment on lines 153 to 154
auto elementValue = extractCompileTimeValue(elementExpr);
tuples.push_back(std::static_pointer_cast<TupleValue>(elementValue));
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@quinntaylor
Copy link
Contributor Author

@swift-ci test

@quinntaylor quinntaylor merged commit 5683a61 into swiftlang:main Jan 5, 2023
@quinntaylor quinntaylor deleted the const-extract-dictionary branch January 5, 2023 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants