Bug 86327 - Spurious error on non-constant expression in constexpr function
Summary: Spurious error on non-constant expression in constexpr function
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: rejects-valid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2018-06-27 01:47 UTC by Barry Revzin
Modified: 2020-04-11 14:52 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-06-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Barry Revzin 2018-06-27 01:47:24 UTC
Reduced example from https://stackoverflow.com/q/51053280/2069064:

int global = 0;

constexpr int f(bool arg) {
    if (arg) {
        return 1;
    }

    return global++;
}

Fails on gcc trunk with:

<source>: In function 'constexpr int f(bool)':
<source>:8:1: error: the value of 'global' is not usable in a constant expression
 }
 ^
<source>:1:5: note: 'int global' is not const
 int global = 0;
     ^~~~~~
Compiler returned: 1

But the function is fine. The near-equivalent formulation of

constexpr int g(bool arg) {
    if (arg) {
        return 1;
    } else {
        return global++;
    }
}

also compiles fine.
Comment 1 Marek Polacek 2020-04-11 14:11:24 UTC
Fixed by r264171, will add the test.
Comment 2 GCC Commits 2020-04-11 14:50:24 UTC
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

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

commit r10-7688-g4ded16a7782fd3a9e7b37291d04bca596b00b8bd
Author: Marek Polacek <polacek@redhat.com>
Date:   Sat Apr 11 10:45:33 2020 -0400

    c++: Add test for PR 86327.
    
    Fixed by r264171.
    
            PR c++/86327
            * g++.dg/cpp1y/constexpr-return5.C: New test.
Comment 3 Marek Polacek 2020-04-11 14:52:38 UTC
Fixed in GCC 9.