Skip to content

<chrono>: Spurious warning C4702: unreachable code #15

@StephanTLavavej

Description

@StephanTLavavej

Basic Info

  • I have read the rules for the Standard Library Modules Bug Bash.

  • I have pulled and rebuilt the bug-bash branch in the last 24 hours.

  • My current commit, as printed by git rev-parse HEAD, is: 6c96fe7

  • This bug is: warning-on-valid

Command-Line Test Case

My test case that works with classic includes but fails with named modules:

D:\GitHub\STL>type repro.cpp
#ifdef USE_CLASSIC_INCLUDES
#include <chrono>
#include <format>
#include <iostream>
#else
import std;
#endif

int main() {
    std::cout << std::format("{}\n", std::chrono::seconds{1});
}
D:\GitHub\STL>cl /EHsc /nologo /W4 /std:c++latest /MTd /Od repro.cpp /DUSE_CLASSIC_INCLUDES && repro.exe
repro.cpp
1s

D:\GitHub\STL>cl /EHsc /nologo /W4 /std:c++latest /MTd /Od repro.cpp std.obj && repro.exe
repro.cpp
D:\GitHub\STL\out\x64\out\inc\chrono(5028) : warning C4702: unreachable code
D:\GitHub\STL\out\x64\out\inc\chrono(5603) : warning C4702: unreachable code
D:\GitHub\STL\out\x64\out\inc\chrono(5704) : warning C4702: unreachable code
D:\GitHub\STL\out\x64\out\inc\chrono(5471) : warning C4702: unreachable code
1s

The STL internally suppresses this warning, but Standard Library Modules isn't handling that suppression.

Additional Context

This test case is from @nico-engels in #14 ; after working around the compiler error reported there, I found this warning.

I've fully reduced this and reported it as internal VSO-1582381 "Standard Library Modules: Bogus warning C4702: unreachable code". Minimal repro:

C:\Temp>type library.ixx
export module library;

#pragma warning(push)
#pragma warning(disable : 4702)

namespace library {
    export template <typename T>
    int func(T t) {
        if constexpr (sizeof(T) == 1) {
            return t * 11;
        }
        return t * 111;
    }
} // namespace library

#pragma warning(pop)
C:\Temp>type meow.cpp
#include <stdio.h>

import library;

int main() {
    printf("%d\n", library::func(static_cast<unsigned char>(3)));
    printf("%d\n", library::func(4));
}
C:\Temp>cl /EHsc /nologo /W4 /std:c++latest /Femeow.exe library.ixx meow.cpp && meow
library.ixx
meow.cpp
Generating Code...
C:\Temp\library.ixx(12) : warning C4702: unreachable code
33
444

Without modules, suppression works:

C:\Temp>type classic.cpp
#include <stdio.h>

#pragma warning(push)
#pragma warning(disable : 4702)

namespace library {
    template <typename T>
    int func(T t) {
        if constexpr (sizeof(T) == 1) {
            return t * 11;
        }
        return t * 111;
    }
} // namespace library

#pragma warning(pop)

int main() {
    printf("%d\n", library::func(static_cast<unsigned char>(3)));
    printf("%d\n", library::func(4));
}
C:\Temp>cl /EHsc /nologo /W4 /std:c++latest classic.cpp && classic
classic.cpp
33
444

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions