Skip to content

Commit dbba478

Browse files
authored
produce a friendlier error on const (#1050)
* produce a friendlier error on const * fix formatting * update changelog * move the entry to changed
1 parent cdd9afa commit dbba478

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111
### Changed
12+
13+
- `quint run` produces a friendlier message when it meets a `const` (#1050)
14+
1215
### Deprecated
1316
### Removed
1417

quint/io-cli-tests.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,9 +801,43 @@ exit $exit_code
801801
```
802802
803803
_1040compileError
804+
HOME/_1040compileError.qnt:2:3 - error: QNT500: Uninitialized const n. Use: import <moduleName>(n=<value>).*
805+
2: const n: int
806+
^^^^^^^^^^^^
807+
804808
HOME/_1040compileError.qnt:5:12 - error: Name n not found
805809
5: assert(n > 0)
806810
^
807811
808812
error: Tests failed
809813
```
814+
815+
### Fail on run with uninitialized constants
816+
817+
<!-- !test in run uninitialized -->
818+
```
819+
output=$(quint run testFixture/_1041compileConst.qnt 2>&1)
820+
exit_code=$?
821+
echo "$output" | sed -e 's/([0-9]*ms)/(duration)/g' \
822+
-e 's#^.*_1041compileConst.qnt# HOME/_1041compileConst.qnt#g'
823+
exit $exit_code
824+
```
825+
826+
<!-- !test exit 1 -->
827+
<!-- !test out run uninitialized -->
828+
```
829+
An example execution:
830+
831+
[failure] Found an issue (duration).
832+
Use --seed=0x0 to reproduce.
833+
Use --verbosity=3 to show executions.
834+
<module_input>:2:3 - error: QNT500: Uninitialized const N. Use: import <moduleName>(N=<value>).*
835+
2: const N: int
836+
^^^^^^^^^^^^
837+
838+
<module_input>:5:24 - error: Name N not found
839+
5: action init = { x' = N }
840+
^
841+
842+
error: Runtime error
843+
```

quint/src/quintError.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export type ErrorCode =
4343
| 'QNT006'
4444
/* QNT007: Type names must start with an uppercase letter */
4545
| 'QNT007'
46+
/* QNT099: Found cyclic definitions */
47+
| 'QNT099'
4648
/* QNT101: Conflicting definitions for '<name>' */
4749
| 'QNT101'
4850
/* QNT102: Module with name '<name>' was already defined */
@@ -61,8 +63,8 @@ export type ErrorCode =
6163
| 'QNT406'
6264
/* QNT407: Cannot import self */
6365
| 'QNT407'
64-
/* QNT099: Found cyclic definitions */
65-
| 'QNT099'
66+
/* QNT500: Unitialized constant */
67+
| 'QNT500'
6668

6769
/* Additional data for a Quint error */
6870
export interface QuintErrorData {

quint/src/runtime/impl/compilerImpl.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { ExecutionListener } from '../trace'
3333
import * as ir from '../../quintIr'
3434

3535
import { RuntimeValue, rv } from './runtimeValue'
36+
import { ErrorCode } from '../../quintError'
3637

3738
import { lastTraceName } from '../compile'
3839

@@ -323,6 +324,13 @@ export class CompilerVisitor implements IRVisitor {
323324
}
324325
}
325326

327+
exitConst(cdef: ir.QuintConst) {
328+
// all constants should be instantiated before running the simulator
329+
const code: ErrorCode = 'QNT500'
330+
const msg = `${code}: Uninitialized const ${cdef.name}. Use: import <moduleName>(${cdef.name}=<value>).*`
331+
this.errorTracker.addCompileError(cdef.id, msg)
332+
}
333+
326334
exitVar(vardef: ir.QuintVar) {
327335
const varName = vardef.name
328336
// simply introduce two registers:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module _1041compileConst {
2+
const N: int
3+
var x: int
4+
5+
action init = { x' = N }
6+
7+
action step = { x' = x - 1 }
8+
}

0 commit comments

Comments
 (0)