public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/101460] New: Useless cascade of overload resolution errors for invalid expression Date: Thu, 15 Jul 2021 11:11:18 +0000 [thread overview] Message-ID: <bug-101460-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101460 Bug ID: 101460 Summary: Useless cascade of overload resolution errors for invalid expression Product: gcc Version: 12.0 Status: UNCONFIRMED Keywords: diagnostic Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: redi at gcc dot gnu.org Target Milestone: --- template<bool> struct enable_if { }; template<> struct enable_if<true> { using type = void; }; template<bool B> using enable_if_t = typename enable_if<B>::type; struct tuple { }; struct pair { }; template<unsigned N> enable_if_t<N == 1> get(tuple&) { } template<unsigned N> enable_if_t<N == 1> get(const tuple&) { } template<unsigned N> enable_if_t<N == 1> get(pair&) { } template<unsigned N> enable_if_t<N == 1> get(const pair&) { } template<int N> constexpr unsigned frob() { static_assert(N == 1, "user-friendly diagnostic"); return unsigned{N}; // narrowing check, reject negative values } template<int N> void get_n(tuple& t) { get<frob<N>()>(t); } int main() { tuple t; get_n<-1>(t); } This prints a wall of errors: stupid.C: In instantiation of 'constexpr unsigned int frob() [with int N = -1]': stupid.C:23:51: required from 'void get_n(tuple&) [with int N = -1]' stupid.C:28:12: required from here stupid.C:19:19: error: static assertion failed: user-friendly diagnostic 19 | static_assert(N == 1, "user-friendly diagnostic"); | ~~^~~~ stupid.C:19:19: note: '(-1 == 1)' evaluates to false stupid.C:20:20: error: narrowing conversion of '-1' from 'int' to 'unsigned int' [-Wnarrowing] 20 | return unsigned{N}; // narrowing check, reject negative values | ^ stupid.C:21:1: error: body of 'constexpr' function 'constexpr unsigned int frob() [with int N = -1]' not a return-statement 21 | } | ^ stupid.C: In instantiation of 'void get_n(tuple&) [with int N = -1]': stupid.C:28:12: required from here stupid.C:23:54: error: no matching function for call to 'get<frob<-1>()>(tuple&)' 23 | template<int N> void get_n(tuple& t) { get<frob<N>()>(t); } | ~~~~~~~~~~~~~~^~~ stupid.C:10:42: note: candidate: 'template<unsigned int N> enable_if_t<(N == 1)> get(tuple&)' 10 | template<unsigned N> enable_if_t<N == 1> get(tuple&) { } | ^~~ stupid.C:10:42: note: template argument deduction/substitution failed: stupid.C:23:51: error: 'constexpr unsigned int frob() [with int N = -1]' called in a constant expression 23 | template<int N> void get_n(tuple& t) { get<frob<N>()>(t); } | ~~~~~~~^~ stupid.C:17:1: note: 'constexpr unsigned int frob() [with int N = -1]' is not usable as a 'constexpr' function because: 17 | frob() | ^~~~ stupid.C:23:51: note: in template argument for type 'unsigned int' 23 | template<int N> void get_n(tuple& t) { get<frob<N>()>(t); } | ~~~~~~~^~ stupid.C:11:42: note: candidate: 'template<unsigned int N> enable_if_t<(N == 1)> get(const tuple&)' 11 | template<unsigned N> enable_if_t<N == 1> get(const tuple&) { } | ^~~ stupid.C:11:42: note: template argument deduction/substitution failed: stupid.C:23:51: error: 'constexpr unsigned int frob() [with int N = -1]' called in a constant expression 23 | template<int N> void get_n(tuple& t) { get<frob<N>()>(t); } | ~~~~~~~^~ stupid.C:23:51: note: in template argument for type 'unsigned int' stupid.C:12:42: note: candidate: 'template<unsigned int N> enable_if_t<(N == 1)> get(pair&)' 12 | template<unsigned N> enable_if_t<N == 1> get(pair&) { } | ^~~ stupid.C:12:42: note: template argument deduction/substitution failed: stupid.C:23:51: error: 'constexpr unsigned int frob() [with int N = -1]' called in a constant expression 23 | template<int N> void get_n(tuple& t) { get<frob<N>()>(t); } | ~~~~~~~^~ stupid.C:23:51: note: in template argument for type 'unsigned int' stupid.C:13:42: note: candidate: 'template<unsigned int N> enable_if_t<(N == 1)> get(const pair&)' 13 | template<unsigned N> enable_if_t<N == 1> get(const pair&) { } | ^~~ stupid.C:13:42: note: substitution of deduced template arguments resulted in errors seen above This is reduced from libstdc++ where there are more overloads of get<N> and all of them get tried, and all of them print exactly the same error. Why are we even attempting overload resolution when the expression is invalid? Is the invalid non-constant frob<-1>() call going to suddenly become valid if we keep trying hard enough? If PR 96286 gets fixed then the static_assert in frob should stop compilation, but if we remove that from the code above then we'll still get a wall of unhelpful errors. Constant evaluation of frob<-1>() failed, so stop trying to use it as a template argument.
next reply other threads:[~2021-07-15 11:11 UTC|newest] Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-07-15 11:11 redi at gcc dot gnu.org [this message] 2021-07-15 11:58 ` [Bug c++/101460] " redi at gcc dot gnu.org 2021-07-15 12:04 ` redi at gcc dot gnu.org 2021-07-15 15:26 ` cvs-commit at gcc dot gnu.org 2021-07-19 12:38 ` cvs-commit at gcc dot gnu.org 2021-08-30 13:45 ` jason at gcc dot gnu.org 2021-08-30 21:23 ` cvs-commit at gcc dot gnu.org 2021-08-30 21:29 ` jason at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-101460-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).