Skip to content

Commit c8fcb33

Browse files
Lishin1215philberty
authored andcommitted
gccrs: fix ICE by skipping invalid (non-FNDEF) candidates
gcc/rust/ChangeLog: * typecheck/rust-hir-dot-operator.cc (MethodResolver::Select): Skip asserts by checking candidate type and using early-continue. (MethodResolver::try_select_predicate_candidates): Skip invalid candidates. gcc/testsuite/ChangeLog: * rust/compile/issue-3958.rs: New test. Signed-off-by: lishin <[email protected]>
1 parent 92706cc commit c8fcb33

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

gcc/rust/typecheck/rust-hir-dot-operator.cc

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ MethodResolver::Select (std::set<MethodCandidate> &candidates,
5151
{
5252
TyTy::BaseType *candidate_type = candidate.candidate.ty;
5353
rust_assert (candidate_type->get_kind () == TyTy::TypeKind::FNDEF);
54+
if (candidate_type == nullptr
55+
|| candidate_type->get_kind () != TyTy::TypeKind::FNDEF)
56+
continue;
5457
TyTy::FnType &fn = *static_cast<TyTy::FnType *> (candidate_type);
5558

5659
// match the number of arguments
@@ -136,11 +139,11 @@ MethodResolver::assemble_inherent_impl_candidates (
136139
TyTy::BaseType *ty = nullptr;
137140
if (!query_type (func->get_mappings ().get_hirid (), &ty))
138141
return true;
139-
rust_assert (ty != nullptr);
140-
if (ty->get_kind () == TyTy::TypeKind::ERROR)
142+
if (ty == nullptr || ty->get_kind () == TyTy::TypeKind::ERROR)
143+
return true;
144+
if (ty->get_kind () != TyTy::TypeKind::FNDEF)
141145
return true;
142146

143-
rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF);
144147
TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
145148
const TyTy::BaseType *impl_self
146149
= TypeCheckItem::ResolveImplBlockSelf (*impl);
@@ -220,10 +223,11 @@ MethodResolver::assemble_trait_impl_candidates (
220223
TyTy::BaseType *ty = nullptr;
221224
if (!query_type (func->get_mappings ().get_hirid (), &ty))
222225
continue;
223-
if (ty->get_kind () == TyTy::TypeKind::ERROR)
226+
if (ty == nullptr || ty->get_kind () == TyTy::TypeKind::ERROR)
227+
continue;
228+
if (ty->get_kind () != TyTy::TypeKind::FNDEF)
224229
continue;
225230

226-
rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF);
227231
TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
228232
const TyTy::BaseType *impl_self
229233
= TypeCheckItem::ResolveImplBlockSelf (*impl);
@@ -282,7 +286,8 @@ MethodResolver::assemble_trait_impl_candidates (
282286
return true;
283287

284288
TyTy::BaseType *ty = item_ref->get_tyty ();
285-
rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF);
289+
if (ty == nullptr || ty->get_kind () != TyTy::TypeKind::FNDEF)
290+
return true;
286291
TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
287292

288293
trait_candidates.emplace_back (func, trait, fnty, trait_ref, item_ref);
@@ -298,7 +303,8 @@ MethodResolver::try_select_predicate_candidates (TyTy::BaseType &receiver)
298303
for (const auto &predicate : predicate_items)
299304
{
300305
const TyTy::FnType *fn = predicate.fntype;
301-
rust_assert (fn->is_method ());
306+
if (!fn->is_method ())
307+
continue;
302308

303309
TyTy::BaseType *fn_self = fn->get_self_type ();
304310
rust_debug ("dot-operator predicate fn_self={%s} can_eq receiver={%s}",
@@ -345,7 +351,8 @@ MethodResolver::try_select_inherent_impl_candidates (
345351
continue;
346352

347353
TyTy::FnType *fn = impl_item.ty;
348-
rust_assert (fn->is_method ());
354+
if (!fn->is_method ())
355+
continue;
349356

350357
TyTy::BaseType *fn_self = fn->get_self_type ();
351358

@@ -383,7 +390,8 @@ MethodResolver::try_select_trait_impl_candidates (
383390
for (auto trait_item : candidates)
384391
{
385392
TyTy::FnType *fn = trait_item.ty;
386-
rust_assert (fn->is_method ());
393+
if (!fn->is_method ())
394+
continue;
387395

388396
TyTy::BaseType *fn_self = fn->get_self_type ();
389397
rust_debug ("dot-operator trait_item fn_self={%s} can_eq receiver={%s}",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// { dg-options "-fsyntax-only" }
2+
trait A {
3+
fn a(&self) -> <Self as A>::X;
4+
}
5+
6+
impl A for u32 {}
7+
8+
fn main() {
9+
let a: u32 = 0;
10+
let b: u32 = a.a();
11+
}

0 commit comments

Comments
 (0)