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 crash with type parameter on type constrained to file-local type #62448

Merged
merged 5 commits into from
Jul 8, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ private TypeParameterConstraintClause GetDefaultTypeParameterConstraintClause(Ty
diagnostics.Add(ErrorCode.ERR_BadVisBound, location, containingSymbol, constraintType.Type);
}

if (constraintType.Type.IsFileTypeOrUsesFileTypes() && !containingSymbol.ContainingType.IsFileTypeOrUsesFileTypes())
// Whether a member is effectively file-local is determined on the type level. When non-type members have constraints we need to check the containing symbol.
// If the 'containingSymbol.ContainingSymbol' is not a type, then 'containingSymbol' is not a member, and we don't need to enforce file types here.
if (constraintType.Type.IsFileTypeOrUsesFileTypes() && (containingSymbol as TypeSymbol ?? containingSymbol.ContainingSymbol as TypeSymbol)?.IsFileTypeOrUsesFileTypes() == false)
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
{
diagnostics.Add(ErrorCode.ERR_FileTypeDisallowedInSignature, location, constraintType.Type, containingSymbol);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests;
Expand Down Expand Up @@ -2074,6 +2075,81 @@ class E
Diagnostic(ErrorCode.ERR_FileTypeDisallowedInSignature, "C").WithArguments("C", "E.M<T>(T)").WithLocation(10, 30));
}

[Theory, WorkItem(62435, "https://github.com/dotnet/roslyn/issues/62435")]
[InlineData("class")]
[InlineData("struct")]
[InlineData("interface")]
[InlineData("record")]
[InlineData("record struct")]
public void Constraints_02(string typeKind)
{
var source = $$"""
file class C { }

file {{typeKind}} D<T> where T : C // ok
{
}

{{typeKind}} E<T> where T : C // 1
{
}
""";

var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (7,{{17 + typeKind.Length}}): error CS9051: File type 'C' cannot be used in a member signature in non-file type 'E<T>'.
// {{typeKind}} E<T> where T : C // 1
Diagnostic(ErrorCode.ERR_FileTypeDisallowedInSignature, "C").WithArguments("C", "E<T>").WithLocation(7, 17 + typeKind.Length));
}

[Fact]
public void Constraints_03()
{
var source = """
file class C { }

file class D
{
void M()
{
local(new C());
void local<T>(T t) where T : C { } // ok
}
}

class E
{
void M()
{
local(new C());
void local<T>(T t) where T : C { } // ok
}
}
""";

// Local functions aren't members, so we don't give any diagnostics when their signatures contain file types.
var comp = CreateCompilation(source);
comp.VerifyDiagnostics();
}

[Fact]
public void Constraints_04()
{
var source = """
file class C { }

file delegate void D1<T>(T t) where T : C; // ok

delegate void D2<T>(T t) where T : C; // 1
""";

var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (5,36): error CS9051: File type 'C' cannot be used in a member signature in non-file type 'D2<T>'.
// delegate void D2<T>(T t) where T : C; // 1
Diagnostic(ErrorCode.ERR_FileTypeDisallowedInSignature, "C").WithArguments("C", "D2<T>").WithLocation(5, 36));
}

[Fact]
public void PrimaryConstructor_01()
{
Expand Down