From aaa3b718093400b3063c27a6aecab601be6586d5 Mon Sep 17 00:00:00 2001 From: Igor Konnov Date: Wed, 13 Dec 2023 14:39:18 +0100 Subject: [PATCH] introduce type defences (#1306) --- quint/src/runtime/impl/runtimeValue.ts | 26 +++++++++++++++----------- quint/test/repl.test.ts | 6 +++++- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/quint/src/runtime/impl/runtimeValue.ts b/quint/src/runtime/impl/runtimeValue.ts index a7ecfde25..250e8350e 100644 --- a/quint/src/runtime/impl/runtimeValue.ts +++ b/quint/src/runtime/impl/runtimeValue.ts @@ -488,19 +488,23 @@ abstract class RuntimeValueBase implements RuntimeValue { } toSet(): Set { - // the default transformation to a set is done via iteration - let set = Set.of() - for (const e of this) { - set = set.add(e.normalForm()) + if (this.isSetLike) { + // the default transformation to a set is done via iteration + let set = Set.of() + for (const e of this) { + set = set.add(e.normalForm()) + } + return set + } else { + throw new Error('Expected a set-like value') } - return set } toList(): List { if (this instanceof RuntimeValueTupleOrList) { return this.list } else { - return List() + throw new Error('Expected a list value') } } @@ -508,7 +512,7 @@ abstract class RuntimeValueBase implements RuntimeValue { if (this instanceof RuntimeValueRecord) { return this.map } else { - return OrderedMap() + throw new Error('Expected a record value') } } @@ -516,7 +520,7 @@ abstract class RuntimeValueBase implements RuntimeValue { if (this instanceof RuntimeValueMap) { return this.map } else { - return Map() + throw new Error('Expected a map value') } } @@ -524,7 +528,7 @@ abstract class RuntimeValueBase implements RuntimeValue { if (this instanceof RuntimeValueBool) { return (this as RuntimeValueBool).value } else { - return false + throw new Error('Expected a Boolean value') } } @@ -532,7 +536,7 @@ abstract class RuntimeValueBase implements RuntimeValue { if (this instanceof RuntimeValueInt) { return (this as RuntimeValueInt).value } else { - return 0n + throw new Error('Expected an integer value') } } @@ -540,7 +544,7 @@ abstract class RuntimeValueBase implements RuntimeValue { if (this instanceof RuntimeValueStr) { return (this as RuntimeValueStr).value } else { - return '' + throw new Error('Expected a string value') } } diff --git a/quint/test/repl.test.ts b/quint/test/repl.test.ts index 8b9e3e357..128b7006b 100644 --- a/quint/test/repl.test.ts +++ b/quint/test/repl.test.ts @@ -148,7 +148,11 @@ describe('repl ok', () => { |^^^^^^^^^ | | - |1 + |runtime error: error: [QNT501] Expected an integer value + |1 + false + |^^^^^^^^^ + | + | |>>> ` ) await assertRepl(input, output)