From d0b74db715a65136d6ca3c9e207264539067e4c3 Mon Sep 17 00:00:00 2001 From: John Doty Date: Sat, 20 Jan 2024 11:13:22 -0800 Subject: [PATCH] [fine] Error checking for some cases --- fine/src/semantics.rs | 11 +++++++-- .../tests/expression/errors/class_errors.fine | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 fine/tests/expression/errors/class_errors.fine diff --git a/fine/src/semantics.rs b/fine/src/semantics.rs index 739e3655..7aeea91c 100644 --- a/fine/src/semantics.rs +++ b/fine/src/semantics.rs @@ -1375,7 +1375,14 @@ impl<'a> Semantics<'a> { // Form 2: { x, ... } let environment = self.environment_of(t); let id = tree.nth_token(0)?; - match environment.bind(id)? { + let declaration = match environment.bind(id) { + Some(d) => d, + None => { + self.report_error_tree(tree, format!("cannot find value {id} here")); + return Some(Type::Error); + } + }; + match declaration { Declaration::Variable { declaration_type, .. } @@ -1662,7 +1669,7 @@ fn check_new_object_expression(s: &Semantics, tree: &Tree) { s.report_error_tree_ref( *field_tree, format!( - "field {} has is of type {}, but this expression generates a {}", + "field {} is of type {}, but this expression generates a {}", f.name, f.field_type, expr_type, ), ); diff --git a/fine/tests/expression/errors/class_errors.fine b/fine/tests/expression/errors/class_errors.fine new file mode 100644 index 00000000..fb6f16a0 --- /dev/null +++ b/fine/tests/expression/errors/class_errors.fine @@ -0,0 +1,24 @@ +class Point { + x: f64; + y: f64; +} + +fun test() { + let one = new Point { x: 23 }; + let two = new Point { y: 23 }; + let three = new Point { y: 23, x: 123, z: "hello" }; + let four = new Point { y: 23, x: "what" }; + + let five = new Point { y: 23, x }; + + let x = "now"; + let six = new Point { y: 23, x }; +} + +// @expect-errors: +// | 7:12: missing an initializer for field y +// | 8:12: missing an initializer for field x +// | 9:41: class Point does not have a field named z +// | 10:32: field x is of type f64, but this expression generates a string +// | 12:32: cannot find value x here +// | 15:31: field x is of type f64, but this expression generates a string