llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.91k stars 11.52k forks source link

`auto` in lambda argument fails without warnings #22883

Open eschnett opened 9 years ago

eschnett commented 9 years ago
Bugzilla Link 22509
Version 3.5
OS MacOS X

Extended Description

In the code below, I create a shared_ptr<double> that is ultimately supposed to hold the value -1.0, yet the actual value is 1.84467e+19. This looks to me as if either wrong code is generated, or as if a size_t value is accidentally interpreted as double.

The code uses auto to declare arguments for a lambda expression. If I replace these auto manually by size_t -- which are they types that should be deduced -- the code works fine.

Strangely, the code builds without warnings. If a size_t is accidentally interpreted as double, I would have expected a warning.

I assume that the deduction of the auto parameters is not working correctly. I may be wrong about this; in this case, I would expect a warning from the compiler about mismatching types.

I am using:

$ clang++ --version
clang version 3.5.1 (tags/RELEASE_351/final)
Target: x86_64-apple-darwin14.1.0
Thread model: posix

I build with:

$ clang++ -std=c++14 -g -Wall -o lambda-auto lambda-auto.cc

The code is:

#include <iostream>
#include <memory>
#include <utility>

template <typename> struct is_shared_ptr : std::false_type {};
template <typename T>
struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};

template <template <typename> class C, typename F, typename... Args,
          typename R = std::result_of_t<F(std::size_t, Args...)>,
          std::enable_if_t<is_shared_ptr<C<R>>::value> * = nullptr>
auto iota(F &&f, std::size_t s, Args &&... args) {
  return std::make_shared<R>(
      std::forward<F>(f)(std::size_t(0), std::forward<Args>(args)...));
}

int main(int argc, char **argv) {
  auto r = iota<std::shared_ptr>([](auto x, auto y) { return double(x + y); },
                                 std::size_t(1), std::size_t(-1));
  static_assert(std::is_same<decltype(r), std::shared_ptr<double>>::value, "");

  std::cout << "*r=" << *r << "\n";
  return 0;
}
llvmbot commented 1 month ago

@llvm/issue-subscribers-c-1

Author: Erik Schnetter (eschnett)

| | | | --- | --- | | Bugzilla Link | [22509](https://llvm.org/bz22509) | | Version | 3.5 | | OS | MacOS X | ## Extended Description In the code below, I create a `shared_ptr<double>` that is ultimately supposed to hold the value `-1.0`, yet the actual value is `1.84467e+19`. This looks to me as if either wrong code is generated, or as if a `size_t` value is accidentally interpreted as double. The code uses `auto` to declare arguments for a lambda expression. If I replace these `auto` manually by `size_t` -- which are they types that should be deduced -- the code works fine. Strangely, the code builds without warnings. If a `size_t` is accidentally interpreted as double, I would have expected a warning. I assume that the deduction of the auto parameters is not working correctly. I may be wrong about this; in this case, I would expect a warning from the compiler about mismatching types. I am using: ```console $ clang++ --version clang version 3.5.1 (tags/RELEASE_351/final) Target: x86_64-apple-darwin14.1.0 Thread model: posix ``` I build with: ```console $ clang++ -std=c++14 -g -Wall -o lambda-auto lambda-auto.cc ``` The code is: ```cpp #include <iostream> #include <memory> #include <utility> template <typename> struct is_shared_ptr : std::false_type {}; template <typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {}; template <template <typename> class C, typename F, typename... Args, typename R = std::result_of_t<F(std::size_t, Args...)>, std::enable_if_t<is_shared_ptr<C<R>>::value> * = nullptr> auto iota(F &&f, std::size_t s, Args &&... args) { return std::make_shared<R>( std::forward<F>(f)(std::size_t(0), std::forward<Args>(args)...)); } int main(int argc, char **argv) { auto r = iota<std::shared_ptr>([](auto x, auto y) { return double(x + y); }, std::size_t(1), std::size_t(-1)); static_assert(std::is_same<decltype(r), std::shared_ptr<double>>::value, ""); std::cout << "*r=" << *r << "\n"; return 0; } ```
llvmbot commented 1 month ago

@llvm/issue-subscribers-clang-frontend

Author: Erik Schnetter (eschnett)

| | | | --- | --- | | Bugzilla Link | [22509](https://llvm.org/bz22509) | | Version | 3.5 | | OS | MacOS X | ## Extended Description In the code below, I create a `shared_ptr<double>` that is ultimately supposed to hold the value `-1.0`, yet the actual value is `1.84467e+19`. This looks to me as if either wrong code is generated, or as if a `size_t` value is accidentally interpreted as double. The code uses `auto` to declare arguments for a lambda expression. If I replace these `auto` manually by `size_t` -- which are they types that should be deduced -- the code works fine. Strangely, the code builds without warnings. If a `size_t` is accidentally interpreted as double, I would have expected a warning. I assume that the deduction of the auto parameters is not working correctly. I may be wrong about this; in this case, I would expect a warning from the compiler about mismatching types. I am using: ```console $ clang++ --version clang version 3.5.1 (tags/RELEASE_351/final) Target: x86_64-apple-darwin14.1.0 Thread model: posix ``` I build with: ```console $ clang++ -std=c++14 -g -Wall -o lambda-auto lambda-auto.cc ``` The code is: ```cpp #include <iostream> #include <memory> #include <utility> template <typename> struct is_shared_ptr : std::false_type {}; template <typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {}; template <template <typename> class C, typename F, typename... Args, typename R = std::result_of_t<F(std::size_t, Args...)>, std::enable_if_t<is_shared_ptr<C<R>>::value> * = nullptr> auto iota(F &&f, std::size_t s, Args &&... args) { return std::make_shared<R>( std::forward<F>(f)(std::size_t(0), std::forward<Args>(args)...)); } int main(int argc, char **argv) { auto r = iota<std::shared_ptr>([](auto x, auto y) { return double(x + y); }, std::size_t(1), std::size_t(-1)); static_assert(std::is_same<decltype(r), std::shared_ptr<double>>::value, ""); std::cout << "*r=" << *r << "\n"; return 0; } ```
Endilll commented 1 month ago

@EugeneZelenko clang:to-be-triaged exists so that I don't flood people who are watching clang:frontend and clang:diagnostics with bugs where I haven't done any analysis on. Please refrain from relabeling them.

EugeneZelenko commented 1 month ago

@Endilll: Sure.

Endilll commented 1 month ago

Reproduces with Clang 18.1 and 20.0: https://godbolt.org/z/Yfj4Y4GG1

cor3ntin commented 1 month ago

No lambda related bug, the code boils down to https://godbolt.org/z/hsd96dMvT Should we emit a conversion warning for what is effectively a cast?