Lines
100 %
Functions
Branches
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
// Cranelift backend refuses to run under Miri.
#![cfg(not(miri))]
//! PERSIST rows: a struct accessor applied to a value that is not a struct
//! names the accessor and the operand's type, not the internal natives.
#[path = "persist/rows.rs"]
mod persist;
use nms::interpreter::Interpreter;
use persist::{Row, assert_pinned, row};
use scripting::nomiscript::{Number, Value};
const PT: &str = "(defstruct pt x)";
fn rows() -> Vec<Row> {
vec![
row("S1", &[PT, "(pt-x (entity-count))"]),
row("S2", &[PT, "(setf (pt-x (entity-count)) 3)"]),
row("S3", &[PT, "(pt-x 5)"]),
row("S4", &[PT, "(let ((n (entity-count))) (pt-x n))"]),
row("S5", &[PT, "(pt-x nil)"]),
row("S6", &[PT, "(setf (pt-x 'a) 3)"]),
row("S7", &[PT, "(pt-x (make-pt :x 1))"]),
row(
"S8",
&[PT, "(let ((p (make-pt :x 1))) (setf (pt-x p) 3) (pt-x p))"],
),
]
}
const S1: &str = "err: Compile error: PT-X: expected a PT, got an i";
const S2: &str = "err: Compile error: (SETF PT-X): expected a PT, g";
/// CL: every refused row is an error at run time, S7 is 1 and S8 is 3. The
/// refusal is a compile error because the operand's type is known.
const CL: &[(&str, &[&str])] = &[
("S1", &["pt", S1]),
("S2", &["pt", S2]),
("S3", &["pt", S1]),
("S4", &["pt", S1]),
(
"S5",
&["pt", "err: Compile error: PT-X: expected a PT, got a bo"],
("S6", &["pt", S2]),
("S7", &["pt", "1"]),
("S8", &["pt", "3"]),
];
#[test]
fn every_row_reads_as_common_lisp() {
assert_pinned(&rows(), CL);
fn error_of(source: &str) -> String {
let mut interp = Interpreter::new(false).expect("interpreter");
interp.eval(PT).expect("defstruct");
interp.eval(source).expect_err("refused").to_string()
fn the_refusal_names_the_accessor_and_the_type() {
assert_eq!(
error_of("(pt-x (entity-count))"),
"Compile error: PT-X: expected a PT, got an i32"
);
error_of("(setf (pt-x (entity-count)) 3)"),
"Compile error: (SETF PT-X): expected a PT, got an i32"
fn a_struct_of_another_layout_is_named_by_its_layout() {
interp.eval("(defstruct box p)").expect("defstruct");
let err = interp.eval("(pt-x (make-box :p 1))").expect_err("refused");
err.to_string(),
"Compile error: PT-X: expected a PT, got a BOX"
/// CL: each of these is an error at run time.
fn the_refusal_reads_the_same_on_every_path() {
let plain = "Compile error: PT-X: expected a PT, got an i32";
for source in [
"(funcall (function pt-x) 5)",
"(apply (function pt-x) (list 5))",
"(mapcar (function pt-x) (list 5 6))",
"(handler-case (pt-x 5) (error (e) 0))",
"(pt-x 5)",
] {
assert_eq!(error_of(source), plain, "{source}");
fn an_operand_that_fails_is_refused_by_its_own_error() {
error_of("(pt-x (car 5))"),
"Compile error: CAR expects a list, got 5"
error_of("(pt-x (make-pt :x (+ 1 \"a\")))"),
"Compile error: + expects numeric arguments, got a string value"
fn an_operand_that_raises_raises_its_own_error() {
assert_eq!(error_of("(pt-x (error 'boom \"bad\"))"), "BOOM: bad");
fn a_runtime_field_is_held_by_the_instance() {
let values = interp
.eval("(pt-x (make-pt :x (entity-count)))")
.expect("reads the field");
assert_eq!(values.last(), Some(&Value::Number(Number::Index(0))));
fn an_accessor_takes_exactly_its_operand() {
error_of("(let ((p (make-pt :x 1))) (pt-x p p))"),
"Arity error: lambda expects 1 arguments, got 2"
fn a_runtime_operand_is_named_by_its_type() {
error_of("(funcall (function pt-x) (entity-count))"),
error_of("(pt-x (+ 1 (entity-count)))"),
fn a_struct_in_a_type_message_is_named_by_its_structure() {
error_of("(+ (make-pt :x 1) 1)"),
"Compile error: + expects numeric arguments, got a PT value"
// A parameter has no home, so the store is refused; SBCL 5.
error_of("(defun f (p) (setf p 5) p) (f (make-pt :x 1))"),
"Compile error: type mismatch: expected PT, got i32"
fn a_dolist_variable_over_structs_takes_a_store() {
.eval("(dolist (p (list (make-pt :x 1))) (setf p 5))")
.expect("stores");
assert_eq!(values.last(), Some(&Value::Nil));
fn error_after(definition: &str, source: &str) -> String {
interp.eval(definition).expect("defstruct");
fn the_article_before_a_struct_name_agrees_with_it() {
error_after("(defstruct apple x)", "(+ (make-apple :x 1) 1)"),
"Compile error: + expects numeric arguments, got an APPLE value"
error_after("(defstruct box y)", "(box-y (make-pt :x 1))"),
"Compile error: BOX-Y: expected a BOX, got a PT"
fn a_repeated_slot_name_is_refused() {
let err = interp.eval("(defstruct dup x x)").expect_err("refused");
"Compile error: DEFSTRUCT DUP: duplicate slot name X"