Bug 96223 - DR 1787 and indeterminate values in constexpr context
Summary: DR 1787 and indeterminate values in constexpr context
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-07-16 19:30 UTC by Marek Polacek
Modified: 2020-09-17 20:19 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-08-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marek Polacek 2020-07-16 19:30:59 UTC
Use -std=c++20:

// DR 1787

#include <cstddef>

constexpr int
fn1 ()
{
  unsigned char foo;
  unsigned char u = foo; // OK: u has an indeterminate value
  return u; // UB: copy-init -> standard conversion to int
}

constexpr int
fn2 ()
{
  unsigned char foo;
  int i = foo; // UB
  return 0;
}

constexpr int
fn3 ()
{
  unsigned char foo;
  char8_t u = foo; // UB: char8_t not an unsigned ordinary character type
  return 0;
}

constexpr int
fn4 ()
{
  std::byte foo;
  std::byte b = foo; // OK
  return 0;
}

constexpr int w1 = fn1 ();
constexpr int w2 = fn2 ();
constexpr int w3 = fn3 ();
constexpr int w4 = fn4 ();

DR 1787 says that if an indeterminate value is produced by an evaluation, the behavior is undefined except in certain cases.  In fn1 and fn4 we issue errors even for some of the "certain cases" (the lines marked with OK) and I think it's a bug.  Uninitialized variables in a constexpr context are OK since C++20 P1331R2.
Comment 1 Marek Polacek 2020-07-16 19:32:38 UTC
Note that is_byte_access_type won't do, because it includes char8_t too.
Comment 2 Richard Smith 2020-08-06 17:54:35 UTC
P1331R2 explicitly disallows in a constant evaluation:

"- an lvalue-to-rvalue conversion that is applied to an object with indeterminate value ([basic.indet]);"

so GCC is correct to reject such cases.
Comment 3 Marek Polacek 2020-08-06 18:00:14 UTC
Aaah, I see.  Thanks a lot!

I'll add the test at least.
Comment 4 CVS Commits 2020-09-17 20:15:00 UTC
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:4f0aa5b051c0d3e81478bcb495e4e072b2d9827d

commit r11-3268-g4f0aa5b051c0d3e81478bcb495e4e072b2d9827d
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Sep 17 15:31:50 2020 -0400

    c++: Add tests for fixed PRs.
    
    Bugzilla inspection turned up a bunch of old(er) PRs that have been
    fixed.  Let's include them not to regress in the future.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/87530
            PR c++/58156
            PR c++/68828
            PR c++/86002
            PR c++/91525
            PR c++/96223
            PR c++/87032
            PR c++/35098
            * g++.dg/cpp0x/move-return4.C: New test.
            * g++.dg/cpp0x/vt-58156.C: New test.
            * g++.dg/cpp2a/concepts-pr68828.C: New test.
            * g++.dg/cpp2a/concepts-pr86002.C: New test.
            * g++.dg/cpp2a/concepts-pr91525.C: New test.
            * g++.dg/cpp2a/constexpr-indeterminate1.C: New test.
            * g++.dg/cpp2a/desig17.C: New test.
            * g++.dg/ext/attrib62.C: New test.
Comment 5 Marek Polacek 2020-09-17 20:19:09 UTC
Test added.