Bug 79520 - Spurious caching for constexpr arguments
Summary: Spurious caching for constexpr arguments
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0.1
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2017-02-14 23:05 UTC by Robert Haberlach
Modified: 2019-08-08 17:55 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-02-17 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Robert Haberlach 2017-02-14 23:05:57 UTC
constexpr int f(int const& x) {return x;}

constexpr struct S {
    int x = 0;
    constexpr S() {(void)f(x); x = 1;}
} s;

static_assert(f(s.x) == 1, "");

---

Presumably s.x is taken to be "immutable" because s is constexpr, while that's obviously not true during the period of construction.
Comment 1 Martin Sebor 2017-02-17 00:39:10 UTC
Confirmed with today's top of trunk:

$ cat t.C && gcc -S -Wall -Wextra -Wpedantic t.C
constexpr int f(int const& x) {return x;}

constexpr struct S {
    int x = 0;
    constexpr S() {(void)f(x); x = 1;}
} s;

static_assert(f(s.x) == 1, "");
t.C:8:1: error: static assertion failed
 static_assert(f(s.x) == 1, "");
 ^~~~~~~~~~~~~
Comment 2 Marek Polacek 2019-08-08 17:50:31 UTC
Fixed by r255788.  Probably it's worth to add the new test.
Comment 3 Marek Polacek 2019-08-08 17:55:29 UTC
Author: mpolacek
Date: Thu Aug  8 17:54:58 2019
New Revision: 274214

URL: https://gcc.gnu.org/viewcvs?rev=274214&root=gcc&view=rev
Log:
	PR c++/79520
	* g++.dg/cpp1y/constexpr-79520.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-79520.C
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 4 Marek Polacek 2019-08-08 17:55:45 UTC
Done.