Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2493,7 +2493,8 @@ protected AssemblySymbol GetForwardedToAssembly(string name, int arity, ref Name

// NOTE: This won't work if the type isn't using CLS-style generic naming (i.e. `arity), but this code is
// only intended to improve diagnostic messages, so false negatives in corner cases aren't a big deal.
var metadataName = MetadataHelpers.ComposeAritySuffixedMetadataName(name, arity);
// File types can't be forwarded, so we won't attempt to determine a file identifier to attach to the metadata name.
var metadataName = MetadataHelpers.ComposeAritySuffixedMetadataName(name, arity, associatedFileIdentifier: null);
var fullMetadataName = MetadataHelpers.BuildQualifiedName(qualifierOpt?.ToDisplayString(SymbolDisplayFormat.QualifiedNameOnlyFormat), metadataName);
var result = GetForwardedToAssembly(fullMetadataName, diagnostics, location);
if ((object)result != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ bool Cci.INamedTypeReference.MangleName
}
}

#nullable enable
string? Cci.INamedTypeReference.AssociatedFileIdentifier
{
get
{
return UnderlyingNamedType.AssociatedFileIdentifier();
}
}
#nullable disable

string Cci.INamedEntity.Name
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,16 @@ bool Cci.INamedTypeReference.MangleName
}
}

#nullable enable
string? Cci.INamedTypeReference.AssociatedFileIdentifier
{
get
{
return AdaptedNamedTypeSymbol.AssociatedFileIdentifier();
}
}
#nullable disable

string Cci.INamedEntity.Name
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,11 @@ public override void VisitNamedType(INamedTypeSymbol symbol)
AddNullableAnnotations(symbol);

if ((format.CompilerInternalOptions & SymbolDisplayCompilerInternalOptions.IncludeContainingFileForFileTypes) != 0
// PROTOTYPE(ft): public API?
&& symbol.GetSymbol() is SourceMemberContainerTypeSymbol { IsFile: true } fileType)
&& symbol is Symbols.PublicModel.Symbol { UnderlyingSymbol: SourceMemberContainerTypeSymbol { AssociatedSyntaxTree: SyntaxTree tree } internalSymbol })
{
var tree = symbol.DeclaringSyntaxReferences[0].SyntaxTree;
var fileDescription = tree.FilePath is { Length: not 0 } path
? Path.GetFileNameWithoutExtension(path)
: $"<tree {fileType.DeclaringCompilation.SyntaxTrees.IndexOf(tree)}>";
var fileDescription = tree.GetDisplayFileName() is { Length: not 0 } path
? path
: $"<tree {internalSymbol.DeclaringCompilation.SyntaxTrees.IndexOf(tree)}>";

builder.Add(CreatePart(SymbolDisplayPartKind.Punctuation, symbol, "@"));
builder.Add(CreatePart(SymbolDisplayPartKind.ModuleName, symbol, fileDescription));
Expand Down
4 changes: 3 additions & 1 deletion src/Compilers/CSharp/Portable/Symbols/NamedTypeSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,14 +478,15 @@ public virtual bool IsImplicitClass
/// </summary>
public abstract override string Name { get; }

#nullable enable
/// <summary>
/// Return the name including the metadata arity suffix.
/// </summary>
public override string MetadataName
{
get
{
return MangleName ? MetadataHelpers.ComposeAritySuffixedMetadataName(Name, Arity) : Name;
return MangleName ? MetadataHelpers.ComposeAritySuffixedMetadataName(Name, Arity, this.AssociatedFileIdentifier()) : Name;
}
}

Expand All @@ -498,6 +499,7 @@ internal abstract bool MangleName
// Intentionally no default implementation to force consideration of appropriate implementation for each new subclass
get;
}
#nullable disable

/// <summary>
/// Collection of names of members declared within this type. May return duplicates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ internal override bool MangleName
{
get
{
return Arity > 0;
return Arity > 0 || IsFile;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal enum GeneratedNameKind
ReusableHoistedLocalField = '7',
LambdaCacheField = '9',
FixedBufferField = 'e',
FileType = 'F',
AnonymousType = 'f',
TransparentIdentifier = 'h',
AnonymousTypeField = 'i',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,5 +494,38 @@ internal static string LambdaCopyParameterName(int ordinal)
{
return "<p" + StringExtensions.GetNumeral(ordinal) + ">";
}

internal static string MakeFileIdentifier(string filePath, int ordinal)
{
var pooledBuilder = PooledStringBuilder.GetInstance();
var sb = pooledBuilder.Builder;
sb.Append('<');
AppendFileName(filePath, sb);
sb.Append('>');
sb.Append((char)GeneratedNameKind.FileType);
sb.Append(ordinal);
sb.Append("__");
return pooledBuilder.ToStringAndFree();
}

internal static void AppendFileName(string? filePath, StringBuilder sb)
{
var fileName = FileNameUtilities.GetFileName(filePath, includeExtension: false);
if (fileName is null)
{
return;
}

foreach (var ch in fileName)
{
sb.Append(ch switch
{
>= 'a' and <= 'z' => ch,
>= 'A' and <= 'Z' => ch,
>= '0' and <= '9' => ch,
_ => '_'
});
}
}
}
}
20 changes: 20 additions & 0 deletions src/Compilers/CSharp/Portable/Symbols/TypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;

Expand Down Expand Up @@ -1361,6 +1362,25 @@ public static bool IsFileTypeOrUsesFileTypes(this TypeSymbol type)
return foundType is not null;
}

internal static string? AssociatedFileIdentifier(this NamedTypeSymbol type)
{
Debug.Assert(type.IsDefinition);
if (type is not SourceMemberContainerTypeSymbol { AssociatedSyntaxTree: SyntaxTree tree })
{
return null;
}
var ordinal = type.DeclaringCompilation.GetSyntaxTreeOrdinal(tree);
return GeneratedNames.MakeFileIdentifier(tree.FilePath, ordinal);
}

internal static string GetDisplayFileName(this SyntaxTree tree)
{
var pooledBuilder = PooledStringBuilder.GetInstance();
var sb = pooledBuilder.Builder;
GeneratedNames.AppendFileName(tree.FilePath, sb);
return pooledBuilder.ToStringAndFree();
}

public static bool IsPointerType(this TypeSymbol type)
{
return type is PointerTypeSymbol;
Expand Down
Loading