microsoft / microsoft/vscode-cpptools

Falsely flagged "expression must have a constant value" for std::numbers

Open
#12,810 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Language Service Visual Studio
Dominant language
TypeScript
Stars
6.2k
Forks
1.7k
Avg merge
14h 46m
Merged PRs (30d)
61

Description

Environment
  • OS and Version: Windows_NT x64 10.0.22631
  • VS Code Version: 1.93.1
  • C/C++ Extension Version: 1.21.6
  • If using SSH remote, specify OS of remote machine: Ubuntu 22.04.4 LTS in WSL2
Bug Summary and Steps to Reproduce

Bug Summary:

The intellisense complains that expression must have a constant value when a constant expression from std::numbers is used in another constant expression.

Steps to reproduce:

#include <numbers>

int main() {
  constexpr double pi_2 = std::numbers::pi / 2.;
  return 0;
}

Expected behavior:

No error.

Configuration and Logs
Configuragions:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/g++-11",
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

Diagnostics:

-------- Diagnostics - 10/7/2024, 12:07:20 AM
Version: 1.21.6
Current Configuration:
{
    "name": "Linux",
    "includePath": [
        "/home/one/experiments/vscode-intellisense-issue/**"
    ],
    "defines": [],
    "compilerPath": "/usr/bin/g++-11",
    "cStandard": "c17",
    "cppStandard": "c++20",
    "intelliSenseMode": "linux-gcc-x64",
    "compilerPathIsExplicit": true,
    "cStandardIsExplicit": true,
    "cppStandardIsExplicit": true,
    "intelliSenseModeIsExplicit": true,
    "compilerPathInCppPropertiesJson": "/usr/bin/g++-11",
    "mergeConfigurations": false,
    "browse": {
        "path": [
            "/home/one/experiments/vscode-intellisense-issue/**",
            "${workspaceFolder}"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
cpptools version (native): 1.21.6.0
Translation Unit Mappings:
[ /home/one/experiments/vscode-intellisense-issue/main.cpp - source TU]:
Translation Unit Configurations:
[ /home/one/experiments/vscode-intellisense-issue/main.cpp ]:
    Process ID: 50136
    Memory Usage: 25 MB
    Compiler Path: /usr/bin/g++-11
    Includes:
    System Includes:
        /usr/include/c++/11
        /usr/include/x86_64-linux-gnu/c++/11
        /usr/include/c++/11/backward
        /usr/lib/gcc/x86_64-linux-gnu/11/include
        /usr/local/include
        /usr/include/x86_64-linux-gnu
        /usr/include
    Standard Version: c++20
    IntelliSense Mode: linux-gcc-x64
    Other Flags:
        --g++
        --gnu_version=110400
Total Memory Usage: 25 MB

------- Workspace parsing diagnostics -------
Number of files discovered (not excluded): 7073
Other Extensions

None

Additional context

image

If the constant is defined without potential narrowing, the intellisense works fine. For example,

template <typename T>
inline constexpr T pi_v = 3.141592653589793238462643383279502884;
inline constexpr double pi = pi_v<double>;

int main() {
  constexpr double pi_2 = pi / 2.;
  return 0;
}

Note that the definition in std::numbers is something like

template <typename T>
inline constexpr T pi_v = T(3.141592653589793238462643383279502884L);
inline constexpr double pi = pi_v<double>;

The narrowing conversion for the literal is definitely legal.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic in main.cpp using the provided C++20 configuration with g++-11, and compare IntelliSense's result with the compiler for the std::numbers expression. Trace the constant-expression handling for the std::numbers definition and narrowing conversion. Done means the shown constexpr expression no longer reports an error.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.