-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28483 from dotnet-maestro-bot/merge/release/7.0.1…
…xx-to-release/7.0.2xx [automated] Merge branch 'release/7.0.1xx' => 'release/7.0.2xx'
- Loading branch information
Showing
75 changed files
with
1,805 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/ApiCompat/Microsoft.DotNet.ApiCompatibility/Rules/CannotChangeVisibility.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.DotNet.ApiCompatibility.Abstractions; | ||
using Microsoft.DotNet.ApiCompatibility.Extensions; | ||
|
||
namespace Microsoft.DotNet.ApiCompatibility.Rules | ||
{ | ||
/// <summary> | ||
/// This class implements a rule to check that the visibility of symbols is not reduced. | ||
/// In strict mode, it also checks that the visibility isn't expanded. | ||
/// </summary> | ||
public class CannotChangeVisibility : IRule | ||
{ | ||
private readonly RuleSettings _settings; | ||
|
||
public CannotChangeVisibility(RuleSettings settings, IRuleRegistrationContext context) | ||
{ | ||
_settings = settings; | ||
context.RegisterOnMemberSymbolAction(RunOnMemberSymbol); | ||
context.RegisterOnTypeSymbolAction(RunOnTypeSymbol); | ||
} | ||
|
||
private static Accessibility NormalizeInternals(Accessibility a) => a switch | ||
{ | ||
Accessibility.ProtectedOrInternal => Accessibility.Protected, | ||
Accessibility.ProtectedAndInternal or Accessibility.Internal => Accessibility.Private, | ||
_ => a, | ||
}; | ||
|
||
private int CompareAccessibility(Accessibility a, Accessibility b) | ||
{ | ||
if (!_settings.IncludeInternalSymbols) | ||
{ | ||
a = NormalizeInternals(a); | ||
b = NormalizeInternals(b); | ||
} | ||
|
||
if (a == b) | ||
{ | ||
return 0; | ||
} | ||
|
||
return (a, b) switch | ||
{ | ||
(Accessibility.Public, _) => 1, | ||
(_, Accessibility.Public) => -1, | ||
(Accessibility.ProtectedOrInternal, _) => 1, | ||
(_, Accessibility.ProtectedOrInternal) => -1, | ||
(Accessibility.Protected or Accessibility.Internal, _) => 1, | ||
(_, Accessibility.Protected or Accessibility.Internal) => -1, | ||
(Accessibility.ProtectedAndInternal, _) => 1, | ||
(_, Accessibility.ProtectedAndInternal) => -1, | ||
_ => throw new NotImplementedException(), | ||
}; | ||
} | ||
|
||
private void RunOnSymbol( | ||
ISymbol? left, | ||
ISymbol? right, | ||
MetadataInformation leftMetadata, | ||
MetadataInformation rightMetadata, | ||
IList<CompatDifference> differences) | ||
{ | ||
// The MemberMustExist rule handles missing symbols and therefore this rule only runs when left and right is not null. | ||
if (left is null || right is null) | ||
{ | ||
return; | ||
} | ||
|
||
Accessibility leftAccess = left.DeclaredAccessibility; | ||
Accessibility rightAccess = right.DeclaredAccessibility; | ||
int accessComparison = CompareAccessibility(leftAccess, rightAccess); | ||
|
||
if (accessComparison > 0) | ||
{ | ||
differences.Add(new CompatDifference(leftMetadata, | ||
rightMetadata, | ||
DiagnosticIds.CannotReduceVisibility, | ||
string.Format(Resources.CannotReduceVisibility, left, leftAccess, rightAccess), | ||
DifferenceType.Changed, | ||
left)); | ||
} | ||
else if (_settings.StrictMode && accessComparison < 0) | ||
{ | ||
differences.Add(new CompatDifference(leftMetadata, | ||
rightMetadata, | ||
DiagnosticIds.CannotExpandVisibility, | ||
string.Format(Resources.CannotExpandVisibility, right, leftAccess, rightAccess), | ||
DifferenceType.Changed, | ||
right)); | ||
} | ||
} | ||
|
||
private void RunOnTypeSymbol( | ||
ITypeSymbol? left, | ||
ITypeSymbol? right, | ||
MetadataInformation leftMetadata, | ||
MetadataInformation rightMetadata, | ||
IList<CompatDifference> differences) => RunOnSymbol(left, right, leftMetadata, rightMetadata, differences); | ||
|
||
private void RunOnMemberSymbol( | ||
ISymbol? left, | ||
ISymbol? right, | ||
ITypeSymbol leftContainingType, | ||
ITypeSymbol rightContainingType, | ||
MetadataInformation leftMetadata, | ||
MetadataInformation rightMetadata, | ||
IList<CompatDifference> differences) => RunOnSymbol(left, right, leftMetadata, rightMetadata, differences); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.