[clang] [Sema] Fix parameter index checks on explicit object member functions (PR #165586)

Vladimir Vuksanovic via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 30 05:59:35 PDT 2025


================
@@ -11,6 +11,11 @@ struct S {
 
   void h(const char*) __attribute__((nonnull(1))); // \
       expected-error{{invalid for the implicit this argument}}
+
+  void i(this S* self, const char*) __attribute__((nonnull(1)));
+
+  void j(this S* self, const char*) __attribute__((nonnull(3))); // \
+      expected-error{{'nonnull' attribute parameter 1 is out of bounds}}
----------------
vvuksanovic wrote:

> That’s assuming we don’t have an off-by-one error in the index, which is what the test is for ;Þ

Ah, I thought you were talking about adding a second index to `nonnull`, not changing the existing one.

Added the suggested test.

> I don’t think it makes sense to disallow `nonnull` for explicit object parameters; to my understanding, something like this would be valid, if a bit weird:
> 
> ```c++
> struct S {
>     void f(this int* x);
>     operator int*() { return nullptr; }
> };
> 
> 
> void f() {
>     S().f(); // Weird, but ok.
> }
> ```

Right, thanks!

https://github.com/llvm/llvm-project/pull/165586


More information about the cfe-commits mailing list