Skip to content

Allow passing values parameters #7137

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

Merged
merged 8 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 27 additions & 15 deletions packages/compiler/src/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,6 @@ export function createChecker(program: Program, resolver: NameResolver): Checker
node: Node,
mapper?: TypeMapper,
constraint?: CheckValueConstraint,
options: { legacyTupleAndModelCast?: boolean } = {},
): Value | null {
const initial = checkNode(node, mapper, constraint);
if (initial === null) {
Expand All @@ -560,15 +559,29 @@ export function createChecker(program: Program, resolver: NameResolver): Checker
} else {
entity = initial;
}
// if (options.legacyTupleAndModelCast && entity !== null && isType(entity)) {
// entity = legacy_tryTypeToValueCast(entity, constraint, node);
// }
if (entity === null) {
return null;
}
if (isValue(entity)) {
return constraint ? inferScalarsFromConstraints(entity, constraint.type) : entity;
}
// If a template parameter that can be a value is used in a template declaration then we allow it but we return null because we don't have an actual value.
if (
entity.kind === "TemplateParameter" &&
entity.constraint?.valueType &&
entity.constraint.type === undefined &&
mapper === undefined
) {
return createValue(
{
entityKind: "Value",
valueKind: "NullValue",
type: entity.constraint.valueType,
value: null,
},
entity.constraint.valueType,
);
}
reportExpectedValue(node, entity);
return null;
}
Expand Down Expand Up @@ -4622,7 +4635,7 @@ export function createChecker(program: Program, resolver: NameResolver): Checker
pendingResolutions.start(sym, ResolutionKind.Type);
type.type = getTypeForNode(prop.value, mapper);
if (prop.default) {
const defaultValue = checkDefaultValue(prop.default, type.type);
const defaultValue = checkDefaultValue(prop.default, type.type, mapper);
if (defaultValue !== null) {
type.defaultValue = defaultValue;
}
Expand Down Expand Up @@ -4659,20 +4672,19 @@ export function createChecker(program: Program, resolver: NameResolver): Checker
};
}

function checkDefaultValue(defaultNode: Node, type: Type): Value | null {
function checkDefaultValue(
defaultNode: Node,
type: Type,
mapper: TypeMapper | undefined,
): Value | null {
if (isErrorType(type)) {
// if the prop type is an error we don't need to validate again.
return null;
}
const defaultValue = getValueForNode(
defaultNode,
undefined,
{
kind: "assignment",
type,
},
{ legacyTupleAndModelCast: true },
);
const defaultValue = getValueForNode(defaultNode, mapper, {
kind: "assignment",
type,
});
if (defaultValue === null) {
return null;
}
Expand Down
28 changes: 28 additions & 0 deletions packages/compiler/test/checker/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,34 @@ describe("compiler: models", () => {
deepStrictEqual(foo.defaultValue?.value, "up-value");
});
});

describe("using a template parameter", () => {
it(`set it with valid constraint`, async () => {
testHost.addTypeSpecFile(
"main.tsp",
`
model A<T extends valueof string> { @test foo?: string = T }
alias Test = A<"Abc">;
`,
);
const { foo } = (await testHost.compile("main.tsp")) as { foo: ModelProperty };
strictEqual(foo.defaultValue?.valueKind, "StringValue");
});

it(`error if constraint is not compatible with property type`, async () => {
testHost.addTypeSpecFile(
"main.tsp",
`
model A<T extends valueof int32> { @test foo?: string = T }
`,
);
const diagnostics = await testHost.diagnose("main.tsp");
expectDiagnostics(diagnostics, {
code: "unassignable",
message: "Type 'int32' is not assignable to type 'string'",
});
});
});
});

describe("doesn't allow a default of different type than the property type", () => {
Expand Down
36 changes: 36 additions & 0 deletions packages/compiler/test/checker/templates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -982,4 +982,40 @@ describe("compiler: templates", () => {
strictEqual(members[1][1].name, "string");
});
});

describe("template declaration passing values", () => {
it("allows passing to a decorator expecting that value", async () => {
testHost.addJsFile("effect.js", {
$call: () => null,
});

testHost.addTypeSpecFile(
"main.tsp",
`
import "./effect.js";
extern dec call(target, arg: valueof string);
@call(T) model Dec<T extends valueof string> {}
`,
);
const diagnostics = await testHost.diagnose("main.tsp");
expectDiagnosticEmpty(diagnostics);
});

it("allows passing to a decorator expecting a composed value", async () => {
testHost.addJsFile("effect.js", {
$call: () => null,
});

testHost.addTypeSpecFile(
"main.tsp",
`
import "./effect.js";
extern dec call(target, arg: valueof unknown);
@call(#{foo: T}) model Dec<T extends valueof string> {}
`,
);
const diagnostics = await testHost.diagnose("main.tsp");
expectDiagnosticEmpty(diagnostics);
});
});
});
Loading