Skip to content

[Clang] Remove explicit object from non member function. #148807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2025

Conversation

cor3ntin
Copy link
Contributor

@cor3ntin cor3ntin commented Jul 15, 2025

To avoid crashing later (as we assume only member functions can have explicit object parameters)

Fixes #113185

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 15, 2025

@llvm/pr-subscribers-clang

Author: Corentin Jabot (cor3ntin)

Changes

To avoid crashing later (as we assume only member functions can have explicit parameters)

Fixes #113185


Full diff: https://github.com/llvm/llvm-project/pull/148807.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaType.cpp (+3-1)
  • (modified) clang/test/SemaCXX/cxx2b-deducing-this.cpp (+10)
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 661746731fdcc..bb114aff2366b 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -4860,7 +4860,9 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
             S.Diag(First->getBeginLoc(),
                    diag::err_explicit_object_parameter_invalid)
                 << First->getSourceRange();
-
+          // Do let non-member function have explicit parameters
+          // to not break assumptions elsewhere in the code.
+          First->setExplicitObjectParameterLoc(SourceLocation());
           D.setInvalidType();
           AreDeclaratorChunksValid = false;
         }
diff --git a/clang/test/SemaCXX/cxx2b-deducing-this.cpp b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
index 6987d0c020457..2253cbb26285e 100644
--- a/clang/test/SemaCXX/cxx2b-deducing-this.cpp
+++ b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
@@ -1347,3 +1347,13 @@ int main() {
     // expected-note@#S3-f-cand2 {{candidate function not viable: no known conversion from 'S3' to 'int' for object argument}}
 }
 }
+
+namespace GH113185 {
+
+void Bar(this int) { // expected-note {{candidate function}}
+    // expected-error@-1 {{an explicit object parameter cannot appear in a non-member function}}
+    Bar(0);
+    Bar(); // expected-error {{no matching function for call to 'Bar'}}
+}
+
+}

To avoid crashing later (as we assume only member functions
can have explicit parameters)

Fixes llvm#113185
@cor3ntin cor3ntin force-pushed the corentin/gh113185 branch from bf204f0 to 25d5589 Compare July 15, 2025 09:10
@cor3ntin cor3ntin merged commit 57d81c2 into llvm:main Jul 15, 2025
10 checks passed

// Do let non-member function have explicit parameters
// to not break assumptions elsewhere in the code.
First->setExplicitObjectParameterLoc(SourceLocation());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize this is invalid and will get diagnosed as so but don't we have the source location?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
4 participants