Skip to content

Conversation

@ysiraichi
Copy link
Collaborator

@ysiraichi ysiraichi commented Nov 26, 2025

This PR improves the error handling and error message of some lowering helper functions found in helpers.{h,cpp} C++ files.

Key Changes:

  • Created Safe* versions of the functions:
    • Flatten
    • DynamicReshape
    • GetDynamicReshapeInfo
  • Created CheckAtMostOneDynamicDimension() function, a version of GetDynamicDimension with improved error handling and messages
  • Removed the shape parameter from SafeFlatten()
    • It should be called separately
  • Add missing includes (IWYU ref)

// 3. In the presence of a dynamic shape in the input, we are unable to map
// it to any of the dimensions of the output
//
static absl::StatusOr<std::optional<DynamicReshapeInfo>>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we still need optional<>? Can we just return an error instead of nullopt?

// 2. The return value of `get_printable_data(el)`
template <class T, class FStar, class FPrint = T (*)(T)>
std::string SpanToStringWithStar(
absl::Span<const T> span, FStar&& needs_star,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Per https://google.github.io/styleguide/cppguide.html#Rvalue_references, avoid Rvalue references here and below.

// For each element, this function will append to the output string:
// 1. A star (*), if `needs_star(el)` returns true
// 2. The return value of `get_printable_data(el)`
template <class T, class FStar, class FPrint = T (*)(T)>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style: rename FStar to NeedStar, rename FPrint to FormatElement.

// For each element, this function will append to the output string:
// 1. A star (*), if `needs_star(el)` returns true
// 2. The return value of `get_printable_data(el)`
template <class T, class FStar, class FPrint = T (*)(T)>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why does FPrint return a T instead of a string?

// 1. The `needs_star` function will also depend on the index of the element
// 2. It will always print the element, instead of the index (i.e. the
// `get_printable_data` is already set)
template <class F>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rename F to NeedStar?

Similar comments for other function templates: make the template type argument names descriptive.

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