Skip to content

Commit 98e07b3

Browse files
committed
gccrs: Fix ICE when constant is missing and expression
This is an invalid test case and doesnt work with rustc, we dont fully pick up the errors. Nr2 does handle this and puts out an extra good diagnostic but the old NR doesnt so for now i added this to the exclude list and then when we remove old name resolver this issue goes away. Fixes #3642 gcc/rust/ChangeLog: * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): check for has_expr * hir/rust-hir-dump.cc (Dump::visit): likewise * hir/tree/rust-hir-item.h: add has_expr helper * resolve/rust-ast-resolve-item.cc (ResolveItem::visit): check for has_expr * resolve/rust-ast-resolve-stmt.h: likewise * typecheck/rust-hir-type-check-stmt.cc (TypeCheckStmt::visit): likewise gcc/testsuite/ChangeLog: * rust/compile/nr2/exclude: nr2 puts out an extra error * rust/compile/issue-3642.rs: New test. Signed-off-by: Philip Herron <[email protected]>
1 parent 3b4d11c commit 98e07b3

File tree

8 files changed

+27
-4
lines changed

8 files changed

+27
-4
lines changed

gcc/rust/hir/rust-ast-lower-item.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,9 @@ ASTLoweringItem::visit (AST::ConstantItem &constant)
367367
HIR::Visibility vis = translate_visibility (constant.get_visibility ());
368368

369369
HIR::Type *type = ASTLoweringType::translate (constant.get_type (), true);
370-
HIR::Expr *expr = ASTLoweringExpr::translate (constant.get_expr ());
370+
HIR::Expr *expr = nullptr;
371+
if (constant.has_expr ())
372+
expr = ASTLoweringExpr::translate (constant.get_expr ());
371373

372374
auto crate_num = mappings.get_current_crate ();
373375
Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1926,7 +1926,8 @@ Dump::visit (ConstantItem &e)
19261926
do_vis_item (e);
19271927
put_field ("identifier", e.get_identifier ().as_string ());
19281928
visit_field ("type", e.get_type ());
1929-
visit_field ("const_expr", e.get_expr ());
1929+
if (e.has_expr ())
1930+
visit_field ("const_expr", e.get_expr ());
19301931
end ("ConstantItem");
19311932
}
19321933

gcc/rust/hir/tree/rust-hir-item.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,8 @@ class ConstantItem : public VisItem, public ImplItem
17971797
return *type;
17981798
}
17991799

1800+
bool has_expr () const { return const_expr != nullptr; }
1801+
18001802
Expr &get_expr () { return *const_expr; }
18011803

18021804
Identifier get_identifier () const { return identifier; }

gcc/rust/resolve/rust-ast-resolve-item.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,8 @@ ResolveItem::visit (AST::ConstantItem &constant)
453453
resolve_visibility (constant.get_visibility ());
454454

455455
ResolveType::go (constant.get_type ());
456-
ResolveExpr::go (constant.get_expr (), path, cpath);
456+
if (constant.has_expr ())
457+
ResolveExpr::go (constant.get_expr (), path, cpath);
457458
}
458459

459460
void

gcc/rust/resolve/rust-ast-resolve-stmt.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class ResolveStmt : public ResolverBase
6767
});
6868

6969
ResolveType::go (constant.get_type ());
70-
ResolveExpr::go (constant.get_expr (), prefix, canonical_prefix);
70+
if (constant.has_expr ())
71+
ResolveExpr::go (constant.get_expr (), prefix, canonical_prefix);
7172
}
7273

7374
void visit (AST::LetStmt &stmt) override

gcc/rust/typecheck/rust-hir-type-check-stmt.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ void
6060
TypeCheckStmt::visit (HIR::ConstantItem &constant)
6161
{
6262
TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ());
63+
if (!constant.has_expr ())
64+
{
65+
infered = type;
66+
return;
67+
}
68+
6369
TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (constant.get_expr ());
6470

6571
infered = coercion_site (
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[lang = "sized"]
2+
trait Sized {}
3+
4+
pub trait T<X> {
5+
const D: i32 = {
6+
// { dg-error "mismatched types, expected .i32. but got .()." "" { target *-*-* } .-1 }
7+
const C: X;
8+
};
9+
}

gcc/testsuite/rust/compile/nr2/exclude

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ issue-1487.rs
44
issue-2015.rs
55
issue-3454.rs
66
impl_trait_generic_arg.rs
7+
issue-3642.rs
78
# please don't delete the trailing newline

0 commit comments

Comments
 (0)