You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "felipecrv (via GitHub)" <gi...@apache.org> on 2024/06/05 23:05:50 UTC

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

felipecrv opened a new pull request, #41995:
URL: https://github.com/apache/arrow/pull/41995

   ### 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.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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

Posted by "felipecrv (via GitHub)" <gi...@apache.org>.
felipecrv commented on code in PR #41995:
URL: https://github.com/apache/arrow/pull/41995#discussion_r1628664156


##########
cpp/src/arrow/compute/kernel.cc:
##########
@@ -445,11 +445,13 @@ const TypeMatcher& InputType::type_matcher() const {
 
 Result<TypeHolder> OutputType::Resolve(KernelContext* ctx,
                                        const std::vector<TypeHolder>& types) const {
-  if (kind_ == OutputType::FIXED) {
-    return type_.get();
-  } else {
-    return resolver_(ctx, types);
+  switch (kind_) {
+    case OutputType::FIXED:
+      return type_;
+    case OutputType::COMPUTED:
+      break;
   }
+  return resolver_(ctx, types);

Review Comment:
   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.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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

Posted by "felipecrv (via GitHub)" <gi...@apache.org>.
felipecrv commented on code in PR #41995:
URL: https://github.com/apache/arrow/pull/41995#discussion_r1628547283


##########
cpp/src/arrow/compute/kernel.cc:
##########
@@ -411,21 +409,23 @@ bool InputType::Matches(const DataType& type) const {
       return type_->Equals(type);
     case InputType::USE_TYPE_MATCHER:
       return type_matcher_->Matches(type);
-    default:
-      // ANY_TYPE
-      return true;
+    case InputType::ANY_TYPE:
+      break;
   }
+  return true;

Review Comment:
   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.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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

Posted by "felipecrv (via GitHub)" <gi...@apache.org>.
felipecrv merged PR #41995:
URL: https://github.com/apache/arrow/pull/41995


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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

Posted by "felipecrv (via GitHub)" <gi...@apache.org>.
felipecrv commented on code in PR #41995:
URL: https://github.com/apache/arrow/pull/41995#discussion_r1628652140


##########
cpp/src/arrow/compute/kernel.cc:
##########
@@ -400,9 +399,8 @@ bool InputType::Equals(const InputType& other) const {
       return type_->Equals(*other.type_);
     case InputType::USE_TYPE_MATCHER:
       return type_matcher_->Equals(*other.type_matcher_);
-    default:
-      return false;
   }
+  return false;

Review Comment:
   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



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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

Posted by "felipecrv (via GitHub)" <gi...@apache.org>.
felipecrv commented on code in PR #41995:
URL: https://github.com/apache/arrow/pull/41995#discussion_r1628665945


##########
cpp/src/arrow/compute/kernel.cc:
##########
@@ -463,11 +465,13 @@ const OutputType::Resolver& OutputType::resolver() const {
 }
 
 std::string OutputType::ToString() const {
-  if (kind_ == OutputType::FIXED) {
-    return type_->ToString();
-  } else {
-    return "computed";
+  switch (kind_) {
+    case OutputType::FIXED:
+      return type_->ToString();
+    case OutputType::COMPUTED:
+      break;
   }
+  return "computed";

Review Comment:
   Same point as above.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #41995:
URL: https://github.com/apache/arrow/pull/41995#issuecomment-2151103682

   :warning: GitHub issue #41994 **has been automatically assigned in GitHub** to PR creator.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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

Posted by "conbench-apache-arrow[bot] (via GitHub)" <gi...@apache.org>.
conbench-apache-arrow[bot] commented on PR #41995:
URL: https://github.com/apache/arrow/pull/41995#issuecomment-2160902663

   After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit 164be4882176c5f84eb7cde52b98d69a72fe7ea8.
   
   There were no benchmark performance regressions. 🎉
   
   The [full Conbench report](https://github.com/apache/arrow/runs/26081412645) has more details. It also includes information about 6 possible false positives for unstable benchmarks that are known to sometimes produce them.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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

Posted by "felipecrv (via GitHub)" <gi...@apache.org>.
felipecrv commented on code in PR #41995:
URL: https://github.com/apache/arrow/pull/41995#discussion_r1628639337


##########
cpp/src/arrow/compute/kernel.cc:
##########
@@ -400,9 +399,8 @@ bool InputType::Equals(const InputType& other) const {
       return type_->Equals(*other.type_);
     case InputType::USE_TYPE_MATCHER:
       return type_matcher_->Equals(*other.type_matcher_);
-    default:
-      return false;
   }
+  return false;

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



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on code in PR #41995:
URL: https://github.com/apache/arrow/pull/41995#discussion_r1628587875


##########
cpp/src/arrow/compute/kernel.cc:
##########
@@ -411,21 +409,23 @@ bool InputType::Matches(const DataType& type) const {
       return type_->Equals(type);
     case InputType::USE_TYPE_MATCHER:
       return type_matcher_->Matches(type);
-    default:
-      // ANY_TYPE
-      return true;
+    case InputType::ANY_TYPE:
+      break;

Review Comment:
   It's not a strong opinion but I like using `return true` here for consistency of other `case`s in this `switch`:
   
   ```suggestion
         return true;
   ```



##########
cpp/src/arrow/compute/kernel.cc:
##########
@@ -400,9 +399,8 @@ bool InputType::Equals(const InputType& other) const {
       return type_->Equals(*other.type_);
     case InputType::USE_TYPE_MATCHER:
       return type_matcher_->Equals(*other.type_matcher_);
-    default:
-      return false;
   }
+  return false;

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



##########
cpp/src/arrow/compute/kernel.cc:
##########
@@ -463,11 +465,13 @@ const OutputType::Resolver& OutputType::resolver() const {
 }
 
 std::string OutputType::ToString() const {
-  if (kind_ == OutputType::FIXED) {
-    return type_->ToString();
-  } else {
-    return "computed";
+  switch (kind_) {
+    case OutputType::FIXED:
+      return type_->ToString();
+    case OutputType::COMPUTED:
+      break;
   }
+  return "computed";

Review Comment:
   ```suggestion
       case OutputType::COMPUTED:
         return "computed";
     }
     return "unknown";
   ```



##########
cpp/src/arrow/compute/kernel.cc:
##########
@@ -445,11 +445,13 @@ const TypeMatcher& InputType::type_matcher() const {
 
 Result<TypeHolder> OutputType::Resolve(KernelContext* ctx,
                                        const std::vector<TypeHolder>& types) const {
-  if (kind_ == OutputType::FIXED) {
-    return type_.get();
-  } else {
-    return resolver_(ctx, types);
+  switch (kind_) {
+    case OutputType::FIXED:
+      return type_;
+    case OutputType::COMPUTED:
+      break;
   }
+  return resolver_(ctx, types);

Review Comment:
   How about returning an error by default?
   
   ```suggestion
         return resolver_(ctx, types);
     }
     return Status::NotImplemented("Must not happen"); // UnknownError?
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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

Posted by "felipecrv (via GitHub)" <gi...@apache.org>.
felipecrv commented on code in PR #41995:
URL: https://github.com/apache/arrow/pull/41995#discussion_r1628657101


##########
cpp/src/arrow/compute/kernel.cc:
##########
@@ -411,21 +409,23 @@ bool InputType::Matches(const DataType& type) const {
       return type_->Equals(type);
     case InputType::USE_TYPE_MATCHER:
       return type_matcher_->Matches(type);
-    default:
-      // ANY_TYPE
-      return true;
+    case InputType::ANY_TYPE:
+      break;

Review Comment:
   OK. I'm changing this to `true` and `false` after the `switch`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org