Skip to content

Commit 1eac69f

Browse files
CopilotCyrusNajmabadijcouvRikkiGibson
authored
Fix ITypeSymbol.BaseType documentation for type parameters (#80770)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com> Co-authored-by: Cyrus Najmabadi <cyrus.najmabadi@gmail.com> Co-authored-by: jcouv <12466233+jcouv@users.noreply.github.com> Co-authored-by: Rikki Gibson <rigibson@microsoft.com>
1 parent 3b5303b commit 1eac69f

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

.github/copilot-instructions.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ var symbolInfo = semanticModel.GetSymbolInfo(expression);
5454
- Test utilities in `Microsoft.CodeAnalysis.Test.Utilities`
5555
- Language-specific test bases: `CSharpTestBase`, `VisualBasicTestBase`
5656
- Add `[WorkItem("https://github.com/dotnet/roslyn/issues/issueNumber")]` attribute to tests that fix specific GitHub issues
57+
- Prefer raw string literals (`"""..."""`) over verbatim strings (`@"..."`) when creating test source code
58+
- Avoid unnecessary intermediary assertions - tests should do the minimal amount of work to validate just the core issue being addressed
59+
- In tests, use concise methods like `.Single()` instead of asserting count and extracting elements
60+
- For compiler tests, validate diagnostics (e.g., `comp.VerifyEmitDiagnostics()`) so reviewers can easily see if the code is in error or represents something legal
5761

5862
## Critical Integration Points
5963

src/Compilers/CSharp/Test/Symbol/Symbols/GenericConstraintTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7460,5 +7460,88 @@ interface Base<N> : Base, ISetup<N> where N : Base<N>.Nest { }
74607460
Assert.Null(model.GetAliasInfo(nest));
74617461
Assert.Equal("Base.Nest", model.GetTypeInfo(nest).Type.ToDisplayString());
74627462
}
7463+
7464+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/41733")]
7465+
public void TypeParameter_BaseType_ReturnsNull()
7466+
{
7467+
var source = """
7468+
abstract class Base
7469+
{
7470+
public abstract void Method();
7471+
}
7472+
7473+
class Derived<T> where T : Base
7474+
{
7475+
}
7476+
""";
7477+
var comp = CreateCompilation(source);
7478+
comp.VerifyDiagnostics();
7479+
7480+
var typeParameter = (ITypeParameterSymbol)comp.GetTypeByMetadataName("Derived`1").TypeParameters[0].GetPublicSymbol();
7481+
Assert.Null(typeParameter.BaseType);
7482+
}
7483+
7484+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/41733")]
7485+
public void MethodTypeParameter_BaseType_ReturnsNull()
7486+
{
7487+
var source = """
7488+
class C
7489+
{
7490+
void M<T>() where T : class
7491+
{
7492+
}
7493+
}
7494+
""";
7495+
var comp = CreateCompilation(source);
7496+
comp.VerifyDiagnostics();
7497+
7498+
var typeParameter = (ITypeParameterSymbol)comp.GetTypeByMetadataName("C").GetMethod("M").TypeParameters[0].GetPublicSymbol();
7499+
Assert.Null(typeParameter.BaseType);
7500+
}
7501+
7502+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/41733")]
7503+
public void ObjectType_BaseType_ReturnsNull()
7504+
{
7505+
var comp = CreateCompilation("");
7506+
comp.VerifyDiagnostics();
7507+
7508+
var objectType = (INamedTypeSymbol)comp.GetSpecialType(SpecialType.System_Object).GetPublicSymbol();
7509+
Assert.Null(objectType.BaseType);
7510+
}
7511+
7512+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/41733")]
7513+
public void InterfaceType_BaseType_ReturnsNull()
7514+
{
7515+
var source = """
7516+
interface IMyInterface
7517+
{
7518+
void Method();
7519+
}
7520+
""";
7521+
var comp = CreateCompilation(source);
7522+
comp.VerifyDiagnostics();
7523+
7524+
var interfaceType = (INamedTypeSymbol)comp.GetTypeByMetadataName("IMyInterface").GetPublicSymbol();
7525+
Assert.Null(interfaceType.BaseType);
7526+
}
7527+
7528+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/41733")]
7529+
public void PointerType_BaseType_ReturnsNull()
7530+
{
7531+
var source = """
7532+
unsafe class C
7533+
{
7534+
int* ptr;
7535+
}
7536+
""";
7537+
var comp = CreateCompilation(source, options: TestOptions.UnsafeDebugDll);
7538+
comp.VerifyDiagnostics(
7539+
// (3,10): warning CS0169: The field 'C.ptr' is never used
7540+
// int* ptr;
7541+
Diagnostic(ErrorCode.WRN_UnreferencedField, "ptr").WithArguments("C.ptr").WithLocation(3, 10));
7542+
7543+
var pointerType = (IPointerTypeSymbol)comp.GetTypeByMetadataName("C").GetField("ptr").Type.GetPublicSymbol();
7544+
Assert.Null(pointerType.BaseType);
7545+
}
74637546
}
74647547
}

src/Compilers/Core/Portable/Symbols/ITypeSymbol.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public interface ITypeSymbol : INamespaceOrTypeSymbol
2626

2727
/// <summary>
2828
/// The declared base type of this type, or null. The object type, interface types,
29-
/// and pointer types do not have a base type. The base type of a type parameter
30-
/// is its effective base class.
29+
/// pointer types, and type parameters do not have a base type.
3130
/// </summary>
3231
INamedTypeSymbol? BaseType { get; }
3332

0 commit comments

Comments
 (0)