Skip to content

Commit 08bfb35

Browse files
committed
gccrs: Fix ICE when compiling path which resolves to trait constant
Fixes #3552 gcc/rust/ChangeLog: * backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile): check for Expr trait * hir/rust-hir-dump.cc (Dump::visit): expr is optional gcc/testsuite/ChangeLog: * rust/compile/issue-3552.rs: New test. Signed-off-by: Philip Herron <herron.philip@googlemail.com>
1 parent ce8e35f commit 08bfb35

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

gcc/rust/backend/rust-compile-resolve-path.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,27 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
301301
trait->get_mappings ().get_defid (), &trait_ref);
302302
rust_assert (ok);
303303

304+
if (trait_item.value ()->get_item_kind ()
305+
== HIR::TraitItem::TraitItemKind::CONST)
306+
{
307+
auto &c
308+
= *static_cast<HIR::TraitItemConst *> (trait_item.value ());
309+
if (!c.has_expr ())
310+
{
311+
rich_location r (line_table, expr_locus);
312+
r.add_range (trait->get_locus ());
313+
r.add_range (c.get_locus ());
314+
rust_error_at (r, "no default expression on trait constant");
315+
return error_mark_node;
316+
}
317+
318+
return CompileExpr::Compile (c.get_expr (), ctx);
319+
}
320+
321+
if (trait_item.value ()->get_item_kind ()
322+
!= HIR::TraitItem::TraitItemKind::FUNC)
323+
return error_mark_node;
324+
304325
// the type resolver can only resolve type bounds to their trait
305326
// item so its up to us to figure out if this path should resolve
306327
// to an trait-impl-block-item or if it can be defaulted to the

gcc/rust/hir/rust-hir-dump.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1932,7 +1932,9 @@ Dump::visit (TraitItemConst &e)
19321932

19331933
put_field ("name", e.get_name ().as_string ());
19341934
visit_field ("type", e.get_type ());
1935-
visit_field ("expr", e.get_expr ());
1935+
if (e.has_expr ())
1936+
visit_field ("expr", e.get_expr ());
1937+
19361938
end ("TraitItemConst");
19371939
}
19381940

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
trait Foo {
2+
const BAR: u32;
3+
}
4+
5+
const TRAIT_REF_BAR: u32 = <Foo>::BAR;
6+
// { dg-error "no default expression on trait constant" "" { target *-*-* } .-1 }
7+
8+
struct GlobalTraitRef;
9+
10+
impl Foo for GlobalTraitRef {
11+
const BAR: u32 = TRAIT_REF_BAR;
12+
}
13+
14+
fn main() {}

0 commit comments

Comments
 (0)