TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
non_constant_p, overflow_p);
+ /* Check we aren't dereferencing a null pointer when calling a non-static
+ member function, which is undefined behaviour. */
+ if (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
+ && integer_zerop (arg)
+ /* But ignore calls from within compiler-generated code, to handle
+ cases like lambda function pointer conversion operator thunks
+ which pass NULL as the 'this' pointer. */
+ && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
+ {
+ if (!ctx->quiet)
+ error_at (cp_expr_loc_or_input_loc (x),
+ "dereferencing a null pointer");
+ *non_constant_p = true;
+ }
/* Don't VERIFY_CONSTANT here. */
if (*non_constant_p && ctx->quiet)
break;
--- /dev/null
+// PR c++/102420
+// { dg-do compile { target c++11 } }
+
+struct X {
+ constexpr int f() { return 0; }
+};
+constexpr int g(X* x) {
+ return x->f(); // { dg-error "dereferencing a null pointer" }
+}
+constexpr int t = g(nullptr); // { dg-message "in .constexpr. expansion" }