提交 b86a8ce9 编写于 作者: A AlekseyTs

Add public API to create an error namespace symbol.

Fixes #14365.
上级 7e0717d2
......@@ -169,9 +169,18 @@ public LanguageVersion LanguageVersion
get;
}
public override INamedTypeSymbol CreateErrorTypeSymbol(INamespaceOrTypeSymbol container, string name, int arity)
protected override INamedTypeSymbol CommonCreateErrorTypeSymbol(INamespaceOrTypeSymbol container, string name, int arity)
{
return new ExtendedErrorTypeSymbol((NamespaceOrTypeSymbol)container, name, arity, null);
return new ExtendedErrorTypeSymbol(
container.EnsureCSharpSymbolOrNull<INamespaceOrTypeSymbol, NamespaceOrTypeSymbol>(nameof(container)),
name, arity, errorInfo: null);
}
protected override INamespaceSymbol CommonCreateErrorNamespaceSymbol(INamespaceSymbol container, string name)
{
return new MissingNamespaceSymbol(
container.EnsureCSharpSymbolOrNull<INamespaceSymbol, NamespaceSymbol>(nameof(container)),
name);
}
#region Constructors and Factories
......
......@@ -2475,7 +2475,6 @@ abstract Microsoft.CodeAnalysis.CSharp.Syntax.XmlAttributeSyntax.StartQuoteToken
override Microsoft.CodeAnalysis.CSharp.AwaitExpressionInfo.Equals(object obj) -> bool
override Microsoft.CodeAnalysis.CSharp.AwaitExpressionInfo.GetHashCode() -> int
override Microsoft.CodeAnalysis.CSharp.CSharpCompilation.ContainsSymbolsWithName(System.Func<string, bool> predicate, Microsoft.CodeAnalysis.SymbolFilter filter = Microsoft.CodeAnalysis.SymbolFilter.TypeAndMember, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> bool
override Microsoft.CodeAnalysis.CSharp.CSharpCompilation.CreateErrorTypeSymbol(Microsoft.CodeAnalysis.INamespaceOrTypeSymbol container, string name, int arity) -> Microsoft.CodeAnalysis.INamedTypeSymbol
override Microsoft.CodeAnalysis.CSharp.CSharpCompilation.DirectiveReferences.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.MetadataReference>
override Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetDeclarationDiagnostics(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Diagnostic>
override Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetDiagnostics(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Diagnostic>
......
......@@ -56,28 +56,20 @@ public override AssemblySymbol ContainingAssembly
{
get
{
return ContainingModule.ContainingAssembly;
return _containingSymbol.ContainingAssembly;
}
}
internal override NamespaceExtent Extent
{
get
{
return new NamespaceExtent(ContainingModule);
}
}
internal override ModuleSymbol ContainingModule
{
get
{
if (_containingSymbol.Kind == SymbolKind.NetModule)
{
return (ModuleSymbol)_containingSymbol;
return new NamespaceExtent((ModuleSymbol)_containingSymbol);
}
return _containingSymbol.ContainingModule;
return ((NamespaceSymbol)_containingSymbol).Extent;
}
}
......
......@@ -5190,14 +5190,64 @@ public void CreateTupleTypeSymbol_ElementTypeIsError()
}
[Fact, WorkItem(13277, "https://github.com/dotnet/roslyn/issues/13277")]
[WorkItem(14365, "https://github.com/dotnet/roslyn/issues/14365")]
public void CreateTupleTypeSymbol_UnderlyingTypeIsError()
{
var comp = CSharpCompilation.Create("test", references: new[] { MscorlibRef });
var comp = CSharpCompilation.Create("test", references: new[] { MscorlibRef, TestReferences.SymbolsTests.netModule.netModule1 });
TypeSymbol intType = comp.GetSpecialType(SpecialType.System_Int32);
var vt2 = comp.CreateErrorTypeSymbol(null, "ValueTuple", 2).Construct(intType, intType);
Assert.Throws<ArgumentException>(() => comp.CreateTupleTypeSymbol(underlyingType: vt2));
var vbComp = CreateVisualBasicCompilation("");
Assert.Throws<ArgumentNullException>(() => comp.CreateErrorTypeSymbol(null, null, 2));
Assert.Throws<ArgumentException>(() => comp.CreateErrorTypeSymbol(null, "a", -1));
Assert.Throws<ArgumentException>(() => comp.CreateErrorTypeSymbol(vbComp.GlobalNamespace, "a", 1));
Assert.Throws<ArgumentNullException>(() => comp.CreateErrorNamespaceSymbol(null, "a"));
Assert.Throws<ArgumentNullException>(() => comp.CreateErrorNamespaceSymbol(vbComp.GlobalNamespace, null));
Assert.Throws<ArgumentException>(() => comp.CreateErrorNamespaceSymbol(vbComp.GlobalNamespace, "a"));
var ns = comp.CreateErrorNamespaceSymbol(comp.GlobalNamespace, "a");
Assert.Equal("a", ns.ToTestDisplayString());
Assert.False(ns.IsGlobalNamespace);
Assert.Equal(NamespaceKind.Compilation, ns.NamespaceKind);
Assert.Same(comp.GlobalNamespace, ns.ContainingSymbol);
Assert.Same(comp.GlobalNamespace.ContainingAssembly, ns.ContainingAssembly);
Assert.Same(comp.GlobalNamespace.ContainingModule, ns.ContainingModule);
ns = comp.CreateErrorNamespaceSymbol(comp.Assembly.GlobalNamespace, "a");
Assert.Equal("a", ns.ToTestDisplayString());
Assert.False(ns.IsGlobalNamespace);
Assert.Equal(NamespaceKind.Assembly, ns.NamespaceKind);
Assert.Same(comp.Assembly.GlobalNamespace, ns.ContainingSymbol);
Assert.Same(comp.Assembly.GlobalNamespace.ContainingAssembly, ns.ContainingAssembly);
Assert.Same(comp.Assembly.GlobalNamespace.ContainingModule, ns.ContainingModule);
ns = comp.CreateErrorNamespaceSymbol(comp.SourceModule.GlobalNamespace, "a");
Assert.Equal("a", ns.ToTestDisplayString());
Assert.False(ns.IsGlobalNamespace);
Assert.Equal(NamespaceKind.Module, ns.NamespaceKind);
Assert.Same(comp.SourceModule.GlobalNamespace, ns.ContainingSymbol);
Assert.Same(comp.SourceModule.GlobalNamespace.ContainingAssembly, ns.ContainingAssembly);
Assert.Same(comp.SourceModule.GlobalNamespace.ContainingModule, ns.ContainingModule);
ns = comp.CreateErrorNamespaceSymbol(comp.CreateErrorNamespaceSymbol(comp.GlobalNamespace, "a"), "b");
Assert.Equal("a.b", ns.ToTestDisplayString());
ns = comp.CreateErrorNamespaceSymbol(comp.GlobalNamespace, "");
Assert.Equal("", ns.ToTestDisplayString());
Assert.False(ns.IsGlobalNamespace);
vt2 = comp.CreateErrorTypeSymbol(comp.CreateErrorNamespaceSymbol(comp.GlobalNamespace, "System"), "ValueTuple", 2).Construct(intType, intType);
Assert.Equal("(System.Int32, System.Int32)", comp.CreateTupleTypeSymbol(underlyingType: vt2).ToTestDisplayString());
vt2 = comp.CreateErrorTypeSymbol(comp.CreateErrorNamespaceSymbol(comp.Assembly.GlobalNamespace, "System"), "ValueTuple", 2).Construct(intType, intType);
Assert.Equal("(System.Int32, System.Int32)", comp.CreateTupleTypeSymbol(underlyingType: vt2).ToTestDisplayString());
vt2 = comp.CreateErrorTypeSymbol(comp.CreateErrorNamespaceSymbol(comp.SourceModule.GlobalNamespace, "System"), "ValueTuple", 2).Construct(intType, intType);
Assert.Equal("(System.Int32, System.Int32)", comp.CreateTupleTypeSymbol(underlyingType: vt2).ToTestDisplayString());
}
[Fact]
......
......@@ -200,7 +200,42 @@ public SemanticModel GetSemanticModel(SyntaxTree syntaxTree, bool ignoreAccessib
/// Returns a new INamedTypeSymbol representing an error type with the given name and arity
/// in the given optional container.
/// </summary>
public abstract INamedTypeSymbol CreateErrorTypeSymbol(INamespaceOrTypeSymbol container, string name, int arity);
public INamedTypeSymbol CreateErrorTypeSymbol(INamespaceOrTypeSymbol container, string name, int arity)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (arity < 0)
{
throw new ArgumentException(nameof(arity));
}
return CommonCreateErrorTypeSymbol(container, name, arity);
}
protected abstract INamedTypeSymbol CommonCreateErrorTypeSymbol(INamespaceOrTypeSymbol container, string name, int arity);
/// <summary>
/// Returns a new INamespaceSymbol representing an error (missing) namespace with the given name.
/// </summary>
public INamespaceSymbol CreateErrorNamespaceSymbol(INamespaceSymbol container, string name)
{
if (container == null)
{
throw new ArgumentNullException(nameof(container));
}
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return CommonCreateErrorNamespaceSymbol(container, name);
}
protected abstract INamespaceSymbol CommonCreateErrorNamespaceSymbol(INamespaceSymbol container, string name);
#region Name
......
......@@ -1631,7 +1631,7 @@ abstract Microsoft.CodeAnalysis.AttributeData.CommonAttributeConstructor.get ->
abstract Microsoft.CodeAnalysis.AttributeData.CommonConstructorArguments.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.TypedConstant>
abstract Microsoft.CodeAnalysis.AttributeData.CommonNamedArguments.get -> System.Collections.Immutable.ImmutableArray<System.Collections.Generic.KeyValuePair<string, Microsoft.CodeAnalysis.TypedConstant>>
abstract Microsoft.CodeAnalysis.Compilation.ContainsSymbolsWithName(System.Func<string, bool> predicate, Microsoft.CodeAnalysis.SymbolFilter filter = Microsoft.CodeAnalysis.SymbolFilter.TypeAndMember, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> bool
abstract Microsoft.CodeAnalysis.Compilation.CreateErrorTypeSymbol(Microsoft.CodeAnalysis.INamespaceOrTypeSymbol container, string name, int arity) -> Microsoft.CodeAnalysis.INamedTypeSymbol
Microsoft.CodeAnalysis.Compilation.CreateErrorTypeSymbol(Microsoft.CodeAnalysis.INamespaceOrTypeSymbol container, string name, int arity) -> Microsoft.CodeAnalysis.INamedTypeSymbol
abstract Microsoft.CodeAnalysis.Compilation.DirectiveReferences.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.MetadataReference>
abstract Microsoft.CodeAnalysis.Compilation.GetDeclarationDiagnostics(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Diagnostic>
abstract Microsoft.CodeAnalysis.Compilation.GetDiagnostics(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Diagnostic>
......
......@@ -729,6 +729,7 @@ Microsoft.CodeAnalysis.SymbolDisplayMemberOptions.IncludeRef = 128 -> Microsoft.
Microsoft.CodeAnalysis.SymbolKind.Discard = 19 -> Microsoft.CodeAnalysis.SymbolKind
Microsoft.CodeAnalysis.Text.SourceText.CanBeEmbedded.get -> bool
Microsoft.CodeAnalysis.Text.SourceText.GetChecksum() -> System.Collections.Immutable.ImmutableArray<byte>
Microsoft.CodeAnalysis.Compilation.CreateErrorNamespaceSymbol(Microsoft.CodeAnalysis.INamespaceSymbol container, string name) -> Microsoft.CodeAnalysis.INamespaceSymbol
abstract Microsoft.CodeAnalysis.CompilationOptions.Language.get -> string
abstract Microsoft.CodeAnalysis.Diagnostics.OperationBlockStartAnalysisContext.RegisterOperationAction(System.Action<Microsoft.CodeAnalysis.Diagnostics.OperationAnalysisContext> action, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.OperationKind> operationKinds) -> void
abstract Microsoft.CodeAnalysis.Diagnostics.OperationBlockStartAnalysisContext.RegisterOperationBlockEndAction(System.Action<Microsoft.CodeAnalysis.Diagnostics.OperationBlockAnalysisContext> action) -> void
......
......@@ -2569,8 +2569,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Get
End Property
Public Overrides Function CreateErrorTypeSymbol(container As INamespaceOrTypeSymbol, name As String, arity As Integer) As INamedTypeSymbol
Return New ExtendedErrorTypeSymbol(DirectCast(container, NamespaceOrTypeSymbol), name, arity)
Protected Overrides Function CommonCreateErrorTypeSymbol(container As INamespaceOrTypeSymbol, name As String, arity As Integer) As INamedTypeSymbol
Return New ExtendedErrorTypeSymbol(
container.EnsureVbSymbolOrNothing(Of NamespaceOrTypeSymbol)(NameOf(container)),
name, arity)
End Function
Protected Overrides Function CommonCreateErrorNamespaceSymbol(container As INamespaceSymbol, name As String) As INamespaceSymbol
Return New MissingNamespaceSymbol(
container.EnsureVbSymbolOrNothing(Of NamespaceSymbol)(NameOf(container)),
name)
End Function
Protected Overrides Function CommonCreateArrayTypeSymbol(elementType As ITypeSymbol, rank As Integer) As IArrayTypeSymbol
......
......@@ -4363,7 +4363,6 @@ Overrides Microsoft.CodeAnalysis.VisualBasic.Syntax.XmlTextSyntax.Accept(visitor
Overrides Microsoft.CodeAnalysis.VisualBasic.Syntax.YieldStatementSyntax.Accept(Of TResult)(visitor As Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor(Of TResult)) -> TResult
Overrides Microsoft.CodeAnalysis.VisualBasic.Syntax.YieldStatementSyntax.Accept(visitor As Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor) -> Void
Overrides Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation.ContainsSymbolsWithName(predicate As System.Func(Of String, Boolean), filter As Microsoft.CodeAnalysis.SymbolFilter = Microsoft.CodeAnalysis.SymbolFilter.TypeAndMember, cancellationToken As System.Threading.CancellationToken = Nothing) -> Boolean
Overrides Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation.CreateErrorTypeSymbol(container As Microsoft.CodeAnalysis.INamespaceOrTypeSymbol, name As String, arity As Integer) -> Microsoft.CodeAnalysis.INamedTypeSymbol
Overrides Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation.DirectiveReferences() -> System.Collections.Immutable.ImmutableArray(Of Microsoft.CodeAnalysis.MetadataReference)
Overrides Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation.GetDeclarationDiagnostics(cancellationToken As System.Threading.CancellationToken = Nothing) -> System.Collections.Immutable.ImmutableArray(Of Microsoft.CodeAnalysis.Diagnostic)
Overrides Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation.GetDiagnostics(cancellationToken As System.Threading.CancellationToken = Nothing) -> System.Collections.Immutable.ImmutableArray(Of Microsoft.CodeAnalysis.Diagnostic)
......@@ -51,23 +51,17 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Public Overrides ReadOnly Property ContainingAssembly As AssemblySymbol
Get
Return ContainingModule.ContainingAssembly
Return _containingSymbol.ContainingAssembly
End Get
End Property
Friend Overrides ReadOnly Property Extent As NamespaceExtent
Get
Return New NamespaceExtent(ContainingModule)
End Get
End Property
Public Overrides ReadOnly Property ContainingModule As ModuleSymbol
Get
If _containingSymbol.Kind = SymbolKind.NetModule Then
Return DirectCast(_containingSymbol, ModuleSymbol)
Return New NamespaceExtent(DirectCast(_containingSymbol, ModuleSymbol))
End If
Return _containingSymbol.ContainingModule
Return DirectCast(_containingSymbol, NamespaceSymbol).Extent
End Get
End Property
......
......@@ -7769,14 +7769,64 @@ fourth]]>)
<Fact()>
<WorkItem(13277, "https://github.com/dotnet/roslyn/issues/13277")>
<WorkItem(14365, "https://github.com/dotnet/roslyn/issues/14365")>
Public Sub CreateTupleTypeSymbol_UnderlyingTypeIsError()
Dim comp = VisualBasicCompilation.Create("test", references:={MscorlibRef})
Dim comp = VisualBasicCompilation.Create("test", references:={MscorlibRef, TestReferences.SymbolsTests.netModule.netModule1})
Dim intType As TypeSymbol = comp.GetSpecialType(SpecialType.System_Int32)
Dim vt2 = comp.CreateErrorTypeSymbol(Nothing, "ValueTuple", 2).Construct(intType, intType)
Assert.Throws(Of ArgumentException)(Function() comp.CreateTupleTypeSymbol(underlyingType:=vt2))
Dim csComp = CreateCSharpCompilation("")
Assert.Throws(Of ArgumentNullException)(Sub() comp.CreateErrorTypeSymbol(Nothing, Nothing, 2))
Assert.Throws(Of ArgumentException)(Sub() comp.CreateErrorTypeSymbol(Nothing, "a", -1))
Assert.Throws(Of ArgumentException)(Sub() comp.CreateErrorTypeSymbol(csComp.GlobalNamespace, "a", 1))
Assert.Throws(Of ArgumentNullException)(Sub() comp.CreateErrorNamespaceSymbol(Nothing, "a"))
Assert.Throws(Of ArgumentNullException)(Sub() comp.CreateErrorNamespaceSymbol(csComp.GlobalNamespace, Nothing))
Assert.Throws(Of ArgumentException)(Sub() comp.CreateErrorNamespaceSymbol(csComp.GlobalNamespace, "a"))
Dim ns = comp.CreateErrorNamespaceSymbol(comp.GlobalNamespace, "a")
Assert.Equal("a", ns.ToTestDisplayString())
Assert.False(ns.IsGlobalNamespace)
Assert.Equal(NamespaceKind.Compilation, ns.NamespaceKind)
Assert.Same(comp.GlobalNamespace, ns.ContainingSymbol)
Assert.Same(comp.GlobalNamespace.ContainingAssembly, ns.ContainingAssembly)
Assert.Same(comp.GlobalNamespace.ContainingModule, ns.ContainingModule)
ns = comp.CreateErrorNamespaceSymbol(comp.Assembly.GlobalNamespace, "a")
Assert.Equal("a", ns.ToTestDisplayString())
Assert.False(ns.IsGlobalNamespace)
Assert.Equal(NamespaceKind.Assembly, ns.NamespaceKind)
Assert.Same(comp.Assembly.GlobalNamespace, ns.ContainingSymbol)
Assert.Same(comp.Assembly.GlobalNamespace.ContainingAssembly, ns.ContainingAssembly)
Assert.Same(comp.Assembly.GlobalNamespace.ContainingModule, ns.ContainingModule)
ns = comp.CreateErrorNamespaceSymbol(comp.SourceModule.GlobalNamespace, "a")
Assert.Equal("a", ns.ToTestDisplayString())
Assert.False(ns.IsGlobalNamespace)
Assert.Equal(NamespaceKind.Module, ns.NamespaceKind)
Assert.Same(comp.SourceModule.GlobalNamespace, ns.ContainingSymbol)
Assert.Same(comp.SourceModule.GlobalNamespace.ContainingAssembly, ns.ContainingAssembly)
Assert.Same(comp.SourceModule.GlobalNamespace.ContainingModule, ns.ContainingModule)
ns = comp.CreateErrorNamespaceSymbol(comp.CreateErrorNamespaceSymbol(comp.GlobalNamespace, "a"), "b")
Assert.Equal("a.b", ns.ToTestDisplayString())
ns = comp.CreateErrorNamespaceSymbol(comp.GlobalNamespace, "")
Assert.Equal("", ns.ToTestDisplayString())
Assert.False(ns.IsGlobalNamespace)
vt2 = comp.CreateErrorTypeSymbol(comp.CreateErrorNamespaceSymbol(comp.GlobalNamespace, "System"), "ValueTuple", 2).Construct(intType, intType)
Assert.Equal("(System.Int32, System.Int32)", comp.CreateTupleTypeSymbol(underlyingType:=vt2).ToTestDisplayString())
vt2 = comp.CreateErrorTypeSymbol(comp.CreateErrorNamespaceSymbol(comp.Assembly.GlobalNamespace, "System"), "ValueTuple", 2).Construct(intType, intType)
Assert.Equal("(System.Int32, System.Int32)", comp.CreateTupleTypeSymbol(underlyingType:=vt2).ToTestDisplayString())
vt2 = comp.CreateErrorTypeSymbol(comp.CreateErrorNamespaceSymbol(comp.SourceModule.GlobalNamespace, "System"), "ValueTuple", 2).Construct(intType, intType)
Assert.Equal("(System.Int32, System.Int32)", comp.CreateTupleTypeSymbol(underlyingType:=vt2).ToTestDisplayString())
End Sub
<Fact>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册