Repository: pytorch
Issue: PyTorch build fail with GCC 13.2.0 due to aten/src/ATen/core/IListRef_inl.h:171:17: error: possibly dangling reference to a temporary [-Werror=dangling-reference]
Labels: module: build,triaged

### 🐛 Describe the bug

**Summary**
GCC 11.3.0 + with WERROR=1 flag : Build pass
GCC 13.2.0 + with WERROR=1 flag : Build fail
GCC 13.2.0 + without WERROR=1 flag : Build pass

**Command**
export USE_CUDA=0
env WERROR=1 python setup.py bdist_wheel

**Error message**

```
[6698/8292] Building CXX object caffe2/CMakeFiles/torch_cpu.dir/__/aten/src/ATen/native/prim_native_functions.cpp.o
[6699/8292] Building CXX object caffe2/CMakeFiles/torch_cpu.dir/__/aten/src/ATen/native/UpSample.cpp.o
[6700/8292] Building CXX object caffe2/CMakeFiles/torch_cpu.dir/__/aten/src/ATen/native/verbose_wrapper.cpp.o
[6701/8292] Building CXX object caffe2/CMakeFiles/torch_cpu.dir/__/aten/src/ATen/core/List.cpp.o
FAILED: caffe2/CMakeFiles/torch_cpu.dir/__/aten/src/ATen/core/List.cpp.o
/usr/bin/ccache /rdrive/ref/gcc/13.2.0/ubt2204/efi2/bin/c++ -DAT_PER_OPERATOR_HEADERS -DBUILD_ONEDNN_GRAPH -DCAFFE2_BUILD_MAIN_LIB -DCPUINFO_SUPPORTED_PLATFORM=1 -DFLASHATTENTION_DISABLE_ALIBI -DFMT_HEADER_ONLY=1 -DFXDIV_USE_INLINE_ASSEMBLY=0 -DHAVE_MALLOC_USABLE_SIZE=1 -DHAVE_MMAP=1 -DHAVE_SHM_OPEN=1 -DHAVE_SHM_UNLINK=1 -DIDEEP_USE_MKL -DMINIZ_DISABLE_ZIP_READER_CRC32_CHECKS -DNNP_CONVOLUTION_ONLY=0 -DNNP_INFERENCE_ONLY=0 -DONNXIFI_ENABLE_EXT=1 -DONNX_ML=1 -DONNX_NAMESPACE=onnx_torch -DUSE_C10D_GLOO -DUSE_C10D_MPI -DUSE_DISTRIBUTED -DUSE_EXTERNAL_MZCRC -DUSE_RPC -DUSE_TENSORPIPE -D_FILE_OFFSET_BITS=64 -Dtorch_cpu_EXPORTS -DTORCH_ASSERT_ONLY_METHOD_OPERATORS -I/localdisk2/yutaowan/test/pytorch/build/aten/src -I/localdisk2/yutaowan/test/pytorch/aten/src -I/localdisk2/yutaowan/test/pytorch/build -I/localdisk2/yutaowan/test/pytorch -I/localdisk2/yutaowan/test/pytorch/cmake/../third_party/benchmark/include -I/localdisk2/yutaowan/test/pytorch/third_party/onnx -I/localdisk2/yutaowan/test/pytorch/build/third_party/onnx -I/localdisk2/yutaowan/test/pytorch/third_party/foxi -I/localdisk2/yutaowan/test/pytorch/build/third_party/foxi -I/localdisk2/yutaowan/test/pytorch/torch/csrc/api -I/localdisk2/yutaowan/test/pytorch/torch/csrc/api/include -I/localdisk2/yutaowan/test/pytorch/caffe2/aten/src/TH -I/localdisk2/yutaowan/test/pytorch/build/caffe2/aten/src/TH -I/localdisk2/yutaowan/test/pytorch/build/caffe2/aten/src -I/localdisk2/yutaowan/test/pytorch/build/caffe2/../aten/src -I/localdisk2/yutaowan/test/pytorch/torch/csrc -I/localdisk2/yutaowan/test/pytorch/third_party/miniz-2.1.0 -I/localdisk2/yutaowan/test/pytorch/third_party/kineto/libkineto/include -I/localdisk2/yutaowan/test/pytorch/third_party/kineto/libkineto/src -I/localdisk2/yutaowan/test/pytorch/aten/src/ATen/.. -I/localdisk2/yutaowan/test/pytorch/third_party/FXdiv/include -I/localdisk2/yutaowan/test/pytorch/c10/.. -I/localdisk2/yutaowan/test/pytorch/third_party/pthreadpool/include -I/localdisk2/yutaowan/test/pytorch/third_party/cpuinfo/include -I/localdisk2/yutaowan/test/pytorch/third_party/QNNPACK/include -I/localdisk2/yutaowan/test/pytorch/aten/src/ATen/native/quantized/cpu/qnnpack/include -I/localdisk2/yutaowan/test/pytorch/aten/src/ATen/native/quantized/cpu/qnnpack/src -I/localdisk2/yutaowan/test/pytorch/third_party/QNNPACK/deps/clog/include -I/localdisk2/yutaowan/test/pytorch/third_party/NNPACK/include -I/localdisk2/yutaowan/test/pytorch/third_party/fbgemm/include -I/localdisk2/yutaowan/test/pytorch/third_party/fbgemm -I/localdisk2/yutaowan/test/pytorch/third_party/fbgemm/third_party/asmjit/src -I/localdisk2/yutaowan/test/pytorch/third_party/ittapi/src/ittnotify -I/localdisk2/yutaowan/test/pytorch/third_party/FP16/include -I/localdisk2/yutaowan/test/pytorch/third_party/tensorpipe -I/localdisk2/yutaowan/test/pytorch/build/third_party/tensorpipe -I/localdisk2/yutaowan/test/pytorch/third_party/tensorpipe/third_party/libnop/include -I/localdisk2/yutaowan/test/pytorch/third_party/fmt/include -I/localdisk2/yutaowan/test/pytorch/build/third_party/ideep/mkl-dnn/include -I/localdisk2/yutaowan/test/pytorch/third_party/ideep/mkl-dnn/src/../include -I/localdisk2/yutaowan/test/pytorch/third_party/flatbuffers/include -isystem /localdisk2/yutaowan/test/pytorch/build/third_party/gloo -isystem /localdisk2/yutaowan/test/pytorch/cmake/../third_party/gloo -isystem /localdisk2/yutaowan/test/pytorch/cmake/../third_party/tensorpipe/third_party/libuv/include -isystem /localdisk2/yutaowan/test/pytorch/cmake/../third_party/googletest/googlemock/include -isystem /localdisk2/yutaowan/test/pytorch/cmake/../third_party/googletest/googletest/include -isystem /localdisk2/yutaowan/test/pytorch/third_party/protobuf/src -isystem /nfs/pdx/proj/icl/cmplrarch/comp/onemkl/lin/20240407_cev_nightly/include -isystem /localdisk2/yutaowan/test/pytorch/third_party/gemmlowp -isystem /localdisk2/yutaowan/test/pytorch/third_party/neon2sse -isystem /localdisk2/yutaowan/test/pytorch/third_party/XNNPACK/include -isystem /localdisk2/yutaowan/test/pytorch/third_party/ittapi/include -isystem /localdisk2/yutaowan/test/pytorch/cmake/../third_party/eigen -isystem /localdisk2/yutaowan/test/pytorch/third_party/ideep/mkl-dnn/include/oneapi/dnnl -isystem /localdisk2/yutaowan/test/pytorch/third_party/ideep/include -isystem /localdisk2/yutaowan/test/pytorch/build/include -isystem /nfs/pdx/proj/icl/rdrive/ref/mpi/lin/2021.12.0.272/intel64/include -D_GLIBCXX_USE_CXX11_ABI=1 -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -DNDEBUG -DUSE_KINETO -DTMP_LIBKINETO_NANOSECOND -DLIBKINETO_NOCUPTI -DLIBKINETO_NOROCTRACER -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DSYMBOLICATE_MOBILE_DEBUG_HANDLE -O2 -fPIC -Wall -Wextra -Werror=return-type -Werror=non-virtual-dtor -Werror=range-loop-construct -Werror=bool-operation -Wnarrowing -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-unused-parameter -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-stringop-overflow -Wsuggest-override -Wno-psabi -Wno-error=pedantic -Wno-error=old-style-cast -Wno-missing-braces -fdiagnostics-color=always -faligned-new -Werror -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Wno-stringop-overflow -DHAVE_AVX512_CPU_DEFINITION -DHAVE_AVX2_CPU_DEFINITION -O3 -DNDEBUG -DNDEBUG -std=gnu++17 -fPIC -DMKL_HAS_SBGEMM -DMKL_HAS_SHGEMM -DTORCH_USE_LIBUV -DCAFFE2_USE_GLOO -Wall -Wextra -Wdeprecated -Wno-unused-parameter -Wno-unused-function -Wno-missing-field-initializers -Wno-unknown-pragmas -Wno-type-limits -Wno-array-bounds -Wno-strict-overflow -Wno-strict-aliasing -Wno-maybe-uninitialized -fvisibility=hidden -O2 -pthread -DASMJIT_STATIC -fopenmp -MD -MT caffe2/CMakeFiles/torch_cpu.dir/__/aten/src/ATen/core/List.cpp.o -MF caffe2/CMakeFiles/torch_cpu.dir/__/aten/src/ATen/core/List.cpp.o.d -o caffe2/CMakeFiles/torch_cpu.dir/__/aten/src/ATen/core/List.cpp.o -c /localdisk2/yutaowan/test/pytorch/aten/src/ATen/core/List.cpp
In file included from /localdisk2/yutaowan/test/pytorch/aten/src/ATen/core/IListRef.h:631,
                 from /localdisk2/yutaowan/test/pytorch/aten/src/ATen/core/ivalue_inl.h:10,
                 from /localdisk2/yutaowan/test/pytorch/aten/src/ATen/core/ivalue.h:1549,
                 from /localdisk2/yutaowan/test/pytorch/aten/src/ATen/core/List_inl.h:4,
                 from /localdisk2/yutaowan/test/pytorch/aten/src/ATen/core/List.h:490,
                 from /localdisk2/yutaowan/test/pytorch/aten/src/ATen/core/List.cpp:1:
/localdisk2/yutaowan/test/pytorch/aten/src/ATen/core/IListRef_inl.h: In static member function ‘static c10::detail::IListRefConstRef<at::OptionalTensorRef> c10::detail::IListRefTagImpl<c10::IListRefTag::Boxed, at::OptionalTensorRef>::iterator_get(const c10::List<std::optional<at::Tensor> >::const_iterator&)’:
/localdisk2/yutaowan/test/pytorch/aten/src/ATen/core/IListRef_inl.h:171:17: error: possibly dangling reference to a temporary [-Werror=dangling-reference]
  171 |     const auto& ivalue = (*it).get();
      |                 ^~~~~~
/localdisk2/yutaowan/test/pytorch/aten/src/ATen/core/IListRef_inl.h:171:35: note: the temporary was destroyed at the end of the full expression ‘(& it)->c10::impl::ListIterator<std::optional<at::Tensor>, __gnu_cxx::__normal_iterator<c10::IValue*, std::vector<c10::IValue> > >::operator*().c10::impl::ListElementReference<std::optional<at::Tensor>, __gnu_cxx::__normal_iterator<c10::IValue*, std::vector<c10::IValue> > >::get()’
  171 |     const auto& ivalue = (*it).get();
      |                          ~~~~~~~~~^~
cc1plus: all warnings being treated as errors

```



### Versions

PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04 LTS (x86_64)
GCC version: (GCC) 13.2.0
Clang version: 14.0.0-1ubuntu1.1
CMake version: version 3.26.4
Libc version: glibc-2.35

Python version: 3.9.18 (main, Sep 11 2023, 13:41:44)  [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-101-generic-x86_64-with-glibc2.35
Is CUDA available: N/A
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      52 bits physical, 57 bits virtual
Byte Order:                         Little Endian
CPU(s):                             240
On-line CPU(s) list:                0-239
Vendor ID:                          GenuineIntel
Model name:                         Genuine Intel(R) CPU 0000%@
CPU family:                         6
Model:                              143
Thread(s) per core:                 2
Core(s) per socket:                 60
Socket(s):                          2
Stepping:                           8
CPU max MHz:                        3500.0000
CPU min MHz:                        800.0000
BogoMIPS:                           3200.00
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cat_l2 cdp_l3 invpcid_single intel_ppin cdp_l2 ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect avx_vnni avx512_bf16 wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b enqcmd fsrm md_clear serialize tsxldtrk pconfig arch_lbr amx_bf16 avx512_fp16 amx_tile amx_int8 flush_l1d arch_capabilities
Virtualization:                     VT-x
L1d cache:                          5.6 MiB (120 instances)
L1i cache:                          3.8 MiB (120 instances)
L2 cache:                           240 MiB (120 instances)
L3 cache:                           225 MiB (2 instances)
NUMA node(s):                       2
NUMA node0 CPU(s):                  0-59,120-179
NUMA node1 CPU(s):                  60-119,180-239
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Not affected
Vulnerability Retbleed:             Not affected
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] optree==0.11.0
[conda] numpy                     1.26.4                   pypi_0    pypi
[conda] optree                    0.11.0                   pypi_0    pypi


cc @malfet @seemethere