https://github.com/spall created 
https://github.com/llvm/llvm-project/pull/244088

Add support for C++14 deduced return types for normal functions.
Closes #235124 

>From 56242df8f16aeafef593028459467e0f97787db9 Mon Sep 17 00:00:00 2001
From: Sarah Spall <[email protected]>
Date: Wed, 18 Nov 2026 17:02:27 -0800
Subject: [PATCH 1/2] add support for deduced return types for normal functions

---
 clang/lib/Sema/SemaDecl.cpp                   |  4 +-
 clang/lib/Sema/SemaExpr.cpp                   |  4 +-
 clang/lib/Sema/SemaStmt.cpp                   |  2 +-
 clang/lib/Sema/SemaType.cpp                   |  2 +-
 clang/test/ParserHLSL/group_shared_202x.hlsl  |  5 +-
 .../SemaHLSL/Language/Auto-return-errors.hlsl | 12 +++
 clang/test/SemaHLSL/Language/Auto-return.hlsl | 76 +++++++++++++++++++
 .../test/SemaHLSL/Language/Auto-warning.hlsl  |  5 ++
 8 files changed, 101 insertions(+), 9 deletions(-)
 create mode 100644 clang/test/SemaHLSL/Language/Auto-return-errors.hlsl
 create mode 100644 clang/test/SemaHLSL/Language/Auto-return.hlsl

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 5a70ab0162f14c..b4877b2f41b756 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -12568,7 +12568,7 @@ bool Sema::CheckFunctionDeclaration(Scope *S, 
FunctionDecl *NewFD,
   // make the placeholder type dependent since it won't be deduced until the
   // definition is instantiated. We do this here because it needs to happen
   // for implicitly instantiated member functions/member function templates.
-  if (getLangOpts().CPlusPlus14 &&
+  if ((getLangOpts().CPlusPlus14 || getLangOpts().HLSL) &&
       (NewFD->isDependentContext() &&
        NewFD->getReturnType()->isUndeducedType())) {
     const FunctionProtoType *FPT =
@@ -17036,7 +17036,7 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt 
*Body, bool IsInstantiation,
         FD->setBody(Body);
       FD->setWillHaveBody(false);
 
-      if (getLangOpts().CPlusPlus14) {
+      if (getLangOpts().CPlusPlus14 || getLangOpts().HLSL) {
         if (!FD->isInvalidDecl() && Body && !FD->isDependentContext() &&
             FD->getReturnType()->isUndeducedType()) {
           // For a function with a deduced result type to return void,
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index d3deecb3ee74c6..23b1bd1da36980 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -91,7 +91,7 @@ bool Sema::CanUseDecl(NamedDecl *D, bool 
TreatUnavailableAsInvalid) {
 
     // If the function has a deduced return type, and we can't deduce it,
     // then we can't use it either.
-    if (getLangOpts().CPlusPlus14 && FD->getReturnType()->isUndeducedType() &&
+    if ((getLangOpts().CPlusPlus14 || getLangOpts().HLSL) && 
FD->getReturnType()->isUndeducedType() &&
         DeduceReturnType(FD, SourceLocation(), /*Diagnose*/ false))
       return false;
 
@@ -315,7 +315,7 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, 
ArrayRef<SourceLocation> Locs,
 
     // If the function has a deduced return type, and we can't deduce it,
     // then we can't use it either.
-    if (getLangOpts().CPlusPlus14 && FD->getReturnType()->isUndeducedType() &&
+    if ((getLangOpts().CPlusPlus14 || getLangOpts().HLSL) && 
FD->getReturnType()->isUndeducedType() &&
         DeduceReturnType(FD, Loc))
       return true;
 
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 4bf5b3f3ec6b74..8af34606fa4d46 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -4199,7 +4199,7 @@ StmtResult Sema::BuildReturnStmt(SourceLocation 
ReturnLoc, Expr *RetValExp,
 
   // FIXME: Add a flag to the ScopeInfo to indicate whether we're performing
   // deduction.
-  if (getLangOpts().CPlusPlus14) {
+  if (getLangOpts().CPlusPlus14 || getLangOpts().HLSL) {
     if (AutoType *AT = FnRetType->getContainedAutoType()) {
       // If we've already decided this function is invalid, e.g. because
       // we saw a `return` whose expression had an error, don't keep
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 19d39b58614859..e0da5ad1026ff2 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -5009,7 +5009,7 @@ static TypeSourceInfo 
*GetFullTypeForDeclarator(TypeProcessingState &state,
         // and not, for instance, a pointer to a function.
         if (D.getDeclSpec().hasAutoTypeSpec() &&
             !FTI.hasTrailingReturnType() && chunkIndex == 0) {
-          if (!S.getLangOpts().CPlusPlus14) {
+          if (!S.getLangOpts().CPlusPlus14 && !S.getLangOpts().HLSL) {
             S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
                    D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto
                        ? diag::err_auto_missing_trailing_return
diff --git a/clang/test/ParserHLSL/group_shared_202x.hlsl 
b/clang/test/ParserHLSL/group_shared_202x.hlsl
index 38fa4527e88c94..876194a37a286d 100644
--- a/clang/test/ParserHLSL/group_shared_202x.hlsl
+++ b/clang/test/ParserHLSL/group_shared_202x.hlsl
@@ -21,9 +21,8 @@ void foo() {
 
 extern groupshared float f;
 const float cf = f;
-// expected-error@#func{{'auto' return without trailing return type; deduced 
return types are a C++14 extension}}
-auto func() { // #func
-  return f; // expected-warning{{implicit conversion turns floating-point 
number into integer: 'float' to 'int'}}
+auto func() {
+  return f;
 }
 
 void other() {
diff --git a/clang/test/SemaHLSL/Language/Auto-return-errors.hlsl 
b/clang/test/SemaHLSL/Language/Auto-return-errors.hlsl
new file mode 100644
index 00000000000000..54240dc76ad130
--- /dev/null
+++ b/clang/test/SemaHLSL/Language/Auto-return-errors.hlsl
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl 
-std=hlsl202x -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl 
-std=hlsl202y -fsyntax-only -verify %s
+
+auto forward(); // expected-note {{declared here}}
+
+void use_before_definition() {
+  forward(); // expected-error {{function 'forward' with deduced return type 
cannot be used before it is defined}}
+}
+
+auto forward() {
+  return 42;
+}
diff --git a/clang/test/SemaHLSL/Language/Auto-return.hlsl 
b/clang/test/SemaHLSL/Language/Auto-return.hlsl
new file mode 100644
index 00000000000000..ec34cec9bc3ff5
--- /dev/null
+++ b/clang/test/SemaHLSL/Language/Auto-return.hlsl
@@ -0,0 +1,76 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl 
-std=hlsl202x -finclude-default-header -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl 
-std=hlsl202y -finclude-default-header -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+using namespace hlsl;
+
+auto return_int() {
+  return 42;
+}
+
+auto return_float() {
+  return 1.0f;
+}
+
+extern groupshared float F;
+
+auto return_groupshared() {
+  return F;
+}
+
+auto return_const() {
+  const float C = 1.0f;
+  return C;
+}
+
+auto return_vector() {
+  return float3(1.0f, 2.0f, 3.0f);
+}
+
+auto return_void_empty() {}
+
+auto return_void_explicit() {
+  return;
+}
+
+template <typename T> auto return_template(T Value) {
+  return Value;
+}
+
+template <typename T> auto return_template_constant() {
+  return 42;
+}
+
+template <typename T> struct ReturnTemplate {
+  auto get() {
+    return T();
+  }
+
+  T call() {
+    return get();
+  }
+};
+
+void use_template_member() {
+  ReturnTemplate<float> Value;
+  Value.call();
+  _Static_assert(__is_same(__decltype(Value.get()), float),
+                 "deduce class template member return type");
+}
+
+_Static_assert(__is_same(__decltype(return_int()), int), "deduce int");
+_Static_assert(__is_same(__decltype(return_float()), float), "deduce float");
+_Static_assert(__is_same(__decltype(return_groupshared()), float),
+               "deduce float without groupshared");
+_Static_assert(__is_same(__decltype(return_const()), float),
+               "deduce float without const");
+_Static_assert(__is_same(__decltype(return_vector()), float3), "deduce 
float3");
+_Static_assert(__is_same(__decltype(return_void_empty()), void),
+               "deduce void without return statements");
+_Static_assert(__is_same(__decltype(return_void_explicit()), void),
+               "deduce void from an explicit return");
+_Static_assert(__is_same(__decltype(return_template(1.0f)), float),
+               "deduce dependent template return type");
+_Static_assert(__is_same(__decltype(return_template_constant<float>()), int),
+               "deduce non-dependent template return type");
diff --git a/clang/test/SemaHLSL/Language/Auto-warning.hlsl 
b/clang/test/SemaHLSL/Language/Auto-warning.hlsl
index 2f84fb8b5fbe41..554a8f2d6c8dd3 100644
--- a/clang/test/SemaHLSL/Language/Auto-warning.hlsl
+++ b/clang/test/SemaHLSL/Language/Auto-warning.hlsl
@@ -16,6 +16,11 @@ auto foo(float A) -> float {
   return A;
 }
 
+// expected-warning@+1{{'auto' type specifier is an HLSL 202x extension}}
+auto deduced_return(float A) {
+  return A;
+}
+
 void bar(float A) {
 // expected-warning@+1{{'auto' type specifier is an HLSL 202x extension}}
   auto C = A;

>From b5c15b304064fc6f2e3a34366941ba7c4d2e6b4d Mon Sep 17 00:00:00 2001
From: Sarah Spall <[email protected]>
Date: Wed, 18 Nov 2026 18:09:58 -0800
Subject: [PATCH 2/2] create lang opt function to check if deduced return types
 are supported

---
 clang/include/clang/Basic/LangOptions.h | 2 ++
 clang/lib/Sema/SemaDecl.cpp             | 4 ++--
 clang/lib/Sema/SemaExpr.cpp             | 7 ++++---
 clang/lib/Sema/SemaStmt.cpp             | 2 +-
 clang/lib/Sema/SemaType.cpp             | 2 +-
 5 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/clang/include/clang/Basic/LangOptions.h 
b/clang/include/clang/Basic/LangOptions.h
index 8607fbae8d8fce..7af55a8553fccf 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -824,6 +824,8 @@ class LangOptions : public LangOptionsBase {
 
   bool allowArrayReturnTypes() const { return HLSL; }
 
+  bool hasDeducedReturnTypes() const { return CPlusPlus14 || HLSL; }
+
   /// Remap path prefix according to -fmacro-prefix-path option.
   void remapPathPrefix(SmallVectorImpl<char> &Path) const;
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b4877b2f41b756..539e5bdad147d0 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -12568,7 +12568,7 @@ bool Sema::CheckFunctionDeclaration(Scope *S, 
FunctionDecl *NewFD,
   // make the placeholder type dependent since it won't be deduced until the
   // definition is instantiated. We do this here because it needs to happen
   // for implicitly instantiated member functions/member function templates.
-  if ((getLangOpts().CPlusPlus14 || getLangOpts().HLSL) &&
+  if (getLangOpts().hasDeducedReturnTypes() &&
       (NewFD->isDependentContext() &&
        NewFD->getReturnType()->isUndeducedType())) {
     const FunctionProtoType *FPT =
@@ -17036,7 +17036,7 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt 
*Body, bool IsInstantiation,
         FD->setBody(Body);
       FD->setWillHaveBody(false);
 
-      if (getLangOpts().CPlusPlus14 || getLangOpts().HLSL) {
+      if (getLangOpts().hasDeducedReturnTypes()) {
         if (!FD->isInvalidDecl() && Body && !FD->isDependentContext() &&
             FD->getReturnType()->isUndeducedType()) {
           // For a function with a deduced result type to return void,
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 23b1bd1da36980..b1cc6f505e255d 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -91,7 +91,8 @@ bool Sema::CanUseDecl(NamedDecl *D, bool 
TreatUnavailableAsInvalid) {
 
     // If the function has a deduced return type, and we can't deduce it,
     // then we can't use it either.
-    if ((getLangOpts().CPlusPlus14 || getLangOpts().HLSL) && 
FD->getReturnType()->isUndeducedType() &&
+    if (getLangOpts().hasDeducedReturnTypes() &&
+        FD->getReturnType()->isUndeducedType() &&
         DeduceReturnType(FD, SourceLocation(), /*Diagnose*/ false))
       return false;
 
@@ -315,8 +316,8 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, 
ArrayRef<SourceLocation> Locs,
 
     // If the function has a deduced return type, and we can't deduce it,
     // then we can't use it either.
-    if ((getLangOpts().CPlusPlus14 || getLangOpts().HLSL) && 
FD->getReturnType()->isUndeducedType() &&
-        DeduceReturnType(FD, Loc))
+    if (getLangOpts().hasDeducedReturnTypes() &&
+        FD->getReturnType()->isUndeducedType() && DeduceReturnType(FD, Loc))
       return true;
 
     if (getLangOpts().CUDA && !CUDA().CheckCall(Loc, FD))
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 8af34606fa4d46..27bed76ba8f346 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -4199,7 +4199,7 @@ StmtResult Sema::BuildReturnStmt(SourceLocation 
ReturnLoc, Expr *RetValExp,
 
   // FIXME: Add a flag to the ScopeInfo to indicate whether we're performing
   // deduction.
-  if (getLangOpts().CPlusPlus14 || getLangOpts().HLSL) {
+  if (getLangOpts().hasDeducedReturnTypes()) {
     if (AutoType *AT = FnRetType->getContainedAutoType()) {
       // If we've already decided this function is invalid, e.g. because
       // we saw a `return` whose expression had an error, don't keep
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index e0da5ad1026ff2..9e9b965be045da 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -5009,7 +5009,7 @@ static TypeSourceInfo 
*GetFullTypeForDeclarator(TypeProcessingState &state,
         // and not, for instance, a pointer to a function.
         if (D.getDeclSpec().hasAutoTypeSpec() &&
             !FTI.hasTrailingReturnType() && chunkIndex == 0) {
-          if (!S.getLangOpts().CPlusPlus14 && !S.getLangOpts().HLSL) {
+          if (!S.getLangOpts().hasDeducedReturnTypes()) {
             S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
                    D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto
                        ? diag::err_auto_missing_trailing_return

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to