Skip to content

Commit

Permalink
improved lsp error location
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Jan 18, 2024
1 parent 6d49966 commit 19e05f1
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/typecheck/typecheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { ConstLiteral, Expr, Program, Statement } from "../ast";
import { TVar, Type, unify, Context, generalize, instantiate } from "./unify";

export type UnifyErrorType = "type-mismatch" | "occurs-check";
export type TypeError<Node> =
export type TypeError<Meta> =
| {
type: "unbound-variable";
ident: string;
node: Node;
node: Expr<Meta>;
}
| {
type: UnifyErrorType;
node: Node;
node: Expr<Meta>;
left: Type;
right: Type;
};
Expand All @@ -20,9 +20,9 @@ export type TypeMeta = { $: TVar };
export function typecheck<T = {}>(
ast: Program<T>,
initialContext: Context = {},
): [Program<T & TypeMeta>, TypeError<Expr<T & TypeMeta>>[]] {
): [Program<T & TypeMeta>, TypeError<T & TypeMeta>[]] {
TVar.resetId();
const errors: TypeError<Expr<T & TypeMeta>>[] = [];
const errors: TypeError<T & TypeMeta>[] = [];
let context: Context = { ...initialContext };

const typedStatements = ast.statements.map<Statement<T & TypeMeta>>(
Expand Down Expand Up @@ -50,7 +50,7 @@ export function typecheck<T = {}>(
function* typecheckAnnotatedExpr<T>(
ast: Expr<T & TypeMeta>,
context: Context,
): Generator<TypeError<Expr<T & TypeMeta>>> {
): Generator<TypeError<T & TypeMeta>> {
switch (ast.type) {
case "constant": {
const t = inferConstant(ast.value);
Expand Down Expand Up @@ -86,6 +86,7 @@ function* typecheckAnnotatedExpr<T>(
return;

case "application":
yield* typecheckAnnotatedExpr(ast.caller, context);
yield* unifyYieldErr(ast, ast.caller.$.asType(), {
type: "fn",
args: ast.args.map((arg) => arg.$.asType()),
Expand All @@ -94,7 +95,6 @@ function* typecheckAnnotatedExpr<T>(
for (const arg of ast.args) {
yield* typecheckAnnotatedExpr(arg, context);
}
yield* typecheckAnnotatedExpr(ast.caller, context);
return;

case "let":
Expand Down Expand Up @@ -173,7 +173,7 @@ function* unifyYieldErr<T>(
ast: Expr<T & TypeMeta>,
t1: Type,
t2: Type,
): Generator<TypeError<Expr<T & TypeMeta>>> {
): Generator<TypeError<T & TypeMeta>> {
const e = unify(t1, t2);
if (e !== undefined) {
yield { type: e.type, left: e.left, right: e.right, node: ast };
Expand Down

0 comments on commit 19e05f1

Please sign in to comment.