Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot committed Sep 18, 2024
1 parent 3ea698c commit a805c61
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
1 change: 0 additions & 1 deletion src/type-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,6 @@ export function typeFromExprType(type: BiblioType): Type {
break;
}
case 'unused': {
// this is really only a return type, but might as well handle it
return { kind: 'enum value', value: 'unused' };
}
}
Expand Down
39 changes: 26 additions & 13 deletions src/typechecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,22 @@ const getExpressionVisitor =
if (error != null) {
let hint: string;
switch (error) {
case 'number-to-math': {
case 'number-to-real': {
hint =
'\nhint: you passed an ES language Number, but this position takes a mathematical value';
break;
}
case 'math-to-number': {
case 'bigint-to-real': {
hint =
'\nhint: you passed an ES language BigInt, but this position takes a mathematical value';
break;
}
case 'real-to-number': {
hint =
'\nhint: you passed a mathematical value, but this position takes an ES language Number';
break;
}
case 'math-to-bigint': {
case 'real-to-bigint': {
hint =
'\nhint: you passed a mathematical value, but this position takes an ES language BigInt';
break;
Expand Down Expand Up @@ -186,10 +191,11 @@ const getExpressionVisitor =
};

type ErrorForUsingTypeXAsTypeY =
| null
| 'number-to-math'
| 'math-to-number'
| 'math-to-bigint'
| null // i.e., no error
| 'number-to-real'
| 'bigint-to-real'
| 'real-to-number'
| 'real-to-bigint'
| 'other';
function getErrorForUsingTypeXAsTypeY(argType: Type, paramType: Type): ErrorForUsingTypeXAsTypeY {
// often we can't infer the argument precisely, so we check only that the intersection is nonempty rather than that the argument type is a subtype of the parameter type
Expand All @@ -204,11 +210,13 @@ function getErrorForUsingTypeXAsTypeY(argType: Type, paramType: Type): ErrorForU
argType.of.kind !== 'never')
) {
if (argType.kind === 'concrete number' && dominates({ kind: 'real' }, paramType)) {
return 'number-to-math';
return 'number-to-real';
} else if (argType.kind === 'concrete bigint' && dominates({ kind: 'real' }, paramType)) {
return 'bigint-to-real';
} else if (argType.kind === 'concrete real' && dominates({ kind: 'number' }, paramType)) {
return 'math-to-number';
return 'real-to-number';
} else if (argType.kind === 'concrete real' && dominates({ kind: 'bigint' }, paramType)) {
return 'math-to-bigint';
return 'real-to-bigint';
}

return 'other';
Expand Down Expand Up @@ -550,17 +558,22 @@ function inspectReturns(

let hint: string;
switch (error) {
case 'number-to-math': {
case 'number-to-real': {
hint =
'\nhint: you returned an ES language Number, but this algorithm returns a mathematical value';
break;
}
case 'math-to-number': {
case 'bigint-to-real': {
hint =
'\nhint: you returned an ES language BigInt, but this algorithm returns a mathematical value';
break;
}
case 'real-to-number': {
hint =
'\nhint: you returned a mathematical value, but this algorithm returns an ES language Number';
break;
}
case 'math-to-bigint': {
case 'real-to-bigint': {
hint =
'\nhint: you returned a mathematical value, but this algorithm returns an ES language BigInt';
break;
Expand Down
6 changes: 4 additions & 2 deletions test/typecheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -1552,8 +1552,10 @@ describe('type system', () => {
await assertTypeError(
'an integer',
'*5*<sub>ℤ</sub>',
'argument (*5*<sub>ℤ</sub>) does not look plausibly assignable to parameter type (integer)',
"returned value (*5*<sub>ℤ</sub>) does not look plausibly assignable to algorithm's return type (integer)",
'argument (*5*<sub>ℤ</sub>) does not look plausibly assignable to parameter type (integer)\n' +
'hint: you passed an ES language BigInt, but this position takes a mathematical value',
"returned value (*5*<sub>ℤ</sub>) does not look plausibly assignable to algorithm's return type (integer)\n" +
'hint: you returned an ES language BigInt, but this algorithm returns a mathematical value',
);

await assertNoTypeError('a BigInt', '*5*<sub>ℤ</sub>');
Expand Down

0 comments on commit a805c61

Please sign in to comment.