Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<vector>: Using std::vector iterators in consteval function results in an error with /D_ITERATOR_DEBUG_LEVEL=2 #5171

Open
Predelnik opened this issue Dec 7, 2024 · 1 comment

Comments

@Predelnik
Copy link

Describe the bug

Using iterators to iterate std::vector in a loop within consteval function leads to an error "failure was caused by a read of a variable outside its lifetime"

Command-line test case

C:\Temp>type repro.cpp
#include <vector>

consteval void func()
{
    std::vector<int> v;
    for (auto it = v.begin(); it != v.end(); ++it) {}
}

int main()
{
  func();
}

C:\Temp>cl /EHsc /W4 /WX /MDd /std:c++latest .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.42.34433 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

/std:c++latest is provided as a preview of language features from the latest C++
working draft, and we're eager to hear about bugs and suggestions for improvements.
However, note that these features are provided as-is without support, and subject
to changes or removal as the working draft evolves. See
https://go.microsoft.com/fwlink/?linkid=2045807 for details.

repro.cpp
.\repro.cpp(11): error C7595: 'func': call to immediate function is not a constant expression
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\xmemory(1352): note: failure was caused by a read of a variable outside its lifetime
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\xmemory(1352): note: see usage of 'it'
.\repro.cpp(11): note: the call stack of the evaluation (the oldest call first) is
.\repro.cpp(11): note: while evaluating function 'void func(void)'
.\repro.cpp(5): note: while evaluating function 'std::vector<int,std::allocator<int>>::~vector(void) noexcept'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\vector(761): note: while evaluating function 'void std::vector<int,std::allocator<int>>::_Tidy(void) noexcept'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\vector(2045): note: while evaluating function 'void std::_Container_base12::_Orphan_all(void) noexcept'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\xmemory(1360): note: while evaluating function 'void std::_Container_base12::_Orphan_all_unlocked_v3(void) noexcept'

Expected behavior

No error with /ITERATOR_DEBUG_LEVEL=2

STL version

Microsoft Visual Studio Community 2022 (64-bit) - Current
Version 17.12.0

Additional context

Example on Godbolt
Could be worked around by compiling in release mode or using _ITERATOR_DEBUG_LEVEL=1(or 0)
Hard to tell if the issue is in compiler or in iterator debug level machinery

@frederick-vs-ja
Copy link
Contributor

frederick-vs-ja commented Dec 8, 2024

This is probably a bug of MSVC which hasn't been analyzed yet:

# Not analyzed. Possible MSVC constexpr bug.
# note: failure was caused by a read of a variable outside its lifetime
std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp:0 FAIL
std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp:1 FAIL
std/containers/sequences/vector.bool/construct_iter_iter.pass.cpp:0 FAIL
std/containers/sequences/vector.bool/construct_iter_iter.pass.cpp:1 FAIL
std/containers/sequences/vector.bool/construct_iter_iter_alloc.pass.cpp:0 FAIL
std/containers/sequences/vector.bool/construct_iter_iter_alloc.pass.cpp:1 FAIL
std/containers/sequences/vector.bool/construct_size.pass.cpp:0 FAIL
std/containers/sequences/vector.bool/construct_size.pass.cpp:1 FAIL
std/containers/sequences/vector.bool/construct_size_value.pass.cpp:0 FAIL
std/containers/sequences/vector.bool/construct_size_value.pass.cpp:1 FAIL
std/containers/sequences/vector.bool/construct_size_value_alloc.pass.cpp:0 FAIL
std/containers/sequences/vector.bool/construct_size_value_alloc.pass.cpp:1 FAIL
std/containers/sequences/vector/reverse_iterators.pass.cpp:0 FAIL
std/containers/sequences/vector/reverse_iterators.pass.cpp:1 FAIL
std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp:0 FAIL
std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp:1 FAIL
std/containers/sequences/vector/vector.cons/construct_iter_iter_alloc.pass.cpp:0 FAIL
std/containers/sequences/vector/vector.cons/construct_iter_iter_alloc.pass.cpp:1 FAIL
std/containers/sequences/vector/vector.cons/construct_size.pass.cpp:0 FAIL
std/containers/sequences/vector/vector.cons/construct_size.pass.cpp:1 FAIL
std/containers/sequences/vector/vector.cons/construct_size_value.pass.cpp:0 FAIL
std/containers/sequences/vector/vector.cons/construct_size_value.pass.cpp:1 FAIL
std/containers/sequences/vector/vector.cons/construct_size_value_alloc.pass.cpp:0 FAIL
std/containers/sequences/vector/vector.cons/construct_size_value_alloc.pass.cpp:1 FAIL
std/containers/views/mdspan/mdspan/index_operator.pass.cpp:0 FAIL
std/containers/views/mdspan/mdspan/index_operator.pass.cpp:1 FAIL

Update: Reduced and reported DevCom-10808176.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants