-
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 speculative semantic model for local function attributes #41300
Fix crash in speculative semantic model for local function attributes #41300
Conversation
|
||
var symbol = speculativeModel.GetDeclaredSymbol(localFunctionSyntax).GetSymbol<LocalFunctionSymbol>(); | ||
var attrs = symbol.GetAttributes(); | ||
Assert.Equal(new[] { "A" }, GetAttributeNames(attrs)); |
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.
What's the underlying symbol here. Do we still have the type name/constructor name problem?
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.
Yes. Will fix that separately.
var method = newTree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().Single(); | ||
Assert.True(model.TryGetSpeculativeSemanticModelForMethodBody(method.Body.SpanStart, method, out var speculativeModel)); | ||
|
||
var symbol = speculativeModel.GetDeclaredSymbol(localFunctionSyntax).GetSymbol<LocalFunctionSymbol>(); |
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.
What happens if you ask the same thing using the regular semantic model?
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.
The old model throws an exception if it is passed the new syntax node.
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.
Right, I meant if you constructed the final tree, then asked the question using a new semantic model.
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.
Got it. I'll add it to the test.
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.
Done
src/Compilers/CSharp/Portable/Symbols/Source/SourceMethodSymbolWithAttributes.cs
Show resolved
Hide resolved
@@ -264,11 +264,15 @@ private CustomAttributesBag<CSharpAttributeData> GetAttributesBag(ref CustomAttr | |||
bagCreatedOnThisThread = LoadAndValidateAttributes( | |||
this.GetReturnTypeAttributeDeclarations(), | |||
ref lazyCustomAttributesBag, | |||
symbolPart: AttributeLocation.Return); | |||
symbolPart: AttributeLocation.Return, | |||
binderOpt: (this as LocalFunctionSymbol)?.SignatureBinder); |
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.
binderOpt: (this as LocalFunctionSymbol)?.SignatureBinder [](start = 20, length = 57)
Consider extracting a local:
else
{
var binderOpt = ...;
bagCreatedOnThisThread = forReturnType ?
LoadAndValidateAttributes(…, binderOpt) :
LoadAndValidateAttributes(…, binderOpt);
}
// Verify with speculative semantic model | ||
var newTree = SyntaxFactory.ParseSyntaxTree(text + " ", TestOptions.RegularPreview); | ||
var newMethod = newTree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().Single(); | ||
Assert.True(model.TryGetSpeculativeSemanticModelForMethodBody(newMethod.Body.SpanStart, newMethod, out var speculativeModel)); |
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.
Please actually insert new code in the attribute and use the speculative model to query info about it.
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.
Spoke last Friday about this. We agree that this test is using the speculative model to get a symbol for the attribute, correct?
[Theory] | ||
[InlineData("[A]")] | ||
[InlineData("[return: A]")] | ||
public void LocalFunctionAttribute_SpeculativeSemanticModel(string attributes) |
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.
Please test on parameters as well.
Done review pass (commit 4) |
I ended up just putting all 4 scenarios into a theory based on the existing test for type parameters. The new test wasn't doing anything meaningfully different from what Andy already wrote so. |
Had a test failure in release_32:
I'm just going to rebase as I want a branch with these changes and the other recent local function changes to work off of. |
1df8582
to
d1effdc
Compare
Related to #38801
Related to (but does not fix) #24135
This fixes a crash when F5ing the local function attributes feature. The fix is based off a similar change made for type parameters.
roslyn/src/Compilers/CSharp/Portable/Symbols/Source/SourceTypeParameterSymbol.cs
Line 186 in 91571a3