[Bug c++/93870] [8/9/10 Regression] User-defined conversion function not working in evaluation of template argument

2020-03-09 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93870

Marek Polacek  changed:

   What|Removed |Added

 Resolution|--- |FIXED
 Status|ASSIGNED|RESOLVED

--- Comment #7 from Marek Polacek  ---
commit d417b4f5414d9076300ab41974a14424f722688c
Author: Marek Polacek 
Date:   Fri Feb 28 16:57:04 2020 -0500

c++: Fix convert_like in template [PR91465, PR93870, PR92031, PR94068]

The point of this patch is to fix the recurring problem of trees
generated by convert_like while processing a template that break when
substituting.  For instance, when convert_like creates a CALL_EXPR
while in a template, substituting such a call breaks in finish_call_expr
because we have two 'this' arguments.  Another problem is that we
can create &TARGET_EXPR<> and then fail when substituting because we're
taking the address of an rvalue.  I've analyzed some of the already fixed
PRs and also some of the currently open ones:

In c++/93870 we create EnumWrapper::operator E(&operator~(E)).
In c++/87145 we create S::operator int (&{N}).
In c++/92031 we create &TARGET_EXPR <0>.

The gist of the problem is when convert_like_real creates a call for
a ck_user or wraps a TARGET_EXPR in & in a template.  So in these cases
use IMPLICIT_CONV_EXPR.  In a template we shouldn't need to perform the
actual conversion, we only need it's result type.
perform_direct_initialization_if_possible and
perform_implicit_conversion_flags can also create an IMPLICIT_CONV_EXPR.

Given the change above, build_converted_constant_expr can return an
IMPLICIT_CONV_EXPR so call fold_non_dependent_expr rather than
maybe_constant_value to deal with that.

To avoid the problem of instantiating something twice in a row I'm
removing a call to instantiate_non_dependent_expr_sfinae in
compute_array_index_type_loc.  And the build_converted_constant_expr
pattern can now be simplified.

2020-03-09  Marek Polacek  

PR c++/92031 - bogus taking address of rvalue error.
PR c++/91465 - ICE with template codes in check_narrowing.
PR c++/93870 - wrong error when converting template non-type arg.
PR c++/94068 - ICE with template codes in check_narrowing.
* call.c (convert_like_real): Return IMPLICIT_CONV_EXPR
in a template when not ck_identity and we're dealing with a class.
(convert_like_real) : Return IMPLICIT_CONV_EXPR
in a template if we need a temporary.
* decl.c (compute_array_index_type_loc): Remove
instantiate_non_dependent_expr_sfinae call.  Call
fold_non_dependent_expr instead of maybe_constant_value.
(build_explicit_specifier): Don't instantiate or create a sentinel
before converting the expression.
* except.c (build_noexcept_spec): Likewise.
* pt.c (convert_nontype_argument): Don't build IMPLICIT_CONV_EXPR.
Set IMPLICIT_CONV_EXPR_NONTYPE_ARG if that's what
build_converted_constant_expr returned.
* typeck2.c (check_narrowing): Call fold_non_dependent_expr instead
of maybe_constant_value.

* g++.dg/cpp0x/conv-tmpl2.C: New test.
* g++.dg/cpp0x/conv-tmpl3.C: New test.
* g++.dg/cpp0x/conv-tmpl4.C: New test.
* g++.dg/cpp0x/conv-tmpl5.C: New test.
* g++.dg/cpp0x/conv-tmpl6.C: New test.
* g++.dg/cpp1z/conv-tmpl1.C: New test.

[Bug c++/93870] [8/9/10 Regression] User-defined conversion function not working in evaluation of template argument

2020-03-04 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93870

Jakub Jelinek  changed:

   What|Removed |Added

   Target Milestone|8.4 |8.5

--- Comment #6 from Jakub Jelinek  ---
GCC 8.4.0 has been released, adjusting target milestone.

[Bug c++/93870] [8/9/10 Regression] User-defined conversion function not working in evaluation of template argument

2020-02-27 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93870

Marek Polacek  changed:

   What|Removed |Added

 Status|NEW |ASSIGNED
   Assignee|unassigned at gcc dot gnu.org  |mpolacek at gcc dot 
gnu.org

--- Comment #5 from Marek Polacek  ---
Mine but it occurs to me I should get back to bug 91465 before attacking this
one.  This one might even be fixed for free by the previous one.

[Bug c++/93870] [8/9/10 Regression] User-defined conversion function not working in evaluation of template argument

2020-02-27 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93870

--- Comment #4 from Marek Polacek  ---
Actually perhaps we should create an IMPLICIT_CONV_EXPR for *any*
value-dependent argument.

[Bug c++/93870] [8/9/10 Regression] User-defined conversion function not working in evaluation of template argument

2020-02-27 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93870

--- Comment #3 from Marek Polacek  ---
Completely untested:

--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -7082,7 +7082,7 @@ convert_nontype_argument (tree type, tree expr,
tsubst_flags_t complain)
 create a TARGET_EXPR, but in a template we can't
 use AGGR_INIT_EXPR, and the TARGET_EXPR would lead
 to a bogus error.  */
- || (val_dep_p && MAYBE_CLASS_TYPE_P (type
+ || val_dep_p))
{
  expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
  IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;

[Bug c++/93870] [8/9/10 Regression] User-defined conversion function not working in evaluation of template argument

2020-02-27 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93870

Marek Polacek  changed:

   What|Removed |Added

   Priority|P3  |P2

[Bug c++/93870] [8/9/10 Regression] User-defined conversion function not working in evaluation of template argument

2020-02-27 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93870

--- Comment #2 from Marek Polacek  ---
Clearly we're trying to take the address of a TARGET_EXPR:

TARGET_EXPR 

That's bad and we've had PRs about this in the past, e.g. Bug 87145.  I suspect
the fix might be along the same lines.

[Bug c++/93870] [8/9/10 Regression] User-defined conversion function not working in evaluation of template argument

2020-02-27 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93870

Marek Polacek  changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
   Last reconfirmed||2020-02-27
 CC||mpolacek at gcc dot gnu.org
   Target Milestone|--- |8.4
Summary|User-defined conversion |[8/9/10 Regression]
   |function not working in |User-defined conversion
   |evaluation of template  |function not working in
   |argument|evaluation of template
   ||argument
 Ever confirmed|0   |1

--- Comment #1 from Marek Polacek  ---
Confirmed.  Started with r7-4012-g16b61424dd309f61326f577a6deb8487c6c1f291.