From patchwork Mon Feb 9 13:27:33 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Azzarone X-Patchwork-Id: 437941 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 2A3AF14012F for ; Tue, 10 Feb 2015 00:28:12 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:date:message-id:subject:from:to:content-type; q= dns; s=default; b=ug6S4YeOKvcI0xpeNBVUdw0LPiP910LlCbUlnAwRodLwRD O+JsIi6CZLODcy+LMlS9hQvvwBhKut99Buw4hBitSKkpY1oss5AOp046B4zjn7Va eFFQLmZXuQKezEFTu2InauAcz6vEGId51w5/p2fPmjjPVXQLhFOu7wPS1Df4Q= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:date:message-id:subject:from:to:content-type; s= default; bh=0E2iBbKyo5YzdVph/Y9yP0jeZfc=; b=wo6sOUw8xhlaDbxfmyD4 ilBJ+KKncKujcYJ5IL2wRj2ob+YnRZG5/WKv6kSsl3ZEENqpxYtDDqVWYjZYE99W hNyb4tWXyaqTEQTXajEChy7dHw8Mx/IBY7cCDxr8AeT6K2pc/FNOym+l2T58ub1s PHEm0Cd+Vm2XvmZ1DbB4ONA= Received: (qmail 20458 invoked by alias); 9 Feb 2015 13:27:39 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 20449 invoked by uid 89); 9 Feb 2015 13:27:39 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.1 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-la0-f52.google.com Received: from mail-la0-f52.google.com (HELO mail-la0-f52.google.com) (209.85.215.52) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Mon, 09 Feb 2015 13:27:37 +0000 Received: by labhv19 with SMTP id hv19so13245062lab.10 for ; Mon, 09 Feb 2015 05:27:33 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.152.178.132 with SMTP id cy4mr10960451lac.10.1423488453202; Mon, 09 Feb 2015 05:27:33 -0800 (PST) Received: by 10.114.181.33 with HTTP; Mon, 9 Feb 2015 05:27:33 -0800 (PST) Date: Mon, 9 Feb 2015 14:27:33 +0100 Message-ID: Subject: [PATCH] PR64959: SFINAE in UDLs From: Andrea Azzarone To: gcc-patches@gcc.gnu.org X-IsSubscribed: yes Hi all, atm enable_if cannot be used to select between overload of UDLs. E.g. https://gcc.gnu.org/bugzilla/attachment.cgi?id=34684 will not compile. This can be easily fixed making sure that lookup_literal_operator returns all the possible candidates and not just the first match. I made some more changes in parser.c to improve diagnostic in case of failures. Four testcases added: * udlit-sfinae.C * udlit-sfinae.neg.C * udlit-char-template-sfinae.C * udlit-char-template-sfinae-neg.C The first make check showed a failures in udlit-resolve.C. Actually the failures was a false positive, because UDLs lookup is a normal unqualified name lookup. I added two tests: * udlit-namespace-ambiguous.C * udlit-namespace-ambiguous-neg.C At the end I also noticed a bug in string template literals. As per N3599 the lookup should give precedence to operator""_t(const char*, unsigned long). I updated the code in parser.C and added a test. Please note that this is my first gcc patch :) 2015-2-9 Andrea Azzarone PR c++/64959 * gcc/cp/parser.c: Make sure lookup_literal_operator returns all the possible candidates. Also improve the diagnostic messages. * gcc/testsuite/g++.dg/cpp0x/udlit-namespace-ambiguous.C: Add test case to make sure gcc detects ambiguous UDL declarations. * gcc/testsuite/g++.dg/cpp0x/udlit-namespace-using-directive.C: Add test case to make sure gcc correctly handles using directive for UDLs. * gcc/testsuite/g++.dg/cpp0x/udlit-resolve.C: Remove a incorrect test case. * gcc/testsuite/g++.dg/cpp0x/udlit-sfinae.C: Add a test case to make sure that enable_if can be used to select between overloads of UDLs. * gcc/testsuite/g++.dg/cpp0x/udlit-sfinae-neg.C: Add a test case to make sure gcc correctly detects substitution failures when all the UDSL overloads are disabled by enable_if. * gcc/testsuite/g++.dg/cpp1y/udlit-char-template-sfinae-neg.C: Like cpp0x/udlit-sfinae-neg.C but for string template literals. * gcc/testsuite/g++.dg/cpp1y/udlit-char-template-sfinae.C: Like cpp0x/udlit-sfinae.C but for string template literals. * gcc/testsuite/g++.dg/cpp1y/udlit-char-template-vs-std-literal-operator.C: Add a test to make sure that string template literals have a smaller priority than standard literal operators. Index: gcc/cp/parser.c =================================================================== --- gcc/cp/parser.c (revision 220454) +++ gcc/cp/parser.c (working copy) @@ -3828,7 +3828,7 @@ lookup_literal_operator (tree name, vec< work in presence of default arguments on the literal operator parameters. */ && parmtypes == void_list_node) - return fn; + return decl; } } @@ -3955,7 +3955,7 @@ cp_parser_userdef_numeric_literal (cp_pa decl = lookup_literal_operator (name, args); if (decl && decl != error_mark_node) { - result = finish_call_expr (decl, &args, false, true, tf_none); + result = finish_call_expr (decl, &args, false, true, tf_warning_or_error); if (result != error_mark_node) { if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0) @@ -3986,7 +3986,7 @@ cp_parser_userdef_numeric_literal (cp_pa decl = lookup_literal_operator (name, args); if (decl && decl != error_mark_node) { - result = finish_call_expr (decl, &args, false, true, tf_none); + result = finish_call_expr (decl, &args, false, true, tf_warning_or_error); if (result != error_mark_node) { release_tree_vector (args); @@ -4004,13 +4004,12 @@ cp_parser_userdef_numeric_literal (cp_pa { tree tmpl_args = make_char_string_pack (num_string); decl = lookup_template_function (decl, tmpl_args); - result = finish_call_expr (decl, &args, false, true, tf_none); - if (result != error_mark_node) - { - release_tree_vector (args); - return result; - } + result = finish_call_expr (decl, &args, false, true, tf_warning_or_error); + + release_tree_vector (args); + return result; } + release_tree_vector (args); error ("unable to find numeric literal operator %qD", name); @@ -4035,6 +4034,24 @@ cp_parser_userdef_string_literal (tree l tree decl, result; vec *args; + /* Build up a call to the user-defined operator */ + /* Lookup the name we got back from the id-expression. */ + args = make_tree_vector (); + vec_safe_push (args, value); + vec_safe_push (args, build_int_cst (size_type_node, len)); + decl = lookup_literal_operator (name, args); + + if (decl && decl != error_mark_node) + { + result = finish_call_expr (decl, &args, false, true, tf_warning_or_error); + if (result != error_mark_node) + { + release_tree_vector (args); + return result; + } + } + release_tree_vector (args); + /* Look for a template function with typename parameter CharT and parameter pack CharT... Call the function with template parameter characters representing the string. */ @@ -4044,31 +4061,15 @@ cp_parser_userdef_string_literal (tree l { tree tmpl_args = make_string_pack (value); decl = lookup_template_function (decl, tmpl_args); - result = finish_call_expr (decl, &args, false, true, tf_none); + result = finish_call_expr (decl, &args, false, true, tf_warning_or_error); if (result != error_mark_node) - { - release_tree_vector (args); - return result; - } + { + release_tree_vector (args); + return result; + } } release_tree_vector (args); - /* Build up a call to the user-defined operator */ - /* Lookup the name we got back from the id-expression. */ - args = make_tree_vector (); - vec_safe_push (args, value); - vec_safe_push (args, build_int_cst (size_type_node, len)); - decl = lookup_name (name); - if (!decl || decl == error_mark_node) - { - error ("unable to find string literal operator %qD", name); - release_tree_vector (args); - return error_mark_node; - } - result = finish_call_expr (decl, &args, false, true, tf_none); - release_tree_vector (args); - if (result != error_mark_node) - return result; error ("unable to find string literal operator %qD with %qT, %qT arguments", name, TREE_TYPE (value), size_type_node); Index: gcc/testsuite/g++.dg/cpp0x/udlit-namespace-ambiguous.C =================================================================== --- gcc/testsuite/g++.dg/cpp0x/udlit-namespace-ambiguous.C (revision 0) +++ gcc/testsuite/g++.dg/cpp0x/udlit-namespace-ambiguous.C (working copy) @@ -0,0 +1,18 @@ +// { dg-do compile { target c++11 } } + +int operator""_t(long long unsigned); // { dg-message "note: candidate"} + +namespace foo { + int operator""_t(long long unsigned); // { dg-message "note: candidate"} +} + +using namespace foo; + +int main() +{ + 10_t; +} + +// { dg-error "call of overloaded|is ambiguous" 13} +// { dg-error "unable to find numeric literal operator" 13} +// { dg-message "note: use -std=gnu++11 or -fext-numeric-literals to enable more built-in suffixes" } Index: gcc/testsuite/g++.dg/cpp0x/udlit-namespace-using-directive.C =================================================================== --- gcc/testsuite/g++.dg/cpp0x/udlit-namespace-using-directive.C (revision 0) +++ gcc/testsuite/g++.dg/cpp0x/udlit-namespace-using-directive.C (working copy) @@ -0,0 +1,16 @@ +// { dg-do compile { target c++11 } } + +int operator""_t(long long unsigned) { + return 0; +} + +namespace foo { + int operator""_t(long long unsigned) { + return 0; + } +} + +int main() { + using foo::operator""_t; + 10_t; +} Index: gcc/testsuite/g++.dg/cpp0x/udlit-resolve.C =================================================================== --- gcc/testsuite/g++.dg/cpp0x/udlit-resolve.C (revision 220454) +++ gcc/testsuite/g++.dg/cpp0x/udlit-resolve.C (working copy) @@ -17,15 +17,10 @@ int operator"" _foo(const char32_t*, std template int operator"" _foo2() { return 20; } int operator"" _foo2(unsigned long long int) { return 21; } -namespace bar { -int operator"" _foo(unsigned long long int) { return 101; } -} -using namespace bar; - int main() { - assert(123_foo == 101); + assert(123_foo == 1); assert(0.123_foo == 2); assert('c'_foo == 3); assert(L'c'_foo == 4); Index: gcc/testsuite/g++.dg/cpp0x/udlit-sfinae-neg.C =================================================================== --- gcc/testsuite/g++.dg/cpp0x/udlit-sfinae-neg.C (revision 0) +++ gcc/testsuite/g++.dg/cpp0x/udlit-sfinae-neg.C (working copy) @@ -0,0 +1,21 @@ +// { dg-do compile { target c++11 } } +// { dg-prune-output "note:" } + +template struct enable_if { }; +template struct enable_if { typedef _Tp type; }; + +template +constexpr typename enable_if::type operator""_t() // { dg-error "no type named|in" } +{ + return 2; +} + +template +constexpr typename enable_if::type operator""_t() // { dg-error "no type named|in" } +{ + return 1; +} + +int a = 45_t; +int b = 4_t; +int c = 100000_t; // { dg-error "no matching function for call to" } Index: gcc/testsuite/g++.dg/cpp0x/udlit-sfinae.C =================================================================== --- gcc/testsuite/g++.dg/cpp0x/udlit-sfinae.C (revision 0) +++ gcc/testsuite/g++.dg/cpp0x/udlit-sfinae.C (working copy) @@ -0,0 +1,38 @@ +// { dg-do run { target c++11 } } + +#include + +template struct enable_if { }; +template struct enable_if { typedef _Tp type; }; + + +template +constexpr typename enable_if::type operator""_t() +{ + return 2; +} + +template +constexpr typename enable_if::type operator""_t() +{ + return 1; +} + +template +constexpr typename enable_if= 3, int>::type operator""_t() +{ + return 100; +} + +int operator""_t(long double) +{ + return 200; +} + +int main() +{ + assert(45_t == 2); + assert(4_t == 1); + assert(100000_t == 100); + assert(200.0_t == 200); +} Index: gcc/testsuite/g++.dg/cpp1y/udlit-char-template-sfinae-neg.C =================================================================== --- gcc/testsuite/g++.dg/cpp1y/udlit-char-template-sfinae-neg.C (revision 0) +++ gcc/testsuite/g++.dg/cpp1y/udlit-char-template-sfinae-neg.C (working copy) @@ -0,0 +1,21 @@ +// { dg-do compile { target c++14 } } +// { dg-prune-output "note:" } + +templatestruct enable_if {}; +template struct enable_if { typedef _Tp type; }; + + +template +typename enable_if::type operator"" _script() { // { dg-error "no type named|in" } + return 1; +} + +template +typename enable_if::type operator"" _script() { // { dg-error "no type named|in" } + return 2; +} + +int a = "1"_script; +int b = "22"_script; +int c = "333"_script; // { dg-error "no matching function for call to"} + // { dg-error "unable to find string literal operator" 20} Index: gcc/testsuite/g++.dg/cpp1y/udlit-char-template-sfinae.C =================================================================== --- gcc/testsuite/g++.dg/cpp1y/udlit-char-template-sfinae.C (revision 0) +++ gcc/testsuite/g++.dg/cpp1y/udlit-char-template-sfinae.C (working copy) @@ -0,0 +1,29 @@ +// { dg-do run { target c++14 } } + +#include + +templatestruct enable_if {}; +template struct enable_if { typedef _Tp type; }; + + +template +typename enable_if::type operator"" _script() { + return 5; +} + +template +typename enable_if::type operator"" _script() { + return 3; +} + +template +typename enable_if::type operator"" _script() { + return 1; +} + +int main() +{ + assert("hello!"_script == 5); + assert(u8"hi!"_script == 3); + assert("hey!"_script == 1); +} Index: gcc/testsuite/g++.dg/cpp1y/udlit-char-template-vs-std-literal-operator.C =================================================================== --- gcc/testsuite/g++.dg/cpp1y/udlit-char-template-vs-std-literal-operator.C (revision 0) +++ gcc/testsuite/g++.dg/cpp1y/udlit-char-template-vs-std-literal-operator.C (working copy) @@ -0,0 +1,17 @@ +// { dg-do run { target c++14 } } + +#include + +template +int operator"" _script() { + return 1; +} + +int operator"" _script(const char*, unsigned long) { + return 2; +} + +int main () +{ + assert("123"_script == 2); +}