-
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
Fix crash in 'use collection initializer' #75242
Merged
CyrusNajmabadi
merged 2 commits into
dotnet:main
from
CyrusNajmabadi:useCollectionCrash
Sep 26, 2024
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 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
26 changes: 0 additions & 26 deletions
26
...aredUtilitiesAndExtensions/Compiler/CSharp/Extensions/ForEachStatementSyntaxExtensions.cs
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -11,7 +11,6 @@ | |
using System.Threading; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.LanguageService; | ||
using Microsoft.CodeAnalysis.Operations; | ||
using Microsoft.CodeAnalysis.Shared.Extensions; | ||
using Roslyn.Utilities; | ||
|
||
|
@@ -94,8 +93,35 @@ public AbstractSpeculationAnalyzer( | |
} | ||
|
||
protected abstract ISyntaxFacts SyntaxFactsService { get; } | ||
|
||
protected abstract SyntaxNode GetSemanticRootForSpeculation(TExpressionSyntax expression); | ||
protected abstract SemanticModel CreateSpeculativeSemanticModel(SyntaxNode originalNode, SyntaxNode nodeToSpeculate, SemanticModel semanticModel); | ||
|
||
protected abstract bool IsInNamespaceOrTypeContext(TExpressionSyntax node); | ||
protected abstract bool ExpressionMightReferenceMember([NotNullWhen(true)] SyntaxNode? node); | ||
protected abstract bool CanAccessInstanceMemberThrough(TExpressionSyntax? expression); | ||
|
||
protected abstract TConversion ClassifyConversion(SemanticModel model, TExpressionSyntax expression, ITypeSymbol targetType); | ||
protected abstract TConversion ClassifyConversion(SemanticModel model, ITypeSymbol originalType, ITypeSymbol targetType); | ||
protected abstract bool ConversionsAreCompatible(SemanticModel model1, TExpressionSyntax expression1, SemanticModel model2, TExpressionSyntax expression2); | ||
protected abstract bool ConversionsAreCompatible(TExpressionSyntax originalExpression, ITypeSymbol originalTargetType, TExpressionSyntax newExpression, ITypeSymbol newTargetType); | ||
protected abstract bool IsReferenceConversion(Compilation model, ITypeSymbol sourceType, ITypeSymbol targetType); | ||
|
||
protected abstract TExpressionSyntax GetForEachStatementExpression(TForEachStatementSyntax forEachStatement); | ||
protected abstract bool IsForEachTypeInferred(TForEachStatementSyntax forEachStatement, SemanticModel semanticModel); | ||
protected abstract bool ForEachConversionsAreCompatible(SemanticModel originalModel, TForEachStatementSyntax originalForEach, SemanticModel newModel, TForEachStatementSyntax newForEach); | ||
protected abstract void GetForEachSymbols( | ||
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. this method changed to add more information back. |
||
SemanticModel model, TForEachStatementSyntax forEach, out IMethodSymbol getEnumeratorMethod, out ITypeSymbol elementType, out ImmutableArray<ILocalSymbol> localVariables); | ||
|
||
protected abstract bool ReplacementChangesSemanticsForNodeLanguageSpecific(SyntaxNode currentOriginalNode, SyntaxNode currentReplacedNode, SyntaxNode? previousOriginalNode, SyntaxNode? previousReplacedNode); | ||
|
||
protected abstract bool IsParenthesizedExpression([NotNullWhen(true)] SyntaxNode? node); | ||
protected abstract bool IsNamedArgument(TArgumentSyntax argument); | ||
protected abstract string GetNamedArgumentIdentifierValueText(TArgumentSyntax argument); | ||
protected abstract TExpressionSyntax GetThrowStatementExpression(TThrowStatementSyntax throwStatement); | ||
protected abstract ImmutableArray<TArgumentSyntax> GetArguments(TExpressionSyntax expression); | ||
protected abstract TExpressionSyntax GetReceiver(TExpressionSyntax expression); | ||
|
||
/// <summary> | ||
/// Original expression to be replaced. | ||
/// </summary> | ||
|
@@ -171,8 +197,6 @@ public SemanticModel SpeculativeSemanticModel | |
|
||
public CancellationToken CancellationToken { get; } | ||
|
||
protected abstract SyntaxNode GetSemanticRootForSpeculation(TExpressionSyntax expression); | ||
|
||
protected virtual SyntaxNode GetSemanticRootOfReplacedExpression(SyntaxNode semanticRootOfOriginalExpression, TExpressionSyntax annotatedReplacedExpression) | ||
=> semanticRootOfOriginalExpression.ReplaceNode(this.OriginalExpression, annotatedReplacedExpression); | ||
|
||
|
@@ -209,8 +233,6 @@ private void EnsureSpeculativeSemanticModel() | |
} | ||
} | ||
|
||
protected abstract SemanticModel CreateSpeculativeSemanticModel(SyntaxNode originalNode, SyntaxNode nodeToSpeculate, SemanticModel semanticModel); | ||
|
||
#region Semantic comparison helpers | ||
|
||
protected virtual bool ReplacementIntroducesDisallowedNullType( | ||
|
@@ -291,9 +313,6 @@ private bool ImplicitConversionsAreCompatible(TExpressionSyntax originalExpressi | |
return ConversionsAreCompatible(originalExpression, originalTargetType, newExpression, newTargetType); | ||
} | ||
|
||
protected abstract bool ConversionsAreCompatible(SemanticModel model1, TExpressionSyntax expression1, SemanticModel model2, TExpressionSyntax expression2); | ||
protected abstract bool ConversionsAreCompatible(TExpressionSyntax originalExpression, ITypeSymbol originalTargetType, TExpressionSyntax newExpression, ITypeSymbol newTargetType); | ||
|
||
protected bool SymbolsAreCompatible(SyntaxNode originalNode, SyntaxNode newNode, bool requireNonNullSymbols = false) | ||
{ | ||
RoslynDebug.AssertNotNull(originalNode); | ||
|
@@ -496,8 +515,6 @@ public bool ReplacementChangesSemantics() | |
skipVerificationForCurrentNode: _skipVerificationForReplacedNode); | ||
} | ||
|
||
protected abstract bool IsParenthesizedExpression([NotNullWhen(true)] SyntaxNode? node); | ||
|
||
protected bool ReplacementChangesSemantics(SyntaxNode currentOriginalNode, SyntaxNode currentReplacedNode, SyntaxNode originalRoot, bool skipVerificationForCurrentNode) | ||
{ | ||
if (this.SpeculativeSemanticModel == null) | ||
|
@@ -551,8 +568,6 @@ public bool SymbolsForOriginalAndReplacedNodesAreCompatible() | |
return SymbolsAreCompatible(this.OriginalExpression, this.ReplacedExpression, requireNonNullSymbols: true); | ||
} | ||
|
||
protected abstract bool ReplacementChangesSemanticsForNodeLanguageSpecific(SyntaxNode currentOriginalNode, SyntaxNode currentReplacedNode, SyntaxNode? previousOriginalNode, SyntaxNode? previousReplacedNode); | ||
|
||
private bool ReplacementChangesSemanticsForNode(SyntaxNode currentOriginalNode, SyntaxNode currentReplacedNode, SyntaxNode? previousOriginalNode, SyntaxNode? previousReplacedNode) | ||
{ | ||
Debug.Assert(previousOriginalNode == null || previousOriginalNode.Parent == currentOriginalNode); | ||
|
@@ -753,10 +768,6 @@ private bool ReplacementBreaksAttribute(TAttributeSyntax attribute, TAttributeSy | |
return !SymbolsAreCompatible(attributeSym, newAttributeSym); | ||
} | ||
|
||
protected abstract TExpressionSyntax GetForEachStatementExpression(TForEachStatementSyntax forEachStatement); | ||
|
||
protected abstract bool IsForEachTypeInferred(TForEachStatementSyntax forEachStatement, SemanticModel semanticModel); | ||
|
||
private bool ReplacementBreaksForEachStatement(TForEachStatementSyntax forEachStatement, TForEachStatementSyntax newForEachStatement) | ||
{ | ||
var forEachExpression = GetForEachStatementExpression(forEachStatement); | ||
|
@@ -766,20 +777,22 @@ private bool ReplacementBreaksForEachStatement(TForEachStatementSyntax forEachSt | |
return false; | ||
} | ||
|
||
GetForEachSymbols(this.OriginalSemanticModel, forEachStatement, out var originalGetEnumerator, out var originalElementType, out var originalLocalVariables); | ||
GetForEachSymbols(this.SpeculativeSemanticModel, newForEachStatement, out var newGetEnumerator, out var newElementType, out var newLocalVariables); | ||
|
||
// inferred variable type compatible | ||
if (IsForEachTypeInferred(forEachStatement, OriginalSemanticModel)) | ||
{ | ||
var local = (ILocalSymbol)OriginalSemanticModel.GetRequiredDeclaredSymbol(forEachStatement, CancellationToken); | ||
var newLocal = (ILocalSymbol)this.SpeculativeSemanticModel.GetRequiredDeclaredSymbol(newForEachStatement, CancellationToken); | ||
if (!SymbolsAreCompatible(local.Type, newLocal.Type)) | ||
{ | ||
if (originalLocalVariables.Length != newLocalVariables.Length) | ||
return true; | ||
|
||
for (int i = 0, n = originalLocalVariables.Length; i < n; i++) | ||
{ | ||
if (!SymbolsAreCompatible(originalLocalVariables[i].Type, newLocalVariables[i].Type)) | ||
return true; | ||
} | ||
} | ||
|
||
GetForEachSymbols(this.OriginalSemanticModel, forEachStatement, out var originalGetEnumerator, out var originalElementType); | ||
GetForEachSymbols(this.SpeculativeSemanticModel, newForEachStatement, out var newGetEnumerator, out var newElementType); | ||
|
||
var newForEachExpression = GetForEachStatementExpression(newForEachStatement); | ||
|
||
if (ReplacementBreaksForEachGetEnumerator(originalGetEnumerator, newGetEnumerator, newForEachExpression) || | ||
|
@@ -792,10 +805,6 @@ private bool ReplacementBreaksForEachStatement(TForEachStatementSyntax forEachSt | |
return false; | ||
} | ||
|
||
protected abstract bool ForEachConversionsAreCompatible(SemanticModel originalModel, TForEachStatementSyntax originalForEach, SemanticModel newModel, TForEachStatementSyntax newForEach); | ||
|
||
protected abstract void GetForEachSymbols(SemanticModel model, TForEachStatementSyntax forEach, out IMethodSymbol getEnumeratorMethod, out ITypeSymbol elementType); | ||
|
||
private bool ReplacementBreaksForEachGetEnumerator(IMethodSymbol getEnumerator, IMethodSymbol newGetEnumerator, TExpressionSyntax newForEachStatementExpression) | ||
{ | ||
if (getEnumerator == null && newGetEnumerator == null) | ||
|
@@ -834,8 +843,6 @@ private bool ReplacementBreaksForEachGetEnumerator(IMethodSymbol getEnumerator, | |
return false; | ||
} | ||
|
||
protected abstract TExpressionSyntax GetThrowStatementExpression(TThrowStatementSyntax throwStatement); | ||
|
||
private bool ReplacementBreaksThrowStatement(TThrowStatementSyntax originalThrowStatement, TThrowStatementSyntax newThrowStatement) | ||
{ | ||
var originalThrowExpression = GetThrowStatementExpression(originalThrowStatement); | ||
|
@@ -848,8 +855,6 @@ private bool ReplacementBreaksThrowStatement(TThrowStatementSyntax originalThrow | |
newThrowExpressionType.IsOrDerivesFromExceptionType(this.SpeculativeSemanticModel.Compilation); | ||
} | ||
|
||
protected abstract bool IsInNamespaceOrTypeContext(TExpressionSyntax node); | ||
|
||
private bool ReplacementBreaksTypeResolution(TTypeSyntax type, TTypeSyntax newType, bool useSpeculativeModel = true) | ||
{ | ||
var symbol = this.OriginalSemanticModel.GetSymbolInfo(type).Symbol; | ||
|
@@ -868,8 +873,6 @@ private bool ReplacementBreaksTypeResolution(TTypeSyntax type, TTypeSyntax newTy | |
return symbol != null && !SymbolsAreCompatible(symbol, newSymbol); | ||
} | ||
|
||
protected abstract bool ExpressionMightReferenceMember([NotNullWhen(true)] SyntaxNode? node); | ||
|
||
private static bool IsDelegateInvoke(ISymbol symbol) | ||
{ | ||
return symbol.Kind == SymbolKind.Method && | ||
|
@@ -969,8 +972,6 @@ protected bool ReplacementBreaksCompoundAssignment( | |
return false; | ||
} | ||
|
||
protected abstract bool IsReferenceConversion(Compilation model, ITypeSymbol sourceType, ITypeSymbol targetType); | ||
|
||
private bool IsCompatibleInterfaceMemberImplementation( | ||
ISymbol symbol, | ||
ISymbol newSymbol, | ||
|
@@ -1060,9 +1061,6 @@ private static bool IsReceiverUniqueInstance(TExpressionSyntax receiver, Semanti | |
receiverSymbol.IsKind(SymbolKind.Property); | ||
} | ||
|
||
protected abstract ImmutableArray<TArgumentSyntax> GetArguments(TExpressionSyntax expression); | ||
protected abstract TExpressionSyntax GetReceiver(TExpressionSyntax expression); | ||
|
||
private bool SymbolsHaveCompatibleParameterLists(ISymbol originalSymbol, ISymbol newSymbol, TExpressionSyntax originalInvocation) | ||
{ | ||
if (originalSymbol.IsKind(SymbolKind.Method) || originalSymbol.IsIndexer()) | ||
|
@@ -1079,9 +1077,6 @@ private bool SymbolsHaveCompatibleParameterLists(ISymbol originalSymbol, ISymbol | |
return true; | ||
} | ||
|
||
protected abstract bool IsNamedArgument(TArgumentSyntax argument); | ||
protected abstract string GetNamedArgumentIdentifierValueText(TArgumentSyntax argument); | ||
|
||
private bool AreCompatibleParameterLists( | ||
ImmutableArray<TArgumentSyntax> specifiedArguments, | ||
ImmutableArray<IParameterSymbol> signature1Parameters, | ||
|
@@ -1231,7 +1226,4 @@ protected void GetConversions( | |
} | ||
} | ||
} | ||
|
||
protected abstract TConversion ClassifyConversion(SemanticModel model, TExpressionSyntax expression, ITypeSymbol targetType); | ||
protected abstract TConversion ClassifyConversion(SemanticModel model, ITypeSymbol originalType, ITypeSymbol targetType); | ||
} |
Oops, something went wrong.
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.
just moved all the abstract methods to the top of the file.