Skip to content
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

Only qualify the first symbol in Quick Info #27946

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public override void VisitNamedType(INamedTypeSymbol symbol)
return;
}

if (format.MiscellaneousOptions.IncludesOption(SymbolDisplayMiscellaneousOptions.UseSpecialTypes))
if (CanUseSpecialTypes)
{
if (AddSpecialTypeKeyword(symbol))
{
Expand Down Expand Up @@ -212,7 +212,7 @@ public override void VisitNamedType(INamedTypeSymbol symbol)
{
if (IncludeNamedType(symbol.ContainingType))
{
symbol.ContainingType.Accept(this.NotFirstVisitor);
symbol.ContainingType.Accept(this.FirstSymbolContainingTypeVisitor);
AddPunctuation(SyntaxKind.DotToken);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ private SymbolDisplayVisitor(
_lazyAliasMap = aliasMap;
}

protected override AbstractSymbolDisplayVisitor MakeFirstSymbolContainingTypeVisitor()
{
if (!isFirstSymbolVisited || format.KindOptions == SymbolDisplayKindOptions.None)
{
return this;
}

return new SymbolDisplayVisitor(
this.builder,
this.format.WithKindOptions(SymbolDisplayKindOptions.None),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the forcing to .None?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The kind is already being printed as part of the nested type. We don't want to include the type kind for the containing type or you could end up with a display like this:

// namespace X
namespace X {
  // class X.A
  class A {
    // struct class X.A.B
    struct B { }
  }
}

this.semanticModelOpt,
this.positionOpt,
_escapeKeywordIdentifiers,
_lazyAliasMap,
isFirstSymbolVisited: true,
inNamespaceOrType);
}

protected override AbstractSymbolDisplayVisitor MakeNotFirstVisitor(bool inNamespaceOrType = false)
{
return new SymbolDisplayVisitor(
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/Core/Portable/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Microsoft.CodeAnalysis.FlowAnalysis.IIsNullOperation.Operand.get -> Microsoft.Co
Microsoft.CodeAnalysis.FlowAnalysis.IStaticLocalInitializationSemaphoreOperation
Microsoft.CodeAnalysis.FlowAnalysis.IStaticLocalInitializationSemaphoreOperation.Local.get -> Microsoft.CodeAnalysis.ILocalSymbol
Microsoft.CodeAnalysis.Operations.CommonConversion.IsImplicit.get -> bool
Microsoft.CodeAnalysis.SymbolDisplayMiscellaneousOptions.QualifyFirstSymbol = 64 -> Microsoft.CodeAnalysis.SymbolDisplayMiscellaneousOptions
abstract Microsoft.CodeAnalysis.Compilation.ClassifyCommonConversion(Microsoft.CodeAnalysis.ITypeSymbol source, Microsoft.CodeAnalysis.ITypeSymbol destination) -> Microsoft.CodeAnalysis.Operations.CommonConversion
abstract Microsoft.CodeAnalysis.Compilation.ContainsSymbolsWithName(string name, Microsoft.CodeAnalysis.SymbolFilter filter = Microsoft.CodeAnalysis.SymbolFilter.TypeAndMember, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> bool
abstract Microsoft.CodeAnalysis.Compilation.GetSymbolsWithName(string name, Microsoft.CodeAnalysis.SymbolFilter filter = Microsoft.CodeAnalysis.SymbolFilter.TypeAndMember, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.ISymbol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal abstract partial class AbstractSymbolDisplayVisitor : SymbolVisitor
protected readonly SemanticModel semanticModelOpt;
protected readonly int positionOpt;

private AbstractSymbolDisplayVisitor _lazyFirstSymbolContainingTypeVisitor;
private AbstractSymbolDisplayVisitor _lazyNotFirstVisitor;
private AbstractSymbolDisplayVisitor _lazyNotFirstVisitorNamespaceOrType;

Expand Down Expand Up @@ -45,6 +46,19 @@ protected AbstractSymbolDisplayVisitor(
}
}

protected AbstractSymbolDisplayVisitor FirstSymbolContainingTypeVisitor
{
get
{
if (_lazyFirstSymbolContainingTypeVisitor == null)
{
_lazyFirstSymbolContainingTypeVisitor = MakeFirstSymbolContainingTypeVisitor();
}

return _lazyFirstSymbolContainingTypeVisitor;
}
}

protected AbstractSymbolDisplayVisitor NotFirstVisitor
{
get
Expand All @@ -71,6 +85,8 @@ protected AbstractSymbolDisplayVisitor NotFirstVisitorNamespaceOrType
}
}

protected abstract AbstractSymbolDisplayVisitor MakeFirstSymbolContainingTypeVisitor();

protected abstract AbstractSymbolDisplayVisitor MakeNotFirstVisitor(bool inNamespaceOrType = false);

protected abstract void AddLiteralValue(SpecialType type, object value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,38 @@ internal abstract partial class AbstractSymbolDisplayVisitor : SymbolVisitor

protected bool IsMinimizing
{
get { return this.semanticModelOpt != null; }
get
{
if (semanticModelOpt == null)
{
return false;
}

if (isFirstSymbolVisited && format.MiscellaneousOptions.IncludesOption(SymbolDisplayMiscellaneousOptions.QualifyFirstSymbol))
{
return false;
}

return true;
}
}

protected bool CanUseSpecialTypes
{
get
{
if (!format.MiscellaneousOptions.IncludesOption(SymbolDisplayMiscellaneousOptions.UseSpecialTypes))
{
return false;
}

if (isFirstSymbolVisited && format.MiscellaneousOptions.IncludesOption(SymbolDisplayMiscellaneousOptions.QualifyFirstSymbol))
{
return false;
}

return true;
}
}

protected bool NameBoundSuccessfullyToSameSymbol(INamedTypeSymbol symbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,14 @@ internal SymbolDisplayFormat(
public SymbolDisplayFormat WithMiscellaneousOptions(SymbolDisplayMiscellaneousOptions options)
{
return new SymbolDisplayFormat(
this.CompilerInternalOptions,
this.GlobalNamespaceStyle,
this.TypeQualificationStyle,
this.GenericsOptions,
this.MemberOptions,
this.ParameterOptions,
this.DelegateStyle,
this.ExtensionMethodStyle,
this.ParameterOptions,
this.PropertyStyle,
this.LocalOptions,
this.KindOptions,
Expand Down Expand Up @@ -462,13 +463,14 @@ public SymbolDisplayFormat RemoveMiscellaneousOptions(SymbolDisplayMiscellaneous
public SymbolDisplayFormat WithGenericsOptions(SymbolDisplayGenericsOptions options)
{
return new SymbolDisplayFormat(
this.CompilerInternalOptions,
this.GlobalNamespaceStyle,
this.TypeQualificationStyle,
options,
this.MemberOptions,
this.ParameterOptions,
this.DelegateStyle,
this.ExtensionMethodStyle,
this.ParameterOptions,
this.PropertyStyle,
this.LocalOptions,
this.KindOptions,
Expand Down Expand Up @@ -511,13 +513,14 @@ public SymbolDisplayFormat RemoveGenericsOptions(SymbolDisplayGenericsOptions op
public SymbolDisplayFormat WithMemberOptions(SymbolDisplayMemberOptions options)
{
return new SymbolDisplayFormat(
this.CompilerInternalOptions,
this.GlobalNamespaceStyle,
this.TypeQualificationStyle,
this.GenericsOptions,
options,
this.ParameterOptions,
this.DelegateStyle,
this.ExtensionMethodStyle,
this.ParameterOptions,
this.PropertyStyle,
this.LocalOptions,
this.KindOptions,
Expand Down Expand Up @@ -564,13 +567,14 @@ public SymbolDisplayFormat RemoveMemberOptions(SymbolDisplayMemberOptions option
public SymbolDisplayFormat WithKindOptions(SymbolDisplayKindOptions options)
{
return new SymbolDisplayFormat(
this.CompilerInternalOptions,
this.GlobalNamespaceStyle,
this.TypeQualificationStyle,
this.GenericsOptions,
this.MemberOptions,
this.ParameterOptions,
this.DelegateStyle,
this.ExtensionMethodStyle,
this.ParameterOptions,
this.PropertyStyle,
this.LocalOptions,
options,
Expand Down Expand Up @@ -615,13 +619,14 @@ public SymbolDisplayFormat RemoveKindOptions(SymbolDisplayKindOptions options)
public SymbolDisplayFormat WithParameterOptions(SymbolDisplayParameterOptions options)
{
return new SymbolDisplayFormat(
this.CompilerInternalOptions,
this.GlobalNamespaceStyle,
this.TypeQualificationStyle,
this.GenericsOptions,
this.MemberOptions,
options,
this.DelegateStyle,
this.ExtensionMethodStyle,
options,
this.PropertyStyle,
this.LocalOptions,
this.KindOptions,
Expand Down Expand Up @@ -666,13 +671,14 @@ public SymbolDisplayFormat RemoveParameterOptions(SymbolDisplayParameterOptions
public SymbolDisplayFormat WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle style)
{
return new SymbolDisplayFormat(
this.CompilerInternalOptions,
style,
this.TypeQualificationStyle,
this.GenericsOptions,
this.MemberOptions,
this.ParameterOptions,
this.DelegateStyle,
this.ExtensionMethodStyle,
this.ParameterOptions,
this.PropertyStyle,
this.LocalOptions,
this.KindOptions,
Expand All @@ -689,13 +695,14 @@ public SymbolDisplayFormat WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespace
public SymbolDisplayFormat WithLocalOptions(SymbolDisplayLocalOptions options)
{
return new SymbolDisplayFormat(
this.CompilerInternalOptions,
this.GlobalNamespaceStyle,
this.TypeQualificationStyle,
this.GenericsOptions,
this.MemberOptions,
this.ParameterOptions,
this.DelegateStyle,
this.ExtensionMethodStyle,
this.ParameterOptions,
this.PropertyStyle,
options,
this.KindOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,13 @@ public enum SymbolDisplayMiscellaneousOptions
/// the special question mark syntax.
/// </summary>
ExpandNullable = 1 << 5,

/// <summary>
/// Suppresses minimization of the first visited symbol.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to admit, it's not clear to me what "first visited symbol" means in this case as an external consumer of the API. The visitor is an implementation detail of this API, no? It seems the term shouldn't appear here then.

/// </summary>
/// <remarks>
/// Has no effect outside <see cref="ISymbol.ToMinimalDisplayString"/>.
/// </remarks>
QualifyFirstSymbol = 1 << 6,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return
End If

If format.MiscellaneousOptions.IncludesOption(SymbolDisplayMiscellaneousOptions.UseSpecialTypes) Then
If CanUseSpecialTypes Then
If AddSpecialTypeKeyword(symbol) Then
Return
End If
Expand Down Expand Up @@ -157,7 +157,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Dim containingType = symbol.ContainingType
If containingType IsNot Nothing Then
visitedParents = True
containingType.Accept(Me.NotFirstVisitor())
containingType.Accept(Me.FirstSymbolContainingTypeVisitor())
AddOperator(SyntaxKind.DotToken)
End If
End If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Me._escapeKeywordIdentifiers = escapeKeywordIdentifiers
End Sub

Protected Overrides Function MakeFirstSymbolContainingTypeVisitor() As AbstractSymbolDisplayVisitor
If Not isFirstSymbolVisited OrElse format.KindOptions = SymbolDisplayKindOptions.None Then
Return Me
End If

Return New SymbolDisplayVisitor(
Me.builder,
Me.format.WithKindOptions(SymbolDisplayKindOptions.None),
Me.semanticModelOpt,
Me.positionOpt,
Me._escapeKeywordIdentifiers,
isFirstSymbolVisited:=True,
inNamespaceOrType)
End Function

' in case the display of a symbol is different for a type that acts as a container, use this visitor
Protected Overrides Function MakeNotFirstVisitor(Optional inNamespaceOrType As Boolean = False) As AbstractSymbolDisplayVisitor
Return New SymbolDisplayVisitor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1864,10 +1864,10 @@ End Class

CompilationUtils.AssertTheseDiagnostics(compilation,
<expected>
BC30508: 'B' cannot expose type 'A.B(Of T).C' in class 'A' through class 'B'.
BC30508: 'B' cannot expose type 'A.B(Of T As A.B(Of T).C).C' in class 'A' through class 'B'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change expected? The resulting error message is harder to read.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my comment above explaining why this happens and a possible alternative. I can go either way but wasn't sure about the broader impact of changing a predefined format.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussing this offline, I think we should consider changing the VB diagnostic format to report A(Of T As X).B(Of U As Y) as A(Of T).B(Of U). That change could be handled separately from this PR, although it might block this PR. If you agree, please log a bug, thanks.

Private Class B(Of T As B(Of T).C)
~~~~~~~~~
BC30508: 'D' cannot expose type 'A.B(Of T).C' in class 'A' through class 'B'.
BC30508: 'D' cannot expose type 'A.B(Of T As A.B(Of T).C).C' in class 'A' through class 'B'.
Public Sub D(Of S As C)()
~
</expected>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ End Class
BC30909: 'B' cannot expose type 'A' outside the project through class 'B'.
Public Class B(Of T As A, U As C)
~
BC30508: 'B' cannot expose type 'B(Of T, U).C' in namespace '<Default>' through class 'B'.
BC30508: 'B' cannot expose type 'B(Of T As A, U As B(Of T, U).C).C' in namespace '<Default>' through class 'B'.
Public Class B(Of T As A, U As C)
~
]]>
Expand Down Expand Up @@ -2931,7 +2931,7 @@ BC36739: Type 'IT(Of T).IAI' does not inherit the generic type parameters of its
BC36739: Type 'IT(Of T).IAI.IAI' does not inherit the generic type parameters of its container.
Implements IT(Of Object).IAI.IAI ' BC36739 (not reported by Dev11)
~~~~~~~~~~~~~~~~~~~~~
BC36739: Type 'ITU(Of T, U).ITU2' does not inherit the generic type parameters of its container.
BC36739: Type 'ITU(Of T, U As T).ITU2' does not inherit the generic type parameters of its container.
Implements ITU(Of Object, Object).ITU2 ' BC36739 (not reported by Dev11)
~~~~~~~~~~~~~~~~~~~~~~~~~~~
BC36739: Type 'IT(Of T).IF' does not inherit the generic type parameters of its container.
Expand All @@ -2940,10 +2940,10 @@ BC36739: Type 'IT(Of T).IF' does not inherit the generic type parameters of its
BC36739: Type 'IT(Of T).IIn' does not inherit the generic type parameters of its container.
Implements IT(Of Object).IIn ' BC36739
~~~~~~~~~~~~~~~~~
BC36739: Type 'IAI(Of T).IT' does not inherit the generic type parameters of its container.
BC36739: Type 'IAI(Of T As {A, I}).IT' does not inherit the generic type parameters of its container.
Implements IAI(Of C).IT ' BC36739 (not reported by Dev11)
~~~~~~~~~~~~
BC36739: Type 'CF(Of T).CT' does not inherit the generic type parameters of its container.
BC36739: Type 'CF(Of T As {Class, New}).CT' does not inherit the generic type parameters of its container.
Inherits CF(Of C).CT ' BC36739
~~~~~~~~~~~
BC36739: Type 'IIn(Of In T).IT' does not inherit the generic type parameters of its container.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ End Class
Assert.Equal("C1(Of C1T1, C1T2) : {C1T1->Integer, C1T2->Integer}", substitution1.ToString())

substitution2 = TypeSubstitution.Create(c4, {c3.TypeParameters(0), c4.TypeParameters(0)}, {bte, chr})
Assert.Equal("C1(Of C1T1, C1T2).C2(Of C2T1, C2T2).C3(Of C3T1, C3T2).C4(Of C4T1) : {C3T1->Byte}, {C4T1->Char}", substitution2.ToString())
Assert.Equal("C1(Of C1T1, C1T2).C2(Of C2T1, C2T2).C3(Of C3T1, C3T2 As C1T1).C4(Of C4T1) : {C3T1->Byte}, {C4T1->Char}", substitution2.ToString())

Assert.Same(substitution1, TypeSubstitution.Concat(c1, Nothing, substitution1))
Assert.Same(substitution1, TypeSubstitution.Concat(c1, substitution1, Nothing))
Expand All @@ -714,7 +714,7 @@ End Class
Assert.Equal("C1(Of C1T1, C1T2).C2(Of C2T1, C2T2) : {C1T1->Integer, C1T2->Integer}, {}", substitution3.ToString())

substitution3 = TypeSubstitution.Concat(c4, substitution1, substitution2)
Assert.Equal("C1(Of C1T1, C1T2).C2(Of C2T1, C2T2).C3(Of C3T1, C3T2).C4(Of C4T1) : {C1T1->Integer, C1T2->Integer}, {}, {C3T1->Byte}, {C4T1->Char}", substitution3.ToString())
Assert.Equal("C1(Of C1T1, C1T2).C2(Of C2T1, C2T2).C3(Of C3T1, C3T2 As C1T1).C4(Of C4T1) : {C1T1->Integer, C1T2->Integer}, {}, {C3T1->Byte}, {C4T1->Char}", substitution3.ToString())

Assert.Null(TypeSubstitution.Create(c4, {c1.TypeParameters(0)}, {c1.TypeParameters(0)}))
End Sub
Expand Down
Loading