[clang] Convert a default argument expression to the parameter type...
authorBruno Ricci <riccibrun@gmail.com>
Thu, 11 Jun 2020 12:13:05 +0000 (13:13 +0100)
committerBruno Ricci <riccibrun@gmail.com>
Thu, 11 Jun 2020 12:18:45 +0000 (13:18 +0100)
commit40ea01f6543d0d4aa2701d1b27a6c5413340e7d5
treeff1152d8ec39d6edb4ebfd67a6f37ec3cdf37f85
parente966a5deaa50f1ddca3810e945acd14b899824c7
[clang] Convert a default argument expression to the parameter type...

...before checking that the default argument is valid with
CheckDefaultArgumentVisitor.

Currently the restrictions on a default argument are checked with the visitor
CheckDefaultArgumentVisitor in ActOnParamDefaultArgument before
performing the conversion to the parameter type in SetParamDefaultArgument.

This was fine before the previous patch but now some valid code post-CWG 2346
is rejected:

void test() {
  const int i2 = 0;
  extern void h2a(int x = i2);     // FIXME: ok, not odr-use
  extern void h2b(int x = i2 + 0); // ok, not odr-use
}

This is because the reference to i2 in h2a has not been marked yet with
NOUR_Constant. i2 is marked NOUR_Constant when the conversion to the parameter
type is done, which is done just after.

The solution is to do the conversion to the parameter type before checking
the restrictions on default arguments with CheckDefaultArgumentVisitor.
This has the side-benefit of improving some diagnostics.

Differential Revision: https://reviews.llvm.org/D81616

Reviewed By: rsmith
clang/include/clang/Sema/Sema.h
clang/lib/Sema/SemaDeclCXX.cpp
clang/lib/Sema/SemaTemplateInstantiate.cpp
clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p7.cpp
clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p8.cpp
clang/test/SemaCXX/vartemplate-lambda.cpp
clang/test/SemaTemplate/instantiate-local-class.cpp