Skip to content
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

fix: also handle literal types when computing upper bound #1103

Merged
merged 1 commit into from
Apr 25, 2024
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 @@ -774,7 +774,9 @@ export class SafeDsTypeComputer {
}

const boundType = this.computeType(upperBound);
if (!(boundType instanceof NamedType)) {
if (boundType instanceof LiteralType) {
return boundType;
} else if (!(boundType instanceof NamedType)) {
return UnknownType;
} else if (options.stopAtTypeVariable || !(boundType instanceof TypeVariable)) {
return boundType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ import { createSafeDsServices, getModuleMembers, getTypeParameters } from '../..
import { Type, UnknownType } from '../../../../src/language/typing/model.js';
import { getNodeOfType } from '../../../helpers/nodeFinder.js';
import { expectEqualTypes } from '../../../helpers/testAssertions.js';
import { IntConstant } from '../../../../src/language/partialEvaluation/model.js';

const services = (await createSafeDsServices(NodeFileSystem)).SafeDs;
const coreTypes = services.typing.CoreTypes;
const typeComputer = services.typing.TypeComputer;
const typeFactory = services.typing.TypeFactory;

const code = `
class MyClass<
Unbounded,
LegalDirectBounds sub Number,
LegalIndirectBounds sub LegalDirectBounds,
UnnamedBounds sub literal<2>,
UnresolvedBounds sub Unresolved,
LiteralBounds sub literal<2>,
NamedBounds sub Number,
TypeParameterBounds sub NamedBounds,

// Illegal
CallableBounds sub () -> (),
UnionBounds sub union<Number, String>,
UnknownBounds sub unknown,
>
`;
const module = await getNodeOfType(services, code, isSdsModule);
Expand All @@ -25,30 +31,40 @@ const classes = getModuleMembers(module).filter(isSdsClass);
const typeParameters = getTypeParameters(classes[0]);

const unbounded = typeParameters[0]!;
const legalDirectBounds = typeParameters[1]!;
const legalIndirectBounds = typeParameters[2]!;
const unnamedBounds = typeParameters[3]!;
const unresolvedBounds = typeParameters[4]!;
const literalBounds = typeParameters[1]!;
const namedBounds = typeParameters[2]!;
const typeParameterBounds = typeParameters[3]!;
const callableBounds = typeParameters[4]!;
const unionBounds = typeParameters[5]!;
const unknownBounds = typeParameters[6]!;

const computeUpperBoundTests: ComputeUpperBoundTest[] = [
{
typeParameter: unbounded,
expected: coreTypes.AnyOrNull,
},
{
typeParameter: legalDirectBounds,
typeParameter: literalBounds,
expected: typeFactory.createLiteralType(new IntConstant(2n)),
},
{
typeParameter: namedBounds,
expected: coreTypes.Number,
},
{
typeParameter: legalIndirectBounds,
typeParameter: typeParameterBounds,
expected: coreTypes.Number,
},
{
typeParameter: unnamedBounds,
typeParameter: callableBounds,
expected: UnknownType,
},
{
typeParameter: unionBounds,
expected: UnknownType,
},
{
typeParameter: unresolvedBounds,
typeParameter: unknownBounds,
expected: UnknownType,
},
];
Expand Down