Skip to content

Commit 5bccf14

Browse files
committed
gccrs: fix ICE in borrows to invalid expressions
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]>
1 parent fa6747f commit 5bccf14

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

gcc/rust/typecheck/rust-hir-type-check-expr.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,8 @@ void
13661366
TypeCheckExpr::visit (HIR::BorrowExpr &expr)
13671367
{
13681368
TyTy::BaseType *resolved_base = TypeCheckExpr::Resolve (expr.get_expr ());
1369+
if (resolved_base->is<TyTy::ErrorType> ())
1370+
return;
13691371

13701372
// In Rust this is valid because of DST's
13711373
//

gcc/rust/typecheck/rust-tyty-call.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,8 @@ TypeCheckCallExpr::visit (FnType &type)
140140
{
141141
location_t arg_locus = argument->get_locus ();
142142
auto argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (*argument);
143-
if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
144-
{
145-
rust_error_at (
146-
argument->get_locus (),
147-
"failed to resolve type for argument expr in CallExpr");
148-
return;
149-
}
143+
if (argument_expr_tyty->is<TyTy::ErrorType> ())
144+
return;
150145

151146
// it might be a variadic function
152147
if (i < type.num_params ())

gcc/testsuite/rust/compile/issue-3046.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ fn test(v: LOption) -> Res {
1212
return Res::BAD;
1313
}
1414

15-
1615
fn main() {
1716
// Should be:
1817
// test(LOption::Some(2));
19-
//
18+
//
2019
test(LOption(2));
2120
// { dg-error "expected function, tuple struct or tuple variant, found enum" "" { target *-*-* } .-1 }
22-
// { dg-error "failed to resolve type for argument expr in CallExpr" "" { target *-*-* } .-2 }
2321
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
enum State {
2+
Succeeded,
3+
Failed,
4+
}
5+
6+
fn print_on_failure(state: &State) {
7+
let mut num = 0;
8+
match *state {
9+
// error: expected unit struct, unit variant or constant, found tuple
10+
// variant `State::Failed`
11+
State::Failed => {
12+
num = 1;
13+
}
14+
State::Succeeded => {
15+
num = 2;
16+
}
17+
_ => (),
18+
}
19+
}
20+
21+
fn main() {
22+
let b = State::Failed(1);
23+
// { dg-error "expected function, tuple struct or tuple variant, found struct .State." "" { target *-*-* } .-1 }
24+
25+
print_on_failure(&b);
26+
// { dg-error "cannot find value .b. in this scope" "" { target *-*-* } .-1 }
27+
}

gcc/testsuite/rust/compile/nr2/exclude

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,5 @@ issue-266.rs
195195
additional-trait-bounds2.rs
196196
auto_traits2.rs
197197
auto_traits3.rs
198+
issue-3140.rs
198199
# please don't delete the trailing newline

0 commit comments

Comments
 (0)