dotnet / dotnet/roslyn

VisualBasicCompilation.GetSpeculativeSymbolInfo returns `null` for anything inside WithStatement's expression

Open
#74,499 0 comments 0 reactions 0 assignees View on GitHub
Area-Compilers Bug
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Version Used**:
4.11.0-2.final

**Steps to Reproduce**:

Get a VB.NET compilation of following code:
```vb
Class C
Sub S()
Dim x As String
With x
End With
End Sub
End Class
```

Then use `GetSpeculativeSymbolInfo` to get the symbol of the `With` statement's expression.

Here is an example of how to do this:

Create new Standalone Roslyn Analyzer project, add following usings:

```cs
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.VisualBasic;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
```

Replace the Main method with following code:

```cs
static async Task Main(string[] args)
{
var refApis = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => !a.IsDynamic)
.Select(a => MetadataReference.CreateFromFile(a.Location));

var tree = SyntaxFactory.ParseSyntaxTree("""
Class C
Sub S()
Dim x As String
With x
End With
End Sub
End Class
""");
var compilation = VisualBasicCompilation.Create("something", [tree], refApis);
var semantic = compilation.GetSemanticModel(tree);

var root = (CompilationUnitSyntax)await tree.GetRootAsync();
var C = (ClassBlockSyntax)root.Members.First();
var S = (MethodBlockSyntax)C.Members.First();
var With = (WithBlockSyntax)S.Statements[1];
var WithExpression = With.WithStatement.Expression;

var s1 = semantic.GetSymbolInfo(WithExpression).Symbol;
var s2 = semantic.GetSpeculativeSymbolInfo(WithExpression.SpanStart, WithExpression, SpeculativeBindingOption.BindAsExpression).Symbol;
var s3 = semantic.GetSpeculativeSymbolInfo(With.SpanStart, WithExpression, SpeculativeBindingOption.BindAsExpression).Symbol;

Debug.Assert(s2 is not null); // fails
Debug.Assert(s1 == s2); // fails
Debug.Assert(s1 == s3);
}
```

**Expected Behavior**:
All three methods to get a symbol should work.

**Actual Behavior**:
`GetSpeculativeSymbolInfo` with position inside a `With` statement's expression returns `null`.

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the provided Visual Basic snippet and standalone analyzer, starting at SemanticModel.GetSpeculativeSymbolInfo for the WithBlockSyntax expression. Compare it with GetSymbolInfo and the speculative lookup at With.SpanStart; done means all three lookups return the expected non-null symbol consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, visualbasic
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.