Skip to content

Commit

Permalink
Allow qualified records in clause guards
Browse files Browse the repository at this point in the history
  • Loading branch information
GearsDatapacks committed Nov 24, 2024
1 parent 054ede8 commit e137d16
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
file of same name without warning. It now produces an error.
([PgBiel](https://github.com/PgBiel))

- Fixed a bug where qualified records were not allowed in clause guards.
([Surya Rose](https://github.com/GearsDatapacks))

## v1.6.1 - 2024-11-19

### Bug fixed
Expand Down
48 changes: 43 additions & 5 deletions compiler-core/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1556,14 +1556,21 @@ where

Some((start, Token::Name { name }, end)) => {
self.advance();
let mut unit = ClauseGuard::Var {
location: SrcSpan { start, end },
type_: (),
name,
};

self.parse_function_call_in_clause_guard(start)?;

let mut unit = if let Some(record) =
self.parse_record_in_clause_guard(&name, SrcSpan { start, end })?
{
record
} else {
ClauseGuard::Var {
location: SrcSpan { start, end },
type_: (),
name,
}
};

loop {
let dot_s = match self.maybe_one(&Token::Dot) {
Some((dot_s, _)) => dot_s,
Expand Down Expand Up @@ -1643,6 +1650,37 @@ where
}
}

fn parse_record_in_clause_guard(
&mut self,
module: &EcoString,
module_location: SrcSpan,
) -> Result<Option<UntypedClauseGuard>, ParseError> {
let (name, end) = match (self.tok0.take(), self.peek_tok1()) {
(Some((_, Token::Dot, _)), Some(Token::UpName { .. })) => {
self.advance(); // dot
let Some((_, Token::UpName { name }, end)) = self.next_tok() else {
return Ok(None);
};
(name, end)
}
(tok0, _) => {
self.tok0 = tok0;
return Ok(None);
}
};

if let Some(record) = self.parse_const_record_finish(
module_location.start,
Some((module.clone(), module_location)),
name,
end,
)? {
Ok(Some(ClauseGuard::Constant(record)))
} else {
Ok(None)
}
}

// examples:
// UpName( args )
fn expect_constructor_pattern(
Expand Down
41 changes: 40 additions & 1 deletion compiler-core/src/type_/tests/guards.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{assert_module_error, assert_module_infer};
use crate::{assert_infer_with_module, assert_module_error, assert_module_infer};

#[test]
fn nested_record_access() {
Expand Down Expand Up @@ -45,3 +45,42 @@ pub fn a(a: String) {
"#
);
}

#[test]
fn qualified_record() {
assert_infer_with_module!(
("wibble", "pub type Wibble { Wibble Wobble }"),
"
import wibble
pub fn main(wibble: wibble.Wibble) {
case wibble {
w if w == wibble.Wobble -> True
_ -> False
}
}
",
vec![("main", "fn(Wibble) -> Bool")]
);
}

#[test]
fn qualified_record_with_arguments() {
assert_infer_with_module!(
(
"wibble",
"pub type Wibble { Wibble(Int) Wobble(Int, Float) }"
),
"
import wibble
pub fn main(wibble: wibble.Wibble) {
case wibble {
w if w == wibble.Wobble(1, 3.8) -> True
_ -> False
}
}
",
vec![("main", "fn(Wibble) -> Bool")]
);
}

0 comments on commit e137d16

Please sign in to comment.