Skip to content

Commit a495c4e

Browse files
committed
rust: Fix ICE with infer type used in struct attribute
When visiting a struct declaration, add check for when a struct's attribute is declared as an infer type, emitting an error if true. Fixes #3583 gcc/rust/ChangeLog: * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Add check for infer type on struct's attribute or its subtypes. * typecheck/rust-tyty.cc (BaseType::contains_infer): Add check for an array type's capacity being an infer type. gcc/testsuite/ChangeLog: * rust/compile/infer-type-issue-3583.rs: New test. Signed-off-by: João Novo <joao.c.novo@tecnico.ulisboa.pt>
1 parent cd8412c commit a495c4e

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ void
248248
TypeCheckItem::visit (HIR::StructStruct &struct_decl)
249249
{
250250
auto lifetime_pin = context->push_clean_lifetime_resolver ();
251+
auto &mappings = Analysis::Mappings::get ();
251252

252253
std::vector<TyTy::SubstitutionParamMapping> substitutions;
253254
if (struct_decl.has_generics ())
@@ -266,6 +267,14 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl)
266267
{
267268
TyTy::BaseType *field_type
268269
= TypeCheckType::Resolve (field.get_field_type ());
270+
auto infer_type = field_type->contains_infer ();
271+
if (infer_type)
272+
{
273+
rust_error_at (mappings.lookup_location (infer_type->get_ref ()),
274+
"the placeholder %<_%> is not allowed within types on "
275+
"item signatures for structs");
276+
return;
277+
}
269278
auto *ty_field
270279
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
271280
field.get_field_name ().as_string (),

gcc/rust/typecheck/rust-tyty.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,10 @@ BaseType::contains_infer () const
743743
}
744744
else if (auto arr = x->try_as<const ArrayType> ())
745745
{
746-
return arr->get_element_type ()->contains_infer ();
746+
auto type_infer = (arr->get_element_type ()->contains_infer ());
747+
if (type_infer)
748+
return type_infer;
749+
return arr->get_capacity ()->contains_infer ();
747750
}
748751
else if (auto slice = x->try_as<const SliceType> ())
749752
{
@@ -778,6 +781,13 @@ BaseType::contains_infer () const
778781
{
779782
return x;
780783
}
784+
else if (x->get_kind () == TyTy::TypeKind::CONST)
785+
{
786+
if (x->as_const_type ()->const_kind () == BaseConstType::Infer)
787+
{
788+
return x;
789+
}
790+
}
781791

782792
return nullptr;
783793
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![feature(no_core)]
2+
#![no_core]
3+
4+
struct Test10 {
5+
a: _, // { dg-error "the placeholder ... is not allowed within types on item" }
6+
}
7+
8+
struct Test11 {
9+
a: _, // { dg-error "the placeholder ... is not allowed within types on item" }
10+
b: i32,
11+
}
12+
13+
struct Test12 {
14+
a: (_, _), // { dg-error "the placeholder ... is not allowed within types on item" }
15+
}
16+
17+
struct Test13 {
18+
a: [_; _], // { dg-error "the placeholder ... is not allowed within types on item" }
19+
}
20+
21+
struct Test14 {
22+
a: [i32; _], // { dg-error "the placeholder ... is not allowed within types on item" }
23+
}

0 commit comments

Comments
 (0)