[flang-commits] [flang] 304c7a8 - [flang][Evaluate] Restrict ConstantBase constructor overload (#138456)

via flang-commits flang-commits at lists.llvm.org
Tue May 6 10:00:32 PDT 2025


Author: Krzysztof Parzyszek
Date: 2025-05-06T12:00:29-05:00
New Revision: 304c7a87d01bd0d7c75a0b875beed0a6b491383e

URL: https://github.com/llvm/llvm-project/commit/304c7a87d01bd0d7c75a0b875beed0a6b491383e
DIFF: https://github.com/llvm/llvm-project/commit/304c7a87d01bd0d7c75a0b875beed0a6b491383e.diff

LOG: [flang][Evaluate] Restrict ConstantBase constructor overload (#138456)

ConstantBase has a constructor that takes a value of any type as an
input: template <typename T> ConstantBase(const T &). A derived type
Constant<T> is a member of many Expr<T> classes (as an alternative in
the member variant).

When trying (erroneously) to create Expr<T> from a wrong input, if the
specific instance of Expr<T> contains Constant<T>, it's that constructor
that will be instantiated, leading to cryptic and confusing errors.

Eliminate the constructor from overload for invalid input values to help
produce more meaningful diagnostics.

Added: 
    

Modified: 
    flang/include/flang/Evaluate/constant.h

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Evaluate/constant.h b/flang/include/flang/Evaluate/constant.h
index 6fc22e3b86aa2..d4c6601c37bca 100644
--- a/flang/include/flang/Evaluate/constant.h
+++ b/flang/include/flang/Evaluate/constant.h
@@ -110,8 +110,12 @@ class ConstantBase : public ConstantBounds {
   using Result = RESULT;
   using Element = ELEMENT;
 
-  template <typename A>
+  // Constructor for creating ConstantBase from an actual value (i.e.
+  // literals, etc.)
+  template <typename A,
+      typename = std::enable_if_t<std::is_convertible_v<A, Element>>>
   ConstantBase(const A &x, Result res = Result{}) : result_{res}, values_{x} {}
+
   ConstantBase(ELEMENT &&x, Result res = Result{})
       : result_{res}, values_{std::move(x)} {}
   ConstantBase(


        


More information about the flang-commits mailing list