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

Fix type parameter scoping for local functions #59968

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -358,7 +358,7 @@ private static Binder GetEnclosingBinderInternalWithinRoot(SyntaxNode node, int
LocalFunctionSymbol function = GetDeclaredLocalFunction(binder, ownerOfTypeParametersInScope.Identifier);
if ((object)function != null)
{
binder = function.SignatureBinder;
binder = function.ParameterBinder;
Copy link
Member Author

Choose a reason for hiding this comment

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

SignatureBinder is used for attribute binding, hence, it doesn't have type parameters in scope. This code path used by LookupPosition wants to have type parameters in scope, so I switched to using ParameterBinder here.

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Symbols
internal sealed class LocalFunctionSymbol : SourceMethodSymbolWithAttributes
{
private readonly Binder _binder;
private readonly Binder? _withTypeParamsBinder;
private readonly Symbol _containingSymbol;
private readonly DeclarationModifiers _declarationModifiers;
private readonly ImmutableArray<SourceMethodTypeParameterSymbol> _typeParameters;
Expand Down Expand Up @@ -60,7 +61,7 @@ internal sealed class LocalFunctionSymbol : SourceMethodSymbolWithAttributes

if (syntax.TypeParameterList != null)
{
binder = new WithMethodTypeParametersBinder(this, binder);
_withTypeParamsBinder = new WithMethodTypeParametersBinder(this, binder);
_typeParameters = MakeTypeParameters(_declarationDiagnostics);
}
else
Expand Down Expand Up @@ -105,7 +106,7 @@ internal sealed class LocalFunctionSymbol : SourceMethodSymbolWithAttributes
/// </summary>
internal Binder ScopeBinder { get; }

internal override Binder ParameterBinder => _binder;
internal override Binder ParameterBinder => _withTypeParamsBinder ?? _binder;

internal LocalFunctionStatementSyntax Syntax => (LocalFunctionStatementSyntax)syntaxReferenceOpt.GetSyntax();

Expand Down Expand Up @@ -180,7 +181,7 @@ private void ComputeParameters()
var diagnostics = BindingDiagnosticBag.GetInstance(_declarationDiagnostics);

var parameters = ParameterHelpers.MakeParameters(
_binder,
_withTypeParamsBinder ?? _binder,
this,
this.Syntax.ParameterList,
arglistToken: out arglistToken,
Expand Down Expand Up @@ -239,7 +240,7 @@ internal void ComputeReturnType()

var diagnostics = BindingDiagnosticBag.GetInstance(_declarationDiagnostics);
TypeSyntax returnTypeSyntax = Syntax.ReturnType;
TypeWithAnnotations returnType = _binder.BindType(returnTypeSyntax.SkipRef(), diagnostics);
TypeWithAnnotations returnType = (_withTypeParamsBinder ?? _binder).BindType(returnTypeSyntax.SkipRef(), diagnostics);

var compilation = DeclaringCompilation;

Expand Down Expand Up @@ -476,7 +477,7 @@ public override ImmutableArray<ImmutableArray<TypeWithAnnotations>> GetTypeParam
var syntax = Syntax;
var diagnostics = BindingDiagnosticBag.GetInstance(_declarationDiagnostics);
var constraints = this.MakeTypeParameterConstraintTypes(
_binder,
_withTypeParamsBinder ?? _binder,
TypeParameters,
syntax.TypeParameterList,
syntax.ConstraintClauses,
Expand All @@ -501,7 +502,7 @@ public override ImmutableArray<TypeParameterConstraintKind> GetTypeParameterCons
{
var syntax = Syntax;
var constraints = this.MakeTypeParameterConstraintKinds(
_binder,
_withTypeParamsBinder ?? _binder,
TypeParameters,
syntax.TypeParameterList,
syntax.ConstraintClauses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7117,5 +7117,38 @@ async void F4()
// async static void B4<await>() { }
Diagnostic(ErrorCode.WRN_LowerCaseTypeName, "await").WithArguments("await").WithLocation(23, 30));
}

[Fact]
public void TypeParameterScope()
{
var comp = CreateCompilation(@"
class C
{
void M()
{
local<object>();

[My(nameof(TParameter))] // works, but why?
void local<TParameter>() { }
}

[My(nameof(TParameter))] // error CS0103: The name 'TParameter' does not exist in the current context
void M2<TParameter>() { }
}

public class MyAttribute : System.Attribute
{
public MyAttribute(string name1) { }
}
");
comp.VerifyDiagnostics(
// (8,20): error CS0103: The name 'TParameter' does not exist in the current context
// [My(nameof(TParameter))] // works, but why?
Diagnostic(ErrorCode.ERR_NameNotInContext, "TParameter").WithArguments("TParameter").WithLocation(8, 20),
// (12,16): error CS0103: The name 'TParameter' does not exist in the current context
// [My(nameof(TParameter))] // error CS0103: The name 'TParameter' does not exist in the current context
Diagnostic(ErrorCode.ERR_NameNotInContext, "TParameter").WithArguments("TParameter").WithLocation(12, 16)
);
}
}
}