On Thu, Mar 26, 2026 at 05:34:47PM +0000, Iain Sandoe wrote:
> This does not fix any current BZ (or anything latent AFAIK) it is
> just a cleanup that is in my stack.  Tested on x86_64-darwin,
> x86_64, powerpc64le-linux,  OK for trunk/when ?
> thanks
> Iain
> 
> --- 8< ---
> 
> We need to view class pointers as const, which we do by wrapping
> them in a view convert expression.  However, we do not need to do
> this if the original is already const-qualified.
> 
> gcc/cp/ChangeLog:
> 
>       * parser.cc (cp_parser_late_contract_condition): Check
>       for const-qualified class pointer before wrapping it.
>       (cp_parser_contract_assert): Likewise.
>       (cp_parser_function_contract_specifier): Likewise.
> 
> Signed-off-by: Iain Sandoe <[email protected]>
> ---
>  gcc/cp/parser.cc | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> index e5157cd7386..a159e9b8520 100644
> --- a/gcc/cp/parser.cc
> +++ b/gcc/cp/parser.cc
> @@ -33574,8 +33574,8 @@ cp_parser_late_contract_condition (cp_parser *parser, 
> tree fn, tree contract)
>    /* If we have a current class object, we need to consider
>       it const when processing the contract condition.  */
>    tree current_class_ref_copy = current_class_ref;
> -  if (flag_contracts && current_class_ref_copy)
> -    current_class_ref = view_as_const (current_class_ref_copy);
> +  if (current_class_ref && !CP_TYPE_CONST_P (TREE_TYPE (current_class_ref)))
> +    current_class_ref = view_as_const (current_class_ref);

This change makes current_class_ref_copy unused.  Looks like this function
is missing the

  /* Revert (any) constification of the current class object.  */
  current_class_ref = current_class_ref_copy;

but there's

  current_class_ref = saved_ccr;

below so I guess current_class_ref_copy can be removed.


Can we move the !CP_TYPE_CONST_P check into view_as_const instead?

>    /* Parse the condition, ensuring that parameters or the return variable
>       aren't flagged for use outside the body of a function.  */
> @@ -33682,8 +33682,8 @@ cp_parser_contract_assert (cp_parser *parser, 
> cp_token *token)
>    /* If we have a current class object, see if we need to consider
>       it const when processing the contract condition.  */
>    tree current_class_ref_copy = current_class_ref;
> -  if (current_class_ref_copy)
> -    current_class_ref = view_as_const (current_class_ref_copy);
> +  if (current_class_ref && !CP_TYPE_CONST_P (TREE_TYPE (current_class_ref)))
> +    current_class_ref = view_as_const (current_class_ref);
>  
>    /* Parse the condition.  */
>    begin_scope (sk_contract, current_function_decl);
> @@ -33848,8 +33848,8 @@ cp_parser_function_contract_specifier (cp_parser 
> *parser)
>        /* If we have a current class object, see if we need to consider
>         it const when processing the contract condition.  */
>        tree current_class_ref_copy = current_class_ref;
> -      if (current_class_ref_copy)
> -      current_class_ref = view_as_const (current_class_ref_copy);
> +      if (current_class_ref && !CP_TYPE_CONST_P (TREE_TYPE 
> (current_class_ref)))
> +     current_class_ref = view_as_const (current_class_ref);
>  
>        /* Parse the condition, ensuring that parameters or the return variable
>         aren't flagged for use outside the body of a function.  */
> -- 
> 2.50.1 (Apple Git-155)
> 

Marek

Reply via email to