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

modules: ambiguous call to overloaded function #4090

Closed
larioteo opened this issue Oct 14, 2023 · 9 comments
Closed

modules: ambiguous call to overloaded function #4090

larioteo opened this issue Oct 14, 2023 · 9 comments
Labels
bug Something isn't working compiler Compiler work involved modules C++23 modules, C++20 header units resolved Successfully resolved without a commit

Comments

@larioteo
Copy link

larioteo commented Oct 14, 2023

Describe the bug

As it seems there is a bug if you use export import std; in one module, than for example import <chrono>; in another. Now if you import these two modules in a third one, you get various errors depending on the used header unit in the second. I tried it with 'string', 'chrono' and 'print', but it seems that it doesn't matter which one I use.

Expected behavior

I'm not sure, I tried reimporting my modules, and the compiler doesn't seem to complain, so I think there should be no problem at all if you reimport std, or a header unit which it contains. I mean also with headers there was never such thing, thanks to '#pragma once'.

STL version

  • Option 1: Visual Studio v17.7.5 or Visual Studio v17.8 preview 3
    • Example:
      Microsoft Visual Studio Community 2022
      Version 17.7.5 |  v17.8 Preview 3.0
      

Additional context

I created a small project to reproduce this bug.
Test.zip

@StephanTLavavej StephanTLavavej changed the title modules: abiguous call to overloaded function modules: ambiguous call to overloaded function Oct 16, 2023
@StephanTLavavej
Copy link
Member

I observe that this works if I replace import <chrono>; with import std; in Timer.ixx. This appears to be a known compiler bug/limitation regarding any mixing of the named Standard Library Modules with header units and/or classic includes. At this time, if you use the std module, you should do so consistently throughout your project.

@StephanTLavavej StephanTLavavej added bug Something isn't working compiler Compiler work involved modules C++23 modules, C++20 header units labels Oct 16, 2023
@StephanTLavavej
Copy link
Member

Slightly reduced:

C:\Users\stl\Downloads\Test\Test>type Core.ixx
export module Core;

export import std;
C:\Users\stl\Downloads\Test\Test>type Timer.ixx
module;

export module Timer;

#ifdef WORKAROUND
import std;
#else
import <chrono>;
#endif

export {
    std::string GetTimeStamp() {
        const auto now = std::chrono::system_clock::now();
        const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
        return std::to_string(ms.count());
    }
}
C:\Users\stl\Downloads\Test\Test>type Logger.ixx
module;

export module Logger;

import Core;
import Timer;

export void PrintMessage(const std::string& message) {
    std::println("{}: {}", GetTimeStamp(), message);
}
C:\Users\stl\Downloads\Test\Test>type App.ixx
export module App;

import Logger;

export struct App {
    static void Start() {
        PrintMessage("Start");
    }

    static void Stop() {
        PrintMessage("Stop");
    }
};
C:\Users\stl\Downloads\Test\Test>type main.cpp
import App;
import Logger;

int main() {
    App::Start();
    PrintMessage("Hello C++ Modules!");
    App::Stop();
}
C:\Users\stl\Downloads\Test\Test>cl /EHsc /nologo /W4 /std:c++latest /MTd /Od /exportHeader /headerName:angle /Fo chrono && cl /EHsc /nologo /W4 /std:c++latest /MTd /Od /Femain.exe /headerUnit:angle chrono=chrono.ifc chrono.obj "%VCToolsInstallDir%\modules\std.ixx" Core.ixx Timer.ixx Logger.ixx App.ixx main.cpp && main.exe
chrono
std.ixx
Core.ixx
Timer.ixx
Logger.ixx
C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.38.33126\include\format(3674): error C2668: 'std::_Parse_format_string': ambiguous call to overloaded function
C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.38.33126\include\format(1473): note: could be 'void std::_Parse_format_string<_Elem,std::__p2286::_Format_checker<_CharT,std::basic_string<_Elem,std::char_traits<char>,std::allocator<char>>,std::basic_string<_Elem,std::char_traits<char>,std::allocator<char>>>>(std::basic_string_view<_Elem,_Traits>,_HandlerT &&)' [found using argument-dependent lookup]
        with
        [
            _Elem=char,
            _CharT=char,
            _Traits=std::char_traits<char>,
            _HandlerT=std::__p2286::_Format_checker<char,std::basic_string<char,std::char_traits<char>,std::allocator<char>>,std::basic_string<char,std::char_traits<char>,std::allocator<char>>>
        ]
C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.38.33126\include\format(1473): note: or       'void std::_Parse_format_string<_Elem,std::__p2286::_Format_checker<_CharT,std::basic_string<_Elem,std::char_traits<char>,std::allocator<char>>,std::basic_string<_Elem,std::char_traits<char>,std::allocator<char>>>>(std::basic_string_view<_Elem,_Traits>,_HandlerT &&)'
        with
        [
            _Elem=char,
            _CharT=char,
            _Traits=std::char_traits<char>,
            _HandlerT=std::__p2286::_Format_checker<char,std::basic_string<char,std::char_traits<char>,std::allocator<char>>,std::basic_string<char,std::char_traits<char>,std::allocator<char>>>
        ]
C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.38.33126\include\format(3674): note: while trying to match the argument list '(std::basic_string_view<_Elem,_Traits>, std::__p2286::_Format_checker<_CharT,std::basic_string<char,std::char_traits<char>,std::allocator<char>>,std::basic_string<char,std::char_traits<char>,std::allocator<char>>>)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        and
        [
            _CharT=char
        ]
C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.38.33126\include\format(3674): note: the template instantiation context (the oldest one first) is
Logger.ixx(9): note: see reference to function template instantiation 'std::basic_format_string<char,std::basic_string<char,std::char_traits<char>,std::allocator<char>>,const std::string &>::basic_format_string<char[7]>(const _Ty (&))' being compiled
        with
        [
            _Ty=char [7]
        ]
Logger.ixx(9): error C7595: 'std::basic_format_string<char,std::basic_string<char,std::char_traits<char>,std::allocator<char>>,const std::string &>::basic_format_string': call to immediate function is not a constant expression
C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.38.33126\include\format(3674): note: a non-constant (sub-)expression was encountered
Logger.ixx(9): note: the call stack of the evaluation (the oldest call first) is
Logger.ixx(9): note: while evaluating function 'std::basic_format_string<char,std::basic_string<char,std::char_traits<char>,std::allocator<char>>,const std::string &>::basic_format_string<char[7]>(const _Ty (&))'
        with
        [
            _Ty=char [7]
        ]
App.ixx
App.ixx(3): error C2230: could not find module 'Logger'
App.ixx(7): error C3861: 'PrintMessage': identifier not found
App.ixx(11): error C3861: 'PrintMessage': identifier not found
main.cpp
main.cpp(1): error C2230: could not find module 'App'
main.cpp(2): error C2230: could not find module 'Logger'
main.cpp(5): error C2653: 'App': is not a class or namespace name
main.cpp(5): error C3861: 'Start': identifier not found
main.cpp(6): error C3861: 'PrintMessage': identifier not found
main.cpp(7): error C2653: 'App': is not a class or namespace name
main.cpp(7): error C3861: 'Stop': identifier not found
Generating Code...
C:\Users\stl\Downloads\Test\Test>cl /EHsc /nologo /W4 /std:c++latest /MTd /Od /Femain.exe /DWORKAROUND "%VCToolsInstallDir%\modules\std.ixx" Core.ixx Timer.ixx Logger.ixx App.ixx main.cpp && main.exe
std.ixx
Core.ixx
Timer.ixx
Logger.ixx
App.ixx
main.cpp
Generating Code...
1697491976759: Start
1697491976759: Hello C++ Modules!
1697491976759: Stop

@davidhunter22
Copy link

You state "At this time, if you use the std module, you should do so consistently throughout your project."
Do you mean a single project in VS terminology, so a bunch of translation units that create one library.
So are there any known issue with VS solution containing multiple projects some of which use old style includes and some import std; and that reference each other?

@StephanTLavavej
Copy link
Member

Mixing named modules and classic includes or header units within a single TU is super doomed due to the compiler bug/limitation.

Consistent TUs being mixed together during linking is probably still doomed but I haven't checked. The compiler team mentioned something about the linker having a fallback to deal with what happens when it sees strong ownership being mixed with classic compilation (I think those words make sense in that order) but that didn't seem to be my experience when I was initially updating the STL to make import std; work with separately compiled classic code (we got that to work with liberal doses of extern "C++").

Sorry I can't be more precise (I don't have sufficient time right now to explore the limits of this compiler limitation since there's nothing actionable for the STL).

@larioteo
Copy link
Author

larioteo commented Jan 8, 2024

Hello again,

I started the project from scratch, so I can focus on solely using modules, either way, it doesn't seem to be restricted to mixing modules with includes. The issue also arises when exclusively using modules in a larger project.. I tried to track down the issue, but again, it proved impossible. I am using the latest Visual Studio 2022 17.9.0 Preview 2.1.

It happens if I simply add a string to a struct, there is nothing crazy going on behind, but I think the crucial part is, having a large project. As I said I started from scratch, created a lot of empty modules and so on, the most of them are empty and planned to be used in future.

Here's the line (GitHub) that triggers the following error:

1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.39.33321\include\xstring(3182,9): error C2668: 'std::_Delete_plain_internal': ambiguous call to overloaded function
1>(compiling source file 'Platform/WinAPI/WinWindow.cxx')
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.39.33321\include\xmemory(1139,19):
1>could be 'void std::_Delete_plain_internal<std::allocator<std::_Container_proxy>>(_Alloc &,std::_Container_proxy *const ) noexcept' [found using argument-dependent lookup]
1>        with
1>        [
1>            _Alloc=std::allocator<std::_Container_proxy>
1>        ]
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.39.33321\include\xmemory(1139,19):
1>or       'void std::_Delete_plain_internal<std::allocator<std::_Container_proxy>>(_Alloc &,std::_Container_proxy *const ) noexcept'
1>        with
1>        [
1>            _Alloc=std::allocator<std::_Container_proxy>
1>        ]
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.39.33321\include\xstring(3182,9):
1>while trying to match the argument list '(std::allocator<std::_Container_proxy>, std::_Container_proxy *const )'
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.39.33321\include\xstring(3182,9):
1>the template instantiation context (the oldest one first) is
1>	D:\Development\Apps\Hedron\Source\Vite\Base\System\Window.ixx(50,12):
1>	see reference to class template instantiation 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' being compiled
1>	C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.39.33321\include\xstring(3176,19):
1>	while compiling class template member function 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string(void) noexcept'
1>		D:\Development\Apps\Hedron\Source\Vite\Base\System\Window.ixx(50,18):
1>		see the first reference to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string' in 'Hedron::WindowSettings::WindowSettings'
1>Done building project "Vite.vcxproj" -- FAILED.

I tried solely import std; in this module, the same error. Also (for fun) I tried including string, and the error hell started to show up:

1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.39.33321\include\xstring(2336,52): error C2572: 'std::basic_string': redefinition of default argument: parameter 1
1>(compiling source file 'Base/System/Window.ixx')
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.39.33321\include\xstring(2337,7):
1>see declaration of 'std::basic_string'
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.39.33321\include\xstring(2336,87): error C2572: 'std::basic_string': redefinition of default argument: parameter 2
1>(compiling source file 'Base/System/Window.ixx')
1>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.39.33321\include\xstring(2337,7):
1>see declaration of 'std::basic_string'
1>Done building project "Vite.vcxproj" -- FAILED.

I am not sure if it will be helpful, but I uploaded the intermediate files in case they can help identify the problem.
(it's a 7z renamed to zip, due to GitHub upload limitations...)
Vite.zip

@larioteo
Copy link
Author

larioteo commented Jan 8, 2024

Hmm, I've found the culprit; it was a third-party import of a header unit. However, I found an alternative, 'Bridges,' in this DevBlog that helps me move forward.

Nevertheless, thank you guys for your great work!

@LiAuTraver
Copy link

same issue here.

#include <some standard header>
import std;

would always report redefined function/variable error
it's sad that many lib still use header such as Qt so its extremely annoying that i cannot use module there.

@larioteo
Copy link
Author

larioteo commented Feb 11, 2024

This should work according to the Change-Log after the 19.10 Preview 1. But only in that order as you posted it. Using a header after the import would also break the build.

I am waiting since november to be able to use this, cause it was quickly fixed internally (look at discord), but we the community have to wait months until Microsoft releases this compiler fix to the public, which is realy unfortunate.

I hope that Microsoft gets a change of heart and open sources the compiler some day, so that things can get really fast.

It's really a shame, cause their compiler is the best one for modules right now, but also has a lot of issues around them, and sadly it's 2024.

@StephanTLavavej
Copy link
Member

Yep, I've verified that #4154 in VS 2022 17.10 Preview 1 has fixed this scenario. I can now successfully build the repro (with 17.10 Preview 4):

C:\Users\stl\Downloads\Test>"C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Auxiliary\Build\vcvarsall.bat" x64
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.10.0-pre.4.0
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

C:\Users\stl\Downloads\Test>cl /EHsc /nologo /W4 /std:c++latest /MDd /Od /c "%VCToolsInstallDir%\modules\std.ixx"
std.ixx

C:\Users\stl\Downloads\Test>cl /EHsc /nologo /W4 /std:c++latest /MDd /Od /c /exportHeader /headerName:angle /Fo chrono
chrono

C:\Users\stl\Downloads\Test>cl /EHsc /nologo /W4 /std:c++latest /MDd /Od /Femain.exe /headerUnit:angle chrono=chrono.ifc Core.ixx Timer.ixx Logger.ixx App.ixx main.cpp std.obj chrono.obj
Core.ixx
Timer.ixx
Logger.ixx
App.ixx
main.cpp
main.cpp(6): warning C4100: 'argc': unreferenced formal parameter
main.cpp(6): warning C4100: 'argv': unreferenced formal parameter
Generating Code...

C:\Users\stl\Downloads\Test>main.exe
1713650992658: Start
1713650992658: Hello C++ Modules!
1713650992658: Stop

@StephanTLavavej StephanTLavavej added the resolved Successfully resolved without a commit label Apr 20, 2024
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 modules C++23 modules, C++20 header units resolved Successfully resolved without a commit
Projects
None yet
Development

No branches or pull requests

4 participants