Skip to content
Merged
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 @@ -9156,5 +9156,54 @@ public void PreprocessingSymbol()
],
actual: displayParts);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/36654")]
public void MinimalNameWithConflict()
{
var source =
"""
using System;
namespace Abc.System {}
namespace Abc
{
public class Test
{
public void fff()
{
Video vvv = null;
foreach (var ooo in vvv.getlist()) {}
}
}
}

class Video
{
public System.Collections.Generic.IEnumerable<int> getlist() { return null; }
}
""";

var compilation = CreateCompilation(source);
var tree = compilation.SyntaxTrees[0];
var model = compilation.GetSemanticModel(tree);

var invocation = tree.GetRoot().DescendantNodes().OfType<InvocationExpressionSyntax>().Single();
Copy link
Member

Choose a reason for hiding this comment

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

nit: FYI, we have a helper I like to use that lets you do var invocation = GetSyntax<InvocationExpressionSyntax>(tree, "vvv.getlist()");
The advantage is that it is immediately apparent to reviewers what invocation syntax we're grabbing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@copilot please apply the above suggestion.

Copy link
Member

Choose a reason for hiding this comment

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

FWIW, I don't think it did it

var type = model.GetTypeInfo(invocation).Type;

// ToMinimalDisplayString does not include 'global::' by default even with a conflict due it
// explicitly having the flag SymbolDisplayGlobalNamespaceStyle.Omitted.
Assert.Equal("System.Collections.Generic.IEnumerable<int>", type.ToMinimalDisplayString(model, invocation.SpanStart));

// Demonstrate that one can opt into getting 'global::' included.
Assert.Equal(
"global::System.Collections.Generic.IEnumerable<int>",
type.ToMinimalDisplayString(model, invocation.SpanStart,
SymbolDisplayFormat.MinimallyQualifiedFormat.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Included)));

// However, opting into getting 'global::' included does not force it to be included when not needed.
Assert.Equal(
"System.Collections.Generic.IEnumerable<int>",
type.ToMinimalDisplayString(model, tree.GetRoot().Span.End,
SymbolDisplayFormat.MinimallyQualifiedFormat.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Included)));
}
}
}