From 6804337e94b6626998b71a56e208eb6cc4d6e965 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Tue, 16 Apr 2024 16:27:25 -0400 Subject: [PATCH] Fix ambiguity --- crates/rue-compiler/src/lowerer.rs | 18 ++++++++---------- crates/rue-parser/src/grammar.rs | 12 ++++++------ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/crates/rue-compiler/src/lowerer.rs b/crates/rue-compiler/src/lowerer.rs index c98492e..6747b8c 100644 --- a/crates/rue-compiler/src/lowerer.rs +++ b/crates/rue-compiler/src/lowerer.rs @@ -1021,11 +1021,8 @@ impl<'a> Lowerer<'a> { DiagnosticInfo::NonAtomEquality(self.type_name(lhs.ty())), text_range, ); - } else { - self.type_check(rhs.ty(), lhs.ty(), text_range); - } - - if self.is_assignable_to(lhs.ty(), self.nil_type, false, &mut HashSet::new()) { + } else if self.is_assignable_to(lhs.ty(), self.nil_type, false, &mut HashSet::new()) + { if let Hir::Reference(symbol_id) = self.db.hir(rhs.hir()) { guards.insert( *symbol_id, @@ -1040,6 +1037,8 @@ impl<'a> Lowerer<'a> { Guard::new(self.nil_type, self.try_unwrap_optional(lhs.ty())), ); } + } else { + self.type_check(rhs.ty(), lhs.ty(), text_range); } (BinOp::Equals, self.bool_type) @@ -1052,11 +1051,8 @@ impl<'a> Lowerer<'a> { DiagnosticInfo::NonAtomEquality(self.type_name(lhs.ty())), text_range, ); - } else { - self.type_check(rhs.ty(), lhs.ty(), text_range); - } - - if self.is_assignable_to(lhs.ty(), self.nil_type, false, &mut HashSet::new()) { + } else if self.is_assignable_to(lhs.ty(), self.nil_type, false, &mut HashSet::new()) + { if let Hir::Reference(symbol_id) = self.db.hir(rhs.hir()) { guards.insert( *symbol_id, @@ -1071,6 +1067,8 @@ impl<'a> Lowerer<'a> { Guard::new(self.try_unwrap_optional(lhs.ty()), self.nil_type), ); } + } else { + self.type_check(rhs.ty(), lhs.ty(), text_range); } (BinOp::NotEquals, self.bool_type) diff --git a/crates/rue-parser/src/grammar.rs b/crates/rue-parser/src/grammar.rs index f8cda36..a5da4b4 100644 --- a/crates/rue-parser/src/grammar.rs +++ b/crates/rue-parser/src/grammar.rs @@ -175,7 +175,7 @@ fn let_stmt(p: &mut Parser) { fn if_stmt_maybe_else(p: &mut Parser, expr_only: bool) -> bool { let cp = p.checkpoint(); p.expect(SyntaxKind::If); - expr(p); + expr_binding_power(p, 0, false); block(p); let mut has_else = false; if expr_only || p.at(SyntaxKind::Else) { @@ -241,14 +241,14 @@ fn binding_power(op: BinaryOp) -> (u8, u8) { const EXPR_RECOVERY_SET: &[SyntaxKind] = &[SyntaxKind::OpenBrace, SyntaxKind::CloseBrace]; fn expr(p: &mut Parser) { - expr_binding_power(p, 0); + expr_binding_power(p, 0, true); } -fn expr_binding_power(p: &mut Parser, minimum_binding_power: u8) { +fn expr_binding_power(p: &mut Parser, minimum_binding_power: u8, allow_initializer: bool) { if p.at(SyntaxKind::Not) { p.start(SyntaxKind::PrefixExpr); p.bump(); - expr_binding_power(p, 255); + expr_binding_power(p, 255, allow_initializer); p.finish(); return; } @@ -267,7 +267,7 @@ fn expr_binding_power(p: &mut Parser, minimum_binding_power: u8) { } else if p.at(SyntaxKind::Ident) { path(p); - if p.at(SyntaxKind::OpenBrace) { + if p.at(SyntaxKind::OpenBrace) && allow_initializer { p.start_at(checkpoint, SyntaxKind::InitializerExpr); p.bump(); while !p.at(SyntaxKind::CloseBrace) { @@ -381,7 +381,7 @@ fn expr_binding_power(p: &mut Parser, minimum_binding_power: u8) { p.bump(); p.start_at(checkpoint, SyntaxKind::BinaryExpr); - expr_binding_power(p, right_binding_power); + expr_binding_power(p, right_binding_power, allow_initializer); p.finish(); } }