microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.54k stars 1.56k forks source link

False positive `expression must have a constant value` error in const struct initialization #12983

Open thekovic opened 4 days ago

thekovic commented 4 days ago

Environment

Bug Summary and Steps to Reproduce

Bug Summary: VSCode reports a false positive expression must have a constant value error when constructing a const struct with a struct member that is being copy assigned from an existing const struct instance.

Steps to reproduce:

  1. Open VSCode
  2. Open source code file containing code example described below.
  3. See error squiggles reported.

Expected behavior: No error should be reported in this case.

Configuration and Logs

`C/C++: Log Diagnostics`:

-------- Diagnostics - 11/21/2024, 1:43:27 AM
Version: 1.22.11
Current Configuration:
{
    "name": "Win32",
    "includePath": [
        "C:/Code/libdragon/include",
        "C:/Code/tiny3d/include"
    ],
    "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
    ],
    "windowsSdkVersion": "10.0.22621.0",
    "compilerPathIsExplicit": true,
    "cStandardIsExplicit": true,
    "cppStandardIsExplicit": true,
    "intelliSenseModeIsExplicit": true,
    "cStandard": "gnu17",
    "cppStandard": "gnu++20",
    "intelliSenseMode": "windows-gcc-x64",
    "mergeConfigurations": false,
    "compilerPath": "C:\\msys64\\ucrt64\\bin\\g++.exe",
    "browse": {
        "path": [
            "C:/Code/libdragon/include",
            "C:/Code/tiny3d/include",
            "${workspaceFolder}"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
Modified Settings:
{
    "C_Cpp.default.includePath": [
        "C:\\Code\\libdragon\\include",
        "C:\\Code\\tiny3d\\include"
    ],
    "C_Cpp.default.intelliSenseMode": "windows-gcc-x64",
    "C_Cpp.default.compilerPath": "C:\\msys64\\ucrt64\\bin\\g++.exe",
    "C_Cpp.default.cStandard": "gnu17",
    "C_Cpp.default.cppStandard": "gnu++20"
}
Additional Tracked Settings:
{
    "editorTabSize": 4,
    "editorInsertSpaces": true,
    "editorAutoClosingBrackets": "languageDefined",
    "filesEncoding": "utf8",
    "filesAssociations": {},
    "filesExclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/.DS_Store": true,
        "**/Thumbs.db": true
    },
    "filesAutoSaveAfterDelay": false,
    "editorInlayHintsEnabled": true,
    "editorParameterHintsEnabled": true,
    "searchExclude": {
        "**/node_modules": true,
        "**/bower_components": true,
        "**/*.code-search": true
    },
    "workbenchSettingsEditor": "ui"
}
cpptools version (native): 1.22.11.0
Current database path: C:\USERS\KOVIC\APPDATA\LOCAL\MICROSOFT\VSCODE-CPPTOOLS\1830F5E21FE12B228B13E26BCB2FACA7\.BROWSE.VC.DB
Translation Unit Mappings:
[ C:\Code\a\main.c - source TU]:
    C:\msys64\ucrt64\include\stdio.h
    C:\msys64\ucrt64\include\vadefs.h
Translation Unit Configurations:
[ C:\Code\a\main.c ]
    Process ID: 3368
    Memory Usage: 63 MB
    Compiler Path: C:\msys64\ucrt64\bin\g++.exe
    Includes:
        C:\Code\libdragon\include
        C:\Code\tiny3d\include
    System Includes:
        C:\msys64\ucrt64\lib\gcc\x86_64-w64-mingw32\14.2.0\include
        C:\msys64\ucrt64\include
        C:\msys64\ucrt64\lib\gcc\x86_64-w64-mingw32\14.2.0\include-fixed
    Defines:
        _DEBUG
        UNICODE
        _UNICODE
    Standard Version: c17
    IntelliSense Mode: windows-gcc-x64
    Other Flags:
        --gcc
        --gnu_version=140200
Total Memory Usage: 63 MB

------- Workspace parsing diagnostics -------
Number of files discovered (not excluded): 3837
Number of files parsed: 1

Language server log:

loggingLevel: Debug
loggingLevel has changed to: Debug
LSP: (received) cpptools/didChangeCppProperties (id: 350)
LSP: (invoked) cpptools/didChangeCppProperties (id: 350)
LSP: Sending response (id: 350)
LSP: (received) cpptools/didChangeVisibleTextEditors
LSP: (invoked) cpptools/didChangeVisibleTextEditors
LSP: (received) cpptools/didChangeVisibleTextEditors
LSP: (invoked) cpptools/didChangeVisibleTextEditors
LSP: (received) cpptools/didChangeVisibleTextEditors
LSP: (invoked) cpptools/didChangeVisibleTextEditors
Intellisense update pending for: file:///c%3A/Code/a/main.c
LSP: (received) cpptools/didChangeActiveEditor: file:///c%3A/Code/a/main.c
LSP: (invoked) cpptools/didChangeActiveEditor: file:///c%3A/Code/a/main.c
LSP: (received) cpptools/getCodeActions: file:///c%3A/Code/a/main.c (id: 351)
LSP: (invoked) cpptools/getCodeActions: file:///c%3A/Code/a/main.c (id: 351)
LSP: Sending response (id: 351)
LSP: (received) cpptools/didChangeTextEditorSelection
LSP: (invoked) cpptools/didChangeTextEditorSelection
IntelliSense update scheduled and TU acquisition started for: file:///c%3A/Code/a/main.c
Update IntelliSense time (sec): 0.007
LSP: (received) cpptools/getFoldingRanges: file:///c%3A/Code/a/main.c (id: 352)
LSP: (invoked) cpptools/getFoldingRanges: file:///c%3A/Code/a/main.c (id: 352)
LSP: Sending response (id: 352)
LSP: (received) cpptools/getCodeActions: file:///c%3A/Code/a/main.c (id: 353)
LSP: (invoked) cpptools/getCodeActions: file:///c%3A/Code/a/main.c (id: 353)
LSP: Sending response (id: 353)
LSP: (received) cpptools/getFoldingRanges: file:///c%3A/Code/a/main.c (id: 354)
LSP: (invoked) cpptools/getFoldingRanges: file:///c%3A/Code/a/main.c (id: 354)
LSP: Sending response (id: 354)

Other Extensions

No response

Additional context

The following is a minimal code example reproducing the issue:

#include <stdio.h>
#include <stdint.h>

typedef struct vi_borders_s{
    uint16_t left, right, up, down;
} vi_borders_t;

const vi_borders_t VI_BORDERS_NONE = {42, 42, 42, 42};

typedef struct {
    int32_t width;
    int32_t height;
    vi_borders_t overscan_borders;
} resolution_t;

const resolution_t RESOLUTION_256x240 = {.width = 256, .height = 240, .overscan_borders = VI_BORDERS_NONE};

int main(void)
{
    printf("%i", RESOLUTION_256x240.overscan_borders.up);

    return 0;
}

The code compiles and runs without issues as expected with gcc.exe and g++.exe (version 14.2.0) from UCRT64/MSYS2.