-
-
Notifications
You must be signed in to change notification settings - Fork 660
fix: prohibit capturing variables with destructors in closures (fixes #21497 #18704) #21514
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
Open
gorsing
wants to merge
21
commits into
dlang:master
Choose a base branch
from
gorsing:checkClosure-destructor-check
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c8667f2
add ddoc for some func
gorsing c051b27
rewrite
gorsing 47e504a
fix after review
gorsing 9857a52
fix after review
gorsing 3ae0fa0
fix after review
gorsing 0d1d3b8
Merge branch 'dlang:master' into master
gorsing f64dcd4
Merge branch 'dlang:master' into master
gorsing c7f154f
Merge branch 'dlang:master' into master
gorsing 2b30d14
Merge branch 'dlang:master' into master
gorsing 668f726
refactor/irstate-attrs
gorsing dccd3a6
refactoring
gorsing 3a10958
add pragma(inline, true)
gorsing 3118f2f
change pragma(inline, true) -> pragma(inline, false)
gorsing 07ec4d6
Merge branch 'dlang:master' into master
gorsing 45ca953
Merge branch 'dlang:master' into master
gorsing 3f72b64
revent
gorsing 55cc867
Merge branch 'dlang:master' into master
gorsing 9d92140
Merge branch 'dlang:master' into master
gorsing 7a81c97
Merge branch 'dlang:master' into master
gorsing a13aba5
Merge branch 'dlang:master' into master
gorsing 1d31970
Fix && refactoring closure issue in appendSplitPath by marking array …
gorsing 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Test that capturing a class without destructor in a closure is allowed. | ||
*/ | ||
|
||
class C | ||
{ | ||
int x; | ||
} | ||
|
||
void main() | ||
{ | ||
auto obj = new C(); | ||
|
||
auto dg = () { return obj; }; // OK: no destructor | ||
} |
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,15 @@ | ||
/** | ||
* Test that capturing a local struct without destructor in a closure is allowed. | ||
*/ | ||
|
||
void main() | ||
{ | ||
struct Local | ||
{ | ||
int x; | ||
} | ||
|
||
Local s; | ||
|
||
auto dg = () { return s; }; // OK: no destructor | ||
} |
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,20 @@ | ||
/** | ||
* Test that capturing a variable without destructor in a closure inside a nested function is allowed. | ||
*/ | ||
|
||
struct S | ||
{ | ||
int x; | ||
} | ||
|
||
void main() | ||
{ | ||
S s; | ||
|
||
void nested() | ||
{ | ||
auto dg = () { return s; }; // OK: no destructor | ||
} | ||
|
||
nested(); | ||
} |
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,15 @@ | ||
/** | ||
* Test that capturing a struct without destructor in a closure is allowed. | ||
*/ | ||
|
||
struct NoDtor | ||
{ | ||
int x; | ||
} | ||
|
||
void main() | ||
{ | ||
NoDtor s; | ||
|
||
auto dg = () { return s; }; // OK: no destructor | ||
} |
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,19 @@ | ||
/** | ||
* Test that capturing `this` of a struct without destructor in a closure is allowed. | ||
*/ | ||
|
||
struct S | ||
{ | ||
int x; | ||
|
||
void foo() | ||
{ | ||
auto dg = () { return this; }; // OK: no destructor | ||
} | ||
} | ||
|
||
void main() | ||
{ | ||
S s; | ||
s.foo(); | ||
} |
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,22 @@ | ||
/* | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/closure_class_dtor.d(19): Error: scoped class variable `closure_class_dtor.main.obj` has destructor, cannot build closure | ||
--- | ||
*/ | ||
|
||
/** | ||
* Test that capturing a class with destructor in a closure is forbidden. | ||
*/ | ||
|
||
class C | ||
{ | ||
~this() {} | ||
} | ||
|
||
void main() | ||
{ | ||
scope obj = new C(); | ||
|
||
auto dg = () { return obj; }; // should be banned | ||
} |
23 changes: 23 additions & 0 deletions
23
compiler/test/fail_compilation/closure_local_struct_dtor.d
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,23 @@ | ||
/* | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/closure_local_struct_dtor.d(20): Error: variable `closure_local_struct_dtor.main.s` has scoped destruction, cannot build closure | ||
--- | ||
*/ | ||
|
||
/** | ||
* Test that capturing a local struct with destructor in a closure is forbidden. | ||
*/ | ||
|
||
void main() | ||
{ | ||
struct Local | ||
{ | ||
~this() {} | ||
int x; | ||
} | ||
|
||
Local s; | ||
|
||
auto dg = () { return s; }; // should be banned | ||
} |
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,27 @@ | ||
/* | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/closure_nested_func_dtor.d(19): Error: variable `closure_nested_func_dtor.main.s` has scoped destruction, cannot build closure | ||
--- | ||
*/ | ||
|
||
/** | ||
* Test that capturing a variable with destructor in a closure inside a nested function is forbidden. | ||
*/ | ||
|
||
struct S | ||
{ | ||
~this() {} | ||
} | ||
|
||
void main() | ||
{ | ||
S s; | ||
|
||
void nested() | ||
{ | ||
auto dg = () { return s; }; // should be banned | ||
} | ||
|
||
nested(); | ||
} |
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,26 @@ | ||
/* | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/closure_this_dtor.d(16): Error: variable `closure_this_dtor.S.foo.this` has scoped destruction, cannot build closure | ||
--- | ||
*/ | ||
|
||
/** | ||
* Test that capturing `this` of a struct with destructor in a closure is forbidden. | ||
*/ | ||
|
||
struct S | ||
{ | ||
~this() {} | ||
|
||
void foo() | ||
{ | ||
auto dg = () { return this; }; // should be banned | ||
} | ||
} | ||
|
||
void main() | ||
{ | ||
S s; | ||
s.foo(); | ||
} |
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,22 @@ | ||
/* | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/closure_with_dtor.d(19): Error: variable `closure_with_dtor.main.s` has scoped destruction, cannot build closure | ||
--- | ||
*/ | ||
|
||
/** | ||
* Test that capturing a struct with destructor in a closure is forbidden. | ||
*/ | ||
|
||
struct S | ||
{ | ||
~this() {} | ||
} | ||
|
||
void main() | ||
{ | ||
S s; | ||
|
||
auto dg = () { return s; }; // should be banned | ||
} | ||
Oops, something went wrong.
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.
FAOD, in all these examples, none of the closures escape, so calling dtor on
s
at the end of scope is correct behaviour and should remain valid.