Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix type bugs with arrays and pointer subtraction

- Remove superfluous Number cast when the right-hand side is already a numeric
  type.
- Fix pointer subtraction. Force both operands to have bases of equal size.
- Fix alignment of array types.
  • Loading branch information...
commit 4db5ee41fb1171f9fa9e41cb463891cc644502f3 1 parent cc01593
@syg syg authored
Showing with 16 additions and 10 deletions.
  1. +12 −5 src/compiler.js
  2. +0 −1  src/types.js
  3. +4 −4 test/arrays-stack.ljs
View
17 src/compiler.js
@@ -277,6 +277,8 @@
ArrayType.prototype.lint = function () {
check(this.base, "array without element type");
+ this.base.lint();
+ this.align = this.base.align;
};
StructType.prototype.lint = function () {
@@ -294,8 +296,8 @@
this.fields.push(field = members[i]);
type = field.type;
- // Recursively lint inline structs.
- if (type instanceof StructType) {
+ // Recursively lint field types.
+ if (type) {
type.lint();
}
@@ -843,9 +845,10 @@
if (rty instanceof PrimitiveType && rty.integral) {
ty = lty;
} else if (rty instanceof PointerType && op === "-") {
- if (lty.assignableFrom(rty)) {
- ty = Types.i32ty;
- }
+ check(lty.base.size === rty.base.size,
+ "subtraction with incompatible pointer types " +
+ quote(lty) + " and " + quote(rty));
+ ty = Types.i32ty;
}
} else if (BINOP_COMPARISON.indexOf(op) >= 0) {
if (lty instanceof PointerType && isNull(this.right)) {
@@ -1142,6 +1145,10 @@
return expr;
}
+ if (rty && rty.numeric) {
+ return expr;
+ }
+
if (!this.integral) {
return new CallExpression(new Identifier("Number"), [expr], expr.loc);
}
View
1  src/types.js
@@ -183,7 +183,6 @@
};
PointerType.prototype.align = u32ty;
- ArrayType.prototype.align = u32ty;
exports.TypeAlias = TypeAlias;
exports.PrimitiveType = PrimitiveType;
View
8 test/arrays-stack.ljs
@@ -49,7 +49,7 @@ describe('Arrays-Stack', function () {
assert (arr2[1] === 10);
let ST s;
- assert (&s - arr === 126);
+ assert (&s - (ST *)(arr) === 126);
});
it('allocates a struct with an inline array', function () {
@@ -57,7 +57,7 @@ describe('Arrays-Stack', function () {
s.arr[5] = 10;
let ST s2;
- assert (&s2 - &s === 100);
+ assert (&s2 - (ST *)(&s) === 100);
});
it('allocates a struct with an inline array of structs with arrays', function () {
@@ -65,6 +65,6 @@ describe('Arrays-Stack', function () {
s.arr[5].arr[5] = 5;
let ST s2;
- assert (&s2 - &s === 100 * 100);
+ assert (&s2 - (ST *)(&s) === 100 * 100);
});
-});
+});
Please sign in to comment.
Something went wrong with that request. Please try again.