microsoft / microsoft/vscode-cpptools

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

オープン
#8,450 コメント 1 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug Language Service Visual Studio
主要言語
TypeScript
スター
6.2k
フォーク
1.7k
平均マージ
14時間 46分
マージ済み PR(30日)
61

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず m.cpp と u.cpp のサンプル、および .vscode/c_cpp_properties.json の設定から始め、次に示されている MSVC C++20 モジュールコマンドで m.cpp をコンパイルし、t.privateMember に対する言語サービスの診断を確認します。friend アクセスによって inaccessible-member エラーが発生せず、サンプルが引き続き正しくコンパイルおよびリンクできれば作業完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
cpp
領域
tooling
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。