-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang] Check std::initializer_list more strictly
#133822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -12071,6 +12071,37 @@ NamespaceDecl *Sema::getOrCreateStdNamespace() { | |||||
| return getStdNamespace(); | ||||||
| } | ||||||
|
|
||||||
| /// Check that the template-head of this class template is acceptable for | ||||||
| /// a declaration of 'std::initializer_list', and optionally diagnose if | ||||||
| /// it is not. | ||||||
| /// \returns true if any issues were found. | ||||||
| static bool CheckStdInitializerList(Sema &S, ClassTemplateDecl *Template, | ||||||
| bool Diagnose) { | ||||||
| TemplateParameterList *Params = Template->getTemplateParameters(); | ||||||
|
||||||
| TemplateParameterList *Params = Template->getTemplateParameters(); | |
| const TemplateParameterList *Params = Template->getTemplateParameters(); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| auto *Param = dyn_cast<TemplateTypeParmDecl>(Params->getParam(0)); | |
| const auto *Param = dyn_cast<TemplateTypeParmDecl>(Params->getParam(0)); |
I think the Template parameter can be const as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks; updated.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please start all the comments with an uppercase latter and end them with a period.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // RUN: %clang_cc1 %s -verify=expected,type-param -std=c++23 -DTYPE_PARAM | ||
| // RUN: %clang_cc1 %s -verify=expected,others -std=c++23 -DCONSTANT_PARAM | ||
| // RUN: %clang_cc1 %s -verify=expected,others -std=c++23 -DTYPE_TEMPLATE_PARAM | ||
| // RUN: %clang_cc1 %s -verify=expected,others -std=c++23 -DDEFAULT_ARG | ||
| // RUN: %clang_cc1 %s -verify=expected,others -std=c++23 -DMULTIPLE_PARAMS | ||
| // RUN: %clang_cc1 %s -verify=expected,others -std=c++23 -DPARAM_PACK | ||
| // RUN: %clang_cc1 %s -verify=expected,others -std=c++23 -DCONSTRAINED_PARAM | ||
| // RUN: %clang_cc1 %s -verify=expected,others -std=c++23 -DREQUIRES_CLAUSE | ||
| // RUN: %clang_cc1 %s -verify=expected,others -std=c++23 -DNONCLASS_TEMPLATE | ||
| // RUN: %clang_cc1 %s -verify=expected,others -std=c++23 -DNONTEMPLATE | ||
|
|
||
|
Comment on lines
+1
to
+11
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why isn't that a single test?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because otherwise they're redefined |
||
| namespace std { | ||
|
|
||
| #ifdef TYPE_PARAM | ||
| template<class> class initializer_list; | ||
| // expected-note@-1 2 {{template is declared here}} | ||
| #elifdef CONSTANT_PARAM | ||
| template<int> class initializer_list; | ||
| // expected-error@-1 2 {{std::initializer_list must have a type template parameter}} | ||
| #elifdef TYPE_TEMPLATE_PARAM | ||
| template<template<class> class> class initializer_list; | ||
| // expected-error@-1 2 {{std::initializer_list must have a type template parameter}} | ||
| #elifdef DEFAULT_ARG | ||
| template<class = int> class initializer_list; | ||
| // expected-error@-1 2 {{std::initializer_list cannot have default template arguments}} | ||
| #elifdef MULTIPLE_PARAMS | ||
| template<class, class> class initializer_list; | ||
| // expected-error@-1 2 {{std::initializer_list must have exactly one template parameter}} | ||
| #elifdef PARAM_PACK | ||
| template<class...> class initializer_list; | ||
| // expected-error@-1 2 {{std::initializer_list cannot be a variadic template}} | ||
| #elifdef CONSTRAINED_PARAM | ||
| template<class> concept C = true; | ||
| template<C> class initializer_list; | ||
| // expected-error@-1 2 {{std::initializer_list cannot have associated constraints}} | ||
| #elifdef REQUIRES_CLAUSE | ||
| template<class> requires true class initializer_list; | ||
| // expected-error@-1 2 {{std::initializer_list cannot have associated constraints}} | ||
| #elifdef NONCLASS_TEMPLATE | ||
| template<class> class IL; | ||
| template<class T> using initializer_list = IL<T>; | ||
| // expected-error@-1 2 {{std::initializer_list must be a class template}} | ||
| #elifdef NONTEMPLATE | ||
| class initializer_list; | ||
| // expected-error@-1 2 {{std::initializer_list must be a class template}} | ||
| #else | ||
| #error Unexpected test kind | ||
| #endif | ||
|
|
||
| } | ||
|
|
||
| struct Test { // expected-note 2 {{candidate constructor}} | ||
| #ifdef CONSTANT_PARAM | ||
| Test(std::initializer_list<1>); // expected-note {{candidate constructor}} | ||
| #elifdef TYPE_TEMPLATE_PARAM | ||
| template<class> using A = double; | ||
| Test(std::initializer_list<A>); // expected-note {{candidate constructor}} | ||
| #elifdef MULTIPLE_PARAMS | ||
| Test(std::initializer_list<double, double>); // expected-note {{candidate constructor}} | ||
| #elifdef NONTEMPLATE | ||
| Test(std::initializer_list); // expected-note {{candidate constructor}} | ||
| #else | ||
| Test(std::initializer_list<double>); // expected-note {{candidate constructor}} | ||
| #endif | ||
| }; | ||
| Test test {1.2, 3.4}; // expected-error {{no matching constructor}} | ||
|
|
||
| auto x = {1}; | ||
| // type-param-error@-1 {{implicit instantiation of undefined template}} | ||
| // others-note@-2 {{used here}} | ||
|
|
||
| void f() { | ||
| for(int x : {1, 2}); | ||
| // type-param-error@-1 {{implicit instantiation of undefined template}} | ||
| // type-param-error@-2 {{invalid range expression}} | ||
| // others-note@-3 {{used here}} | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use "enum_select" format instead of "select" format?
See https://clang.llvm.org/docs/InternalsManual.html#formatting-a-diagnostic-argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is a lot nicer, thank you.