https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107200

            Bug ID: 107200
           Summary: False positive -Wdangling-pointer?
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: carlosgalvezp at gmail dot com
  Target Milestone: ---

Hi, 

The following code triggers a -Wdangling-pointer on GCC 12 and GCC 13:

#include <iostream>
#include <Eigen/Core>

Eigen::Vector2d foo() { 
    return {1.0, 2.0};
}

int main()
{
    auto x = 0.5 * foo();
    std::cout << x(0) << ", " << x(1) << std::endl;
}

<source>: In function 'int main()':
<source>:10:23: note: unnamed temporary defined here
   10 |     auto x = 0.5 * foo();
      |                    ~~~^~
In member function 'const Eigen::internal::scalar_product_op<LhsScalar,
RhsScalar>::result_type Eigen::internal::scalar_product_op<LhsScalar,
RhsScalar>::operator()(const LhsScalar&, const RhsScalar&) const [with
LhsScalar = double; RhsScalar = double]',
    inlined from
'Eigen::internal::binary_evaluator<Eigen::CwiseBinaryOp<BinaryOp, Lhs, Rhs>,
Eigen::internal::IndexBased, Eigen::internal::IndexBased>::CoeffReturnType
Eigen::internal::binary_evaluator<Eigen::CwiseBinaryOp<BinaryOp, Lhs, Rhs>,
Eigen::internal::IndexBased, Eigen::internal::IndexBased>::coeff(Eigen::Index)
const [with BinaryOp = Eigen::internal::scalar_product_op<double, double>; Lhs
= const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>,
const Eigen::Matrix<double, 2, 1> >; Rhs = const Eigen::Matrix<double, 2, 1>]'
at
/opt/compiler-explorer/libs/eigen/v3.3.9/Eigen/src/Core/CoreEvaluators.h:719:21,
    inlined from 'Eigen::DenseCoeffsBase<Derived, 0>::CoeffReturnType
Eigen::DenseCoeffsBase<Derived, 0>::coeff(Eigen::Index) const [with Derived =
Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<double, double>, const
Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const
Eigen::Matrix<double, 2, 1> >, const Eigen::Matrix<double, 2, 1> >]' at
/opt/compiler-explorer/libs/eigen/v3.3.9/Eigen/src/Core/DenseCoeffsBase.h:144:59,
    inlined from 'Eigen::DenseCoeffsBase<Derived, 0>::CoeffReturnType
Eigen::DenseCoeffsBase<Derived, 0>::operator()(Eigen::Index) const [with
Derived = Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<double,
double>, const
Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const
Eigen::Matrix<double, 2, 1> >, const Eigen::Matrix<double, 2, 1> >]' at
/opt/compiler-explorer/libs/eigen/v3.3.9/Eigen/src/Core/DenseCoeffsBase.h:181:19,
    inlined from 'int main()' at <source>:11:37:
/opt/compiler-explorer/libs/eigen/v3.3.9/Eigen/src/Core/functors/BinaryFunctors.h:86:128:
warning: using a dangling pointer to an unnamed temporary [-Wdangling-pointer=]
   86 |   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type operator()
(const LhsScalar& a, const RhsScalar& b) const { return a * b; }
      |     

Reproducible on Godbolt: https://godbolt.org/z/7475f7WvK

The warning goes away with this change:

-    auto x = 0.5 * foo();
+    Eigen::Vector2d x = 0.5 * foo();

What could be the reason for this behavior?

Thanks!

Reply via email to