public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "miro.palmu at helsinki dot fi" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/112642] ranges::fold_left tries to access inactive union member of string in constant expression
Date: Tue, 21 Nov 2023 17:48:12 +0000	[thread overview]
Message-ID: <bug-112642-4-zN2L7W3tXI@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-112642-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #5 from Miro Palmu <miro.palmu at helsinki dot fi> ---
I have been trying to figure out where exactly the bug is and these are my
findings.
> Or:
>
> #include <string>
>
> consteval void bar() {
>     auto _ = [](std::string s) { return s; }({});
> }
>
> int main() {
>    bar();    
> }
>
> Or:
> 
> #include <string>
> constexpr auto foo(std::string init) { return init; }
> constexpr auto bar() { return foo("").size(); }
> constexpr auto i = bar();
>
>Clang compiles both of these without problems (it can't compile anything using ""s in a constant expression, maybe due to https://github.com/llvm/llvm-project/issues/68527)
>
> So I am pretty sure this is a g++ front end bug.

If you use libstdc++ on clang these will not compile but with different errors.

Then with following example I try to showcase the bug without std::string.
Try it out: https://godbolt.org/z/rvoeMEaxc

This is bare minimum of  libstdc++ basic_string to reproduce this bug:

---

struct S {
    union {
        char a[1];
    };
    char* ptr; 
    constexpr S() : ptr{a} {
        a[0] = {};
    }
    constexpr S(S&&) = delete;
    constexpr S(const S&) = delete;
    constexpr S operator=(S&&) = delete;
    constexpr S operator=(const S&) = delete;
    constexpr ~S() = default;
}

---

Then to reproduce the bug instance of this class has to be function parameter
and the function has to be constant evaluated.
This can happens in std::basic_string move constructor bits/basic_string.h:682
and following tester functions tries to emulate what happens in it.

---

// Should always be false
constexpr bool test(const S& s){
    return s.ptr != s.a;
}
consteval void tester1(S param = {}) { 
    S notparam = {};
    if (test(notparam)){
        throw std::logic_error("compiletime notparam!");
    }

    if (test(param)) {
        // gcc ends up here so fails to compile
        // in std::basic_string move constructor
        // compilation would fail due to accessing
        // inactive union member
        // clang and msvc never end up here

        throw std::logic_error("compiletime param");
    }
}

int main() { tester(); )

---

Notice that here only the parameter version fails.
In non-constant evaluated context (see godbolt link)
all of the test evaluate false as they should.

  parent reply	other threads:[~2023-11-21 17:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-20 15:28 [Bug libstdc++/112642] New: " miro.palmu at helsinki dot fi
2023-11-20 15:35 ` [Bug libstdc++/112642] " redi at gcc dot gnu.org
2023-11-20 16:02 ` redi at gcc dot gnu.org
2023-11-20 16:50 ` miro.palmu at helsinki dot fi
2023-11-20 17:10 ` [Bug c++/112642] " redi at gcc dot gnu.org
2023-11-21 17:48 ` miro.palmu at helsinki dot fi [this message]
2023-11-21 20:14 ` redi at gcc dot gnu.org
2023-11-22 11:22 ` miro.palmu at helsinki dot fi
2023-11-22 12:04 ` redi at gcc dot gnu.org
2023-11-22 12:31 ` miro.palmu at helsinki dot fi
2023-11-22 17:19 ` redi 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-112642-4-zN2L7W3tXI@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).