Skip to content

Misleading error message in constexpr function when variable is uninitialized #51536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Kered13 mannequin opened this issue Oct 16, 2021 · 3 comments
Closed

Misleading error message in constexpr function when variable is uninitialized #51536

Kered13 mannequin opened this issue Oct 16, 2021 · 3 comments
Labels
bugzilla Issues migrated from bugzilla c++11 clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Comments

@Kered13
Copy link
Mannequin

Kered13 mannequin commented Oct 16, 2021

Bugzilla Link 52194
Version trunk
OS Linux
CC @Kered13,@zygoloid

Extended Description

Minimal example:

constexpr int func(int a)
{
	int sum;
	sum += a;
	return sum;
}
constexpr int val = func(4);

When I compile this on Godbolt using the "x86-64 clang (trunk)" branch and the -std=c++20 flag I get the following error message:

<source>:7:15: error: constexpr variable 'val' must be initialized by a constant expression
constexpr int val = func(4);
              ^     ~~~~~~~
<source>:4:6: note: subexpression not valid in a constant expression
        sum += a;
            ^
<source>:7:21: note: in call to 'func(4)'
constexpr int val = func(4);
                    ^
1 error generated.

This is misleading because the actual problem is that sum was not initialized. If sum is initialized with sum=0 then there is no error. This problem exists for other modify and assign operators as well. Using separate assignment and equality operators does not have this problem. For example:

constexpr int func(int a)
{
	int sum;
	sum = sum + a;
	return sum;
}
constexpr int val = func(4);

Give this error message:

<source>:7:15: error: constexpr variable 'val' must be initialized by a constant expression
constexpr int val = func(4);
              ^     ~~~~~~~
<source>:4:8: note: read of uninitialized object is not allowed in a constant expression
        sum = sum + a;
              ^
<source>:7:21: note: in call to 'func(4)'
constexpr int val = func(4);
                    ^
1 error generated.

Which correctly identifies that the problem is that sum is uninitialized.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@ilya-biryukov ilya-biryukov added c++23 and removed c++20 labels Aug 3, 2022
@llvmbot
Copy link
Member

llvmbot commented Aug 3, 2022

@llvm/issue-subscribers-c-2b

@AaronBallman AaronBallman added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer c++11 and removed c++23 labels Aug 3, 2022
@llvmbot
Copy link
Member

llvmbot commented Aug 3, 2022

@llvm/issue-subscribers-c-11

@AaronBallman
Copy link
Collaborator

I think this actually goes back farther than C++20. Even in C++11 the diagnostic doesn't clearly tell you that sum is the issue. You will get warnings about extensions allowing uninitialized variables in a constexpr function, but those extension warnings are not uncommon to disable for code bases not intending to port to newer language standards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++11 clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
Projects
None yet
Development

No branches or pull requests

3 participants