Skip to content

Commit

Permalink
gccrs: fix ICE in borrows to invalid expressions
Browse files Browse the repository at this point in the history
We need to check if the borrowed value is valid before creating the
reference type. Otherwise this will lead to an ICE.

Fixes #3140

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): check for error
	* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): likewise and remove debug error

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3046.rs: remove old error message
	* rust/compile/nr2/exclude: nr2 cant handle this
	* rust/compile/issue-3140.rs: New test.

Signed-off-by: Philip Herron <[email protected]>
  • Loading branch information
philberty committed Jan 7, 2025
1 parent fa6747f commit 5bccf14
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
2 changes: 2 additions & 0 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,8 @@ void
TypeCheckExpr::visit (HIR::BorrowExpr &expr)
{
TyTy::BaseType *resolved_base = TypeCheckExpr::Resolve (expr.get_expr ());
if (resolved_base->is<TyTy::ErrorType> ())
return;

// In Rust this is valid because of DST's
//
Expand Down
9 changes: 2 additions & 7 deletions gcc/rust/typecheck/rust-tyty-call.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,8 @@ TypeCheckCallExpr::visit (FnType &type)
{
location_t arg_locus = argument->get_locus ();
auto argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (*argument);
if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
{
rust_error_at (
argument->get_locus (),
"failed to resolve type for argument expr in CallExpr");
return;
}
if (argument_expr_tyty->is<TyTy::ErrorType> ())
return;

// it might be a variadic function
if (i < type.num_params ())
Expand Down
4 changes: 1 addition & 3 deletions gcc/testsuite/rust/compile/issue-3046.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ fn test(v: LOption) -> Res {
return Res::BAD;
}


fn main() {
// Should be:
// test(LOption::Some(2));
//
//
test(LOption(2));
// { dg-error "expected function, tuple struct or tuple variant, found enum" "" { target *-*-* } .-1 }
// { dg-error "failed to resolve type for argument expr in CallExpr" "" { target *-*-* } .-2 }
}
27 changes: 27 additions & 0 deletions gcc/testsuite/rust/compile/issue-3140.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
enum State {
Succeeded,
Failed,
}

fn print_on_failure(state: &State) {
let mut num = 0;
match *state {
// error: expected unit struct, unit variant or constant, found tuple
// variant `State::Failed`
State::Failed => {
num = 1;
}
State::Succeeded => {
num = 2;
}
_ => (),
}
}

fn main() {
let b = State::Failed(1);
// { dg-error "expected function, tuple struct or tuple variant, found struct .State." "" { target *-*-* } .-1 }

print_on_failure(&b);
// { dg-error "cannot find value .b. in this scope" "" { target *-*-* } .-1 }
}
1 change: 1 addition & 0 deletions gcc/testsuite/rust/compile/nr2/exclude
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,5 @@ issue-266.rs
additional-trait-bounds2.rs
auto_traits2.rs
auto_traits3.rs
issue-3140.rs
# please don't delete the trailing newline

0 comments on commit 5bccf14

Please sign in to comment.