-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Avoid NoReturn analysis for Any or Unknown calls #11419
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
Open
bschnurr
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
bschnurr:perf/no-return-unknown-guard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -335,25 +335,20 @@ export function isTypeVarSame(type1: TypeVarType, type2: Type) { | |
| return false; | ||
| } | ||
|
|
||
| let isCompatible = true; | ||
| doForEachSubtype(type2, (subtype) => { | ||
| if (!isCompatible) { | ||
| return; | ||
| } | ||
|
|
||
| return allSubtypes(type2, (subtype) => { | ||
| if (!isTypeSame(type1, subtype)) { | ||
| const conditions = getTypeCondition(subtype); | ||
|
|
||
| if ( | ||
| !conditions || | ||
| !conditions.some((condition) => condition.typeVar.priv.nameWithScope === type1.priv.nameWithScope) | ||
| ) { | ||
| isCompatible = false; | ||
| return false; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return isCompatible; | ||
| return true; | ||
| }); | ||
| } | ||
|
|
||
| export function makeInferenceContext( | ||
|
|
@@ -796,11 +791,11 @@ export function someSubtypes(type: Type, callback: (type: Type) => boolean): boo | |
| export function allSubtypes(type: Type, callback: (type: Type) => boolean): boolean { | ||
| if (isUnion(type)) { | ||
| return type.priv.subtypes.every((subtype) => { | ||
| callback(subtype); | ||
| return callback(subtype); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }); | ||
| } else { | ||
| return callback(type); | ||
| } | ||
|
|
||
| return callback(type); | ||
| } | ||
|
|
||
| export function doForEachSignature( | ||
|
|
@@ -869,23 +864,17 @@ export function isUnionableType(subtypes: Type[]): boolean { | |
| } | ||
|
|
||
| export function derivesFromAnyOrUnknown(type: Type): boolean { | ||
| let anyOrUnknown = false; | ||
| return someSubtypes(type, (subtype) => { | ||
| if (isAnyOrUnknown(subtype)) { | ||
| return true; | ||
| } | ||
|
|
||
| doForEachSubtype(type, (subtype) => { | ||
| if (isAnyOrUnknown(type)) { | ||
| anyOrUnknown = true; | ||
| } else if (isInstantiableClass(subtype)) { | ||
| if (ClassType.derivesFromAnyOrUnknown(subtype)) { | ||
| anyOrUnknown = true; | ||
| } | ||
| } else if (isClassInstance(subtype)) { | ||
| if (ClassType.derivesFromAnyOrUnknown(subtype)) { | ||
| anyOrUnknown = true; | ||
| } | ||
| if (isInstantiableClass(subtype) || isClassInstance(subtype)) { | ||
| return ClassType.derivesFromAnyOrUnknown(subtype); | ||
| } | ||
| }); | ||
|
|
||
| return anyOrUnknown; | ||
| return false; | ||
| }); | ||
| } | ||
|
|
||
| export function getFullNameOfType(type: Type): string | undefined { | ||
|
|
@@ -1301,18 +1290,19 @@ export function getLiteralTypeClassName(type: Type): string | undefined { | |
|
|
||
| if (isUnion(type)) { | ||
| let className: string | undefined; | ||
| let foundMismatch = false; | ||
|
|
||
| doForEachSubtype(type, (subtype) => { | ||
| for (const subtype of type.priv.subtypes) { | ||
| const subtypeLiteralTypeName = getLiteralTypeClassName(subtype); | ||
| if (!subtypeLiteralTypeName) { | ||
| foundMismatch = true; | ||
| return undefined; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use early exit |
||
| } else if (!className) { | ||
| className = subtypeLiteralTypeName; | ||
| } else if (className !== subtypeLiteralTypeName) { | ||
| return undefined; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| return foundMismatch ? undefined : className; | ||
| return className; | ||
| } | ||
|
|
||
| return undefined; | ||
|
|
@@ -2744,9 +2734,8 @@ export function combineSameSizedTuples(type: Type, tupleType: Type | undefined): | |
| } | ||
|
|
||
| let tupleEntries: Type[][] | undefined; | ||
| let isValid = true; | ||
|
|
||
| doForEachSubtype(type, (subtype) => { | ||
| const subtypes = isUnion(type) ? type.priv.subtypes : [type]; | ||
| for (const subtype of subtypes) { | ||
| if (isClassInstance(subtype)) { | ||
| let tupleClass: ClassType | undefined; | ||
| if (isClass(subtype) && isTupleClass(subtype) && !isUnboundedTupleClass(subtype)) { | ||
|
|
@@ -2768,20 +2757,20 @@ export function combineSameSizedTuples(type: Type, tupleType: Type | undefined): | |
| tupleEntries![index].push(entry.type); | ||
| }); | ||
| } else { | ||
| isValid = false; | ||
| return type; | ||
| } | ||
| } else { | ||
| tupleEntries = tupleClass.priv.tupleTypeArgs.map((entry) => [entry.type]); | ||
| } | ||
| } else { | ||
| isValid = false; | ||
| return type; | ||
| } | ||
| } else { | ||
| isValid = false; | ||
| return type; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| if (!isValid || !tupleEntries) { | ||
| if (!tupleEntries) { | ||
| return type; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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,172 @@ | ||
| /* | ||
| * typeUtils.test.ts | ||
| * Copyright (c) Microsoft Corporation. | ||
| * Licensed under the MIT license. | ||
| * | ||
| * Unit tests for typeUtils module. | ||
| */ | ||
|
|
||
| import * as assert from 'assert'; | ||
|
|
||
| import { | ||
| allSubtypes, | ||
| combineSameSizedTuples, | ||
| derivesFromAnyOrUnknown, | ||
| getLiteralTypeClassName, | ||
| someSubtypes, | ||
| } from '../analyzer/typeUtils'; | ||
| import { | ||
| AnyType, | ||
| ClassType, | ||
| ClassTypeFlags, | ||
| isClassInstance, | ||
| Type, | ||
| UnionableType, | ||
| UnionType, | ||
| UnknownType, | ||
| } from '../analyzer/types'; | ||
| import { Uri } from '../common/uri/uri'; | ||
|
|
||
| test('AllSubtypes', () => { | ||
| const unionType = createUnion(createClassType('A'), createClassType('B'), createClassType('C')); | ||
| const visitedSubtypes: Type[] = []; | ||
|
|
||
| const result = allSubtypes(unionType, (subtype) => { | ||
| visitedSubtypes.push(subtype); | ||
| return visitedSubtypes.length < 2; | ||
| }); | ||
|
|
||
| assert.strictEqual(result, false); | ||
| assert.strictEqual(visitedSubtypes.length, 2); | ||
|
|
||
| assert.strictEqual( | ||
| allSubtypes(unionType, () => { | ||
| return true; | ||
| }), | ||
| true | ||
| ); | ||
|
|
||
| const singleType = createClassType('D'); | ||
| assert.strictEqual( | ||
| allSubtypes(singleType, (subtype) => { | ||
| assert.strictEqual(subtype, singleType); | ||
| return true; | ||
| }), | ||
| true | ||
| ); | ||
| }); | ||
|
|
||
| test('SomeSubtypes', () => { | ||
| const unionType = createUnion(createClassType('A'), createClassType('B'), createClassType('C')); | ||
| const visitedSubtypes: Type[] = []; | ||
|
|
||
| const result = someSubtypes(unionType, (subtype) => { | ||
| visitedSubtypes.push(subtype); | ||
| return visitedSubtypes.length === 2; | ||
| }); | ||
|
|
||
| assert.strictEqual(result, true); | ||
| assert.strictEqual(visitedSubtypes.length, 2); | ||
|
|
||
| assert.strictEqual( | ||
| someSubtypes(unionType, () => { | ||
| return false; | ||
| }), | ||
| false | ||
| ); | ||
|
|
||
| const singleType = createClassType('D'); | ||
| assert.strictEqual( | ||
| someSubtypes(singleType, (subtype) => { | ||
| assert.strictEqual(subtype, singleType); | ||
| return false; | ||
| }), | ||
| false | ||
| ); | ||
| }); | ||
|
|
||
| test('DerivesFromAnyOrUnknownUnion', () => { | ||
| const classType = createClassType('A'); | ||
|
|
||
| assert.strictEqual(derivesFromAnyOrUnknown(createUnion(classType, UnknownType.create())), true); | ||
| assert.strictEqual(derivesFromAnyOrUnknown(createUnion(classType, AnyType.create())), true); | ||
| assert.strictEqual(derivesFromAnyOrUnknown(createUnion(classType, createClassType('B'))), false); | ||
| }); | ||
|
|
||
| test('GetLiteralTypeClassName', () => { | ||
| const intLiteral1 = createLiteralInstance('int', 1); | ||
| const intLiteral2 = createLiteralInstance('int', 2); | ||
| const strLiteral = createLiteralInstance('str', ''); | ||
| const nonLiteralInt = ClassType.cloneAsInstance(createClassType('int', ClassTypeFlags.BuiltIn)); | ||
|
|
||
| assert.strictEqual(getLiteralTypeClassName(intLiteral1), 'int'); | ||
| assert.strictEqual(getLiteralTypeClassName(createUnion(intLiteral1, intLiteral2)), 'int'); | ||
| assert.strictEqual(getLiteralTypeClassName(createUnion(intLiteral1, strLiteral)), undefined); | ||
| assert.strictEqual(getLiteralTypeClassName(createUnion(intLiteral1, nonLiteralInt)), undefined); | ||
| }); | ||
|
|
||
| test('CombineSameSizedTuples', () => { | ||
| const tupleClass = createClassType('tuple', ClassTypeFlags.BuiltIn); | ||
| const intType = ClassType.cloneAsInstance(createClassType('int', ClassTypeFlags.BuiltIn)); | ||
| const strType = ClassType.cloneAsInstance(createClassType('str', ClassTypeFlags.BuiltIn)); | ||
| const boolType = ClassType.cloneAsInstance(createClassType('bool', ClassTypeFlags.BuiltIn)); | ||
|
|
||
| const tuple1 = createTupleInstance(tupleClass, [intType, strType]); | ||
| const tuple2 = createTupleInstance(tupleClass, [strType, boolType]); | ||
| const tupleUnion = createUnion(tuple1, tuple2); | ||
|
|
||
| const combinedTuple = combineSameSizedTuples(tupleUnion, tupleClass); | ||
| assert.notStrictEqual(combinedTuple, tupleUnion); | ||
| assert.strictEqual(isClassInstance(combinedTuple), true); | ||
| assert.strictEqual((combinedTuple as ClassType).priv.tupleTypeArgs?.length, 2); | ||
|
|
||
| const mismatchedTuple = createTupleInstance(tupleClass, [intType]); | ||
| const mismatchedTupleUnion = createUnion(tuple1, mismatchedTuple); | ||
| assert.strictEqual(combineSameSizedTuples(mismatchedTupleUnion, tupleClass), mismatchedTupleUnion); | ||
|
|
||
| const nonTupleUnion = createUnion(tuple1, intType); | ||
| assert.strictEqual(combineSameSizedTuples(nonTupleUnion, tupleClass), nonTupleUnion); | ||
| }); | ||
|
|
||
| function createLiteralInstance(name: string, literalValue: string | number | boolean) { | ||
| return ClassType.cloneAsInstance( | ||
| ClassType.cloneWithLiteral(createClassType(name, ClassTypeFlags.BuiltIn), literalValue) | ||
| ); | ||
| } | ||
|
|
||
| function createTupleInstance(tupleClass: ClassType, entries: UnionableType[]) { | ||
| return ClassType.cloneAsInstance( | ||
| ClassType.specialize( | ||
| tupleClass, | ||
| [createUnion(...entries)], | ||
| /* isTypeArgExplicit */ true, | ||
| /* includeSubclasses */ false, | ||
| entries.map((type) => { | ||
| return { type, isUnbounded: false }; | ||
| }) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| function createClassType(name: string, flags = ClassTypeFlags.None) { | ||
| const classType = ClassType.createInstantiable( | ||
| name, | ||
| name, | ||
| '', | ||
| Uri.empty(), | ||
| flags, | ||
| 0, | ||
| /* declaredMetaclass*/ undefined, | ||
| /* effectiveMetaclass */ undefined | ||
| ); | ||
| classType.shared.mro.push(classType); | ||
| return classType; | ||
| } | ||
|
|
||
| function createUnion(...subtypes: UnionableType[]) { | ||
| const unionType = UnionType.create(); | ||
| subtypes.forEach((subtype) => { | ||
| UnionType.addType(unionType, subtype); | ||
| }); | ||
| return unionType; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.