public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/111284] [11/12/13/14 Regression] Some passing-by-value parameters are mishandled since GCC 9, affecting libstdc++'s constexpr std::string
Date: Thu, 25 Apr 2024 18:46:09 +0000	[thread overview]
Message-ID: <bug-111284-4-z2RhUdbz3g@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-111284-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111284

--- Comment #10 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:f541757ba4632e204169dd08a5f10c782199af42

commit r14-10134-gf541757ba4632e204169dd08a5f10c782199af42
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Apr 25 20:45:04 2024 +0200

    c++: Fix constexpr evaluation of parameters passed by invisible reference
[PR111284]

    My r9-6136 changes to make a copy of constexpr function bodies before
    genericization modifies it broke the constant evaluation of non-POD
    arguments passed by value.
    In the callers such arguments are passed as reference to usually a
    TARGET_EXPR, but on the callee side until genericization they are just
    direct uses of a PARM_DECL with some class type.
    In cxx_bind_parameters_in_call I've used convert_from_reference to
    pretend it is passed by value and then cxx_eval_constant_expression
    is called there and evaluates that as an rvalue, followed by
    adjust_temp_type if the types don't match exactly (e.g. const Foo
    argument and passing to it reference to Foo TARGET_EXPR).

    The reason this doesn't work is that when the TARGET_EXPR in the caller
    is constant initialized, this for it is the address of the
TARGET_EXPR_SLOT,
    but if the code later on pretends the PARM_DECL is just initialized to the
    rvalue of the constant evaluation of the TARGET_EXPR, it is as if there
    is a bitwise copy of the TARGET_EXPR to the callee, so this in the callee
    is then address of the PARM_DECL in the callee.

    The following patch attempts to fix that by constexpr evaluation of such
    arguments in the caller as an lvalue instead of rvalue, and on the callee
    side when seeing such a PARM_DECL, if we want an lvalue, lookup the value
    (lvalue) saved in ctx->globals (if any), and if wanting an rvalue,
    recursing with vc_prvalue on the looked up value (because it is there
    as an lvalue, nor rvalue).

    adjust_temp_type doesn't work for lvalues of non-scalarish types, for
    such types it relies on changing the type of a CONSTRUCTOR, but on the
    other side we know what we pass to the argument is addressable, so
    the patch on type mismatch takes address of the argument value, casts
    to reference to the desired type and dereferences it.

    2024-04-25  Jakub Jelinek  <jakub@redhat.com>

            PR c++/111284
            * constexpr.cc (cxx_bind_parameters_in_call): For PARM_DECLs with
            TREE_ADDRESSABLE types use vc_glvalue rather than vc_prvalue for
            cxx_eval_constant_expression and if it doesn't have the same
            type as it should, cast the reference type to reference to type
            before convert_from_reference and instead of adjust_temp_type
            take address of the arg, cast to reference to type and then
            convert_from_reference.
            (cxx_eval_constant_expression) <case PARM_DECL>: For lval case
            on parameters with TREE_ADDRESSABLE types lookup result in
            ctx->globals if possible.  Otherwise if lookup in ctx->globals
            was successful for parameter with TREE_ADDRESSABLE type,
            recurse with vc_prvalue on the returned value.

            * g++.dg/cpp1z/constexpr-111284.C: New test.
            * g++.dg/cpp1y/constexpr-lifetime7.C: Expect one error on a
different
            line.

  parent reply	other threads:[~2024-04-25 18:46 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-04 11:16 [Bug c++/111284] New: Some passing-by-value parameters are miscompiled since GCC 9 de34 at live dot cn
2023-09-05 14:37 ` [Bug c++/111284] [11/12/13/14 Regression] Some passing-by-value parameters are mishandled " mpolacek at gcc dot gnu.org
2023-09-05 14:37 ` mpolacek at gcc dot gnu.org
2023-09-05 18:00 ` jakub at gcc dot gnu.org
2023-09-05 18:04 ` jakub at gcc dot gnu.org
2023-09-05 19:21 ` jakub at gcc dot gnu.org
2023-09-05 19:27 ` jakub at gcc dot gnu.org
2023-09-11 11:32 ` jakub at gcc dot gnu.org
2023-09-11 12:12 ` jakub at gcc dot gnu.org
2024-03-07 18:58 ` [Bug c++/111284] [11/12/13/14 Regression] Some passing-by-value parameters are mishandled since GCC 9, affecting libstdc++'s constexpr std::string jakub at gcc dot gnu.org
2024-03-11 15:12 ` ppalka at gcc dot gnu.org
2024-04-25 18:46 ` cvs-commit at gcc dot gnu.org [this message]
2024-04-25 18:48 ` [Bug c++/111284] [11/12/13 " jakub at gcc dot gnu.org
2024-05-09  4:26 ` cvs-commit at gcc dot gnu.org
2024-05-09  8:17 ` [Bug c++/111284] [11/12 " jakub at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-111284-4-z2RhUdbz3g@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).