Skip to content
Merged
Show file tree
Hide file tree
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 @@ -215,6 +215,39 @@ class Class
struct MutableInt { public int Value; }
""");

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/76815")]
[InlineData("DateTime")]
[InlineData("ArraySegment<int>")]
[InlineData("DateTimeOffset")]
[InlineData("Guid")]
[InlineData("Index")]
[InlineData("Range")]
[InlineData("ReadOnlyMemory<int>")]
[InlineData("ReadOnlySpan<int>")]
[InlineData("TimeSpan")]
public Task TestWellKnownImmutableValueType1(string typeName)
=> TestInRegularAndScript1Async(
$$"""
class Class
{
[|System.{{typeName}} i|];

System.{{typeName}} P
{
get
{
return i;
}
}
}
""",
$$"""
class Class
{
System.{{typeName}} P { get; }
}
""");

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/28511")]
public Task TestMutableValueType1()
=> TestMissingInRegularAndScriptAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,29 +674,32 @@ public static bool IsEnumType([NotNullWhen(true)] this ITypeSymbol? type, [NotNu
break;
}

// Special case certain structs that we know for certain are immutable, but which may have not been marked that
// way (especially in older frameworks before we had the `readonly struct` feature).
if (IsWellKnownImmutableValueType(type))
return false;

// An error type may or may not be a struct, and it may or may not be mutable. As we cannot make a determination,
// we return null to allow the caller to decide what to do.
if (type.IsErrorType())
{
return null;
}

if (type.TypeKind != TypeKind.Struct)
{
return false;
}

if (type.IsReadOnly)
return false;

var hasPrivateField = false;
foreach (var member in type.GetMembers())
{
if (member is not IFieldSymbol fieldSymbol)
{
continue;
}

hasPrivateField |= fieldSymbol.DeclaredAccessibility == Accessibility.Private;
if (!fieldSymbol.IsConst && !fieldSymbol.IsReadOnly && !fieldSymbol.IsStatic)
{
return true;
}

hasPrivateField |= fieldSymbol.DeclaredAccessibility == Accessibility.Private;
}

if (!hasPrivateField)
Expand All @@ -714,6 +717,38 @@ public static bool IsEnumType([NotNullWhen(true)] this ITypeSymbol? type, [NotNu
}

return false;

static bool IsWellKnownImmutableValueType(ITypeSymbol type)
{
// We know that these types are immutable, even if they don't have the IsReadOnly attribute.
if (type is not INamedTypeSymbol
{
ContainingNamespace:
{
Name: nameof(System),
ContainingNamespace.IsGlobalNamespace: true
}
})
{
return false;
}

if (type.Name
is nameof(DateTime)
or nameof(ArraySegment<>)
or nameof(DateTimeOffset)
or nameof(Guid)
or nameof(Index)
or nameof(Range)
or nameof(ReadOnlyMemory<>)
or nameof(ReadOnlySpan<>)
or nameof(TimeSpan))
{
return true;
}

return false;
}
}

public static bool IsDisposable([NotNullWhen(returnValue: true)] this ITypeSymbol? type, [NotNullWhen(returnValue: true)] ITypeSymbol? iDisposableType)
Expand Down
Loading