On Fri, 30 Jan 2026, Jakub Jelinek wrote:

> On Thu, Jan 29, 2026 at 06:59:34PM +0800, Jason Merrill wrote:
> > I guess since NULLPTR_TYPE is in tree.def it makes sense for
> > fold_convert_loc to handle it.
> 
> Here is a fold-const.cc version of that.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.  I'll note that POINTER_TYPE_P does not cover NULLPTR_TYPE, so
there might be interesting effects if such constants leak throughout
the ME.  But I do see few cases where it's already handled.

Thanks,
Richard.

> 2026-01-30  Jakub Jelinek  <[email protected]>
> 
>       PR c++/123790
>       * fold-const.cc (fold_convert_const): Handle conversion of
>       integer_zerop to NULLPTR_TYPE.
>       (fold_convert_loc): Likewise.
> 
>       * g++.dg/cpp0x/nullptr47.C: New test.
> 
> --- gcc/fold-const.cc.jj      2026-01-14 13:21:06.136656098 +0100
> +++ gcc/fold-const.cc 2026-01-29 12:48:18.263320685 +0100
> @@ -2566,6 +2566,8 @@ fold_convert_const (enum tree_code code,
>         return v.build ();
>       }
>      }
> +  else if (TREE_CODE (type) == NULLPTR_TYPE && integer_zerop (arg1))
> +    return build_zero_cst (type);
>    return NULL_TREE;
>  }
>  
> @@ -2788,6 +2790,10 @@ fold_convert_loc (location_t loc, tree t
>        tem = fold_ignored_result (arg);
>        return fold_build1_loc (loc, NOP_EXPR, type, tem);
>  
> +    case NULLPTR_TYPE:
> +      if (integer_zerop (arg))
> +     return build_zero_cst (type);
> +      /* FALLTHRU */
>      default:
>        if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
>       return fold_build1_loc (loc, NOP_EXPR, type, arg);
> --- gcc/testsuite/g++.dg/cpp0x/nullptr47.C.jj 2026-01-29 12:43:59.155035248 
> +0100
> +++ gcc/testsuite/g++.dg/cpp0x/nullptr47.C    2026-01-29 12:43:59.155035248 
> +0100
> @@ -0,0 +1,10 @@
> +// PR c++/123790
> +// { dg-do compile { target c++11 } }
> +
> +int
> +main ()
> +{
> +  using nullptr_t = decltype (nullptr);
> +  constexpr nullptr_t zero = nullptr;
> +  constexpr nullptr_t other_zero = zero;
> +}
> 
> 
>       Jakub
> 
> 

-- 
Richard Biener <[email protected]>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)

Reply via email to