diff options
| author | Faisal Vali <faisalv@yahoo.com> | 2017-05-20 19:58:04 +0000 |
|---|---|---|
| committer | Faisal Vali <faisalv@yahoo.com> | 2017-05-20 19:58:04 +0000 |
| commit | 56f1de4dcbf9bde050a3a8093869d1e8abb1925c (patch) | |
| tree | b6251ed6dad8043cc4cc079c712175b562147c4d /clang/test | |
| parent | 1e39e5e96417808bb6ce51d2237e4f62c0f8525b (diff) | |
| download | bcm5719-llvm-56f1de4dcbf9bde050a3a8093869d1e8abb1925c.tar.gz bcm5719-llvm-56f1de4dcbf9bde050a3a8093869d1e8abb1925c.zip | |
Fix PR25627: constant expressions being odr-used in template arguments.
This patch ensures that clang processes the expression-nodes that are generated when disambiguating between types and expressions within template arguments as constant-expressions by installing the ConstantEvaluated ExpressionEvaluationContext just before attempting the disambiguation - and then making sure that Context carries through into ParseConstantExpression (by refactoring it out into a function that does not create its own EvaluationContext: ParseConstantExpressionInExprEvalContext)
Note, prior to this patch, trunk would correctly disambiguate and identify the expression as an expression - and while it would annotate the token with the expression - it would fail to complete the odr-use processing (specifically, failing to trigger Sema::UpdateMarkingForLValueToRValue as is done for all Constant Expressions, which would remove it from being considered odr-used). By installing the ConstantExpression Evaluation Context prior to disambiguation, and making sure it carries though, we ensure correct processing of the expression-node.
For e.g:
template<int> struct X { };
void f() {
const int N = 10;
X<N> x; // should be OK.
[] { return X<N>{}; }; // Should be OK - no capture - but clang errors!
}
See a related bug: https://bugs.llvm.org//show_bug.cgi?id=25627
In summary (and reiteration), the fix is as follows:
- Remove the EnteredConstantEvaluatedContext action from ParseTemplateArgumentList (relying on ParseTemplateArgument getting it right)
- Add the EnteredConstantEvaluatedContext action just prior to undergoing the disambiguating parse, and if the parse succeeds for an expression, carry the context though into a refactored version of ParseConstantExpression that does not create its own ExpressionEvaluationContext.
See https://reviews.llvm.org/D31588 for additional context regarding some of the more fragile and complicated approaches attempted, and Richard's feedback that eventually shaped the simpler and more robust rendition that is being committed.
Thanks Richard!
llvm-svn: 303492
Diffstat (limited to 'clang/test')
| -rw-r--r-- | clang/test/SemaCXX/lambda-expressions.cpp | 14 | ||||
| -rw-r--r-- | clang/test/SemaCXX/local-classes.cpp | 12 |
2 files changed, 24 insertions, 2 deletions
diff --git a/clang/test/SemaCXX/lambda-expressions.cpp b/clang/test/SemaCXX/lambda-expressions.cpp index e0ab15dc632..efbb47681ad 100644 --- a/clang/test/SemaCXX/lambda-expressions.cpp +++ b/clang/test/SemaCXX/lambda-expressions.cpp @@ -525,9 +525,9 @@ template <int N> class S {}; void foo() { - const int num = 18; // expected-note {{'num' declared here}} + const int num = 18; auto outer = []() { - auto inner = [](S<num> &X) {}; // expected-error {{variable 'num' cannot be implicitly captured in a lambda with no capture-default specified}} + auto inner = [](S<num> &X) {}; }; } } @@ -573,3 +573,13 @@ void foo1() { auto s1 = S1{[name=name]() {}}; // expected-error {{use of undeclared identifier 'name'; did you mean 'name1'?}} } } + +namespace PR25627_dont_odr_use_local_consts { + + template<int> struct X {}; + + void foo() { + const int N = 10; + (void) [] { X<N> x; }; + } +} diff --git a/clang/test/SemaCXX/local-classes.cpp b/clang/test/SemaCXX/local-classes.cpp index f4ca79159dc..eb0b7e43ebe 100644 --- a/clang/test/SemaCXX/local-classes.cpp +++ b/clang/test/SemaCXX/local-classes.cpp @@ -40,3 +40,15 @@ namespace Templates { }; } } + +namespace PR25627_dont_odr_use_local_consts { + template<int> struct X { X(); X(int); }; + + void foo() { + const int N = 10; + + struct Local { + void f(X<N> = X<N>()) {} // OK + }; + } +} |

