Skip to content

Commit cac576d

Browse files
authored
Merge pull request #655 from OpenVADL/bugifx/fix-crash-constant-mismatch-shift
typechecker: Fix crash shift constant mismatch
2 parents ea13411 + 6672892 commit cac576d

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

vadl/main/vadl/ast/TypeChecker.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText : © 2025 TU Wien <[email protected]>
1+
// SPDX-FileCopyrightText : © 2025-2026 TU Wien <[email protected]>
22
// SPDX-License-Identifier: GPL-3.0-or-later
33
//
44
// This program is free software: you can redistribute it and/or modify
@@ -2593,9 +2593,23 @@ public Void visit(BinaryExpr expr) {
25932593
.build());
25942594
}
25952595

2596-
var result = constantEvaluator.eval(expr);
2597-
expr.type = result.type();
2598-
return null;
2596+
if (constantEvaluator.isConstant(expr.right)) {
2597+
var result = constantEvaluator.eval(expr);
2598+
expr.type = result.type();
2599+
return null;
2600+
}
2601+
2602+
// The left side is constant but the right isn't
2603+
// lets throw an error because it doesn't make sense to cast the left side to the right
2604+
// side if the types are independent of each other.
2605+
throw addErrorAndStopChecking(error("Type Mismatch", expr)
2606+
.description(
2607+
"%s",
2608+
"Cannot infer a type for the result of this operation, because the left side is "
2609+
+ "constant but the right side is not: `%s`.".formatted(rightTyp)
2610+
)
2611+
.help("You can cast the left argument to an explicit type.")
2612+
.build());
25992613
}
26002614

26012615
expr.type = leftTyp;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// There has been a bug where the compiler crashed with this input
2+
// https://github.com/OpenVADL/openvadl/issues/654#issuecomment-3716074918
3+
4+
instruction set architecture ISA = {
5+
register A : Bits<32>
6+
7+
format T : Bits<1> = { a [0..0] }
8+
instruction I: T = A := 6 >> a
9+
assembly I = ("a", "b", 'c', 'd')
10+
encoding I = {a = 1}
11+
}
12+
13+
14+
// Reported Diagnostics:
15+
//
16+
// error: Type Mismatch
17+
// ╭──[test/resources/diagnostics/typechecker/invalidShiftLeftConstRightExplicit.vadl:8:28]
18+
// │
19+
// 8 │ instruction I: T = A := 6 >> a
20+
// │ ^^^^^^
21+
// │
22+
// Cannot infer a type for the result of this operation, because the left side is constant but the right side is not: `UInt<1>`.
23+
// help: You can cast the left argument to an explicit type.
24+
//
25+
//
26+
// Part of the class vadl.ast.DiagnosticsTest

0 commit comments

Comments
 (0)