Skip to content
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

cc2 #6600

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft

cc2 #6600

wants to merge 17 commits into from

Conversation

tarrinneal
Copy link
Contributor

@tarrinneal tarrinneal commented Apr 23, 2024

@jmagman jmagman mentioned this pull request May 6, 2024
11 tasks
'WriteValue(EncodableValue(std::any_cast<${customType.name}>(*custom_value).ToEncodableList()), stream);');
} else if (customType.type == CustomTypes.customEnum) {
indent.writeln(
'WriteValue(EncodableValue(std::any_cast<int>(*custom_value)), stream);');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WriteValue(EncodableValue(static_cast<int>(std::any_cast<${customType.name}>(*custom_value))), stream);

(Not doing as a GitHub code suggestion since I'm not sure which of my email addresses committing it would use, and I don't want to blow up the CLA check.)

: std::make_optional<AnEnum>(
static_cast<AnEnum>(an_enum_arg_value));
const auto* an_enum_arg = &(std::any_cast<const AnEnum&>(
std::get<CustomEncodableValue>(encodable_an_enum_arg)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bad variant access error is coming from this pattern: if the arg's value is null, then encodable_an_enum_arg isn't a CustomEncodableValue at all, it's a monotype (i.e., IsNull() is true). So you need to check IsNull() before doing a std::get instead of after, or you need to use a get_if<CustomEncodableValue>(&encodable_an_enum_arg) and then if that's null, pass null, and if it's not not, do the any-cast. (The thing that tripped me up in my first attempt to write a fix for this is that you can't any_cast a CustomEncodableValue* because of how implicit conversions work, so you'll need to do some gymnastics with a value variable like before I think.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of clunky, but the only way I can find to do it in a single statement involves a pointer to a reference that I can't prove to myself has a lifetime beyond that statement, and I'd rather the code be longer than have undefined behavior...

AnEnum an_enum_arg_value;
const AnEnum* an_enum_arg = nullptr;
if (!encodable_an_enum_arg.IsNull()) {
  an_enum_arg_value = std::any_cast<AnEnum>(std::get<CustomEncodableValue>(encodable_an_enum_arg));
  an_enum_arg = &an_enum_arg_value;
}
...EchoNullableEnum(an_enum_arg);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants