microsoft / microsoft/vscode-cpptools

Incorrect member inaccessible error in friend class/function when class is in separate C++ 20 module

Offen
#8,450 1 Kommentar 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug Language Service Visual Studio
Vorherrschende Sprache
TypeScript
Sterne
6.2k
Forks
1.7k
Ø Merge
14 Std. 46 Min.
Gemergte PRs (30 T.)
61

Beschreibung

Bug type: Language Service

Describe the bug

  • OS and Version: Windows 11 x64 22000.318
  • VS Code Version: 1.62.3
  • C/C++ Extension Version: v1.7.1
  • MSVC Version: 14.30.30705, x64 (in VS 2022)
  • Other extensions you installed (and if the issue persists after disabling them):
    • Was using the Meson extension and various others, however the issue exists with all other extensions disabled.

When using C++20 modules, the language server gives a false positive member inaccessible error when using a friend class or friend function to access a private member of a class if the class is in another C++20 module.

Using a single project, single folder workspace (not tested on others but probably will have the same result)

#6302 is relevant to this issue, but separate. Module import IntelliSense is not required for the extension to detect imported types from other modules (via IFC files).

Steps to reproduce

  1. Create a new workspace directory with the code samples and C/C++ configuration below.

    • The important part is to have a class in one module m, and have a friend class or friend function of that class in another module that imports m and accesses a private member of an instance of that class.
  2. Compile m.cpp (just to have the IFC file)

    cl.exe /c /std:c++20 /experimental:module /interface m.cpp
    
  3. Observe the following:

    • IntelliSense correctly does not produce errors for the C++20 module syntax and correctly identifies the class Test (can be verified with Go to Definition)
    • However, the use of t.privateMember does produce an error, even though the code compiles fine:
      member "Test::privateMember" (declared at line 5 of module "m" ("<...>\m.cpp")) is inaccessible
      

Expected behavior
This error is a false-positive. The code for both files compiles and links fine. Accessing the private member should work because int main() is a friend function of class Test.

Code sample and logs

  • Code sample

    m.cpp:

    export module m;
    
    export class Test {
    private:
        int privateMember;
        friend int main();
    };
    

    u.cpp:

    import m;
    
    int main()
    {
      Test t;
      t.privateMember = 0;
      return 0;
    }
    
  • Configurations in c_cpp_properties.json

    .vscode/c_cpp_properties.json:

    {
        "configurations": [
            {
                "name": "Win32",
                "includePath": [
                    "${workspaceFolder}/**"
                ],
                "defines": [
                    "_DEBUG",
                    "UNICODE",
                    "_UNICODE"
                ],
                "windowsSdkVersion": "10.0.19041.0",
                "compilerPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.30.30705\\bin\\Hostx64\\x64\\cl.exe",
                "cStandard": "c17",
                "cppStandard": "c++20",
                "intelliSenseMode": "windows-msvc-x64",
                "compilerArgs": [
                    "/experimental:module",
                    "/interface"
                ]
            }
        ],
        "version": 4
    }
    

    The important part is to be using MSVC, c++20, and have /experimental:module and /interface. Everything else is default.

  • Logs from running C/C++: Log Diagnostics from the VS Code command palette

  • Logs from the language server logging

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit den Beispielen m.cpp und u.cpp sowie der Konfiguration .vscode/c_cpp_properties.json, kompiliere dann m.cpp mit dem gezeigten MSVC C++20-Modulbefehl und untersuche die Diagnose des Language Service für t.privateMember. Die Arbeit ist abgeschlossen, wenn der Friend-Zugriff keinen Fehler wegen eines unzugänglichen Members mehr erzeugt und das Beispiel weiterhin korrekt kompiliert und gelinkt wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
cpp
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.