Skip to content

Commit 70cd502

Browse files
committed
Add left/right type to TupleType
1 parent e4adb9d commit 70cd502

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

packages/cashc/src/semantic/TypeCheckTraversal.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ export default class TypeCheckTraversal extends AstTraversal {
168168
throw new IndexOutOfBoundsError(node);
169169
}
170170

171-
node.type = (node.tuple.type as TupleType).elementType;
171+
node.type = node.index === 0 ? (node.tuple.type as TupleType).leftType : (node.tuple.type as TupleType).rightType;
172+
172173
return node;
173174
}
174175

@@ -243,6 +244,7 @@ export default class TypeCheckTraversal extends AstTraversal {
243244
// Result of split are two unbounded bytes types (could be improved to do type inference)
244245
node.type = new TupleType(
245246
node.left.type instanceof BytesType ? new BytesType() : PrimitiveType.STRING,
247+
node.left.type instanceof BytesType ? new BytesType() : PrimitiveType.STRING,
246248
);
247249
return node;
248250
default:
@@ -371,7 +373,9 @@ function expectSameSizeBytes(node: BinaryOpNode, left?: Type, right?: Type): voi
371373

372374
function expectTuple(node: ExpectedNode, actual?: Type): void {
373375
if (!(actual instanceof TupleType)) {
374-
throw new UnsupportedTypeError(node, actual, new TupleType());
376+
// We use a placeholder tuple to indicate that we're expecting *any* tuple at all
377+
const placeholderTuple = new TupleType(new BytesType(), new BytesType());
378+
throw new UnsupportedTypeError(node, actual, placeholderTuple);
375379
}
376380
}
377381

packages/utils/src/types.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export class ArrayType {
44
constructor(
55
public elementType: Type,
66
public bound?: number,
7-
) {}
7+
) { }
88

99
toString(): string {
1010
return `${this.elementType}[${this.bound ?? ''}]`;
@@ -14,7 +14,7 @@ export class ArrayType {
1414
export class BytesType {
1515
constructor(
1616
public bound?: number,
17-
) {}
17+
) { }
1818

1919
static fromString(str: string): BytesType {
2020
const bound = str === 'byte' ? 1 : Number.parseInt(str.substring(5), 10) || undefined;
@@ -28,11 +28,12 @@ export class BytesType {
2828

2929
export class TupleType {
3030
constructor(
31-
public elementType?: Type,
32-
) {}
31+
public leftType: Type,
32+
public rightType: Type,
33+
) { }
3334

3435
toString(): string {
35-
return `(${this.elementType}, ${this.elementType})`;
36+
return `(${this.leftType}, ${this.rightType})`;
3637
}
3738
}
3839

0 commit comments

Comments
 (0)