mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-14 16:35:03 +01:00
fix generics
This commit is contained in:
parent
dbd49d852f
commit
66d95dfeeb
18 changed files with 154 additions and 158 deletions
|
|
@ -141,7 +141,7 @@ export type FunctionArg<P extends Phase> = {
|
|||
|
||||
export type ItemKindType<P extends Phase> = {
|
||||
kind: "type";
|
||||
generics: Ident[];
|
||||
genericParams: Ident[];
|
||||
type: TypeDefKind<P>;
|
||||
ty?: Ty;
|
||||
};
|
||||
|
|
@ -417,7 +417,7 @@ export const UNARY_KINDS: UnaryKind[] = ["!", "-"];
|
|||
export type TypeKind<P extends Phase> =
|
||||
| {
|
||||
kind: "ident";
|
||||
generics: Type<P>[];
|
||||
genericArgs: Type<P>[];
|
||||
value: IdentWithRes<P>;
|
||||
}
|
||||
| {
|
||||
|
|
@ -793,7 +793,7 @@ export function superFoldType<From extends Phase, To extends Phase>(
|
|||
case "ident": {
|
||||
return {
|
||||
kind: "ident",
|
||||
generics: type.generics.map((type) => folder.type(type)),
|
||||
genericArgs: type.genericArgs.map((type) => folder.type(type)),
|
||||
value: folder.ident(type.value),
|
||||
span,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ it("should compute struct layout correctly", () => {
|
|||
kind: "struct",
|
||||
itemId: ItemId.dummy(),
|
||||
genericArgs: [],
|
||||
params: [],
|
||||
_name: "",
|
||||
fields_no_subst: [
|
||||
["uwu", TYS.I32],
|
||||
|
|
@ -54,7 +53,6 @@ it("should compute single field struct layout correctly", () => {
|
|||
kind: "struct",
|
||||
itemId: ItemId.dummy(),
|
||||
genericArgs: [],
|
||||
params: [],
|
||||
_name: "",
|
||||
fields_no_subst: [["owo", TYS.INT]],
|
||||
};
|
||||
|
|
|
|||
|
|
@ -384,8 +384,7 @@ function lowerFunc(cx: Context, func: ItemFunction<Typecked>) {
|
|||
fcx.wasm.body = body.instructions;
|
||||
} else {
|
||||
lowerExpr(fcx, wasmFunc.body, body);
|
||||
|
||||
paramLocations.forEach((local) => {
|
||||
paramLocations.forEach((local) => {
|
||||
const refcount = needsRefcount(local.ty);
|
||||
if (refcount !== undefined) {
|
||||
// TODO: correctly deal with tuples
|
||||
|
|
@ -1264,7 +1263,6 @@ function argRetAbi(param: Ty): ArgRetAbi {
|
|||
return [];
|
||||
case "var":
|
||||
case "param":
|
||||
case "alias":
|
||||
case "error":
|
||||
codegenUnreachableTy(param);
|
||||
}
|
||||
|
|
@ -1320,7 +1318,6 @@ function wasmTypeForBody(ty: Ty): wasm.ValType[] {
|
|||
return [];
|
||||
case "var":
|
||||
case "param":
|
||||
case "alias":
|
||||
case "error":
|
||||
codegenUnreachableTy(ty);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -173,7 +173,7 @@ function parseItem(t: State): [State, Item<Parsed>] {
|
|||
{
|
||||
kind: "type",
|
||||
name: name.ident,
|
||||
generics,
|
||||
genericParams: generics,
|
||||
type,
|
||||
span: name.span,
|
||||
id: ItemId.dummy(),
|
||||
|
|
@ -694,7 +694,7 @@ function parseType(t: State): [State, Type<Parsed>] {
|
|||
t,
|
||||
{
|
||||
kind: "ident",
|
||||
generics,
|
||||
genericArgs: generics,
|
||||
value: { name: tok.ident, span },
|
||||
span,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -65,9 +65,9 @@ function printFunction(func: ItemFunction<AnyPhase>): string {
|
|||
|
||||
function printTypeDef(type: ItemType<AnyPhase>): string {
|
||||
const head = `type ${type.name}${
|
||||
type.generics.length === 0
|
||||
type.genericParams.length === 0
|
||||
? ""
|
||||
: `[${type.generics.map((ident) => ident.name).join(", ")}]`
|
||||
: `[${type.genericParams.map((ident) => ident.name).join(", ")}]`
|
||||
} = `;
|
||||
switch (type.type.kind) {
|
||||
case "struct": {
|
||||
|
|
@ -284,8 +284,6 @@ export function printTy(ty: Ty): string {
|
|||
return "!";
|
||||
case "param":
|
||||
return ty.name;
|
||||
case "alias":
|
||||
return printTy(substituteTy(ty.genericArgs, ty.actual));
|
||||
case "error":
|
||||
return "<ERROR>";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -225,7 +225,7 @@ function resolveModule(
|
|||
};
|
||||
}
|
||||
case "type": {
|
||||
tyParamScopes = item.generics.map(({ name }) => name);
|
||||
tyParamScopes = item.genericParams.map(({ name }) => name);
|
||||
|
||||
const type = { ...superFoldItem(item, this) };
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import {
|
|||
Folder,
|
||||
LOGICAL_KINDS,
|
||||
LoopId,
|
||||
Resolution,
|
||||
Resolved,
|
||||
StructLiteralField,
|
||||
Type,
|
||||
|
|
@ -91,7 +90,6 @@ export function checkBody(
|
|||
fnTy: TyFn,
|
||||
): Expr<Typecked> {
|
||||
const infcx = new InferContext(cx.gcx.error);
|
||||
|
||||
const fcx: FuncCtx = {
|
||||
cx,
|
||||
infcx,
|
||||
|
|
@ -246,7 +244,8 @@ export function checkBody(
|
|||
break;
|
||||
}
|
||||
case "item": {
|
||||
ty = typeOfItem(fcx.cx, res.id, [], span);
|
||||
// TODO: what do we do about generis here?
|
||||
ty = typeOfItem(fcx.cx, res.id, span);
|
||||
break;
|
||||
}
|
||||
case "builtin":
|
||||
|
|
@ -592,7 +591,7 @@ function checkStructLiteral(
|
|||
}
|
||||
|
||||
// TODO: Handle generic arugments
|
||||
const structTy = typeOfItem(fcx.cx, name.res.id, [], name.span);
|
||||
const structTy = typeOfItem(fcx.cx, name.res.id, name.span);
|
||||
|
||||
if (structTy.kind !== "struct") {
|
||||
const err: ErrorEmitted = emitError(
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ export function typeck(gcx: GlobalContext, ast: Pkg<Resolved>): Pkg<Typecked> {
|
|||
switch (item.kind) {
|
||||
case "function": {
|
||||
// Functions do not have generic arguments right now.
|
||||
const fnTy = typeOfItem(cx, item.id, [], item.span) as TyFn;
|
||||
const fnTy = typeOfItem(cx, item.id, item.span) as TyFn;
|
||||
const body = checkBody(cx, ast, item.body, fnTy);
|
||||
|
||||
return {
|
||||
|
|
@ -43,7 +43,7 @@ export function typeck(gcx: GlobalContext, ast: Pkg<Resolved>): Pkg<Typecked> {
|
|||
};
|
||||
}
|
||||
case "import": {
|
||||
const fnTy = typeOfItem(cx, item.id, [], item.span) as TyFn;
|
||||
const fnTy = typeOfItem(cx, item.id, item.span) as TyFn;
|
||||
|
||||
fnTy.params.forEach((param, i) => {
|
||||
switch (param.kind) {
|
||||
|
|
@ -87,7 +87,7 @@ export function typeck(gcx: GlobalContext, ast: Pkg<Resolved>): Pkg<Typecked> {
|
|||
};
|
||||
}
|
||||
case "type": {
|
||||
const ty = typeOfItem(cx, item.id, [], item.span);
|
||||
const ty = typeOfItem(cx, item.id, item.span);
|
||||
|
||||
switch (item.type.kind) {
|
||||
case "struct": {
|
||||
|
|
@ -135,7 +135,7 @@ export function typeck(gcx: GlobalContext, ast: Pkg<Resolved>): Pkg<Typecked> {
|
|||
return item;
|
||||
}
|
||||
case "global": {
|
||||
const ty = typeOfItem(cx, item.id, [], item.span);
|
||||
const ty = typeOfItem(cx, item.id, item.span);
|
||||
const { init } = item;
|
||||
|
||||
let initChecked: Expr<Typecked>;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { ItemId, Resolved, Type } from "../ast";
|
||||
import { Ident, Item, ItemId, Resolved, Type, Typecked } from "../ast";
|
||||
import { CompilerError, Span } from "../error";
|
||||
import { printTy } from "../printer";
|
||||
import { TYS, Ty, substituteTy } from "../types";
|
||||
import { TYS, Ty, createIdentityGenericArgs, substituteTy } from "../types";
|
||||
import { TypeckCtx, tyError, tyErrorFrom } from "./base";
|
||||
|
||||
function builtinAsTy(cx: TypeckCtx, name: string, span: Span): Ty {
|
||||
|
|
@ -36,58 +36,77 @@ export function lowerAstTy(cx: TypeckCtx, type: Type<Resolved>): Ty {
|
|||
const ident = type.value;
|
||||
const res = ident.res;
|
||||
|
||||
const generics = type.generics.map((type) => lowerAstTy(cx, type));
|
||||
const genericArgs = type.genericArgs.map((type) => lowerAstTy(cx, type));
|
||||
let ty: Ty;
|
||||
let generics: Generics;
|
||||
// We only actually substitute anything when "peeking" behind a type into its
|
||||
// internals, where the params are used. This is only the case for aliases today.
|
||||
let isAlias = false;
|
||||
switch (res.kind) {
|
||||
case "local": {
|
||||
throw new Error("Item type cannot refer to local variable");
|
||||
}
|
||||
case "item": {
|
||||
ty = typeOfItem(cx, res.id, generics, type.span);
|
||||
ty = typeOfItem(cx, res.id, type.span);
|
||||
const item = cx.gcx.findItem<Resolved>(res.id, cx.ast);
|
||||
if (item.kind === "type" && item.type.kind === "alias") {
|
||||
isAlias = true;
|
||||
}
|
||||
generics = itemGenerics(item);
|
||||
break;
|
||||
}
|
||||
case "builtin": {
|
||||
ty = builtinAsTy(cx, res.name, ident.span);
|
||||
generics = { kind: "none" };
|
||||
break;
|
||||
}
|
||||
case "tyParam": {
|
||||
ty = { kind: "param", idx: res.index, name: res.name };
|
||||
generics = { kind: "none" };
|
||||
break;
|
||||
}
|
||||
case "error": {
|
||||
ty = tyErrorFrom(res);
|
||||
break;
|
||||
// Skip generics validation, it's fine!
|
||||
return tyErrorFrom(res);
|
||||
}
|
||||
}
|
||||
|
||||
if (ty.kind === "struct" || ty.kind === "alias") {
|
||||
if (generics.length === ty.params.length) {
|
||||
if (ty.kind === "alias") {
|
||||
return substituteTy(ty.genericArgs, ty.actual);
|
||||
}
|
||||
return { ...ty, genericArgs: generics };
|
||||
} else {
|
||||
return tyError(
|
||||
cx,
|
||||
new CompilerError(
|
||||
`expected ${ty.params.length} generic arguments, found ${generics.length}`,
|
||||
type.span,
|
||||
),
|
||||
);
|
||||
}
|
||||
} else if (ty.kind !== "error") {
|
||||
if (generics.length > 0) {
|
||||
return tyError(
|
||||
cx,
|
||||
new CompilerError(
|
||||
`type ${printTy(ty)} does not take generic arguments`,
|
||||
type.span,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (
|
||||
(generics.kind === "none" || generics.params.length === 0) &&
|
||||
genericArgs.length > 0
|
||||
) {
|
||||
return tyError(
|
||||
cx,
|
||||
new CompilerError(
|
||||
`type ${printTy(ty)} does not take any generic arguments but ${
|
||||
genericArgs.length
|
||||
} were passed`,
|
||||
type.span,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (
|
||||
generics.kind === "some" &&
|
||||
generics.params.length > genericArgs.length
|
||||
) {
|
||||
return tyError(
|
||||
cx,
|
||||
new CompilerError(
|
||||
`missing generics for type ${printTy(ty)}, expected ${
|
||||
generics.params.length
|
||||
}, but only ${genericArgs.length} were passed`,
|
||||
type.span,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (isAlias) {
|
||||
return substituteTy(genericArgs, ty);
|
||||
} else {
|
||||
if (ty.kind === "struct") {
|
||||
return { ...ty, genericArgs };
|
||||
}
|
||||
return ty;
|
||||
}
|
||||
|
||||
return ty;
|
||||
}
|
||||
case "tuple": {
|
||||
return {
|
||||
|
|
@ -115,12 +134,31 @@ export function lowerAstTy(cx: TypeckCtx, type: Type<Resolved>): Ty {
|
|||
}
|
||||
}
|
||||
|
||||
export function typeOfItem(
|
||||
cx: TypeckCtx,
|
||||
itemId: ItemId,
|
||||
genericArgs: Ty[],
|
||||
cause: Span,
|
||||
): Ty {
|
||||
type Generics =
|
||||
| {
|
||||
kind: "none";
|
||||
}
|
||||
| {
|
||||
kind: "some";
|
||||
params: Ident[];
|
||||
};
|
||||
|
||||
function itemGenerics(item: Item<Typecked> | Item<Resolved>): Generics {
|
||||
const none: Generics = { kind: "none" };
|
||||
switch (item.kind) {
|
||||
case "function":
|
||||
case "extern":
|
||||
case "error":
|
||||
case "global":
|
||||
case "mod":
|
||||
case "import":
|
||||
return none;
|
||||
case "type":
|
||||
return { kind: "some", params: item.genericParams };
|
||||
}
|
||||
}
|
||||
|
||||
export function typeOfItem(cx: TypeckCtx, itemId: ItemId, cause: Span): Ty {
|
||||
if (itemId.pkgId !== cx.ast.id) {
|
||||
// Look up foreign items in the foreign pkgs, we don't need to lower those
|
||||
// ourselves.
|
||||
|
|
@ -131,7 +169,7 @@ export function typeOfItem(
|
|||
case "import":
|
||||
case "type":
|
||||
case "global":
|
||||
return substituteTy(genericArgs, item.ty!);
|
||||
return item.ty!;
|
||||
case "mod": {
|
||||
return tyError(
|
||||
cx,
|
||||
|
|
@ -187,14 +225,7 @@ export function typeOfItem(
|
|||
case "struct": {
|
||||
ty = {
|
||||
kind: "struct",
|
||||
genericArgs: item.generics.map(
|
||||
({ name }, idx): Ty => ({
|
||||
kind: "param",
|
||||
name,
|
||||
idx,
|
||||
}),
|
||||
),
|
||||
params: item.generics.map((ident) => ident.name),
|
||||
genericArgs: createIdentityGenericArgs(item.genericParams),
|
||||
itemId: item.id,
|
||||
_name: item.name,
|
||||
fields_no_subst: [
|
||||
|
|
@ -214,52 +245,41 @@ export function typeOfItem(
|
|||
case "alias": {
|
||||
const actual = lowerAstTy(cx, item.type.type);
|
||||
|
||||
ty = {
|
||||
kind: "alias",
|
||||
actual,
|
||||
genericArgs: item.generics.map(
|
||||
({ name }, idx): Ty => ({
|
||||
kind: "param",
|
||||
name,
|
||||
idx,
|
||||
}),
|
||||
),
|
||||
params: item.generics.map((ident) => ident.name),
|
||||
};
|
||||
ty = actual;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "mod": {
|
||||
return tyError(
|
||||
ty = tyError(
|
||||
cx,
|
||||
new CompilerError(
|
||||
`module ${item.name} cannot be used as a type or value`,
|
||||
cause,
|
||||
),
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "extern": {
|
||||
return tyError(
|
||||
ty = tyError(
|
||||
cx,
|
||||
new CompilerError(
|
||||
`extern declaration ${item.name} cannot be used as a type or value`,
|
||||
cause,
|
||||
),
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "global": {
|
||||
ty = lowerAstTy(cx, item.type);
|
||||
break;
|
||||
}
|
||||
case "error": {
|
||||
return tyErrorFrom(item);
|
||||
ty = tyErrorFrom(item);
|
||||
}
|
||||
}
|
||||
|
||||
ty = substituteTy(genericArgs, ty);
|
||||
|
||||
cx.itemTys.set(item.id, ty);
|
||||
return ty;
|
||||
}
|
||||
|
|
|
|||
22
src/types.ts
22
src/types.ts
|
|
@ -1,4 +1,4 @@
|
|||
import { ItemId, Resolution } from "./ast";
|
||||
import { Ident, ItemId, Resolution } from "./ast";
|
||||
import { ErrorEmitted } from "./error";
|
||||
|
||||
export type TyString = {
|
||||
|
|
@ -41,7 +41,6 @@ export type TyVar = {
|
|||
export type TyStruct = {
|
||||
kind: "struct";
|
||||
itemId: ItemId;
|
||||
params: string[];
|
||||
genericArgs: Ty[];
|
||||
_name: string;
|
||||
fields_no_subst: [string, Ty][];
|
||||
|
|
@ -67,13 +66,6 @@ export type TyParam = {
|
|||
name: string;
|
||||
};
|
||||
|
||||
export type TyAlias = {
|
||||
kind: "alias";
|
||||
actual: Ty;
|
||||
genericArgs: Ty[];
|
||||
params: string[];
|
||||
};
|
||||
|
||||
export type TyError = {
|
||||
kind: "error";
|
||||
err: ErrorEmitted;
|
||||
|
|
@ -91,7 +83,6 @@ export type Ty =
|
|||
| TyRawPtr
|
||||
| TyNever
|
||||
| TyParam
|
||||
| TyAlias
|
||||
| TyError;
|
||||
|
||||
export function tyIsUnit(ty: Ty): ty is TyUnit {
|
||||
|
|
@ -140,7 +131,6 @@ export function substituteTy(genericArgs: Ty[], ty: Ty): Ty {
|
|||
params: ty.params.map(subst),
|
||||
};
|
||||
case "struct":
|
||||
case "alias":
|
||||
return {
|
||||
...ty,
|
||||
genericArgs: ty.genericArgs.map(subst),
|
||||
|
|
@ -158,3 +148,13 @@ export function substituteTy(genericArgs: Ty[], ty: Ty): Ty {
|
|||
return ty;
|
||||
}
|
||||
}
|
||||
|
||||
export function createIdentityGenericArgs(params: Ident[]): Ty[] {
|
||||
return params.map(
|
||||
(name, idx): Ty => ({
|
||||
kind: "param",
|
||||
name: name.name,
|
||||
idx,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue