Skip to content

Commit

Permalink
instantiation: Add unit tests for name resolution and typecheck
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenArthur committed Feb 7, 2024
1 parent 1765c13 commit e73ac20
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
34 changes: 32 additions & 2 deletions name_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,11 @@ impl<'enclosing> NameResolveCtx<'enclosing> {
.fold(HashMap::new(), |mut map, reqd| {
let required_node = &fir[&reqd];

let scope = match required_node.kind {
let scope = match &required_node.kind {
Kind::Instantiation { to, .. } => Scope(to.expect_resolved()),
_ => unreachable!(),
other => unreachable!(
"interpreter error - expected Instantiation, got {other:#?}"
),
};

map.insert(reqd, scope);
Expand Down Expand Up @@ -704,4 +706,32 @@ mod tests {

assert!(fir.is_err())
}

#[test]
fn field_instantiation_valid() {
let ast = ast! {
type Foo;

type Record(of: Foo);
where x = Record(of: Foo);
};

let fir = ast.flatten().name_resolve();

assert!(fir.is_ok());
}

#[test]
fn field_instantiation_invalid() {
let ast = ast! {
type Foo;

type Record(of: Foo);
where x = Record(not_of: Foo);
};

let fir = ast.flatten().name_resolve();

assert!(fir.is_err());
}
}
29 changes: 29 additions & 0 deletions typecheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ mod tests {
}

#[test]
#[ignore = "Reevaluate once #653 is done"]
fn assignment_valid() {
let ast = ast! {
type Marker0;
Expand All @@ -235,6 +236,7 @@ mod tests {
}

#[test]
#[ignore = "Reevaluate once #653 is done"]
fn assignment_invalid() {
let ast = ast! {
type Marker0;
Expand All @@ -248,6 +250,33 @@ mod tests {
assert!(fir.is_err());
}

#[test]
fn field_instantiation_valid() {
let ast = ast! {
type Foo;

type Record(m: Foo);
where x = Record(m: Foo);
};
let fir = fir!(ast).type_check();

assert!(fir.is_ok());
}

#[test]
fn field_instantiation_invalid() {
let ast = ast! {
type Foo;
type Bar;

type Record(m: Foo);
where x = Record(m: Bar);
};
let fir = fir!(ast).type_check();

assert!(fir.is_err());
}

#[test]
fn constant_bool() {
let ast = ast! {
Expand Down

0 comments on commit e73ac20

Please sign in to comment.