From: Philip Herron <herron.phi...@googlemail.com>

Fixes Rust-GCC#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.phi...@googlemail.com>
---
 gcc/rust/backend/rust-compile-resolve-path.cc | 21 +++++++++++++++++++
 gcc/rust/hir/rust-hir-dump.cc                 |  4 +++-
 gcc/testsuite/rust/compile/issue-3552.rs      | 14 +++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/rust/compile/issue-3552.rs

diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc 
b/gcc/rust/backend/rust-compile-resolve-path.cc
index 2b6880c9b1a..115dd046465 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -301,6 +301,27 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType 
*lookup,
            trait->get_mappings ().get_defid (), &trait_ref);
          rust_assert (ok);
 
+         if (trait_item.value ()->get_item_kind ()
+             == HIR::TraitItem::TraitItemKind::CONST)
+           {
+             auto &c
+               = *static_cast<HIR::TraitItemConst *> (trait_item.value ());
+             if (!c.has_expr ())
+               {
+                 rich_location r (line_table, expr_locus);
+                 r.add_range (trait->get_locus ());
+                 r.add_range (c.get_locus ());
+                 rust_error_at (r, "no default expression on trait constant");
+                 return error_mark_node;
+               }
+
+             return CompileExpr::Compile (c.get_expr (), ctx);
+           }
+
+         if (trait_item.value ()->get_item_kind ()
+             != HIR::TraitItem::TraitItemKind::FUNC)
+           return error_mark_node;
+
          // the type resolver can only resolve type bounds to their trait
          // item so its up to us to figure out if this path should resolve
          // to an trait-impl-block-item or if it can be defaulted to the
diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc
index d4958410013..0a9d617a919 100644
--- a/gcc/rust/hir/rust-hir-dump.cc
+++ b/gcc/rust/hir/rust-hir-dump.cc
@@ -1932,7 +1932,9 @@ Dump::visit (TraitItemConst &e)
 
   put_field ("name", e.get_name ().as_string ());
   visit_field ("type", e.get_type ());
-  visit_field ("expr", e.get_expr ());
+  if (e.has_expr ())
+    visit_field ("expr", e.get_expr ());
+
   end ("TraitItemConst");
 }
 
diff --git a/gcc/testsuite/rust/compile/issue-3552.rs 
b/gcc/testsuite/rust/compile/issue-3552.rs
new file mode 100644
index 00000000000..9a4451b14b8
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3552.rs
@@ -0,0 +1,14 @@
+trait Foo {
+    const BAR: u32;
+}
+
+const TRAIT_REF_BAR: u32 = <Foo>::BAR;
+// { dg-error "no default expression on trait constant" "" { target *-*-* } 
.-1 }
+
+struct GlobalTraitRef;
+
+impl Foo for GlobalTraitRef {
+    const BAR: u32 = TRAIT_REF_BAR;
+}
+
+fn main() {}
-- 
2.49.0

Reply via email to