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

GH-41994 [C++]: kernel.cc: Remove defaults on switch so that compiler can check full enum coverage for us #41995

Merged
merged 2 commits into from
Jun 6, 2024

Conversation

felipecrv
Copy link
Contributor

@felipecrv felipecrv commented Jun 5, 2024

Rationale for this change

To let the compiler warn us about missing cases and make the non-handled cases more obvious.

What changes are included in this PR?

Removal of default in the switches and improving some dchecks with a message.

Are these changes tested?

By existing tests.

@github-actions github-actions bot added the awaiting committer review Awaiting committer review label Jun 5, 2024
Copy link

github-actions bot commented Jun 5, 2024

⚠️ GitHub issue #41994 has been automatically assigned in GitHub to PR creator.

}
return true;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This means that if kind_ is poisoned memory, we return true instead of poison (true or false non-deterministically). Making the code identical to the previous version but without the default case.

@github-actions github-actions bot added awaiting changes Awaiting changes and removed awaiting committer review Awaiting committer review labels Jun 5, 2024
@felipecrv felipecrv requested review from pitrou and kou June 5, 2024 23:10
Copy link
Member

@kou kou left a comment

Choose a reason for hiding this comment

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

+1

}
return false;
Copy link
Member

Choose a reason for hiding this comment

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

Can we add a comment about "must not be reached" or something here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might be reached if kind_ is bogus memory. I could remove this line altogether and let poison (true/false randomly) be returned.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm assuming @wesm was intentional in having the default case here to protect against that kind of UB.

I checked this on godbolt: https://godbolt.org/z/qEaGMGdzh

// ANY_TYPE
return true;
case InputType::ANY_TYPE:
break;
Copy link
Member

Choose a reason for hiding this comment

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

It's not a strong opinion but I like using return true here for consistency of other cases in this switch:

Suggested change
break;
return true;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. I'm changing this to true and false after the switch.

Comment on lines +452 to +454
break;
}
return resolver_(ctx, types);
Copy link
Member

Choose a reason for hiding this comment

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

How about returning an error by default?

Suggested change
break;
}
return resolver_(ctx, types);
return resolver_(ctx, types);
}
return Status::NotImplemented("Must not happen"); // UnknownError?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Overkill. That return Status would only ever be reached if kind_ is poisoned memory: in that case all that we need is consistent behavior to not go crazy debugging.

Comment on lines +471 to +474
case OutputType::COMPUTED:
break;
}
return "computed";
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
case OutputType::COMPUTED:
break;
}
return "computed";
case OutputType::COMPUTED:
return "computed";
}
return "unknown";

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same point as above.

@github-actions github-actions bot added awaiting merge Awaiting merge awaiting changes Awaiting changes and removed awaiting changes Awaiting changes awaiting merge Awaiting merge labels Jun 6, 2024
@github-actions github-actions bot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels Jun 6, 2024
Copy link
Member

@pitrou pitrou left a comment

Choose a reason for hiding this comment

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

+1, no problem from me

@felipecrv felipecrv merged commit 164be48 into apache:main Jun 6, 2024
37 of 39 checks passed
@felipecrv felipecrv removed the awaiting change review Awaiting change review label Jun 6, 2024
@felipecrv felipecrv deleted the kernel_type_matcher branch June 6, 2024 17:27
Copy link

After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit 164be48.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 6 possible false positives for unstable benchmarks that are known to sometimes produce them.

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

Successfully merging this pull request may close these issues.

3 participants