diff --git a/fine/build.rs b/fine/build.rs index 78b94784..24565124 100644 --- a/fine/build.rs +++ b/fine/build.rs @@ -17,7 +17,7 @@ fn generate_test_for_file(path: PathBuf) -> String { }; let line = line.trim(); - if line == "@concrete:" { + if line == "concrete:" { let mut concrete = String::new(); while let Some(line) = lines.next() { let line = match line.strip_prefix("// | ") { @@ -32,7 +32,7 @@ fn generate_test_for_file(path: PathBuf) -> String { assertions.push(quote! { crate::assert_concrete(&_tree, #concrete, #display_path); }); - } else if let Some(line) = line.strip_prefix("@type:") { + } else if let Some(line) = line.strip_prefix("type:") { let (pos, expected) = line .trim() .split_once(' ') @@ -45,7 +45,7 @@ fn generate_test_for_file(path: PathBuf) -> String { assertions.push(quote! { crate::assert_type_at(&_tree, &_lines, #pos, #expected, #display_path); }); - } else if let Some(line) = line.strip_prefix("@type-error:") { + } else if let Some(line) = line.strip_prefix("type-error:") { let (pos, expected) = line .trim() .split_once(' ') diff --git a/fine/src/main.rs b/fine/src/main.rs index 62c9b484..fe40cff2 100644 --- a/fine/src/main.rs +++ b/fine/src/main.rs @@ -18,7 +18,7 @@ pub fn process_file(file: &str) { // This is... probably wrong, I don't know, what am I doing? for t in tree.trees() { - let _ = semantics.type_of(t); + let _ = semantics.type_of(t, false); } // OK now there might be errors. diff --git a/fine/src/semantics.rs b/fine/src/semantics.rs index 7d087afd..f32eb2c3 100644 --- a/fine/src/semantics.rs +++ b/fine/src/semantics.rs @@ -90,7 +90,6 @@ impl Type { (Type::String, Type::String) => true, (Type::Bool, Type::Bool) => true, (Type::Unreachable, Type::Unreachable) => true, - (Type::Nothing, Type::Nothing) => true, // Avoid introducing more errors (Type::Error, _) => true, @@ -127,7 +126,7 @@ pub struct Semantics<'a> { syntax_tree: &'a SyntaxTree<'a>, lines: &'a Lines, errors: RefCell>, - types: RefCell>, + types: RefCell>, } impl<'a> Semantics<'a> { @@ -210,27 +209,29 @@ impl<'a> Semantics<'a> { } } - pub fn type_of(&self, t: TreeRef) -> Option { - if let Some(existing) = self.types.borrow().get(&t) { + pub fn type_of(&self, t: TreeRef, value_required: bool) -> Option { + if let Some(existing) = self.types.borrow().get(&(t, value_required)) { return Some(existing.clone()); } let tree = &self.syntax_tree[t]; let result = match tree.kind { TreeKind::Error => Some(Type::Error), - TreeKind::UnaryExpression => self.type_of_unary(tree), - TreeKind::BinaryExpression => self.type_of_binary(tree), - TreeKind::TypeExpression => self.type_of_type_expr(tree), - TreeKind::Block => self.type_of_block(tree), + TreeKind::UnaryExpression => self.type_of_unary(tree, value_required), + TreeKind::BinaryExpression => self.type_of_binary(tree, value_required), + TreeKind::TypeExpression => self.type_of_type_expr(tree, value_required), + TreeKind::Block => self.type_of_block(tree, value_required), TreeKind::LiteralExpression => self.type_of_literal(tree), - TreeKind::GroupingExpression => self.type_of_grouping(tree), - TreeKind::ConditionalExpression => self.type_of_conditional(tree), + TreeKind::GroupingExpression => self.type_of_grouping(tree, value_required), + TreeKind::ConditionalExpression => self.type_of_conditional(tree, value_required), TreeKind::CallExpression => self.type_of_call(tree), TreeKind::Argument => self.type_of_argument(tree), TreeKind::LetStatement => Some(Type::Nothing), TreeKind::ReturnStatement => Some(Type::Unreachable), - TreeKind::ExpressionStatement => self.type_of_expression_statement(tree), + TreeKind::ExpressionStatement => { + self.type_of_expression_statement(tree, value_required) + } TreeKind::Identifier => self.type_of_identifier(tree), _ => return None, }; @@ -238,18 +239,20 @@ impl<'a> Semantics<'a> { // NOTE: These return `None` if they encounter some problem. let result = result.unwrap_or(Type::Error); - self.types.borrow_mut().insert(t, result.clone()); + self.types + .borrow_mut() + .insert((t, value_required), result.clone()); Some(result) } - fn type_of_unary(&self, tree: &Tree) -> Option { + fn type_of_unary(&self, tree: &Tree, value_required: bool) -> Option { assert_eq!(tree.kind, TreeKind::UnaryExpression); let op = tree.nth_token(0)?; let expr = tree.nth_tree(1)?; let argument_type = self - .type_of(expr) + .type_of(expr, value_required) .expect("Our argument should be an expression"); match (op.kind, argument_type) { @@ -283,14 +286,14 @@ impl<'a> Semantics<'a> { } } - fn type_of_binary(&self, tree: &Tree) -> Option { + fn type_of_binary(&self, tree: &Tree, value_required: bool) -> Option { assert_eq!(tree.kind, TreeKind::BinaryExpression); let lhs = self - .type_of(tree.nth_tree(0)?) + .type_of(tree.nth_tree(0)?, value_required) .expect("must be an expression"); let op = tree.nth_token(1)?; let rhs = self - .type_of(tree.nth_tree(2)?) + .type_of(tree.nth_tree(2)?, value_required) .expect("must be an expression"); match (op.kind, lhs, rhs) { @@ -335,12 +338,12 @@ impl<'a> Semantics<'a> { } } - fn type_of_type_expr(&self, tree: &Tree) -> Option { + fn type_of_type_expr(&self, tree: &Tree, _value_required: bool) -> Option { assert_eq!(tree.kind, TreeKind::TypeExpression); Some(Type::Error) } - fn type_of_block(&self, tree: &Tree) -> Option { + fn type_of_block(&self, tree: &Tree, value_required: bool) -> Option { assert_eq!(tree.kind, TreeKind::Block); if tree.children.len() < 2 { @@ -360,7 +363,7 @@ impl<'a> Semantics<'a> { let mut is_unreachable = false; for i in 1..last_index { is_unreachable = self - .type_of(tree.nth_tree(i)?) + .type_of(tree.nth_tree(i)?, false) .map(|t| matches!(t, Type::Unreachable)) .unwrap_or(false) || is_unreachable; @@ -371,7 +374,7 @@ impl<'a> Semantics<'a> { // (And explicitly not Error, which is what returning None // would yield.) let last_type = self - .type_of(tree.nth_tree(last_index)?) + .type_of(tree.nth_tree(last_index)?, value_required) .unwrap_or(Type::Nothing); // If anything in this block generated an "Unreachable" then the @@ -396,29 +399,34 @@ impl<'a> Semantics<'a> { Some(pig) } - fn type_of_grouping(&self, tree: &Tree) -> Option { + fn type_of_grouping(&self, tree: &Tree, value_required: bool) -> Option { assert_eq!(tree.kind, TreeKind::GroupingExpression); let expr = tree.nth_tree(1)?; Some( - self.type_of(expr) + self.type_of(expr, value_required) .expect("the thing in the parenthesis must have some type"), ) } - fn type_of_conditional(&self, tree: &Tree) -> Option { + fn type_of_conditional(&self, tree: &Tree, value_required: bool) -> Option { assert_eq!(tree.kind, TreeKind::ConditionalExpression); let cond_tree = tree.nth_tree(1)?; - let cond_type = self.type_of(cond_tree).expect("must be expression"); - let then_type = self.type_of(tree.nth_tree(2)?).expect("must be expression"); + let cond_type = self.type_of(cond_tree, true).expect("must be expression"); + let then_type = self + .type_of(tree.nth_tree(2)?, value_required) + .expect("must be expression"); let has_else = tree .nth_token(3) .map(|t| t.kind == TokenKind::Else) .unwrap_or(false); let else_type = if has_else { - Some(self.type_of(tree.nth_tree(4)?).expect("must be expression")) + Some( + self.type_of(tree.nth_tree(4)?, value_required) + .expect("must be expression"), + ) } else { None }; @@ -432,14 +440,23 @@ impl<'a> Semantics<'a> { match (then_type, else_type) { (Type::Error, _) => Some(Type::Error), (_, Some(Type::Error)) => Some(Type::Error), - - (Type::Unreachable, None) => Some(Type::Unreachable), - (Type::Unreachable, Some(t)) => Some(t), - (t, Some(Type::Unreachable)) => Some(t), - - (then_type, else_type) => { - let else_type = else_type.unwrap_or(Type::Nothing); - if !then_type.compatible_with(&else_type) { + (_, None) if value_required => { + self.report_error_tree( + tree, + "this conditional expression needs an else arm to produce a value", + ); + Some(Type::Error) + } + (then_type, Some(else_type)) if value_required => { + if else_type.compatible_with(&Type::Unreachable) { + // Doesn't matter if the value is required; the else branch + // will never generate a value for us so let's ignore it. + Some(then_type) + } else if then_type.compatible_with(&Type::Unreachable) { + // Or the then branch is unreachable, same thing with else + // then. + Some(else_type) + } else if !then_type.compatible_with(&else_type) { self.report_error_tree( tree, format!("the type of the `then` branch ({then_type}) must match the type of the `else` branch ({else_type})"), @@ -449,6 +466,10 @@ impl<'a> Semantics<'a> { Some(then_type) } } + (_, _) => { + assert!(!value_required); + Some(Type::Unreachable) + } } } } @@ -463,20 +484,22 @@ impl<'a> Semantics<'a> { Some(Type::Error) } - fn type_of_expression_statement(&self, tree: &Tree) -> Option { + fn type_of_expression_statement(&self, tree: &Tree, value_required: bool) -> Option { assert_eq!(tree.kind, TreeKind::ExpressionStatement); let last_is_semicolon = tree .nth_token(tree.children.len() - 1) .map(|t| t.kind == TokenKind::Semicolon) .unwrap_or(false); - let expression_type = self.type_of(tree.nth_tree(0)?).expect("must be expression"); + let expression_type = self + .type_of(tree.nth_tree(0)?, value_required && !last_is_semicolon) + .expect("must be expression"); Some(match expression_type { + Type::Error => Type::Error, Type::Unreachable => Type::Unreachable, _ => { // A semicolon at the end of an expression statement discards - // the value, leaving us with nothing. (Even if the - // expression otherwise generated a type error!) + // the value, leaving us with nothing. if last_is_semicolon { Type::Nothing } else { diff --git a/fine/tests/README.md b/fine/tests/README.md deleted file mode 100644 index 62bedeec..00000000 --- a/fine/tests/README.md +++ /dev/null @@ -1,79 +0,0 @@ -# Snapshot Tests - -The `.fine` files in this directory and its descendants are processed -by `build.rs` into a series of snapshot-tests. The various test -assertions are specified by `@` directives in comments in the file. - -e.g., a test might look like this: - -```fine -// @concrete: -// | File -// | ExpressionStatement -// | BinaryExpression -// | BinaryExpression -// | LiteralExpression -// | Number:'"1"' -// | Star:'"*"' -// | LiteralExpression -// | Number:'"2"' -// | Plus:'"+"' -// | BinaryExpression -// | UnaryExpression -// | Minus:'"-"' -// | LiteralExpression -// | Number:'"3"' -// | Star:'"*"' -// | LiteralExpression -// | Number:'"4"' -// | Semicolon:'";"' -// -1 * 2 + -3 * 4; - -// @type: 532 f64 -``` - -## Assertions - -The various assertions are as follows: - -- The `// @concrete:` assertion says that the following lines - (prefixed with `// | `, as above) describe the concrete syntax tree - of the file after parsing. - - e.g.: - - ```fine - // @concrete: - // | File - // | ExpressionStatement - // | LiteralExpression - // | String:'"\"Hello world\""' - // | Semicolon:'";"' - // - "Hello world"; - ``` - -- The `// @type:` assertion says that the type of the tree at the - given point will match the given type. `@type` assertions usually go - after the contents of the file to make the probe points more stable - in the face of new assertions and whatnot. - - e.g.: - - ```fine - "Hello world!" - // @type: 2 string - ``` - -- The `// @type-error:` assertion says that the type of the tree at - the given point should be an error, and that the error message - provided should be among the generated errors. (Like `@type`, these - usually go after the code, for stability.) - - e.g.: - - ```fine - - "twenty five"; - // @type-error: 0 cannot apply unary operator '-' to value of type string - ``` diff --git a/fine/tests/example_tests.rs b/fine/tests/example_tests.rs index d2bd33b2..ba991106 100644 --- a/fine/tests/example_tests.rs +++ b/fine/tests/example_tests.rs @@ -150,7 +150,7 @@ fn assert_type_at( None => semantic_panic!(&semantics, "Unable to find the subtee at position {pos}"), }; - let tree_type = semantics.type_of(tree_ref); + let tree_type = semantics.type_of(tree_ref, true); let actual = format!("{}", tree_type.unwrap_or(Type::Error)); semantic_assert_eq!( &semantics, @@ -173,7 +173,7 @@ fn assert_type_error_at( None => semantic_panic!(&semantics, "Unable to find the subtee at position {pos}"), }; - let tree_type = semantics.type_of(tree_ref); + let tree_type = semantics.type_of(tree_ref, true); semantic_assert!( &semantics, matches!(tree_type, Some(Type::Error)), diff --git a/fine/tests/expression/arithmetic.fine b/fine/tests/expression/arithmetic.fine index 610838d1..f5ac71e3 100644 --- a/fine/tests/expression/arithmetic.fine +++ b/fine/tests/expression/arithmetic.fine @@ -1,4 +1,4 @@ -// @concrete: +// concrete: // | File // | ExpressionStatement // | BinaryExpression @@ -21,4 +21,4 @@ // 1 * 2 + -3 * 4; -// @type: 532 f64 \ No newline at end of file +// type: 532 f64 \ No newline at end of file diff --git a/fine/tests/expression/boolean.fine b/fine/tests/expression/boolean.fine index e3405c51..01f570f1 100644 --- a/fine/tests/expression/boolean.fine +++ b/fine/tests/expression/boolean.fine @@ -1,4 +1,4 @@ -// @concrete: +// concrete: // | File // | ExpressionStatement // | BinaryExpression @@ -21,4 +21,4 @@ // true and false or false and !true; -// @type: 549 bool +// type: 549 bool diff --git a/fine/tests/expression/conditional.fine b/fine/tests/expression/conditional.fine index c5383036..a8c03cd7 100644 --- a/fine/tests/expression/conditional.fine +++ b/fine/tests/expression/conditional.fine @@ -1,4 +1,4 @@ -// @concrete: +// concrete: // | File // | IfStatement // | ConditionalExpression @@ -27,18 +27,18 @@ if true { "discarded"; 23 } else { 45 } // Here come some type probes! // (type of the condition) -// @type: 667 bool +// type: 667 bool // // (the discarded expression) -// @type: 674 string +// type: 674 string // // (the "then" clause) -// @type: 686 f64 -// @type: 689 f64 +// type: 686 f64 +// type: 689 f64 // // (the "else" clause) -// @type: 696 f64 -// @type: 699 f64 +// type: 696 f64 +// type: 699 f64 // // (the overall expression) -// @type: 664 f64 \ No newline at end of file +// type: 664 f64 \ No newline at end of file diff --git a/fine/tests/expression/errors/binary_mismatch.fine b/fine/tests/expression/errors/binary_mismatch.fine index 9f3e3629..9de7ab12 100644 --- a/fine/tests/expression/errors/binary_mismatch.fine +++ b/fine/tests/expression/errors/binary_mismatch.fine @@ -1,4 +1,4 @@ 112 - "twenty five"; "twenty five" - 112; -// @type-error: 4 cannot apply binary operator '-' to expressions of type 'f64' (on the left) and 'string' (on the right) -// @type-error: 35 cannot apply binary operator '-' to expressions of type 'string' (on the left) and 'f64' (on the right) \ No newline at end of file +// type-error: 4 cannot apply binary operator '-' to expressions of type 'f64' (on the left) and 'string' (on the right) +// type-error: 35 cannot apply binary operator '-' to expressions of type 'string' (on the left) and 'f64' (on the right) \ No newline at end of file diff --git a/fine/tests/expression/errors/if_mismatched_arms.fine b/fine/tests/expression/errors/if_mismatched_arms.fine index 101ca2c4..65dbf06b 100644 --- a/fine/tests/expression/errors/if_mismatched_arms.fine +++ b/fine/tests/expression/errors/if_mismatched_arms.fine @@ -1,2 +1,2 @@ if true { "blarg" } else { 23 } -// @type-error: 0 the type of the `then` branch (string) must match the type of the `else` branch (f64) +// type-error: 0 the type of the `then` branch (string) must match the type of the `else` branch (f64) diff --git a/fine/tests/expression/errors/if_not_bool.fine b/fine/tests/expression/errors/if_not_bool.fine index 770b28e1..01d436af 100644 --- a/fine/tests/expression/errors/if_not_bool.fine +++ b/fine/tests/expression/errors/if_not_bool.fine @@ -1,2 +1,2 @@ if 23 { "what" } else { "the" } -// @type-error: 0 conditions must yield a boolean +// type-error: 0 conditions must yield a boolean diff --git a/fine/tests/expression/errors/if_requires_else.fine b/fine/tests/expression/errors/if_requires_else.fine index 90468291..f258ee5a 100644 --- a/fine/tests/expression/errors/if_requires_else.fine +++ b/fine/tests/expression/errors/if_requires_else.fine @@ -1,2 +1,2 @@ if (if false { true }) { 32 } else { 23 } -// @type-error: 4 the type of the `then` branch (bool) must match the type of the `else` branch (()) +// type-error: 4 this conditional expression needs an else arm to produce a value diff --git a/fine/tests/expression/errors/unary_mismatch.fine b/fine/tests/expression/errors/unary_mismatch.fine index 9f9a4c87..1f8b79e0 100644 --- a/fine/tests/expression/errors/unary_mismatch.fine +++ b/fine/tests/expression/errors/unary_mismatch.fine @@ -1,2 +1,2 @@ - "twenty five"; -// @type-error: 0 cannot apply unary operator '-' to value of type string +// type-error: 0 cannot apply unary operator '-' to value of type string diff --git a/fine/tests/expression/number.fine b/fine/tests/expression/number.fine index 4995ea31..ae01ea54 100644 --- a/fine/tests/expression/number.fine +++ b/fine/tests/expression/number.fine @@ -1,4 +1,4 @@ -// @concrete: +// concrete: // | File // | ExpressionStatement // | LiteralExpression @@ -7,4 +7,4 @@ 42; -// @type: 129 f64 +// type: 129 f64 diff --git a/fine/tests/expression/strings.fine b/fine/tests/expression/strings.fine index b179635f..808ff8ff 100644 --- a/fine/tests/expression/strings.fine +++ b/fine/tests/expression/strings.fine @@ -1,4 +1,4 @@ -// @concrete: +// concrete: // | File // | ExpressionStatement // | BinaryExpression @@ -11,4 +11,4 @@ // "Hello " + 'world!'; -// @type: 261 string \ No newline at end of file +// type: 261 string \ No newline at end of file