-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] static operators should evaluate object argument (reland) #80108
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
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
0327626
[clang] static operators should evaluate object argument
SuperSodaSea 63a3627
Deal with static operator in EmitCall
SuperSodaSea 1269bc3
Apply suggestions from cor3ntin
SuperSodaSea 755bcaa
Minor changes
SuperSodaSea 12d3ea2
Apply suggestions from shafik
SuperSodaSea e725a8f
Ignore `this` in constexpr evaluation
SuperSodaSea 441ca98
Merge remote-tracking branch 'upstream/main' into fix/static-operator
SuperSodaSea 5accc5d
Update clang/docs/ReleaseNotes.rst
SuperSodaSea 389d6d4
Merge remote-tracking branch 'upstream/main' into fix/static-operator
SuperSodaSea 6bcc590
Update ast-dump-static-operators.cpp
SuperSodaSea ab1dc2c
Merge branch 'main' into fix/static-operator
SuperSodaSea eb42407
Should work with const / volatile
SuperSodaSea fa85d87
Format code
SuperSodaSea 3e1e4cd
Merge remote-tracking branch 'upstream/main' into fix/static-operator
SuperSodaSea 9a724c9
Merge branch 'main' into fix/static-operator
SuperSodaSea c679700
Update clang/docs/ReleaseNotes.rst
SuperSodaSea d3edbd1
Update clang/docs/ReleaseNotes.rst
SuperSodaSea 490fd0f
Apply suggestions from @cor3ntin
SuperSodaSea 93e647c
Merge remote-tracking branch 'upstream/main' into fix/static-operator
SuperSodaSea 94195a2
Update wording
SuperSodaSea a141494
Make CI happy
SuperSodaSea fdfa350
Merge remote-tracking branch 'upstream/main' into fix/static-operator
SuperSodaSea 08208f3
Merge branch 'main' into fix/static-operator
SuperSodaSea 62b5040
Merge branch 'main' into fix/static-operator
SuperSodaSea 497738f
Merge branch 'main' into fix/static-operator
SuperSodaSea b389560
Merge branch 'main' into fix/static-operator
SuperSodaSea 6aafb82
Merge branch 'main' into fix/static-operator
SuperSodaSea ca4c70c
Merge branch 'main' into fix/static-operator
SuperSodaSea 08b43d2
Merge branch 'main' into fix/static-operator
SuperSodaSea 22be6a2
Merge branch 'main' into fix/static-operator
SuperSodaSea 5c3dfb3
Apply suggestion from @AaronBallman
SuperSodaSea c560b38
Apply suggestion from @AaronBallman
SuperSodaSea 1ceaae4
Use upper case
SuperSodaSea dea08ec
Fix clangd test break
SuperSodaSea 910ae40
Apply suggestion from @zyn0217
SuperSodaSea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// RUN: %clang_cc1 -std=c++23 %s -ast-dump -triple x86_64-unknown-unknown -o - | FileCheck -strict-whitespace %s | ||
|
||
struct Functor { | ||
static int operator()(int x, int y) { | ||
return x + y; | ||
} | ||
static int operator[](int x, int y) { | ||
return x + y; | ||
} | ||
}; | ||
|
||
Functor& get_functor() { | ||
static Functor functor; | ||
return functor; | ||
} | ||
|
||
void call_static_operators() { | ||
Functor functor; | ||
|
||
int z1 = functor(1, 2); | ||
// CHECK: CXXOperatorCallExpr {{.*}} 'int' '()' | ||
// CHECK-NEXT: |-ImplicitCastExpr {{.*}} <col:19, col:24> 'int (*)(int, int)' <FunctionToPointerDecay> | ||
// CHECK-NEXT: | `-DeclRefExpr {{.*}} <col:19, col:24> 'int (int, int)' lvalue CXXMethod {{.*}} 'operator()' 'int (int, int)' | ||
// CHECK-NEXT: |-DeclRefExpr {{.*}} <col:12> 'Functor' lvalue Var {{.*}} 'functor' 'Functor' | ||
// CHECK-NEXT: |-IntegerLiteral {{.*}} <col:20> 'int' 1 | ||
// CHECK-NEXT: `-IntegerLiteral {{.*}} <col:23> 'int' 2 | ||
|
||
int z2 = functor[1, 2]; | ||
// CHECK: CXXOperatorCallExpr {{.*}} 'int' '[]' | ||
// CHECK-NEXT: |-ImplicitCastExpr {{.*}} <col:19, col:24> 'int (*)(int, int)' <FunctionToPointerDecay> | ||
// CHECK-NEXT: | `-DeclRefExpr {{.*}} <col:19, col:24> 'int (int, int)' lvalue CXXMethod {{.*}} 'operator[]' 'int (int, int)' | ||
// CHECK-NEXT: |-DeclRefExpr {{.*}} <col:12> 'Functor' lvalue Var {{.*}} 'functor' 'Functor' | ||
// CHECK-NEXT: |-IntegerLiteral {{.*}} <col:20> 'int' 1 | ||
// CHECK-NEXT: `-IntegerLiteral {{.*}} <col:23> 'int' 2 | ||
|
||
int z3 = get_functor()(1, 2); | ||
// CHECK: CXXOperatorCallExpr {{.*}} 'int' '()' | ||
// CHECK-NEXT: |-ImplicitCastExpr {{.*}} <col:25, col:30> 'int (*)(int, int)' <FunctionToPointerDecay> | ||
// CHECK-NEXT: | `-DeclRefExpr {{.*}} <col:25, col:30> 'int (int, int)' lvalue CXXMethod {{.*}} 'operator()' 'int (int, int)' | ||
// CHECK-NEXT: |-CallExpr {{.*}} <col:12, col:24> 'Functor' lvalue | ||
// CHECK-NEXT: | `-ImplicitCastExpr {{.*}} <col:12> 'Functor &(*)()' <FunctionToPointerDecay> | ||
// CHECK-NEXT: | `-DeclRefExpr {{.*}} <col:12> 'Functor &()' lvalue Function {{.*}} 'get_functor' 'Functor &()' | ||
// CHECK-NEXT: |-IntegerLiteral {{.*}} <col:26> 'int' 1 | ||
// CHECK-NEXT: `-IntegerLiteral {{.*}} <col:29> 'int' 2 | ||
|
||
int z4 = get_functor()[1, 2]; | ||
// CHECK: CXXOperatorCallExpr {{.*}} 'int' '[]' | ||
// CHECK-NEXT: |-ImplicitCastExpr {{.*}} <col:25, col:30> 'int (*)(int, int)' <FunctionToPointerDecay> | ||
// CHECK-NEXT: | `-DeclRefExpr {{.*}} <col:25, col:30> 'int (int, int)' lvalue CXXMethod {{.*}} 'operator[]' 'int (int, int)' | ||
// CHECK-NEXT: |-CallExpr {{.*}} <col:12, col:24> 'Functor' lvalue | ||
// CHECK-NEXT: | `-ImplicitCastExpr {{.*}} <col:12> 'Functor &(*)()' <FunctionToPointerDecay> | ||
// CHECK-NEXT: | `-DeclRefExpr {{.*}} <col:12> 'Functor &()' lvalue Function {{.*}} 'get_functor' 'Functor &()' | ||
// CHECK-NEXT: |-IntegerLiteral {{.*}} <col:26> 'int' 1 | ||
// CHECK-NEXT: `-IntegerLiteral {{.*}} <col:29> 'int' 2 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++23 %s | ||
|
||
// expected-no-diagnostics | ||
|
||
namespace A { | ||
|
||
struct Foo { | ||
static int operator()(int a, int b) { return a + b; } | ||
static int operator[](int a, int b) { return a + b; } | ||
}; | ||
|
||
void ok() { | ||
// Should pass regardless of const / volatile | ||
Foo foo; | ||
foo(1, 2); | ||
foo[1, 2]; | ||
|
||
const Foo fooC; | ||
fooC(1, 2); | ||
fooC[1, 2]; | ||
|
||
const Foo fooV; | ||
fooV(1, 2); | ||
fooV[1, 2]; | ||
|
||
const volatile Foo fooCV; | ||
fooCV(1, 2); | ||
fooCV[1, 2]; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don;t understand where the change is.
isInstance
is implied byhasCXXExplicitFunctionObjectParameter
so isn't that change doing... nothing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IsFunctor
can be true whenisInstance()
is false, this is the situation (static functor) we are dealing with.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://github.com/llvm/llvm-project/pull/68485/files#diff-19c518dbc68b30c66e1a2b6bd523c005fb2050dcf1a0e92305df7ab3e1b9e9f3L15744-L15751. The patch actually changes the behavior when we put an implied object argument. (I was replying at wrong place in that PR...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we may have an implied object argument for static operator() calls without an explicit object parameter, while it was only applied previously to non-static (
isInstance()
) operator() calls.