-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Extended property patterns: address PROTOTYPE comments #53594
Merged
jcouv
merged 14 commits into
dotnet:features/extended-property-patterns
from
jcouv:property-patterns
May 27, 2021
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
95a950f
Extended property patterns: address PROTOTYPE comments
jcouv 062fff4
formatting
jcouv f5ca3bd
Adjust IOperation and fix regressions
jcouv a08ee8f
Learn from pure null tests on extended properties early in switches
jcouv 2ae8251
Use binding error instead
jcouv ee7416a
Fix tests
jcouv f057729
Fix flow analysis issue
jcouv e577d0e
tweak name
jcouv 6945eb1
Address feedback
jcouv 715546c
Address feedback
jcouv 44c1373
Address feedback
jcouv 717e253
Address feedback from Cyrus
jcouv 60393b0
Use a theory
jcouv ae4ea73
Address feedback from Ali
jcouv 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 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 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 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 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 |
---|---|---|
|
@@ -200,6 +200,12 @@ internal BoundPattern BindConstantPatternWithFallbackToTypePattern( | |
bool hasErrors, | ||
BindingDiagnosticBag diagnostics) | ||
{ | ||
if (hasSuppression(expression)) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_IllegalSuppression, expression.Location); | ||
hasErrors = true; | ||
} | ||
|
||
ExpressionSyntax innerExpression = SkipParensAndNullSuppressions(expression); | ||
if (innerExpression.Kind() == SyntaxKind.DefaultLiteralExpression) | ||
{ | ||
|
@@ -222,6 +228,23 @@ internal BoundPattern BindConstantPatternWithFallbackToTypePattern( | |
bool isExplicitNotNullTest = boundType.Type.SpecialType == SpecialType.System_Object; | ||
return new BoundTypePattern(node, boundType, isExplicitNotNullTest, inputType, boundType.Type, hasErrors); | ||
} | ||
|
||
static bool hasSuppression(ExpressionSyntax e) | ||
{ | ||
while (true) | ||
{ | ||
switch (e) | ||
{ | ||
case ParenthesizedExpressionSyntax p: | ||
e = p.Expression; | ||
break; | ||
case PostfixUnaryExpressionSyntax { RawKind: (int)SyntaxKind.SuppressNullableWarningExpression }: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private ExpressionSyntax SkipParensAndNullSuppressions(ExpressionSyntax e) | ||
|
@@ -777,10 +800,12 @@ deconstructMethod is null && | |
for (int i = 0; i < node.Subpatterns.Count; i++) | ||
{ | ||
var subPattern = node.Subpatterns[i]; | ||
// If the expression before the colon isn't just a name, we'll have reported a parsing error. | ||
Debug.Assert(subPattern.NameColon is not null || subPattern.ExpressionColon is null || subPattern.ExpressionColon.HasErrors); | ||
|
||
bool isError = hasErrors || outPlaceholders.IsDefaultOrEmpty || i >= outPlaceholders.Length; | ||
TypeSymbol elementType = isError ? CreateErrorType() : outPlaceholders[i].Type; | ||
ParameterSymbol? parameter = null; | ||
// PROTOTYPE(extended-property-patterns) ExpressionColon | ||
if (subPattern.NameColon != null && !isError) | ||
{ | ||
// Check that the given name is the same as the corresponding parameter of the method. | ||
|
@@ -819,7 +844,9 @@ private void BindITupleSubpatterns( | |
var objectType = Compilation.GetSpecialType(SpecialType.System_Object); | ||
foreach (var subpatternSyntax in node.Subpatterns) | ||
{ | ||
// PROTOTYPE(extended-property-patterns) ExpressionColon | ||
// If the expression before the colon isn't just a name, we'll have reported a parsing error. | ||
Debug.Assert(subpatternSyntax.NameColon is not null || subpatternSyntax.ExpressionColon is null || subpatternSyntax.ExpressionColon.HasErrors); | ||
|
||
if (subpatternSyntax.NameColon != null) | ||
{ | ||
// error: name not permitted in ITuple deconstruction | ||
|
@@ -873,10 +900,12 @@ private void BindValueTupleSubpatterns( | |
for (int i = 0; i < node.Subpatterns.Count; i++) | ||
{ | ||
var subpatternSyntax = node.Subpatterns[i]; | ||
// If the expression before the colon isn't just a name, we'll have reported a parsing error. | ||
Debug.Assert(subpatternSyntax.NameColon is not null || subpatternSyntax.ExpressionColon is null || subpatternSyntax.ExpressionColon.HasErrors); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When |
||
|
||
bool isError = i >= elementTypesWithAnnotations.Length; | ||
TypeSymbol elementType = isError ? CreateErrorType() : elementTypesWithAnnotations[i].Type; | ||
FieldSymbol? foundField = null; | ||
// PROTOTYPE(extended-property-patterns) ExpressionColon | ||
if (subpatternSyntax.NameColon != null && !isError) | ||
{ | ||
string name = subpatternSyntax.NameColon.Name.Identifier.ValueText; | ||
|
This file contains 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 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 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 |
---|---|---|
|
@@ -1580,87 +1580,93 @@ public override void VisitPattern(BoundPattern pattern) | |
base.VisitPattern(pattern); | ||
var whenFail = StateWhenFalse; | ||
SetState(StateWhenTrue); | ||
AssignPatternVariables(pattern); | ||
assignPatternVariablesAndMarkReadProperties(pattern); | ||
SetConditionalState(this.State, whenFail); | ||
} | ||
|
||
/// <summary> | ||
/// Find the pattern variables of the pattern, and make them definitely assigned if <paramref name="definitely"/>. | ||
/// That would be false under "not" and "or" patterns. | ||
/// </summary> | ||
private void AssignPatternVariables(BoundPattern pattern, bool definitely = true) | ||
{ | ||
switch (pattern.Kind) | ||
// Find the pattern variables of the pattern, and make them definitely assigned if <paramref name="definitely"/>. | ||
// That would be false under "not" and "or" patterns. | ||
void assignPatternVariablesAndMarkReadProperties(BoundPattern pattern, bool definitely = true) | ||
{ | ||
case BoundKind.DeclarationPattern: | ||
{ | ||
var pat = (BoundDeclarationPattern)pattern; | ||
if (definitely) | ||
Assign(pat, value: null, isRef: false, read: false); | ||
break; | ||
} | ||
case BoundKind.DiscardPattern: | ||
break; | ||
case BoundKind.ConstantPattern: | ||
{ | ||
var pat = (BoundConstantPattern)pattern; | ||
this.VisitRvalue(pat.Value); | ||
switch (pattern.Kind) | ||
{ | ||
case BoundKind.DeclarationPattern: | ||
{ | ||
var pat = (BoundDeclarationPattern)pattern; | ||
if (definitely) | ||
Assign(pat, value: null, isRef: false, read: false); | ||
break; | ||
} | ||
case BoundKind.DiscardPattern: | ||
break; | ||
} | ||
case BoundKind.RecursivePattern: | ||
{ | ||
var pat = (BoundRecursivePattern)pattern; | ||
if (!pat.Deconstruction.IsDefaultOrEmpty) | ||
case BoundKind.ConstantPattern: | ||
{ | ||
var pat = (BoundConstantPattern)pattern; | ||
this.VisitRvalue(pat.Value); | ||
break; | ||
} | ||
case BoundKind.RecursivePattern: | ||
{ | ||
foreach (var subpat in pat.Deconstruction) | ||
var pat = (BoundRecursivePattern)pattern; | ||
if (!pat.Deconstruction.IsDefaultOrEmpty) | ||
{ | ||
AssignPatternVariables(subpat.Pattern, definitely); | ||
foreach (var subpat in pat.Deconstruction) | ||
{ | ||
assignPatternVariablesAndMarkReadProperties(subpat.Pattern, definitely); | ||
} | ||
} | ||
if (!pat.Properties.IsDefaultOrEmpty) | ||
{ | ||
foreach (BoundSubpattern sub in pat.Properties) | ||
{ | ||
if (sub is BoundPropertySubpattern { Member: var member } ) | ||
{ | ||
while (member is not null && !member.HasErrors) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
NoteRead(member.Symbol); | ||
member = member.Receiver; | ||
} | ||
} | ||
assignPatternVariablesAndMarkReadProperties(sub.Pattern, definitely); | ||
} | ||
} | ||
if (definitely) | ||
Assign(pat, null, false, false); | ||
break; | ||
} | ||
if (!pat.Properties.IsDefaultOrEmpty) | ||
case BoundKind.ITuplePattern: | ||
{ | ||
foreach (BoundSubpattern sub in pat.Properties) | ||
var pat = (BoundITuplePattern)pattern; | ||
foreach (var subpat in pat.Subpatterns) | ||
{ | ||
AssignPatternVariables(sub.Pattern, definitely); | ||
assignPatternVariablesAndMarkReadProperties(subpat.Pattern, definitely); | ||
} | ||
break; | ||
} | ||
if (definitely) | ||
Assign(pat, null, false, false); | ||
case BoundKind.TypePattern: | ||
break; | ||
} | ||
case BoundKind.ITuplePattern: | ||
{ | ||
var pat = (BoundITuplePattern)pattern; | ||
foreach (var subpat in pat.Subpatterns) | ||
case BoundKind.RelationalPattern: | ||
{ | ||
AssignPatternVariables(subpat.Pattern, definitely); | ||
var pat = (BoundRelationalPattern)pattern; | ||
this.VisitRvalue(pat.Value); | ||
break; | ||
} | ||
break; | ||
} | ||
case BoundKind.TypePattern: | ||
break; | ||
case BoundKind.RelationalPattern: | ||
{ | ||
var pat = (BoundRelationalPattern)pattern; | ||
this.VisitRvalue(pat.Value); | ||
break; | ||
} | ||
case BoundKind.NegatedPattern: | ||
{ | ||
var pat = (BoundNegatedPattern)pattern; | ||
AssignPatternVariables(pat.Negated, definitely: false); | ||
break; | ||
} | ||
case BoundKind.BinaryPattern: | ||
{ | ||
var pat = (BoundBinaryPattern)pattern; | ||
bool def = definitely && !pat.Disjunction; | ||
AssignPatternVariables(pat.Left, def); | ||
AssignPatternVariables(pat.Right, def); | ||
break; | ||
} | ||
default: | ||
throw ExceptionUtilities.UnexpectedValue(pattern.Kind); | ||
case BoundKind.NegatedPattern: | ||
{ | ||
var pat = (BoundNegatedPattern)pattern; | ||
assignPatternVariablesAndMarkReadProperties(pat.Negated, definitely: false); | ||
break; | ||
} | ||
case BoundKind.BinaryPattern: | ||
{ | ||
var pat = (BoundBinaryPattern)pattern; | ||
bool def = definitely && !pat.Disjunction; | ||
assignPatternVariablesAndMarkReadProperties(pat.Left, def); | ||
assignPatternVariablesAndMarkReadProperties(pat.Right, def); | ||
break; | ||
} | ||
default: | ||
throw ExceptionUtilities.UnexpectedValue(pattern.Kind); | ||
} | ||
} | ||
} | ||
|
||
|
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.
Thanks a lot for sorting this out. Have we fixed the code action or this is done manually? (I have no idea how this could be done manually) #Resolved
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've done it semi-manually: copy sections of generated code into real files, then trigger publicAPI fixer on those, repeat... It's better than manually editting, by far :-)