Skip to content

[202x] Enable auto return types - #8901

Open
Chris B (llvm-beanz) wants to merge 7 commits into
microsoft:mainfrom
llvm-beanz:202x-auto-returns
Open

Chris B (llvm-beanz) wants to merge 7 commits into
microsoft:mainfrom
llvm-beanz:202x-auto-returns

Conversation

@llvm-beanz

@llvm-beanz Chris B (llvm-beanz) commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator

This PR enables return type deduction for normal functions following C++ 14's use of auto as the return type in normal function prototype syntax.

The HLSL proposal is in progress here:
https://hlsl-tc57.github.io/tc57/proposal/0010

While this proposal is still in Refinement, so this is getting a bit ahead of the standard committee it seems like a highly likely feature that brings a lot of value for a fairly small set of changes.

Resolves #8903

Assisted-by: GitHub Copilot

This PR enables return type deduction for normal functions following
C++ 14's use of `auto` as the return type in normal function prototype
syntax.

The HLSL proposal is in progress here:
https://hlsl-tc57.github.io/tc57/proposal/0010

While this proposal is still in Refinement, so this is getting a bit
ahead of the standard committee it seems like a highly likely feature
that brings a lot of value for a fairly small set of changes.

Assisted-by: GitHub Copilot

../tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-errors.hlsl
../tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-extension-war
ning.hlsl
../tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-type.hlsl

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

HLSL-specific deduction restrictions are bypassed, and important test and release-note coverage is missing.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Enables C++14-style auto return-type deduction for HLSL functions across semantic analysis and code generation.

Changes:

  • Enables deduction during declaration, return analysis, and function use.
  • Adds DXIL and SPIR-V validation.
  • Adds diagnostics and legacy-version warning tests.
File summaries
File Description
tools/clang/lib/Sema/SemaType.cpp Permits deduced function return types in HLSL.
tools/clang/lib/Sema/SemaStmt.cpp Deduces types from return statements.
tools/clang/lib/Sema/SemaExpr.cpp Handles uses of functions with undeduced returns.
tools/clang/lib/Sema/SemaDecl.cpp Handles declarations, templates, and implicit void.
tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-type.hlsl Tests standard deduction and DXIL output.
tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-extension-warning.hlsl Tests legacy-mode warnings.
tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-errors.hlsl Tests invalid deduction cases.
tools/clang/test/CodeGenSPIRV/fn.auto.return.hlsl Tests SPIR-V return types.
Review details
  • Files reviewed: 8/8 changed files
  • Comments generated: 4
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tools/clang/lib/Sema/SemaStmt.cpp
Comment on lines +7488 to 7491
if ((getLangOpts().CPlusPlus14 || getLangOpts().HLSL) && // HLSL Change
(NewFD->isDependentContext() ||
(isFriend && CurContext->isDependentContext())) &&
NewFD->getReturnType()->isUndeducedType()) {
Comment thread tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-errors.hlsl Outdated
Comment on lines 3756 to +3758
if (D.getDeclSpec().containsPlaceholderType() &&
!FTI.hasTrailingReturnType() && chunkIndex == 0 &&
!S.getLangOpts().CPlusPlus14) {
!(S.getLangOpts().CPlusPlus14 || S.getLangOpts().HLSL)) {
@bob80905

Copy link
Copy Markdown
Collaborator

Do we want to disallow auto for entry-point functions?

Copilot AI review requested due to automatic review settings September 10, 2026 23:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Dynamic descriptor-heap returns bypass existing auto restrictions, and release-note coverage is missing.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (2)

tools/clang/lib/Sema/SemaStmt.cpp:3080

  • The new return-type path omits the dynamic descriptor-heap check used for auto variables in SemaDecl.cpp:6421-6433 and 9047-9053. Those heap expressions have HLSLDynamicResourceAttr, not HLSLNonAutoDeducibleAttr, so IsTypeDeducibleWithAuto accepts them and a function can deduce an internal dynamic resource/sampler return type even though auto x = ResourceDescriptorHeap[i] is rejected. Apply the same heap-expression check here before generic type classification and add resource/sampler return tests.
    if (getLangOpts().HLSL && !Deduced->isDependentType() &&
        !hlsl::IsTypeDeducibleWithAuto(*this, Deduced)) {
      Diag(RetExpr->getExprLoc(), diag::err_hlsl_auto_undeducible_type)
          << Deduced;

tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-errors.hlsl:29

  • The comment announces initializer-list coverage, but no initializer-list return test follows it, so the existing err_auto_fn_return_init_list path is not exercised in HLSL mode. Add the omitted auto function with return { 1, 2, 3 }; and verify cannot deduce return type from initializer list.
// Returning an initializer list as the deduced return value is not allowed.
// (Initializer lists in return statements are also unsupported in HLSL more
// broadly, but the auto-return-type case is still flagged early.)
  • Files reviewed: 9/9 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment on lines +2661 to +2665
// HLSL Change Begin - HLSL supports 'auto' as a function declarator return
// type with C++14-style deduction; skip this check for functions.
if (ContainsPlaceholderType &&
(!SemaRef.getLangOpts().CPlusPlus11 || !D.isFunctionDeclarator())) {
(!(SemaRef.getLangOpts().CPlusPlus11 || SemaRef.getLangOpts().HLSL) ||
!D.isFunctionDeclarator())) {
Copilot AI review requested due to automatic review settings September 11, 2026 01:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Auto returns currently bypass descriptor-heap restrictions, and release-note and test coverage gaps remain.

Review details

Suppressed comments (4)

Previously missed (1) — in code that hasn't changed since the last review.

tools/clang/lib/Sema/SemaStmt.cpp:3081

  • This check misses the separate descriptor-heap rule used for auto variables. .Resource/.Sampler carry HLSLDynamicResourceAttr, not HLSLNonAutoDeducibleAttr, so IsTypeDeducibleWithAuto returns true and an auto function can expose the otherwise-forbidden placeholder type with return ResourceDescriptorHeap[i] (or SamplerDescriptorHeap[i]). Apply the equivalent dynamic-heap expression check here, ideally via a shared helper, and add return-type cases alongside auto-no-descriptor-heap.hlsl.

tools/clang/lib/Sema/SemaType.cpp:2665

  • This is a user-visible HLSL language feature, so the repository's release-note policy requires an entry in docs/ReleaseNotes.md under Upcoming Release / HLSL Language. Please add that entry, or identify the related PR that will provide shared release-note coverage.
  // HLSL Change Begin - HLSL supports 'auto' as a function declarator return
  // type with C++14-style deduction; skip this check for functions.
  if (ContainsPlaceholderType &&
      (!(SemaRef.getLangOpts().CPlusPlus11 || SemaRef.getLangOpts().HLSL) ||
       !D.isFunctionDeclarator())) {

tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-errors.hlsl:29

  • This comment describes an initializer-list diagnostic, but the file contains no initializer-list return case after it, so that behavior is not actually tested. Add an auto function returning a braced initializer with the expected diagnostic (or remove the claim if it is intentionally out of scope).
// Returning an initializer list as the deduced return value is not allowed.
// (Initializer lists in return statements are also unsupported in HLSL more
// broadly, but the auto-return-type case is still flagged early.)

tools/clang/lib/Sema/SemaDecl.cpp:7490

  • The new dependent-context path is not exercised by the added tests: auto-template.hlsl only has templates with explicit return types. Add an HLSL template such as template<typename T> auto Identity(T value) { return value; } and instantiate it for multiple types; also cover deferred rejection of a non-auto-deducible type so the instantiation-time check is verified.
    if ((getLangOpts().CPlusPlus14 || getLangOpts().HLSL) && // HLSL Change
        (NewFD->isDependentContext() ||
         (isFriend && CurContext->isDependentContext())) &&
        NewFD->getReturnType()->isUndeducedType()) {
  • Files reviewed: 9/9 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

../tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-errors.hlsl
../tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-undeducible-t
ypes.hlsl

../tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-errors.hlsl
../tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-undeducible-t
ypes.hlsl
Copilot AI review requested due to automatic review settings September 11, 2026 14:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Release-note coverage, template-instantiation tests, and updated attribute documentation are still needed.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (2)

tools/clang/lib/Sema/SemaType.cpp:3758

  • This is a user-visible HLSL language feature, but the PR does not add the release note required for new language features by CONTRIBUTING.md:115-134. Please add a concise entry under docs/ReleaseNotes.mdUpcoming ReleaseHLSL Language (or point to the related PR that will provide the shared entry).
        // HLSL Change Begin - HLSL supports C++14-style deduced return types.
        if (D.getDeclSpec().containsPlaceholderType() &&
            !FTI.hasTrailingReturnType() && chunkIndex == 0 &&
            !(S.getLangOpts().CPlusPlus14 || S.getLangOpts().HLSL)) {

tools/clang/lib/Sema/SemaDecl.cpp:7490

  • This explicitly enables deduced returns for dependent functions, but none of the added tests exercises template<typename T> auto f(T); the existing auto-template.hlsl only uses auto for local variables in templates. Please cover successful instantiation (ideally for multiple types) and an instantiation whose return type is non-auto-deducible, since this branch defers deduction and validation until instantiation.
    if ((getLangOpts().CPlusPlus14 || getLangOpts().HLSL) && // HLSL Change
        (NewFD->isDependentContext() ||
         (isFriend && CurContext->isDependentContext())) &&
        NewFD->getReturnType()->isUndeducedType()) {
  • Files reviewed: 10/10 changed files
  • Comments generated: 1
  • Review effort level: Balanced

CXXRecordDecl *recordDecl = typeDeclBuilder.getRecordDecl();
recordDecl->addAttr(
HLSLDynamicResourceAttr::CreateImplicit(context, bSampler));
recordDecl->addAttr(HLSLNonAutoDeducibleAttr::CreateImplicit(context));
Copilot AI review requested due to automatic review settings September 11, 2026 14:40
../tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-return-undeducible-t
ypes.hlsl

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The user-visible language feature needs a release note and template return-deduction coverage.

Review details

Suppressed comments (2)

tools/clang/lib/Sema/SemaType.cpp:2665

  • This is a user-visible HLSL language feature, but the PR does not add the release note required by CONTRIBUTING.md for significant language changes. Please add a one-sentence entry under docs/ReleaseNotes.mdUpcoming ReleaseHLSL Language, or point to the related PR that will provide the shared release-note coverage.
      (!(SemaRef.getLangOpts().CPlusPlus11 || SemaRef.getLangOpts().HLSL) ||
       !D.isFunctionDeclarator())) {

tools/clang/lib/Sema/SemaDecl.cpp:7490

  • This branch explicitly enables deduced returns in dependent function contexts, but the new tests cover only non-template functions; the existing auto-template.hlsl uses explicit return types. Please add coverage for an instantiated template<typename T> auto function (including at least two concrete return types), so regressions in the template-instantiation deduction path are detected.
    if ((getLangOpts().CPlusPlus14 || getLangOpts().HLSL) && // HLSL Change
        (NewFD->isDependentContext() ||
         (isFriend && CurContext->isDependentContext())) &&
        NewFD->getReturnType()->isUndeducedType()) {
  • Files reviewed: 11/11 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

Copilot AI review requested due to automatic review settings September 11, 2026 14:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Release-note coverage and tests for the newly enabled dependent-function path are missing.

Review details

Suppressed comments (2)

tools/clang/lib/Sema/SemaDecl.cpp:7490

  • This branch also enables deduced return types for dependent functions, but none of the added tests declares a function template with an auto return type; the existing auto-template.hlsl only uses auto for local variables. Please cover at least successful instantiations with distinct deduced types and an instantiation whose return type is non-auto-deducible, so this newly enabled path and its deferred check cannot regress.
    if ((getLangOpts().CPlusPlus14 || getLangOpts().HLSL) && // HLSL Change
        (NewFD->isDependentContext() ||
         (isFriend && CurContext->isDependentContext())) &&
        NewFD->getReturnType()->isUndeducedType()) {

tools/clang/lib/Sema/SemaType.cpp:2665

  • This enables a user-visible HLSL language feature, but the PR does not update docs/ReleaseNotes.md. The repository policy in CONTRIBUTING.md:117-128 explicitly lists new language features as significant compiler changes requiring a release note. Please add a single-sentence entry under Upcoming ReleaseHLSL Language, or point to the related PR that will provide shared release-note coverage.
      (!(SemaRef.getLangOpts().CPlusPlus11 || SemaRef.getLangOpts().HLSL) ||
       !D.isFunctionDeclarator())) {
  • Files reviewed: 12/12 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

Copilot AI review requested due to automatic review settings September 11, 2026 14:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The semantic changes are focused and covered by positive, negative, compatibility-warning, DXIL, and SPIR-V tests.

Review details
  • Files reviewed: 12/12 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@dnovillo Diego Novillo (dnovillo) added the spirv Work related to SPIR-V label Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

spirv Work related to SPIR-V

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

[202x] auto return types for normal functions

4 participants