arduino / arduino-cli

Arduino command line tool
https://arduino.github.io/arduino-cli/latest/
GNU General Public License v3.0
4.24k stars 371 forks source link

Due SAM: The compiler attempts to instantiate a mismatched template specialization #2537

Open Silver-Fang opened 4 months ago

Silver-Fang commented 4 months ago

Describe the problem

C:\Users\vhtmf\Documents\Arduino\sketch_feb10a\sketch_feb10a.ino: In instantiation of 'constexpr const bool _Allow_inheriting_unwrap<unsigned int*, void>::value':
C:\Users\vhtmf\Documents\Arduino\sketch_feb10a\sketch_feb10a.ino:22:70:   required from here
C:\Users\vhtmf\Documents\Arduino\sketch_feb10a\sketch_feb10a.ino:19:25: error: 'unsigned int*' is not a class, struct, or union type
   static constexpr bool value = is_same<_Iter, typename _Iter::_Prevent_inheriting_unwrap>::value;
                         ^

exit status 1

Compilation error: 'unsigned int*' is not a class, struct, or union type

To reproduce

template<class, class>
struct is_same {
  static constexpr bool value = false;  // determine whether arguments are the same type
};
template<class _Ty>
struct is_same<_Ty, _Ty> {
  static constexpr bool value = true;
};

template<class... _Types>
using void_t = void;

template<class _Iter, class = void>
struct _Allow_inheriting_unwrap {
  static constexpr bool value = true;
};
template<class _Iter>
struct _Allow_inheriting_unwrap<_Iter, void_t<typename _Iter::_Prevent_inheriting_unwrap>> {
  static constexpr bool value = is_same<_Iter, typename _Iter::_Prevent_inheriting_unwrap>::value;
};
void setup() {
  constexpr auto BB = _Allow_inheriting_unwrap<unsigned int*, void>::value;
}

void loop() {
  // put your main code here, to run repeatedly:
}

Expected behavior

This code can be compiled in both MSVC and Godbolt. Even in the Arduino IDE, the value of constexpr auto BB is correctly displayed as bool true. But once it enters the compile phase, the compiler mistakenly tries to match the specialized _Allow_inheriting_unwrap and then fails because unsigned int * has no members to reference.

Arduino CLI version

0.35.2

Operating system

Windows

Operating system version

Windows 11

Additional context

This issue only appears on the Arduino Due SAM platform. The AVR platform is normal.

Issue checklist

Silver-Fang commented 4 months ago

This may indicate a problem with the compiler's handling of SFINAE.

Silver-Fang commented 4 months ago

At present, I need to do the following workaround for void_t:

template<class... _Types>
struct void_s {
  using type = void;
};
template<class T, class... _Types>
struct void_s<T, _Types...> {
  using type = T;
};
template<typename... _Types>
using void_t = typename void_s<_Types...>::type;

If anyone else is having a similar problem, try my workaround idea.