Skip to content

Commit

Permalink
[clang] Fix CTAD not work for function-type and array-type arguments. (
Browse files Browse the repository at this point in the history
…#78159)

Fixes #51710.

When transforming a constructor into a corresponding deduction guide,
the decayed types (function/array type) were not handled properly which
made clang fail to compile valid code. The patch teaches clang handle
these decayed type in the transformation.
  • Loading branch information
hokein committed Jan 16, 2024
1 parent cd263a7 commit f725bb9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,8 @@ Bug Fixes in This Version
- Fix an issue where clang cannot find conversion function with template
parameter when instantiation of template class.
Fixes (`#77583 <https://github.com/llvm/llvm-project/issues/77583>`_)
- Fix an issue where CTAD fails for function-type/array-type arguments.
Fixes (`#51710 <https://github.com/llvm/llvm-project/issues/51710>`_)

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
18 changes: 9 additions & 9 deletions clang/lib/Sema/SemaTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2587,15 +2587,15 @@ struct ConvertConstructorToDeductionGuideTransform {
: ParamTy->isRValueReferenceType() ? VK_XValue
: VK_PRValue);
}

ParmVarDecl *NewParam = ParmVarDecl::Create(SemaRef.Context, DC,
OldParam->getInnerLocStart(),
OldParam->getLocation(),
OldParam->getIdentifier(),
NewDI->getType(),
NewDI,
OldParam->getStorageClass(),
NewDefArg.get());
// Handle arrays and functions decay.
auto NewType = NewDI->getType();
if (NewType->isArrayType() || NewType->isFunctionType())
NewType = SemaRef.Context.getDecayedType(NewType);

ParmVarDecl *NewParam = ParmVarDecl::Create(
SemaRef.Context, DC, OldParam->getInnerLocStart(),
OldParam->getLocation(), OldParam->getIdentifier(), NewType, NewDI,
OldParam->getStorageClass(), NewDefArg.get());
NewParam->setScopeInfo(OldParam->getFunctionScopeDepth(),
OldParam->getFunctionScopeIndex());
SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam, NewParam);
Expand Down
33 changes: 33 additions & 0 deletions clang/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,37 @@ namespace undefined_warnings {
auto test2 = TemplDObj(.0f);
}
}

namespace GH51710 {
template<typename T>
struct A {
A(T f()) {}
A(int f(), T) {}

A(T array[10]) {}
A(int array[10], T) {}
};

template<typename T>
struct B {
B(T array[]) {}
B(int array[], T) {}
};


int foo();

void bar() {
A test1(foo);
A test2(foo, 1);

int array[10];
A test3(array);
A test4(array, 1);

B test5(array);
B test6(array, 1);
}
} // namespace GH51710

#endif

0 comments on commit f725bb9

Please sign in to comment.