Skip to content

Commit

Permalink
Update SymbolDisplay API to include scoped for parameters and locals (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cston authored Oct 1, 2022
1 parent 191eb64 commit 6260291
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -812,20 +812,36 @@ public override void VisitParameter(IParameterSymbol symbol)

if (includeType)
{
AddParameterRefKindIfNeeded(symbol);
AddCustomModifiersIfNeeded(symbol.RefCustomModifiers, leadingSpace: false, trailingSpace: true);

if (symbol.ScopedKind == ScopedKind.ScopedValue &&
format.CompilerInternalOptions.IncludesOption(SymbolDisplayCompilerInternalOptions.IncludeScoped))
if (format.ParameterOptions.IncludesOption(SymbolDisplayParameterOptions.IncludeModifiers))
{
AddKeyword(SyntaxKind.ScopedKeyword);
AddSpace();
// Add 'scoped' unless the parameter is an out parameter or
// 'this' since those cases are implicitly scoped.
if (symbol.ScopedKind == ScopedKind.ScopedRef &&
symbol.RefKind != RefKind.Out &&
!symbol.IsThis)
{
AddKeyword(SyntaxKind.ScopedKeyword);
AddSpace();
}

AddParameterRefKind(symbol.RefKind);
}

if (symbol.IsParams && format.ParameterOptions.IncludesOption(SymbolDisplayParameterOptions.IncludeParamsRefOut))
AddCustomModifiersIfNeeded(symbol.RefCustomModifiers, leadingSpace: false, trailingSpace: true);

if (format.ParameterOptions.IncludesOption(SymbolDisplayParameterOptions.IncludeModifiers))
{
AddKeyword(SyntaxKind.ParamsKeyword);
AddSpace();
if (symbol.ScopedKind == ScopedKind.ScopedValue && symbol.RefKind == RefKind.None)
{
AddKeyword(SyntaxKind.ScopedKeyword);
AddSpace();
}

if (symbol.IsParams)
{
AddKeyword(SyntaxKind.ParamsKeyword);
AddSpace();
}
}

symbol.Type.Accept(this.NotFirstVisitor);
Expand Down Expand Up @@ -1111,26 +1127,6 @@ private void AddReadOnlyIfNeeded()
}
}

private void AddParameterRefKindIfNeeded(IParameterSymbol symbol)
{
if (format.ParameterOptions.IncludesOption(SymbolDisplayParameterOptions.IncludeParamsRefOut))
{
if (symbol.ScopedKind == ScopedKind.ScopedRef &&
!symbol.IsThis &&
format.CompilerInternalOptions.IncludesOption(SymbolDisplayCompilerInternalOptions.IncludeScoped))
{
var parameter = (symbol as Symbols.PublicModel.ParameterSymbol)?.GetSymbol<ParameterSymbol>();
if (parameter is null || !ParameterHelpers.IsRefScopedByDefault(parameter))
{
AddKeyword(SyntaxKind.ScopedKeyword);
AddSpace();
}
}

AddParameterRefKind(symbol.RefKind);
}
}

private void AddParameterRefKind(RefKind refKind)
{
switch (refKind)
Expand Down
35 changes: 17 additions & 18 deletions src/Compilers/CSharp/Portable/SymbolDisplay/SymbolDisplayVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,33 +187,32 @@ private void AddGlobalNamespace(INamespaceSymbol globalNamespace)

public override void VisitLocal(ILocalSymbol symbol)
{
if (symbol.IsRef &&
format.LocalOptions.IncludesOption(SymbolDisplayLocalOptions.IncludeRef))
if (format.LocalOptions.IncludesOption(SymbolDisplayLocalOptions.IncludeModifiers))
{
if (symbol.ScopedKind == ScopedKind.ScopedRef &&
format.CompilerInternalOptions.IncludesOption(SymbolDisplayCompilerInternalOptions.IncludeScoped))
if (symbol.IsRef)
{
AddKeyword(SyntaxKind.ScopedKeyword);
AddSpace();
}
if (symbol.ScopedKind == ScopedKind.ScopedRef)
{
AddKeyword(SyntaxKind.ScopedKeyword);
AddSpace();
}

AddKeyword(SyntaxKind.RefKeyword);
AddSpace();
AddKeyword(SyntaxKind.RefKeyword);
AddSpace();

if (symbol.RefKind == RefKind.RefReadOnly)
if (symbol.RefKind == RefKind.RefReadOnly)
{
AddKeyword(SyntaxKind.ReadOnlyKeyword);
AddSpace();
}
}
else if (symbol.ScopedKind == ScopedKind.ScopedValue)
{
AddKeyword(SyntaxKind.ReadOnlyKeyword);
AddKeyword(SyntaxKind.ScopedKeyword);
AddSpace();
}
}

if (symbol.ScopedKind == ScopedKind.ScopedValue &&
format.CompilerInternalOptions.IncludesOption(SymbolDisplayCompilerInternalOptions.IncludeScoped))
{
AddKeyword(SyntaxKind.ScopedKeyword);
AddSpace();
}

if (format.LocalOptions.IncludesOption(SymbolDisplayLocalOptions.IncludeType))
{
symbol.Type.Accept(this.NotFirstVisitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public static void F(scoped ref int i) { }
}";
var comp = CreateCompilation(new[] { ScopedRefAttributeDefinition, source });
var expected =
@"void Program.F(ref System.Int32 i)
[ScopedRef] ref System.Int32 i
@"void Program.F(scoped ref System.Int32 i)
[ScopedRef] scoped ref System.Int32 i
";
CompileAndVerify(comp, symbolValidator: module =>
{
Expand All @@ -62,8 +62,8 @@ public static void F(scoped ref int i) { }
}";
comp = CreateCompilation(source, references: new[] { ref0 });
var expected =
@"void Program.F(ref System.Int32 i)
[ScopedRef] ref System.Int32 i
@"void Program.F(scoped ref System.Int32 i)
[ScopedRef] scoped ref System.Int32 i
";
CompileAndVerify(comp, symbolValidator: module =>
{
Expand Down Expand Up @@ -258,22 +258,22 @@ static void Main()
Diagnostic(ErrorCode.ERR_BindToBogus, "F2").WithArguments("A.F2(int)").WithLocation(7, 11));

var method = comp.GetMember<MethodSymbol>("A.F1");
Assert.Equal("void A.F1(scoped R r)", method.ToDisplayString(SymbolDisplayFormat.TestFormat.WithCompilerInternalOptions(SymbolDisplayCompilerInternalOptions.IncludeScoped)));
Assert.Equal("void A.F1(scoped R r)", method.ToTestDisplayString());
var parameter = method.Parameters[0];
Assert.Equal(DeclarationScope.ValueScoped, parameter.EffectiveScope);

method = comp.GetMember<MethodSymbol>("A.F2");
Assert.Equal("void A.F2(System.Int32 y)", method.ToDisplayString(SymbolDisplayFormat.TestFormat.WithCompilerInternalOptions(SymbolDisplayCompilerInternalOptions.IncludeScoped)));
Assert.Equal("void A.F2(System.Int32 y)", method.ToTestDisplayString());
parameter = method.Parameters[0];
Assert.Equal(DeclarationScope.Unscoped, parameter.EffectiveScope);

method = comp.GetMember<MethodSymbol>("A.F3");
Assert.Equal("void A.F3(System.Object x, scoped ref System.Int32 y)", method.ToDisplayString(SymbolDisplayFormat.TestFormat.WithCompilerInternalOptions(SymbolDisplayCompilerInternalOptions.IncludeScoped)));
Assert.Equal("void A.F3(System.Object x, scoped ref System.Int32 y)", method.ToTestDisplayString());
parameter = method.Parameters[1];
Assert.Equal(DeclarationScope.RefScoped, parameter.EffectiveScope);

method = comp.GetMember<MethodSymbol>("A.F4");
Assert.Equal("void A.F4(scoped ref R r)", method.ToDisplayString(SymbolDisplayFormat.TestFormat.WithCompilerInternalOptions(SymbolDisplayCompilerInternalOptions.IncludeScoped)));
Assert.Equal("void A.F4(scoped ref R r)", method.ToTestDisplayString());
parameter = method.Parameters[0];
Assert.Equal(DeclarationScope.RefScoped, parameter.EffectiveScope);
}
Expand Down Expand Up @@ -338,15 +338,15 @@ public static void F(scoped R r) { }
}";
var comp = CreateCompilation(source);
var expected =
@"S..ctor(ref System.Int32 i)
[ScopedRef] ref System.Int32 i
void S.F(R r)
[ScopedRef] R r
S S.op_Addition(S a, in R b)
@"S..ctor(scoped ref System.Int32 i)
[ScopedRef] scoped ref System.Int32 i
void S.F(scoped R r)
[ScopedRef] scoped R r
S S.op_Addition(S a, scoped in R b)
S a
[ScopedRef] in R b
System.Object S.this[in System.Int32 i].get
[ScopedRef] in System.Int32 i
[ScopedRef] scoped in R b
System.Object S.this[scoped in System.Int32 i].get
[ScopedRef] scoped in System.Int32 i
";
CompileAndVerify(comp, symbolValidator: module =>
{
Expand Down Expand Up @@ -414,10 +414,11 @@ public static void F5(scoped in R r) { }
}";
var comp = CreateCompilation(source);
var expected =
@"void Program.F4(ref R r)
[ScopedRef] ref R r
void Program.F5(in R r)
[ScopedRef] in R r";
@"void Program.F4(scoped ref R r)
[ScopedRef] scoped ref R r
void Program.F5(scoped in R r)
[ScopedRef] scoped in R r
";
CompileAndVerify(comp, symbolValidator: module =>
{
Assert.Equal("System.Runtime.CompilerServices.ScopedRefAttribute", GetScopedRefType(module).ToTestDisplayString());
Expand All @@ -436,16 +437,16 @@ public void EmitAttribute_DelegateParameters()
";
var comp = CreateCompilation(source);
var expected =
@"void D.Invoke(in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 x, R y)
[ScopedRef] in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 x
[ScopedRef] R y
System.IAsyncResult D.BeginInvoke(in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 x, R y, System.AsyncCallback callback, System.Object @object)
[ScopedRef] in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 x
[ScopedRef] R y
@"void D.Invoke(scoped in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 x, scoped R y)
[ScopedRef] scoped in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 x
[ScopedRef] scoped R y
System.IAsyncResult D.BeginInvoke(scoped in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 x, scoped R y, System.AsyncCallback callback, System.Object @object)
[ScopedRef] scoped in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 x
[ScopedRef] scoped R y
System.AsyncCallback callback
System.Object @object
void D.EndInvoke(in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 x, System.IAsyncResult result)
[ScopedRef] in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 x
void D.EndInvoke(scoped in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 x, System.IAsyncResult result)
[ScopedRef] scoped in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 x
System.IAsyncResult result
";
CompileAndVerify(
Expand Down Expand Up @@ -473,17 +474,17 @@ static void Main()
}";
var comp = CreateCompilation(source);
var expected =
@"void D.Invoke(in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 i)
[ScopedRef] in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 i
System.IAsyncResult D.BeginInvoke(in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 i, System.AsyncCallback callback, System.Object @object)
[ScopedRef] in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 i
@"void D.Invoke(scoped in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 i)
[ScopedRef] scoped in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 i
System.IAsyncResult D.BeginInvoke(scoped in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 i, System.AsyncCallback callback, System.Object @object)
[ScopedRef] scoped in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 i
System.AsyncCallback callback
System.Object @object
void D.EndInvoke(in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 i, System.IAsyncResult result)
[ScopedRef] in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 i
void D.EndInvoke(scoped in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 i, System.IAsyncResult result)
[ScopedRef] scoped in modreq(System.Runtime.InteropServices.InAttribute) System.Int32 i
System.IAsyncResult result
void Program.<>c.<Main>b__0_0(in System.Int32 i)
[ScopedRef] in System.Int32 i
void Program.<>c.<Main>b__0_0(scoped in System.Int32 i)
[ScopedRef] scoped in System.Int32 i
";
CompileAndVerify(
source,
Expand All @@ -509,8 +510,8 @@ void L(scoped in int i) { }
}";
var comp = CreateCompilation(source);
var expected =
@"void Program.<M>g__L|0_0(in System.Int32 i)
[ScopedRef] in System.Int32 i
@"void Program.<M>g__L|0_0(scoped in System.Int32 i)
[ScopedRef] scoped in System.Int32 i
";
CompileAndVerify(
source,
Expand Down Expand Up @@ -539,14 +540,14 @@ static void Main()
}";
var comp = CreateCompilation(source);
var expected =
@"void <>f__AnonymousDelegate0.Invoke(in System.Int32 value)
[ScopedRef] in System.Int32 value
R <>f__AnonymousDelegate1.Invoke(R value)
[ScopedRef] R value
void Program.<>c.<Main>b__0_0(in System.Int32 i)
[ScopedRef] in System.Int32 i
R Program.<>c.<Main>b__0_1(R r)
[ScopedRef] R r
@"void <>f__AnonymousDelegate0.Invoke(scoped in System.Int32 value)
[ScopedRef] scoped in System.Int32 value
R <>f__AnonymousDelegate1.Invoke(scoped R value)
[ScopedRef] scoped R value
void Program.<>c.<Main>b__0_0(scoped in System.Int32 i)
[ScopedRef] scoped in System.Int32 i
R Program.<>c.<Main>b__0_1(scoped R r)
[ScopedRef] scoped R r
";
CompileAndVerify(
source,
Expand Down
Loading

0 comments on commit 6260291

Please sign in to comment.