Skip to content

Commit

Permalink
fix: also handle literal types when computing upper bound (#1103)
Browse files Browse the repository at this point in the history
Related to #1081

### Summary of Changes

Also update the type computer to handle literal types as upper bounds of
type parameters.
  • Loading branch information
lars-reimann authored Apr 25, 2024
1 parent c14159b commit 3f1ab6f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
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

0 comments on commit 3f1ab6f

Please sign in to comment.