-
Notifications
You must be signed in to change notification settings - Fork 1.8k
C#: C#9 Add target typed conditional tests #4614
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
tamasvajk
merged 2 commits into
github:main
from
tamasvajk:feature/csharp9-target-typed
Nov 27, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
public class TargetType | ||
{ | ||
public void M2() | ||
{ | ||
var rand = new Random(); | ||
var condition = rand.NextDouble() > 0.5; | ||
|
||
int? x0 = 12; | ||
x0 = 13; | ||
int? x1 = null; | ||
|
||
int? x2 = condition | ||
? 12 | ||
: null; | ||
|
||
int? x3 = condition | ||
? (int?)12 | ||
: null; | ||
|
||
int? x4 = condition | ||
? 12 | ||
: (int?)null; | ||
|
||
IEnumerable<int> xs0 = new List<int>() { 0, 1 }; | ||
IEnumerable<int> xs1 = new int[] { 2, 3 }; | ||
|
||
IEnumerable<int> xs2 = x2 is null | ||
? new List<int>() { 0, 1 } | ||
: new int[] { 2, 3 }; | ||
|
||
int? c = condition | ||
? new TargetType() | ||
: 12; | ||
} | ||
|
||
public static implicit operator int(TargetType d) => 0; | ||
} |
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,14 @@ | ||
conditional | ||
| TargetType.cs:16:19:18:18 | ... ? ... : ... | int? | int? | null | | ||
| TargetType.cs:20:19:22:18 | ... ? ... : ... | int? | int? | null | | ||
| TargetType.cs:24:19:26:24 | ... ? ... : ... | int? | int? | int? | | ||
| TargetType.cs:31:32:33:32 | ... ? ... : ... | IEnumerable<Int32> | List<Int32> | Int32[] | | ||
| TargetType.cs:35:18:37:16 | ... ? ... : ... | int | int | int | | ||
implicitCasts | ||
| TargetType.cs:12:19:12:20 | (...) ... | int? | int | | ||
| TargetType.cs:13:14:13:15 | (...) ... | int? | int | | ||
| TargetType.cs:17:15:17:16 | (...) ... | int? | int | | ||
| TargetType.cs:25:15:25:16 | (...) ... | int? | int | | ||
| TargetType.cs:35:18:37:16 | (...) ... | int? | int | | ||
implicitConversions | ||
| TargetType.cs:36:15:36:30 | call to operator implicit conversion | int | TargetType | |
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 @@ | ||
import csharp | ||
|
||
query predicate conditional(ConditionalExpr expr, string t, string t1, string t2) { | ||
expr.getType().toStringWithTypes() = t and | ||
expr.getThen().getType().toStringWithTypes() = t1 and | ||
expr.getElse().getType().toStringWithTypes() = t2 | ||
} | ||
|
||
query predicate implicitCasts(CastExpr expr, string type, string childType) { | ||
expr.getFile().getStem() = "TargetType" and | ||
expr.fromSource() and | ||
not exists(expr.getTypeAccess()) and | ||
type = expr.getType().toStringWithTypes() and | ||
childType = expr.getExpr().getType().toStringWithTypes() | ||
} | ||
|
||
query predicate implicitConversions(OperatorCall opCall, string type, string childType) { | ||
opCall.getFile().getStem() = "TargetType" and | ||
opCall.fromSource() and | ||
opCall.getTarget().getFunctionName() = "op_Implicit" and | ||
opCall.getTarget().getReturnType().toStringWithTypes() = type and | ||
opCall.getAnArgument().getType().toStringWithTypes() = childType | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.