]> granicus.if.org Git - clang/commitdiff
Make integral-o-pointer conversions in SFINAE illegal.
authorErich Keane <erich.keane@intel.com>
Thu, 17 Jan 2019 23:11:15 +0000 (23:11 +0000)
committerErich Keane <erich.keane@intel.com>
Thu, 17 Jan 2019 23:11:15 +0000 (23:11 +0000)
As reported in PR40362, allowing the conversion from an integral to a
pointer type (despite being illegal in the C++ standard) will cause
surprsing results when testing for certain behaviors in SFINAE.  This
patch converts the error to a SFINAE Error and adds a test to ensure
that it is still a warning in non-SFINAE but an error in it.

Change-Id: I1f475637fa4d83217ae37dc6b5dbf653e118fae4

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@351495 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
test/SemaCXX/int-ptr-cast-SFINAE.cpp [new file with mode: 0644]

index a1029a22eb4481b94aa3fd625ee52ff3df25268e..05e2d2dec2deaf7294ba581db0acffe32e979427 100644 (file)
@@ -6817,7 +6817,7 @@ def ext_typecheck_convert_int_pointer : ExtWarn<
   "; take the address with &|"
   "; remove *|"
   "; remove &}3">,
-  InGroup<IntConversion>;
+  InGroup<IntConversion>, SFINAEFailure;
 def ext_typecheck_convert_pointer_void_func : Extension<
   "%select{%diff{assigning to $ from $|assigning to different types}0,1"
   "|%diff{passing $ to parameter of type $|"
diff --git a/test/SemaCXX/int-ptr-cast-SFINAE.cpp b/test/SemaCXX/int-ptr-cast-SFINAE.cpp
new file mode 100644 (file)
index 0000000..1366eb4
--- /dev/null
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++17
+
+void foo(int* a, int *b) {
+  a -= b; // expected-warning {{incompatible integer to pointer conversion assigning to 'int *' from 'long'}}
+}
+
+template<typename T> T declval();
+struct true_type { static const bool value = true; };
+struct false_type { static const bool value = false; };
+template<bool, typename T, typename U> struct select { using type = T; };
+template<typename T, typename U> struct select<false, T, U> { using type = U; };
+
+
+template<typename T>
+typename select<(sizeof(declval<T>() -= declval<T>(), 1) != 1), true_type, false_type>::type test(...);
+template<typename T> false_type test(...);
+
+template<typename T>
+static const auto has_minus_assign = decltype(test<T>())::value;
+
+static_assert(has_minus_assign<int*>, "failed"); // expected-error {{static_assert failed due to requirement 'has_minus_assign<int *>' "failed"}}