microsoft / microsoft/vscode-cpptools
False positive `expression must have a constant value` error in const struct initialization
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 6.2k
- Forks
- 1.7k
- Avg merge
- 14h 46m
- Merged PRs (30d)
- 61
Description
Environment
- OS and Version: Windows 11 23H2
- VS Code Version: 1.95.3
- C/C++ Extension Version: v1.22.11
- If using SSH remote, specify OS of remote machine: N/A, local machine
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:
- Open VSCode
- Open source code file containing code example described below.
- 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the diagnostic in the minimal main.c example using the reported c17, cpp20, and Windows GCC configuration. Trace the IntelliSense constant-expression checking for the const struct initializer and compare it with GCC's result. Done means the valid initializer no longer produces an error squiggle.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, cpp
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100