Skip to content
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

Standard Library Modules: uniform_real_distribution emits error C2512: 'std::_Unsigned128': no appropriate default constructor available #4899

Closed
larioteo opened this issue Aug 20, 2024 · 8 comments · Fixed by #4906
Labels
bug Something isn't working compiler Compiler work involved fixed Something works now, yay! modules C++23 modules, C++20 header units

Comments

@larioteo
Copy link

larioteo commented Aug 20, 2024

Describe the bug

It seems there was a bug introduced with the latest preview version, std::uniform_real_distribution doesn't work as expected under C++ Latest with experimental modules enabled.

Simple Example

import std;

int main() {
    std::mt19937 engine{ std::random_device{}() };
    std::uniform_real_distribution<double> dist(0.0, 1.0);
    double result = dist(engine);
    return 0;
}

Expected behavior

The code should compile and run without errors as it did in previous versions

STL version

Microsoft Visual Studio Community 2022 (64-bit) - Preview
Version 17.12.0 Preview 1.0

Additional context

Worked on 17.11.0 Preview 6.0

Compiler Log

Build started at 20:40...
1>------ Build started: Project: Random, Configuration: Debug x64 ------
1>Scanning sources for module dependencies...
1>std.ixx
1>std.compat.ixx
1>Compiling...
1>std.ixx
1>std.compat.ixx
1>Random.cpp
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\__msvc_int128.hpp(826,22): error C2512: 'std::_Unsigned128': no appropriate default constructor available
1>(compiling source file 'Random.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(273,48):
1>    while evaluating constexpr function 'std::operator +'
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(311,34):
1>    while evaluating constexpr function 'std::_Generate_canonical_params'
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\__msvc_int128.hpp(826,22):
1>    the template instantiation context (the oldest one first) is
1>        D:\Research\RandomBug\Random\Random.cpp(6,21):
1>        see reference to function template instantiation 'double std::uniform_real_distribution<double>::operator ()<std::mt19937>(_Engine &)' being compiled
1>        with
1>        [
1>            _Engine=std::mt19937
1>        ]
1>            D:\Research\RandomBug\Random\Random.cpp(6,19):
1>            see the first reference to 'std::uniform_real_distribution<double>::operator ()' in 'main'
1>        C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(3287,25):
1>        see reference to function template instantiation 'double std::uniform_real<_Ty>::operator ()<_Engine>(_Engine &)' being compiled
1>        with
1>        [
1>            _Ty=double,
1>            _Engine=std::mt19937
1>        ]
1>        C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(3182,16):
1>        see reference to function template instantiation 'double std::uniform_real<_Ty>::_Eval<_Engine>(_Engine &,const std::uniform_real<_Ty>::param_type &) const' being compiled
1>        with
1>        [
1>            _Ty=double,
1>            _Engine=std::mt19937
1>        ]
1>        C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(3222,16):
1>        see reference to function template instantiation '_Real std::_Nrand_impl<_Ty,_Engine>(_Gen &)' being compiled
1>        with
1>        [
1>            _Real=double,
1>            _Ty=double,
1>            _Engine=std::mt19937,
1>            _Gen=std::mt19937
1>        ]
1>        C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(419,21):
1>        see reference to function template instantiation '_Real std::generate_canonical<_Real,53,_Gen>(_Gen &)' being compiled
1>        with
1>        [
1>            _Real=double,
1>            _Gen=std::mt19937
1>        ]
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(311,32): error C2131: expression did not evaluate to a constant
1>(compiling source file 'Random.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\__msvc_int128.hpp(826,22):
1>    a non-constant (sub-)expression was encountered
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(314,46): error C2975: '_Test': invalid template argument for 'std::conditional_t', expected compile-time constant expression
1>(compiling source file 'Random.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\xtr1common(68,28):
1>    see declaration of '_Test'
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(313,59): error C2975: '_Test': invalid template argument for 'std::conditional_t', expected compile-time constant expression
1>(compiling source file 'Random.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\xtr1common(68,28):
1>    see declaration of '_Test'
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(315,18): error C2512: 'std::_Unsigned128': no appropriate default constructor available
1>(compiling source file 'Random.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\__msvc_int128.hpp(706,8):
1>    see declaration of 'std::_Unsigned128'
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(317,30): error C2131: expression did not evaluate to a constant
1>(compiling source file 'Random.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\__msvc_int128.hpp(826,22):
1>    a non-constant (sub-)expression was encountered
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(319,39): error C2131: expression did not evaluate to a constant
1>(compiling source file 'Random.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\__msvc_int128.hpp(826,22):
1>    a non-constant (sub-)expression was encountered
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(321,46): error C2131: expression did not evaluate to a constant
1>(compiling source file 'Random.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\__msvc_int128.hpp(826,22):
1>    a non-constant (sub-)expression was encountered
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(322,40): error C2131: expression did not evaluate to a constant
1>(compiling source file 'Random.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\__msvc_int128.hpp(826,22):
1>    a non-constant (sub-)expression was encountered
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(336,39): error C2131: expression did not evaluate to a constant
1>(compiling source file 'Random.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\__msvc_int128.hpp(826,22):
1>    a non-constant (sub-)expression was encountered
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(337,39): error C2131: expression did not evaluate to a constant
1>(compiling source file 'Random.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\__msvc_int128.hpp(826,22):
1>    a non-constant (sub-)expression was encountered
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(342,43): error C2131: expression did not evaluate to a constant
1>(compiling source file 'Random.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\__msvc_int128.hpp(826,22):
1>    a non-constant (sub-)expression was encountered
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\random(344,50): error C2131: expression did not evaluate to a constant
1>(compiling source file 'Random.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.42.34226\include\__msvc_int128.hpp(826,22):
1>    a non-constant (sub-)expression was encountered
1>Done building project "Random.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 20:40 and took 03,809 seconds ==========
@CaseyCarter CaseyCarter added bug Something isn't working compiler Compiler work involved modules C++23 modules, C++20 header units labels Aug 20, 2024
@CaseyCarter
Copy link
Member

CaseyCarter commented Aug 20, 2024

_Unsigned128 inherits a default constructor from _Base128. This looks like a compiler regression. The workaround we have for pre-C++17-inherited constructors seems to fix this as well:

#if !_HAS_CXX17
constexpr _Unsigned128() noexcept : _Base128{} {}
#endif // !_HAS_CXX17

@StephanTLavavej StephanTLavavej changed the title <std.ixx> Bug: std::uniform_real_distribution fails to compile with MSVC 17.12.0 Preview 1.0 under C++ Latest with modules support enabled Standard Library Modules: uniform_real_distribution emits error C2512: 'std::_Unsigned128': no appropriate default constructor available Aug 20, 2024
@StephanTLavavej
Copy link
Member

Thanks for the report! Can we ask you to report this on VS Developer Community, at https://developercommunity.visualstudio.com/cpp/report ?

The compiler team wants to track all issues there and they'd like customers to directly report issues (instead of MSVC team members). Sorry for asking you to do a bit more paperwork.

@StephanTLavavej
Copy link
Member

I'll report it.

By the way, when using import std; or import std.compat;, all you need is /std:c++latest (or /std:c++20).

You don't need the "experimental modules" selected in the VS Installer, and you shouldn't use the /experimental:module compiler option - that was for the older, limited, non-Standard import std.core; et al.

@StephanTLavavej
Copy link
Member

DevCom-10729775 "Standard Library Modules: Random number machinery emits bogus error C2512: 'std::_Unsigned128': no appropriate default constructor available".

@larioteo
Copy link
Author

Sorry for being late; I simply haven't had time until now :).

Next time, I'll report it through the developer community as well.

Thanks! I overlooked that the /experimental:module option is no longer needed.

@StephanTLavavej StephanTLavavej added the fixed Something works now, yay! label Aug 25, 2024
@StephanTLavavej
Copy link
Member

Awesome, thanks again for being an early adopter of modules! I've merged the library workarounds for VS 2022 17.12 Preview 3. However, I encountered a compiler assertion in our internal "checked" builds (that aren't active in the "retail" builds that we ship publicly). This assertion might be over-eager, but it might also indicate remaining problems that could appear with the retail compiler under more intensive usage. Please let us know if you run into any remaining weirdness.

@peter1745
Copy link

This seems to be happening in Visual Studio 17.12 Preview 1.0 and Preview 2.0

@CaseyCarter
Copy link
Member

This seems to be happening in Visual Studio 17.12 Preview 1.0 and Preview 2.0

The fix should be in VS 2022 17.12 Preview 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler Compiler work involved fixed Something works now, yay! modules C++23 modules, C++20 header units
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants