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
15 changes: 12 additions & 3 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,18 @@ 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 ());

infered = TyTy::TupleType::get_unit_type ();
TyTy::BaseType *unit_ty = nullptr;
ok = context->lookup_builtin ("()", &unit_ty);
rust_assert (ok);

infered
= coercion_site (expr.get_mappings ().get_hirid (),
TyTy::TyWithLocation (unit_ty),
TyTy::TyWithLocation (block_type,
expr.get_if_block ().get_locus ()),
expr.get_locus ());
}

void
Expand Down Expand Up @@ -1584,7 +1593,7 @@ TypeCheckExpr::visit (HIR::ClosureExpr &expr)
TyTy::TyVar result_type
= expr.has_return_type ()
? TyTy::TyVar (
TypeCheckType::Resolve (expr.get_return_type ())->get_ref ())
TypeCheckType::Resolve (expr.get_return_type ())->get_ref ())
: TyTy::TyVar::get_implicit_infer_var (expr.get_locus ());

// resolve the block
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 ...." }
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);
}

}
}