Internal compiler error (CL version: 14.27.29110) - c++

There is code
#include <array>
struct Foo {
int bar;
};
int main() {
constexpr auto v = std::array{Foo{}};
return 0;
}
While compile whith C++17:
fatal error C1001: Internal compiler error. ... (compiler file 'msc1.cpp', line 1591)
INTERNAL COMPILER ERROR in 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.27.29110\bin\HostX86\x86\CL.exe'
But this - compiles
#include <array>
struct Foo {
int bar;
};
int main() {
constexpr std::array<Foo, 1> v{Foo{}};
return 0;
}
All optimizations are disabled as described here
Is this a compiler bug?

This looks like the same bug reported on developercommunity.visualstudio.com under Regression: c++ internal compiler error in 16.7.0 with /std:c++17 (compiler file 'msc1.cpp', line 1591). I suggest you upvote that bug report, and keep an eye on it for possible resolutions.
The issue was introduced in the 16.7.0 update, and is not fixed as of the latest 16.7.5. Interim workarounds are to revert to 16.6.5, or drop /std:c++17 and use /std:default or /std:c++14 instead.

Related

What is the Visual Studio warning equivalent of GCC's -Wreturn-type?

Does Visual Studio have a warning (or warnings) that is the equivalent of GCC's -Wreturn-type?
More specifically, I am looking for a Visual Studio warning (or warnings) that will warn for instances in functions whose return types are not void where
There is a return statement with no return value; or
It is possible function execution could "fall off" the end of the function body without returning a value
I am not concerned with the other part of -Wreturn-type that warns whenever a function is defined with a return type that defaults to int.
For reference, the GCC warning options can be found here.
As pointed out in the comments, this can be done with C4033, C4716, C4715.
User n. 1.8e9-where's-my-share m. also makes a very good point about how to find MSVC warnings in general:
If you want to find out whether a warning that you want exists, just enable all [using /Wall] and test against a small piece of code. If there is a warning, congrats, you found it. If not, tough luck, there isn't any.
I test both with .c and .cpp file extensions, just in case the compiler behaved differently based on the language it is compiling (and sure enough, the behaviour was different for test 2).
None of my tests ever complain about main(), because main() is special, as it is the only function in C and C++ that defaults to returning 0 if no explicit return is provided.
All of the tests below were done using Visual Studio 2015's compiler (i.e., C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\cl.exe), and the commands were issued from the VS2015 x86 Native Tools Command Prompt.
If I am missing any test cases, please leave a comment to let me know.
Tests
C tests
Test 1 - Empty function with int return type
test_warnings.c:
int main() {}
int foo() {}
Compile results:
>cl /nologo /W0 /we4033 /we4716 /we4715 C:\Users\Administrator\src\test-code\test_warnings.c
test_warnings.c
c:\users\administrator\src\test-code\test_warnings.c(3) : error C4716: 'foo': must return a value
Test 2 - Function with int return type with a return without a value
test_warnings.c:
int main() {}
int foo() {
return;
}
Compile results:
>cl /nologo /W0 /we4033 /we4716 /we4715 C:\Users\Administrator\src\test-code\test_warnings.c
test_warnings.c
C:\Users\Administrator\src\test-code\test_warnings.c(4): error C4033: 'foo' must return a value
Test 3 - Function with int return type where execution could "fall off" the end of the function
This test demonstrates that these warnings are not enough, as there is no warning or error emitted for this code.
test_warnings.c:
#include <stdlib.h>
#include <time.h>
int main() {}
int foo() {
int rand_num;
srand(time(0));
rand_num = rand();
if (rand_num > 1) {
return 0;
}
}
Compile results:
>cl /nologo /W0 /we4033 /we4716 /we4715 C:\Users\Administrator\src\test-code\test_warnings.c
test_warnings.c
c:\users\administrator\src\test-code\test_warnings.c(14) : error C4715: 'foo': not all control paths return a value
C++ tests
Test 1 - Empty function with int return type
test_warnings.cpp:
int main() {}
int foo() {}
Compile results:
>cl /nologo /W0 /we4033 /we4716 /we4715 C:\Users\Administrator\src\test-code\test_warnings.cpp
test_warnings.cpp
c:\users\administrator\src\test-code\test_warnings.cpp(3) : error C4716: 'foo': must return a value
Test 2 - Function with int return type with a return without a value
test_warnings.cpp:
int main() {}
int foo() {
return;
}
Compile results:
>cl /nologo /W0 /we4033 /we4716 /we4715 C:\Users\Administrator\src\test-code\test_warnings.cpp
test_warnings.cpp
C:\Users\Administrator\src\test-code\test_warnings.cpp(4): error C2561: 'foo': function must return a value
C:\Users\Administrator\src\test-code\test_warnings.cpp(3): note: see declaration of 'foo'
Test 3 - Function with int return type where execution could "fall off" the end of the function
test_warnings.cpp:
#include <stdlib.h>
#include <time.h>
int main() {}
int foo() {
int rand_num;
srand(time(0));
rand_num = rand();
if (rand_num > 1) {
return 0;
}
}
Compile results:
>cl /nologo /W0 /we4033 /we4716 /we4715 C:\Users\Administrator\src\test-code\test_warnings.cpp
test_warnings.cpp
c:\users\administrator\src\test-code\test_warnings.cpp(14) : error C4715: 'foo': not all control paths return a value
Can you get this with just C4715?
I reran my tests above to see if you can get the same behaviour with just C4715, and here are my results. The command I used for testing this was
cl /nologo /W0 /we4715 <path to file>
Test
C
C++
Test 1
No warning or error
Triggers C4716 as an error, even though this is not turned on (which makes sense, as the docs for this warning say it is automatically promoted to error unless #pragma warning is used to prevent this)
Test 2
No warning or error
Triggers C2561 (a compiler error)
Test 3
Triggers C4715
Triggers C4715
This means C4715 is sufficient for C++, but not sufficient for C.
Notes
C4715 may warn if you call a function that never returns. For example, if you call a function that ends with while (true) {} or throw "error message";. To avoid this, declare the function that never returns with __declspec(noreturn), or if you are using C++11 or newer, you can use the more portable [[noreturn]] in the function declaration. (If you are calling a standard library function like exit(), the compiler will not issue a warning because it will know that function never returns.)
For some interesting related discussion, see Why does flowing off the end of a non-void function without returning a value not produce a compiler error?.

C++ : No warning for unused local constants with /W4 (MSVC)

The following code was compiled with MSVC 19.29.30133.0
Given the following c++ line in a function
int foo = 0;
If the variable isn't referenced, MSCV will give you the following warning: warning C4189: 'foo': local variable is initialized but not referenced
However, if const is added in front of it, the warning disappears.
const int foo = 0; // <<--This line yields no warning with MSVC 19.29.30133.0
But this unused variable should still be removed, isn't it?
So why is it that MSVC gives no warning? Is it a bug, is there a specification for unused constants?
minimal reproduction:
int main() {
int a = 0;
const int b = 0;
return 0;
}
output:
Starting build...
cl.exe /Zi /EHsc /nologo /W4 /std:c++20 /Fe: F:\prog\INF1005C\tst\unused_constant.exe F:\prog\INF1005C\tst\unused_constant.cpp
unused_constant.cpp
F:\prog\INF1005C\tst\unused_constant.cpp(2): warning C4189: 'a': local variable is initialized but not referenced
Build finished with warning(s).

VS2015 rejects in-class initialization in unnamed type

The following code fails to compile in VS2015.
struct Foo
{
Foo(int value) { }
};
struct Moo
{
struct
{
Foo foo = 0;
} fooHolder;
};
int main()
{
Moo moo;
}
The following error is shown.
1>c:\xxx\main.cpp(81): error C2512: 'Foo' : no appropriate default constructor available
1> This diagnostic occurred in the compiler generated function 'Moo::<unnamed-type-fooHolder>::(void) restrict(cpu, amp)'
If the unnamed struct is given a name, the code compiles.
struct NamedHolder
{
Foo foo = 0;
} fooHolder;
The code compiled in clang and gcc. http://coliru.stacked-crooked.com/a/3b4ab035a967eed9
Is it rejecting valid code?
This code is perfectly fine and it compiles with VS2015 Update 1 RC (just verified). Maybe you're missing something . The system on which I tested :
Microsoft Visual Studio Community 2015
Version 14.0.24627.00 Update 1 RC
Microsoft .NET Framework
Version 4.6.01040
Installed Version: Community
Visual C++ 2015 RC 00322-20000-00000-AA392
Microsoft Visual C++ 2015 RC
...

VS C++ 2012(13) can not convert overloaded-function to std::function

I have such code:
#include <functional>
struct Foo {
template<typename T>
static void f(int);
};
template<>
void Foo::f<int>(int) {}
int main()
{
std::function<void(int)> func;
//func = static_cast<void (*)(int)>(Foo::f<int>);/works
func = Foo::f<int>;//compilation failure
return 0;
}
VS 2012 and 2013 Preview give compile time error at line:
func = Foo::f<int>;//compilation failure
error C3867: Foo::f: in function call no argument list, use "&Foo::f" to create pointer to member
error C2440: =: can not convert "overloaded-function" into "std::function"
But gcc 4.8.1, clang 3.3 and Intel Compiler 13.1.3 compile
this code with options (-Wall -pedantic -std=c++11) without
any warnings and errors.
So is this compiler bug of C++ compiler of Visual Studio?
Yes, it is a bug, visual studio 2013 rc compiles this without a problem so it looks like they have fixed it.

Compiler Bug in Visual C++ 10.0 SP1 - cl.exe version 16.0.40219.1 Access violation [confirmed]

I have ran into a problem compiling some template code with Visual Stuido 2010 SP1, cl.exe version 16.0.40219.1
The following code will cause the compiler to access violate:
template<typename T>
class A
{
A(){}
};
template<typename T>
class B : public A<T>
{
using A::A(); // Compiler access violates
// **EDIT**
//using A<T>::A<T>; // Compiler succeeds
//using A<T>::A(); // Compiler reports error
};
int main(int argc, char* argv[])
{
return 0;
}
It generates the following error (in addition to the "cl.exe has stopped working, C0000005 exception):
1>d:\projects\cpptest\cpptest\cpptest.cpp(11): fatal error C1001: An internal error has occurred in the compiler.
1> (compiler file 'msc1.cpp', line 1420)
1> To work around this problem, try simplifying or changing the program near the locations listed above.
The code compiles fine (well, that is, it emits a proper error message and doesn't crash the compiler) in Dev-C++ with g++.
main.cpp:11: error: `template<class T> class A' used without template parameters
main.cpp:11: error: expected nested-name-specifier before "A"
main.cpp:11: error: using-declaration for non-member at class scope
main.cpp:11: error: expected `;' before '(' token
main.cpp:11: error: expected unqualified-id before ')' token
make.exe: *** [main.o] Error 1
EDIT
The following, however, compiles fine, without access violation, so it seems this is related to templates:
class A
{
A(){}
};
class B : public A
{
using A::A;
};
int main(int argc, char* argv[])
{
return 0;
}
Do you think this is worth reporting to Microsoft? Can anyone else reproduce this?
Maybe try in Visual Studio 2013 to see if it still occurs?
Since this is reproducible by others on Visual C++ platforms, I have opened a bug report on Microsoft Connect as "answer".
Also, as workaround, the following syntax works:
using A<T>::A<T>;
Update 2013-12-06: Microsoft has confirmed the issue and the issue will be fixed in the Visual Studio 2013 C++ compiler.