Skip to content

Commit

Permalink
Add type check on if-expr
Browse files Browse the repository at this point in the history
Check if an if-expr return void type.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
	Add check on if-expr.

gcc/testsuite/ChangeLog:

	* rust/compile/implicit_returns_err3.rs: Change test to be valid.
	* rust/compile/torture/if.rs: Likewise.
	* rust/compile/if-without-else.rs: New test.

Signed-off-by: Benjamin Thos <[email protected]>
  • Loading branch information
Kamiinarii authored and philberty committed Jan 10, 2025
1 parent b4a525c commit 4786b64
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
11 changes: 10 additions & 1 deletion gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,16 @@ TypeCheckExpr::visit (HIR::IfExpr &expr)
expr.get_if_condition ().get_locus ()),
expr.get_locus ());

TypeCheckExpr::Resolve (expr.get_if_block ());
TyTy::BaseType *block_type = TypeCheckExpr::Resolve (expr.get_if_block ());

TyTy::BaseType *null_ty = nullptr;
ok = context->lookup_builtin ("()", &null_ty);
rust_assert (ok);

unify_site (expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (null_ty),
TyTy::TyWithLocation (block_type,
expr.get_if_block ().get_locus ()),
expr.get_locus ());

infered = TyTy::TupleType::get_unit_type ();
}
Expand Down
9 changes: 9 additions & 0 deletions gcc/testsuite/rust/compile/if-without-else.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn foo(pred: bool) -> u8 {
if pred { // { dg-error "mismatched types" }
1
}
3
}

fn main(){
}
2 changes: 1 addition & 1 deletion gcc/testsuite/rust/compile/implicit_returns_err3.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn test(x: i32) -> i32 { // { dg-error "mismatched types, expected .i32. but got ...." }

Check failure on line 1 in gcc/testsuite/rust/compile/implicit_returns_err3.rs

View workflow job for this annotation

GitHub Actions / build-and-check-ubuntu-32bit

Test failure (FAIL)

(test for excess errors)

Check failure on line 1 in gcc/testsuite/rust/compile/implicit_returns_err3.rs

View workflow job for this annotation

GitHub Actions / build-and-check-ubuntu-64bit

Test failure (FAIL)

(test for excess errors)

Check failure on line 1 in gcc/testsuite/rust/compile/implicit_returns_err3.rs

View workflow job for this annotation

GitHub Actions / build-and-check-ubuntu-64bit-glibcxx

Test failure (FAIL)

(test for excess errors)

Check failure on line 1 in gcc/testsuite/rust/compile/implicit_returns_err3.rs

View workflow job for this annotation

GitHub Actions / build-and-check-asan

Test failure (FAIL)

(test for excess errors)

Check failure on line 1 in gcc/testsuite/rust/compile/implicit_returns_err3.rs

View workflow job for this annotation

GitHub Actions / build-alpine-32bit-and-check-alpine-32bit

Test failure (FAIL)

(test for excess errors)
if x > 1 {
1
return 1;
}
}

Expand Down
8 changes: 6 additions & 2 deletions gcc/testsuite/rust/compile/torture/if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ fn foo() -> bool {

fn bar() {}

fn baz(a: i32) {
a;
}

struct Foo1 {
one: i32
}
Expand All @@ -13,7 +17,7 @@ fn main() {
if foo() {
bar();
let a = Foo1{one: 1};
a.one
baz (a.one);
}

}
}

0 comments on commit 4786b64

Please sign in to comment.