Skip to content

Commit 341fc10

Browse files
Harishankar14P-E-P
authored andcommitted
gccrs: Fix ICE in insert_associated_trait_impl due to recursion
Recursive const blocks containing trait implementations previously caused an assertion failure (ICE) because the compiler re-visited existing IDs. This patch adds a check to return early if the ID exists, enabling graceful handling of recursion. Fixes #4166 gcc/rust/ChangeLog: * typecheck/rust-typecheck-context.cc (insert_associated_trait_impl): Prevent ICE by checking for existing ID. * typecheck/rust-hir-type-check.h: Update declarations. gcc/testsuite/ChangeLog: * rust/compile/issue-4166.rs: New test. Signed-off-by: Harishankar <[email protected]>
1 parent edad96f commit 341fc10

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

gcc/rust/typecheck/rust-hir-type-check.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class TypeCheckContext
235235
void insert_trait_reference (DefId id, TraitReference &&ref);
236236
bool lookup_trait_reference (DefId id, TraitReference **ref);
237237

238-
void insert_associated_trait_impl (HirId id,
238+
bool insert_associated_trait_impl (HirId id,
239239
AssociatedImplTrait &&associated);
240240
bool lookup_associated_trait_impl (HirId id,
241241
AssociatedImplTrait **associated);

gcc/rust/typecheck/rust-typecheck-context.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,17 @@ TypeCheckContext::lookup_trait_reference (DefId id, TraitReference **ref)
248248
return true;
249249
}
250250

251-
void
251+
bool
252252
TypeCheckContext::insert_associated_trait_impl (
253253
HirId id, AssociatedImplTrait &&associated)
254254
{
255-
rust_assert (associated_impl_traits.find (id)
256-
== associated_impl_traits.end ());
255+
auto it = associated_impl_traits.find (id);
256+
if (it != associated_impl_traits.end ())
257+
{
258+
return false;
259+
}
257260
associated_impl_traits.emplace (id, std::move (associated));
261+
return true;
258262
}
259263

260264
bool
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
pub trait Foo {
3+
type Bar;
4+
fn foo(bar: Self::bar); // { dg-error "failed to resolve path segment using an impl Probe" }
5+
}
6+
7+
pub struct FooImpl;
8+
9+
const foo_impl: () = {
10+
impl Foo for FooImpl {
11+
type Bar = ();
12+
fn foo(_bar: Self::Bar) { // { dg-error "method .foo. has an incompatible type|mismatched types" }
13+
// This is the recursive reference that used to cause the ICE
14+
let () = foo_impl;
15+
}
16+
}
17+
};
18+
19+
fn main() {}

0 commit comments

Comments
 (0)