Skip to content

Commit ee52e7b

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_path unconditionally accessed a canonical path record via get_record, asserting that such a record always exists. However, some AST nodes (e.g. inline items inside const expressions) are not guaranteed to have canonical paths, leading to an ICE. Update canonical path lookup to be optional and handle the absence of a record explicitly, avoiding the assertion failure. gcc/rust/ChangeLog: * resolve/rust-name-resolution-context.h (CanonicalPathCtx::get_path): Move implementation to source file. (CanonicalPathCtx::get_path_opt): New helper returning an optional canonical path. * resolve/rust-name-resolution-context.cc (CanonicalPathCtx::get_path): Use optional canonical path lookup and return empty path instead of asserting on missing records. Signed-off-by: shreyas-omkar <shreyashegdeplus06@gmail.com>
1 parent 9ed1331 commit ee52e7b

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

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)