diff --git a/.chronus/changes/fix-min-max-value-erorr-location-2025-10-7-9-1-48.md b/.chronus/changes/fix-min-max-value-erorr-location-2025-10-7-9-1-48.md new file mode 100644 index 00000000000..f441bba5f08 --- /dev/null +++ b/.chronus/changes/fix-min-max-value-erorr-location-2025-10-7-9-1-48.md @@ -0,0 +1,6 @@ +--- +changeKind: internal +packages: + - "@typespec/compiler" +--- + diff --git a/packages/compiler/src/lib/decorators.ts b/packages/compiler/src/lib/decorators.ts index a3ea46a42e1..cc36071911e 100644 --- a/packages/compiler/src/lib/decorators.ts +++ b/packages/compiler/src/lib/decorators.ts @@ -336,7 +336,7 @@ function validateValueAssignableToTarget( const [assignable, diagnostics] = $(context.program).value.isOfType.withDiagnostics( value, targetType, - value, + context.getArgumentTarget(0)!, ); if (!assignable) { context.program.reportDiagnostics(diagnostics); diff --git a/packages/compiler/src/typekit/kits/value.ts b/packages/compiler/src/typekit/kits/value.ts index 94222a15e5f..1640dd1935e 100644 --- a/packages/compiler/src/typekit/kits/value.ts +++ b/packages/compiler/src/typekit/kits/value.ts @@ -3,9 +3,9 @@ import { isValue } from "../../core/type-utils.js"; import type { ArrayValue, BooleanValue, + DiagnosticTarget, Entity, EnumValue, - Node, NullValue, NumericValue, ObjectValue, @@ -113,7 +113,7 @@ export interface ValueKit { * @param diagnosticTarget Target for the diagnostic */ isAssignableTo: Diagnosable< - (source: Value, target: Entity, diagnosticTarget?: Entity | Node) => boolean + (source: Value, target: Entity, diagnosticTarget?: DiagnosticTarget) => boolean >; /** @@ -122,7 +122,9 @@ export interface ValueKit { * @param target Target type * @param diagnosticTarget Target for the diagnostic */ - isOfType: Diagnosable<(source: Value, target: Type, diagnosticTarget?: Entity | Node) => boolean>; + isOfType: Diagnosable< + (source: Value, target: Type, diagnosticTarget?: DiagnosticTarget) => boolean + >; /** * Resolve a value reference to a TypeSpec value. diff --git a/packages/compiler/test/decorators/range-limits.test.ts b/packages/compiler/test/decorators/range-limits.test.ts index f51f5fb404a..0ed993e0cac 100644 --- a/packages/compiler/test/decorators/range-limits.test.ts +++ b/packages/compiler/test/decorators/range-limits.test.ts @@ -124,6 +124,20 @@ describe("compiler: range limiting decorators", () => { }); }); + it("emit diagnostic if not assignable to the numeric type", async () => { + const [{ pos }, diagnostics] = await Tester.compileAndDiagnose(` + model Foo { + @minValue(/*error*/1234) + name: int8; + } + `); + expectDiagnostics(diagnostics, { + code: "unassignable", + message: "Type '1234' is not assignable to type 'int8'", + pos: pos.error.pos, + }); + }); + describe("datetime types", () => { function expectScalarValue( value: Numeric | ScalarValue | undefined, diff --git a/website/src/content/docs/docs/standard-library/reference/typekits.mdx b/website/src/content/docs/docs/standard-library/reference/typekits.mdx index c42aa18ca79..93bab0a07f6 100644 --- a/website/src/content/docs/docs/standard-library/reference/typekits.mdx +++ b/website/src/content/docs/docs/standard-library/reference/typekits.mdx @@ -1847,9 +1847,9 @@ $(program).value.isArray(type: Entity): type is ArrayValue; * @param diagnosticTarget - Target for the diagnostic */ $(program).value - .isAssignableTo(source: Value, target: Entity, diagnosticTarget?: Entity | Node): boolean; + .isAssignableTo(source: Value, target: Entity, diagnosticTarget?: DiagnosticTarget): boolean; $(program).value.isAssignableTo - .withDiagnostics(source: Value, target: Entity, diagnosticTarget?: Entity | Node): [boolean, readonly Diagnostic[]]; + .withDiagnostics(source: Value, target: Entity, diagnosticTarget?: DiagnosticTarget): [boolean, readonly Diagnostic[]]; ``` ### isBoolean @@ -1920,9 +1920,9 @@ $(program).value.isObject(type: Entity): type is ObjectValue; * @param diagnosticTarget - Target for the diagnostic */ $(program).value - .isOfType(source: Value, target: Type, diagnosticTarget?: Entity | Node): boolean; + .isOfType(source: Value, target: Type, diagnosticTarget?: DiagnosticTarget): boolean; $(program).value.isOfType - .withDiagnostics(source: Value, target: Type, diagnosticTarget?: Entity | Node): [boolean, readonly Diagnostic[]]; + .withDiagnostics(source: Value, target: Type, diagnosticTarget?: DiagnosticTarget): [boolean, readonly Diagnostic[]]; ``` ### isScalar