Skip to content

Commit ed34bbd

Browse files
committed
gccrs: avoid ICE when canonical path record is missing
This fixes an Internal Compiler Error (ICE) in canonical path resolution triggered by const-generic expressions containing inline item definitions (e.g. structs inside const blocks). Previously, CanonicalPathCtx::get_record unconditionally asserted that a canonical path record exists for a NodeId. However, not all AST nodes (e.g. inline items in const expressions) are guaranteed to have canonical paths, leading to an ICE. Introduce optional canonical path lookup and update callers to handle the absence of a canonical path explicitly, avoiding the assertion failure. gcc/rust/ChangeLog: * resolve/rust-name-resolution-context.h (CanonicalPathCtx::get_path_opt): New helper returning an optional canonical path. * resolve/rust-name-resolution-context.cc: Use optional canonical path lookup instead of asserting on missing records. * testsuite/rust/compile/issue-4143.rs: New regression test. Signed-off-by: shreyas-omkar <shreyashegdeplus06@gmail.com>
1 parent 4c81795 commit ed34bbd

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

gcc/rust/resolve/rust-name-resolution-context.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ CanonicalPathRecordTraitImpl::as_path (const NameResolutionContext &ctx)
184184
impl_id, trait_path_record.as_path (ctx), type_record.as_path (ctx)));
185185
}
186186

187+
Resolver::CanonicalPath
188+
CanonicalPathCtx::get_path (NodeId id) const
189+
{
190+
if (auto rec = get_record_opt (id))
191+
return (*rec)->as_path (*nr_ctx);
192+
193+
return Resolver::CanonicalPath::create_empty ();
194+
}
195+
187196
NameResolutionContext::NameResolutionContext ()
188197
: mappings (Analysis::Mappings::get ()), canonical_ctx (*this)
189198
{}

gcc/rust/resolve/rust-name-resolution-context.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,7 @@ class CanonicalPathCtx
359359
: current_record (nullptr), nr_ctx (&ctx)
360360
{}
361361

362-
Resolver::CanonicalPath get_path (NodeId id) const
363-
{
364-
return get_record (id).as_path (*nr_ctx);
365-
}
362+
Resolver::CanonicalPath get_path (NodeId id) const;
366363

367364
CanonicalPathRecord &get_record (NodeId id) const
368365
{
@@ -380,6 +377,14 @@ class CanonicalPathCtx
380377
return it->second.get ();
381378
}
382379

380+
tl::optional<Resolver::CanonicalPath> get_path_opt (NodeId id) const
381+
{
382+
auto rec = get_record_opt (id);
383+
if (!rec)
384+
return tl::nullopt;
385+
return (*rec)->as_path (*nr_ctx);
386+
}
387+
383388
void insert_record (NodeId id, const Identifier &ident)
384389
{
385390
insert_record (id, ident.as_string ());
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// { dg-do compile }
2+
// { dg-options "-frust-incomplete-and-experimental-compiler-do-not-use" }
3+
// { dg-prune-output "failed to locate crate" }
4+
// { dg-prune-output "unknown crate" }
5+
6+
// ICE regression test for canonical path lookup
7+
// Inline items inside const generic block expressions
8+
// must not require canonical paths.
9+
10+
struct Expr<const N: u32>;
11+
12+
trait Trait0 {
13+
fn required(_: Expr<{
14+
struct Type;
15+
0
16+
}>);
17+
}
18+
19+
fn main() {}

0 commit comments

Comments
 (0)