Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type check on if-expr #3306

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}
}
Loading