Skip to content

Commit

Permalink
Revert "Fixed a bug that leads to an infinite loop when performing pr…
Browse files Browse the repository at this point in the history
…otocol matching under certain circumstances that involve recursive protocol definitions. This addresses #7786."

This reverts commit 2870e25.
  • Loading branch information
erictraut committed Apr 28, 2024
1 parent 2870e25 commit 529c029
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
14 changes: 5 additions & 9 deletions packages/pyright-internal/src/analyzer/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import { assert } from '../common/debug';
import { defaultMaxDiagnosticDepth, DiagnosticAddendum } from '../common/diagnostic';
import { DiagnosticAddendum } from '../common/diagnostic';
import { LocAddendum } from '../localization/localize';
import { assignTypeToTypeVar } from './constraintSolver';
import { DeclarationType } from './declaration';
Expand Down Expand Up @@ -65,7 +65,7 @@ interface ProtocolCompatibility {
const protocolAssignmentStack: ProtocolAssignmentStackEntry[] = [];

// Maximum number of different types that are cached with a protocol.
const maxProtocolCompatibilityCacheEntries = 64;
const maxProtocolCompatibilityCacheEntries = 32;

export function assignClassToProtocol(
evaluator: TypeEvaluator,
Expand Down Expand Up @@ -108,13 +108,9 @@ export function assignClassToProtocol(
}

// If it's known not to be compatible and the caller hasn't requested
// any detailed diagnostic information or we've already exceeded the
// depth of diagnostic information that will be displayed, we can
// return false immediately.
if (!compatibility) {
if (!diag || diag.getNestLevel() > defaultMaxDiagnosticDepth) {
return false;
}
// any detailed diagnostic information, we can return false immediately.
if (!compatibility && !diag) {
return false;
}
}
}
Expand Down
16 changes: 3 additions & 13 deletions packages/pyright-internal/src/common/diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { DiagnosticLevel } from './configOptions';
import { Range, TextRange } from './textRange';
import { Uri } from './uri/uri';

export const defaultMaxDiagnosticDepth = 5;
export const defaultMaxDiagnosticLineCount = 8;
const defaultMaxDepth = 5;
const defaultMaxLineCount = 8;
const maxRecursionCount = 64;

// Corresponds to the CommentTaskPriority enum at https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_git/VS?path=src/env/shell/PackageFramework/Framework/CommentTaskPriority.cs
Expand Down Expand Up @@ -151,11 +151,6 @@ export class DiagnosticAddendum {
private _messages: string[] = [];
private _childAddenda: DiagnosticAddendum[] = [];

// The nest level is accurate only for the common case where all
// addendum are created using createAddendum. This is an upper bound.
// The actual nest level may be smaller.
private _nestLevel: number | undefined;

// Addenda normally don't have their own ranges, but there are cases
// where we want to track ranges that can influence the range of the
// diagnostic.
Expand All @@ -172,12 +167,11 @@ export class DiagnosticAddendum {
// Create a new (nested) addendum to which messages can be added.
createAddendum() {
const newAddendum = new DiagnosticAddendum();
newAddendum._nestLevel = (this._nestLevel ?? 0) + 1;
this.addAddendum(newAddendum);
return newAddendum;
}

getString(maxDepth = defaultMaxDiagnosticDepth, maxLineCount = defaultMaxDiagnosticLineCount): string {
getString(maxDepth = defaultMaxDepth, maxLineCount = defaultMaxLineCount): string {
let lines = this._getLinesRecursive(maxDepth, maxLineCount);

if (lines.length > maxLineCount) {
Expand Down Expand Up @@ -209,10 +203,6 @@ export class DiagnosticAddendum {
return this._messages;
}

getNestLevel() {
return this._nestLevel ?? 0;
}

// Returns undefined if no range is associated with this addendum
// or its children. Returns a non-empty range if there is a single range
// associated.
Expand Down

0 comments on commit 529c029

Please sign in to comment.