c++: can't eval PTRMEM_CST in incomplete class [PR107574]

Message ID 20230203002825.398939-1-polacek@redhat.com
State New
Headers
Series c++: can't eval PTRMEM_CST in incomplete class [PR107574] |

Commit Message

Marek Polacek Feb. 3, 2023, 12:28 a.m. UTC
  Here we're attempting to evaluate a PTRMEM_CST in a class that hasn't
been completed yet, but that doesn't work:

        /* We can't lower this until the class is complete.  */
        if (!COMPLETE_TYPE_P (DECL_CONTEXT (member)))
          return cst;

and then this unlowered PTRMEM_CST is used as EXPR in

    tree op1 = build_nop (ptrdiff_type_node, expr);

and we crash in a subsequent cp_fold_convert which gets type=ptrdiff_type_node,
expr=PTRMEM_CST and does

  else if (TREE_CODE (expr) == PTRMEM_CST
           && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
                           PTRMEM_CST_CLASS (expr)))

where TYPE_PTRMEM_CLASS_TYPE (type) is going to crash since the type
is ptrdiff_type_node.  We could just add a TYPE_PTRMEM_P check before
accessing TYPE_PTRMEM_CLASS_TYPE but I think it's nicer to explain why
we couldn't evaluate the expression.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

	PR c++/107574

gcc/cp/ChangeLog:

	* constexpr.cc (cxx_eval_constant_expression): Emit an error when
	a PTRMEM_CST cannot be evaluated.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/ptrmem-cst1.C: New test.
---
 gcc/cp/constexpr.cc                      |  9 +++++++++
 gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C | 11 +++++++++++
 2 files changed, 20 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C


base-commit: 07c87fce63541846ca2951e22dac04fcaa66475f
  

Comments

Marek Polacek March 1, 2023, 8:34 p.m. UTC | #1
Ping.

On Thu, Feb 02, 2023 at 07:28:25PM -0500, Marek Polacek via Gcc-patches wrote:
> Here we're attempting to evaluate a PTRMEM_CST in a class that hasn't
> been completed yet, but that doesn't work:
> 
>         /* We can't lower this until the class is complete.  */
>         if (!COMPLETE_TYPE_P (DECL_CONTEXT (member)))
>           return cst;
> 
> and then this unlowered PTRMEM_CST is used as EXPR in
> 
>     tree op1 = build_nop (ptrdiff_type_node, expr);
> 
> and we crash in a subsequent cp_fold_convert which gets type=ptrdiff_type_node,
> expr=PTRMEM_CST and does
> 
>   else if (TREE_CODE (expr) == PTRMEM_CST
>            && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
>                            PTRMEM_CST_CLASS (expr)))
> 
> where TYPE_PTRMEM_CLASS_TYPE (type) is going to crash since the type
> is ptrdiff_type_node.  We could just add a TYPE_PTRMEM_P check before
> accessing TYPE_PTRMEM_CLASS_TYPE but I think it's nicer to explain why
> we couldn't evaluate the expression.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> 
> 	PR c++/107574
> 
> gcc/cp/ChangeLog:
> 
> 	* constexpr.cc (cxx_eval_constant_expression): Emit an error when
> 	a PTRMEM_CST cannot be evaluated.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/ptrmem-cst1.C: New test.
> ---
>  gcc/cp/constexpr.cc                      |  9 +++++++++
>  gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C | 11 +++++++++++
>  2 files changed, 20 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C
> 
> diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
> index 5b31f9c27d1..2c03988b097 100644
> --- a/gcc/cp/constexpr.cc
> +++ b/gcc/cp/constexpr.cc
> @@ -7691,6 +7691,15 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
>  	    if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
>  		&& !can_convert_qual (type, op))
>  	      op = cplus_expand_constant (op);
> +	    if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
> +	      {
> +		if (!ctx->quiet)
> +		  error_at (loc, "%qE is not a constant expression when the "
> +			    "class %qT is still incomplete", op,
> +			    PTRMEM_CST_CLASS (op));
> +		*non_constant_p = true;
> +		return t;
> +	      }
>  	    return cp_fold_convert (type, op);
>  	  }
>  
> diff --git a/gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C b/gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C
> new file mode 100644
> index 00000000000..0d6a6b6445d
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C
> @@ -0,0 +1,11 @@
> +// PR c++/107574
> +// { dg-do compile { target c++11 } }
> +
> +struct A { int i; };
> +struct B:A { int j; };
> +struct C:B {
> +  int k;
> +  static_assert((int B::*) &C::k, ""); // { dg-error "non-constant|still incomplete" }
> +};
> +
> +static_assert((int B::*) &C::k, "");
> 
> base-commit: 07c87fce63541846ca2951e22dac04fcaa66475f
> -- 
> 2.39.1
> 

Marek
  
Jason Merrill March 1, 2023, 9:24 p.m. UTC | #2
On 2/2/23 19:28, Marek Polacek wrote:
> Here we're attempting to evaluate a PTRMEM_CST in a class that hasn't
> been completed yet, but that doesn't work:
> 
>          /* We can't lower this until the class is complete.  */
>          if (!COMPLETE_TYPE_P (DECL_CONTEXT (member)))
>            return cst;
> 
> and then this unlowered PTRMEM_CST is used as EXPR in
> 
>      tree op1 = build_nop (ptrdiff_type_node, expr);
> 
> and we crash in a subsequent cp_fold_convert which gets type=ptrdiff_type_node,
> expr=PTRMEM_CST and does
> 
>    else if (TREE_CODE (expr) == PTRMEM_CST
>             && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
>                             PTRMEM_CST_CLASS (expr)))
> 
> where TYPE_PTRMEM_CLASS_TYPE (type) is going to crash since the type
> is ptrdiff_type_node.  We could just add a TYPE_PTRMEM_P check before
> accessing TYPE_PTRMEM_CLASS_TYPE but I think it's nicer to explain why
> we couldn't evaluate the expression.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> 
> 	PR c++/107574
> 
> gcc/cp/ChangeLog:
> 
> 	* constexpr.cc (cxx_eval_constant_expression): Emit an error when
> 	a PTRMEM_CST cannot be evaluated.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/ptrmem-cst1.C: New test.
> ---
>   gcc/cp/constexpr.cc                      |  9 +++++++++
>   gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C | 11 +++++++++++
>   2 files changed, 20 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C
> 
> diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
> index 5b31f9c27d1..2c03988b097 100644
> --- a/gcc/cp/constexpr.cc
> +++ b/gcc/cp/constexpr.cc
> @@ -7691,6 +7691,15 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
>   	    if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
>   		&& !can_convert_qual (type, op))
>   	      op = cplus_expand_constant (op);
> +	    if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
> +	      {
> +		if (!ctx->quiet)
> +		  error_at (loc, "%qE is not a constant expression when the "
> +			    "class %qT is still incomplete", op,
> +			    PTRMEM_CST_CLASS (op));
> +		*non_constant_p = true;
> +		return t;
> +	      }

Hmm, maybe handle this a few lines higher, in this existing if:

>         if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
>           op = cplus_expand_constant (op);

?  OK with that change.
  

Patch

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 5b31f9c27d1..2c03988b097 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -7691,6 +7691,15 @@  cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
 	    if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
 		&& !can_convert_qual (type, op))
 	      op = cplus_expand_constant (op);
+	    if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
+	      {
+		if (!ctx->quiet)
+		  error_at (loc, "%qE is not a constant expression when the "
+			    "class %qT is still incomplete", op,
+			    PTRMEM_CST_CLASS (op));
+		*non_constant_p = true;
+		return t;
+	      }
 	    return cp_fold_convert (type, op);
 	  }
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C b/gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C
new file mode 100644
index 00000000000..0d6a6b6445d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C
@@ -0,0 +1,11 @@ 
+// PR c++/107574
+// { dg-do compile { target c++11 } }
+
+struct A { int i; };
+struct B:A { int j; };
+struct C:B {
+  int k;
+  static_assert((int B::*) &C::k, ""); // { dg-error "non-constant|still incomplete" }
+};
+
+static_assert((int B::*) &C::k, "");