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

Krzysztof Parzyszek via flang-commits flang-commits at lists.llvm.org
Sun May 4 09:12:11 PDT 2025


https://github.com/kparzysz created https://github.com/llvm/llvm-project/pull/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 unexpected errors.

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

>From f9b0da8ad4fca7d2d40c3ad6eb2bf612402a00e3 Mon Sep 17 00:00:00 2001
From: Krzysztof Parzyszek <Krzysztof.Parzyszek at amd.com>
Date: Sun, 4 May 2025 09:58:45 -0500
Subject: [PATCH] [flang][Evaluate] Restrict ConstantBase constructor overload

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 unexpected errors.

Eliminate the constructor from overload for invalid input values to help
produce more meaningful diagnostics.
---
 flang/include/flang/Evaluate/constant.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

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