Version Used: 2.8.0, master
Steps to Reproduce:
Try to get semantic model for syntax tree that's not part of the compilation or is null:
var tree1 = SyntaxFactory.ParseSyntaxTree("");
var tree2 = SyntaxFactory.ParseSyntaxTree(@"
Class C
Sub M()
End Sub
End Class");
var compilation = VisualBasicCompilation.Create(null).AddSyntaxTrees(tree1);
compilation.GetSemanticModel(tree2);
compilation.GetSemanticModel(null);
Expected Behavior: Both calls to GetSemanticModel throw an exception.
Actual Behavior: Both calls to GetSemanticModel succeed.
The relevant code:
|
Public Shadows Function GetSemanticModel(syntaxTree As SyntaxTree, Optional ignoreAccessibility As Boolean = False) As SemanticModel |
|
Return New SyntaxTreeSemanticModel(Me, DirectCast(Me.SourceModule, SourceModuleSymbol), syntaxTree, ignoreAccessibility) |
|
End Function |
|
Friend Sub New(compilation As VisualBasicCompilation, sourceModule As SourceModuleSymbol, syntaxTree As SyntaxTree, Optional ignoreAccessibility As Boolean = False) |
|
_compilation = compilation |
|
_sourceModule = sourceModule |
|
_syntaxTree = syntaxTree |
|
_ignoresAccessibility = ignoreAccessibility |
|
_binderFactory = New BinderFactory(sourceModule, syntaxTree) |
|
End Sub |
Note that C# performs both checks:
|
public new SemanticModel GetSemanticModel(SyntaxTree syntaxTree, bool ignoreAccessibility) |
|
{ |
|
if (syntaxTree == null) |
|
{ |
|
throw new ArgumentNullException(nameof(syntaxTree)); |
|
} |
|
|
|
if (!_syntaxAndDeclarations.GetLazyState().RootNamespaces.ContainsKey(syntaxTree)) |
|
{ |
|
throw new ArgumentException(string.Format(CSharpResources.SyntaxTreeNotFoundTo, syntaxTree), nameof(syntaxTree)); |
|
} |
|
|
|
return new SyntaxTreeSemanticModel(this, (SyntaxTree)syntaxTree, ignoreAccessibility); |
|
} |
Version Used: 2.8.0, master
Steps to Reproduce:
Try to get semantic model for syntax tree that's not part of the compilation or is null:
Expected Behavior: Both calls to
GetSemanticModelthrow an exception.Actual Behavior: Both calls to
GetSemanticModelsucceed.The relevant code:
roslyn/src/Compilers/VisualBasic/Portable/Compilation/VisualBasicCompilation.vb
Lines 1858 to 1860 in 50d4726
roslyn/src/Compilers/VisualBasic/Portable/Compilation/SyntaxTreeSemanticModel.vb
Lines 34 to 40 in 50d4726
Note that C# performs both checks:
roslyn/src/Compilers/CSharp/Portable/Compilation/CSharpCompilation.cs
Lines 1782 to 1795 in 50d4726