Blame SOURCES/gcc9-rh1974984.patch

865e98
https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=d417b4f5414d9076300ab41974a14424f722688c
865e98
https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=75b7b7fdc4597170f24c069ea13aa3e14f37fde7
865e98
https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=8e64d182850560dbedfabb88aac90d4fc6155067
865e98
865e98
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
865e98
index 304c01619da..3a15e6e2e47 100644
865e98
--- a/gcc/cp/call.c
865e98
+++ b/gcc/cp/call.c
865e98
@@ -166,6 +166,8 @@ static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
865e98
 		     /*c_cast_p=*/false, (COMPLAIN))
865e98
 static tree convert_like_real (conversion *, tree, tree, int, bool,
865e98
 			       bool, tsubst_flags_t);
865e98
+static tree convert_like_real_1 (conversion *, tree, tree, int, bool,
865e98
+				 bool, tsubst_flags_t);
865e98
 static void op_error (const op_location_t &, enum tree_code, enum tree_code,
865e98
 		      tree, tree, tree, bool);
865e98
 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
865e98
@@ -6995,6 +6997,39 @@ maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum)
865e98
 	    "  initializing argument %P of %qD", argnum, fn);
865e98
 }
865e98
 
865e98
+/* Wrapper for convert_like_real_1 that handles creating IMPLICIT_CONV_EXPR.  */
865e98
+
865e98
+static tree
865e98
+convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
865e98
+		   bool issue_conversion_warnings,
865e98
+		   bool c_cast_p, tsubst_flags_t complain)
865e98
+{
865e98
+  /* Creating &TARGET_EXPR<> in a template breaks when substituting,
865e98
+     and creating a CALL_EXPR in a template breaks in finish_call_expr
865e98
+     so use an IMPLICIT_CONV_EXPR for this conversion.  We would have
865e98
+     created such codes e.g. when calling a user-defined conversion
865e98
+     function.  */
865e98
+  tree conv_expr = NULL_TREE;
865e98
+  if (processing_template_decl
865e98
+      && convs->kind != ck_identity
865e98
+      && (CLASS_TYPE_P (convs->type) || CLASS_TYPE_P (TREE_TYPE (expr))))
865e98
+    {
865e98
+      conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr);
865e98
+      if (convs->kind != ck_ref_bind)
865e98
+	conv_expr = convert_from_reference (conv_expr);
865e98
+      if (!convs->bad_p)
865e98
+	return conv_expr;
865e98
+      /* Do the normal processing to give the bad_p errors.  But we still
865e98
+	 need to return the IMPLICIT_CONV_EXPR, unless we're returning
865e98
+	 error_mark_node.  */
865e98
+    }
865e98
+  expr = convert_like_real_1 (convs, expr, fn, argnum,
865e98
+			      issue_conversion_warnings, c_cast_p, complain);
865e98
+  if (expr == error_mark_node)
865e98
+    return error_mark_node;
865e98
+  return conv_expr ? conv_expr : expr;
865e98
+}
865e98
+
865e98
 /* Perform the conversions in CONVS on the expression EXPR.  FN and
865e98
    ARGNUM are used for diagnostics.  ARGNUM is zero based, -1
865e98
    indicates the `this' argument of a method.  INNER is nonzero when
865e98
@@ -7006,9 +7041,9 @@ maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum)
865e98
    conversions to inaccessible bases are permitted.  */
865e98
 
865e98
 static tree
865e98
-convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
865e98
-		   bool issue_conversion_warnings,
865e98
-		   bool c_cast_p, tsubst_flags_t complain)
865e98
+convert_like_real_1 (conversion *convs, tree expr, tree fn, int argnum,
865e98
+		     bool issue_conversion_warnings,
865e98
+		     bool c_cast_p, tsubst_flags_t complain)
865e98
 {
865e98
   tree totype = convs->type;
865e98
   diagnostic_t diag_kind;
865e98
@@ -7466,6 +7501,14 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
865e98
 		expr = convert_bitfield_to_declared_type (expr);
865e98
 		expr = fold_convert (type, expr);
865e98
 	      }
865e98
+
865e98
+	    /* Creating &TARGET_EXPR<> in a template would break when
865e98
+	       tsubsting the expression, so use an IMPLICIT_CONV_EXPR
865e98
+	       instead.  This can happen even when there's no class
865e98
+	       involved, e.g., when converting an integer to a reference
865e98
+	       type.  */
865e98
+	    if (processing_template_decl)
865e98
+	      return build1 (IMPLICIT_CONV_EXPR, totype, expr);
865e98
 	    expr = build_target_expr_with_type (expr, type, complain);
865e98
 	  }
865e98
 
865e98
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
865e98
index 782f0f6e60f..b7190fb8761 100644
865e98
--- a/gcc/cp/decl.c
865e98
+++ b/gcc/cp/decl.c
865e98
@@ -9752,13 +9752,12 @@ compute_array_index_type_loc (location_t name_loc, tree name, tree size,
865e98
 	   NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case.  */;
865e98
       else
865e98
 	{
865e98
-	  size = instantiate_non_dependent_expr_sfinae (size, complain);
865e98
 	  size = build_converted_constant_expr (size_type_node, size, complain);
865e98
 	  /* Pedantically a constant expression is required here and so
865e98
 	     __builtin_is_constant_evaluated () should fold to true if it
865e98
 	     is successfully folded into a constant.  */
865e98
-	  size = maybe_constant_value (size, NULL_TREE,
865e98
-				       /*manifestly_const_eval=*/true);
865e98
+	  size = fold_non_dependent_expr (size, complain,
865e98
+					  /*manifestly_const_eval=*/true);
865e98
 
865e98
 	  if (!TREE_CONSTANT (size))
865e98
 	    size = origsize;
865e98
@@ -16784,10 +16783,8 @@ build_explicit_specifier (tree expr, tsubst_flags_t complain)
865e98
     /* Wait for instantiation, tsubst_function_decl will handle it.  */
865e98
     return expr;
865e98
 
865e98
-  expr = instantiate_non_dependent_expr_sfinae (expr, complain);
865e98
-  /* Don't let convert_like_real create more template codes.  */
865e98
-  processing_template_decl_sentinel s;
865e98
   expr = build_converted_constant_bool_expr (expr, complain);
865e98
+  expr = instantiate_non_dependent_expr_sfinae (expr, complain);
865e98
   expr = cxx_constant_value (expr);
865e98
   return expr;
865e98
 }
865e98
diff --git a/gcc/cp/except.c b/gcc/cp/except.c
865e98
index 03a9c8e53c7..b113eacacb3 100644
865e98
--- a/gcc/cp/except.c
865e98
+++ b/gcc/cp/except.c
865e98
@@ -1288,10 +1288,8 @@ build_noexcept_spec (tree expr, tsubst_flags_t complain)
865e98
   if (TREE_CODE (expr) != DEFERRED_NOEXCEPT
865e98
       && !value_dependent_expression_p (expr))
865e98
     {
865e98
-      expr = instantiate_non_dependent_expr_sfinae (expr, complain);
865e98
-      /* Don't let convert_like_real create more template codes.  */
865e98
-      processing_template_decl_sentinel s;
865e98
       expr = build_converted_constant_bool_expr (expr, complain);
865e98
+      expr = instantiate_non_dependent_expr_sfinae (expr, complain);
865e98
       expr = cxx_constant_value (expr);
865e98
     }
865e98
   if (TREE_CODE (expr) == INTEGER_CST)
865e98
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
865e98
index 612557bb717..710956e5a08 100644
865e98
--- a/gcc/cp/pt.c
865e98
+++ b/gcc/cp/pt.c
865e98
@@ -6919,19 +6919,6 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
865e98
       else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
865e98
 	       || cxx_dialect >= cxx17)
865e98
 	{
865e98
-	  /* Calling build_converted_constant_expr might create a call to
865e98
-	     a conversion function with a value-dependent argument, which
865e98
-	     could invoke taking the address of a temporary representing
865e98
-	     the result of the conversion.  */
865e98
-	  if (COMPOUND_LITERAL_P (expr)
865e98
-	      && CONSTRUCTOR_IS_DEPENDENT (expr)
865e98
-	      && MAYBE_CLASS_TYPE_P (expr_type)
865e98
-	      && TYPE_HAS_CONVERSION (expr_type))
865e98
-	    {
865e98
-	      expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
865e98
-	      IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
865e98
-	      return expr;
865e98
-	    }
865e98
 	  /* C++17: A template-argument for a non-type template-parameter shall
865e98
 	     be a converted constant expression (8.20) of the type of the
865e98
 	     template-parameter.  */
865e98
@@ -6940,6 +6927,11 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
865e98
 	    /* Make sure we return NULL_TREE only if we have really issued
865e98
 	       an error, as described above.  */
865e98
 	    return (complain & tf_error) ? NULL_TREE : error_mark_node;
865e98
+	  else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
865e98
+	    {
865e98
+	      IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
865e98
+	      return expr;
865e98
+	    }
865e98
 	  expr = maybe_constant_value (expr, NULL_TREE,
865e98
 				       /*manifestly_const_eval=*/true);
865e98
 	  expr = convert_from_reference (expr);
865e98
diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c
865e98
index aae5ff24c98..7c5a45c0848 100644
865e98
--- a/gcc/cp/typeck2.c
865e98
+++ b/gcc/cp/typeck2.c
865e98
@@ -926,7 +926,11 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain, bool const_only)
865e98
       return ok;
865e98
     }
865e98
 
865e98
-  init = maybe_constant_value (init);
865e98
+  /* Even non-dependent expressions can still have template
865e98
+     codes like CAST_EXPR, so use *_non_dependent_expr to cope.  */
865e98
+  init = fold_non_dependent_expr (init, complain);
865e98
+  if (init == error_mark_node)
865e98
+    return ok;
865e98
 
865e98
   /* If we were asked to only check constants, return early.  */
865e98
   if (const_only && !TREE_CONSTANT (init))
865e98
diff --git a/gcc/testsuite/g++.dg/conversion/op7.C b/gcc/testsuite/g++.dg/conversion/op7.C
865e98
new file mode 100644
865e98
index 00000000000..c6401d109b4
865e98
--- /dev/null
865e98
+++ b/gcc/testsuite/g++.dg/conversion/op7.C
865e98
@@ -0,0 +1,22 @@
865e98
+// PR c++/94190 - wrong no post-decrement operator error in template.
865e98
+
865e98
+struct S { operator long & (); } b;
865e98
+
865e98
+template<int> void
865e98
+foo ()
865e98
+{
865e98
+  b--;
865e98
+  ++b;
865e98
+  --b;
865e98
+  b++;
865e98
+  !b;
865e98
+  ~b;
865e98
+  +b;
865e98
+  -b;
865e98
+}
865e98
+
865e98
+void
865e98
+bar ()
865e98
+{
865e98
+  foo<0> ();
865e98
+}
865e98
diff --git a/gcc/testsuite/g++.dg/conversion/ref4.C b/gcc/testsuite/g++.dg/conversion/ref4.C
865e98
new file mode 100644
865e98
index 00000000000..464a4cf6c0f
865e98
--- /dev/null
865e98
+++ b/gcc/testsuite/g++.dg/conversion/ref4.C
865e98
@@ -0,0 +1,22 @@
865e98
+// PR c++/95789
865e98
+// { dg-do compile { target c++11 } }
865e98
+
865e98
+struct B {
865e98
+    int n;
865e98
+};
865e98
+
865e98
+template <typename T>
865e98
+struct A {
865e98
+    B& get() const { return f; } // { dg-error "binding reference" }
865e98
+
865e98
+    B f;
865e98
+};
865e98
+
865e98
+int main() {
865e98
+    A<int> a;
865e98
+    a.f = {};
865e98
+
865e98
+    a.get().n = 10;
865e98
+    if (a.f.n != 0)
865e98
+      __builtin_abort();
865e98
+}
865e98
diff --git a/gcc/testsuite/g++.dg/conversion/ref5.C b/gcc/testsuite/g++.dg/conversion/ref5.C
865e98
new file mode 100644
865e98
index 00000000000..0042acd0670
865e98
--- /dev/null
865e98
+++ b/gcc/testsuite/g++.dg/conversion/ref5.C
865e98
@@ -0,0 +1,14 @@
865e98
+// PR c++/96104
865e98
+
865e98
+template <typename T> void fn(T &);
865e98
+class E {};
865e98
+struct F {
865e98
+  template <typename T> void mfn(T t) { t, fn(E()); } // { dg-error "cannot bind non-const lvalue reference" }
865e98
+};
865e98
+int
865e98
+main()
865e98
+{
865e98
+  E e;
865e98
+  F f;
865e98
+  f.mfn(e);
865e98
+}
865e98
diff --git a/gcc/testsuite/g++.dg/conversion/ref6.C b/gcc/testsuite/g++.dg/conversion/ref6.C
865e98
new file mode 100644
865e98
index 00000000000..fc87199053c
865e98
--- /dev/null
865e98
+++ b/gcc/testsuite/g++.dg/conversion/ref6.C
865e98
@@ -0,0 +1,24 @@
865e98
+// PR c++/96179
865e98
+// { dg-do compile { target c++11 } }
865e98
+
865e98
+template<typename T> struct vector
865e98
+{
865e98
+  void push_back(T) { }
865e98
+};
865e98
+
865e98
+struct dummy{
865e98
+        int a;
865e98
+};
865e98
+
865e98
+void Modify_Dummy(dummy &d){
865e98
+        d.a=1;
865e98
+}
865e98
+
865e98
+template <bool bla=true> void Templated_Function(){
865e98
+        vector<dummy> A;
865e98
+        A.push_back(Modify_Dummy(dummy{0})); // { dg-error "cannot bind non-const lvalue reference" }
865e98
+}
865e98
+
865e98
+int main(){
865e98
+        Templated_Function();
865e98
+}
865e98
diff --git a/gcc/testsuite/g++.dg/cpp0x/conv-tmpl2.C b/gcc/testsuite/g++.dg/cpp0x/conv-tmpl2.C
865e98
new file mode 100644
865e98
index 00000000000..8a505769c3c
865e98
--- /dev/null
865e98
+++ b/gcc/testsuite/g++.dg/cpp0x/conv-tmpl2.C
865e98
@@ -0,0 +1,21 @@
865e98
+// PR c++/92031 - bogus taking address of rvalue error.
865e98
+// { dg-do compile { target c++11 } }
865e98
+
865e98
+struct x { const int& l; };
865e98
+
865e98
+void a(const x&) {}
865e98
+
865e98
+template<class E>                          
865e98
+void f() {
865e98
+  a(x { 0 });
865e98
+}
865e98
+
865e98
+void g() {
865e98
+  a(x { 0 });
865e98
+}
865e98
+
865e98
+void
865e98
+test ()
865e98
+{
865e98
+  f<int>();
865e98
+}
865e98
diff --git a/gcc/testsuite/g++.dg/cpp0x/conv-tmpl3.C b/gcc/testsuite/g++.dg/cpp0x/conv-tmpl3.C
865e98
new file mode 100644
865e98
index 00000000000..e2021aa13e1
865e98
--- /dev/null
865e98
+++ b/gcc/testsuite/g++.dg/cpp0x/conv-tmpl3.C
865e98
@@ -0,0 +1,16 @@
865e98
+// PR c++/91465 - ICE with template codes in check_narrowing.
865e98
+// { dg-do compile { target c++11 } }
865e98
+
865e98
+enum class D { X };
865e98
+enum class S { Z };
865e98
+
865e98
+D foo(S) { return D{}; }
865e98
+D foo(double) { return D{}; }
865e98
+
865e98
+template <typename>
865e98
+struct Bar {
865e98
+  D baz(S s)
865e98
+  {
865e98
+    return D{foo(s)};
865e98
+  }
865e98
+};
865e98
diff --git a/gcc/testsuite/g++.dg/cpp0x/conv-tmpl4.C b/gcc/testsuite/g++.dg/cpp0x/conv-tmpl4.C
865e98
new file mode 100644
865e98
index 00000000000..966a2e1ac9e
865e98
--- /dev/null
865e98
+++ b/gcc/testsuite/g++.dg/cpp0x/conv-tmpl4.C
865e98
@@ -0,0 +1,33 @@
865e98
+// PR c++/93870 - wrong error when converting template non-type arg.
865e98
+// { dg-do compile { target c++11 } }
865e98
+
865e98
+template <typename ENUM> struct EnumWrapper
865e98
+{
865e98
+	ENUM value;
865e98
+
865e98
+	constexpr operator ENUM() const
865e98
+	{
865e98
+		return value;
865e98
+	}
865e98
+};
865e98
+
865e98
+enum E : int { V };
865e98
+
865e98
+constexpr EnumWrapper<E> operator ~(E a)
865e98
+{
865e98
+    return {E(~int(a))};
865e98
+}
865e98
+
865e98
+template <E X> struct R
865e98
+{
865e98
+    static void Func();
865e98
+};
865e98
+
865e98
+template <E X> struct S : R<~X>
865e98
+{
865e98
+};
865e98
+
865e98
+void Test()
865e98
+{
865e98
+    S<E::V>::Func();
865e98
+}
865e98
diff --git a/gcc/testsuite/g++.dg/cpp0x/conv-tmpl5.C b/gcc/testsuite/g++.dg/cpp0x/conv-tmpl5.C
865e98
new file mode 100644
865e98
index 00000000000..c83e6d83ed9
865e98
--- /dev/null
865e98
+++ b/gcc/testsuite/g++.dg/cpp0x/conv-tmpl5.C
865e98
@@ -0,0 +1,13 @@
865e98
+// PR c++/94068 - ICE with template codes in check_narrowing.
865e98
+// { dg-do compile { target c++11 } }
865e98
+
865e98
+enum class A { A1, A2 };
865e98
+A foo ();
865e98
+long foo (int);
865e98
+
865e98
+template <typename>
865e98
+void
865e98
+bar ()
865e98
+{
865e98
+  const auto c{foo ()};
865e98
+}
865e98
diff --git a/gcc/testsuite/g++.dg/cpp0x/conv-tmpl6.C b/gcc/testsuite/g++.dg/cpp0x/conv-tmpl6.C
865e98
new file mode 100644
865e98
index 00000000000..2df3a6cc129
865e98
--- /dev/null
865e98
+++ b/gcc/testsuite/g++.dg/cpp0x/conv-tmpl6.C
865e98
@@ -0,0 +1,16 @@
865e98
+// { dg-do compile { target c++11 } }
865e98
+
865e98
+struct A
865e98
+{
865e98
+  constexpr A(int) { }
865e98
+  constexpr operator int() const { return 1; };
865e98
+};
865e98
+
865e98
+template <class T, int N>
865e98
+struct B
865e98
+{
865e98
+  static constexpr A a = A(N);
865e98
+  int ar[a];
865e98
+};
865e98
+
865e98
+B<int, 10> b;
865e98
diff --git a/gcc/testsuite/g++.dg/cpp1z/conv-tmpl1.C b/gcc/testsuite/g++.dg/cpp1z/conv-tmpl1.C
865e98
new file mode 100644
865e98
index 00000000000..5b1205349d0
865e98
--- /dev/null
865e98
+++ b/gcc/testsuite/g++.dg/cpp1z/conv-tmpl1.C
865e98
@@ -0,0 +1,10 @@
865e98
+// PR c++/91465 - ICE with template codes in check_narrowing.
865e98
+// { dg-do compile { target c++17 } }
865e98
+
865e98
+enum class E { Z };
865e98
+
865e98
+template <typename F>
865e98
+void foo(F)
865e98
+{
865e98
+  E{char(0)};
865e98
+}