Skip to content

Commit 407b7e6

Browse files
joao-novophilberty
authored andcommitted
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 06e1924 commit 407b7e6

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
@@ -325,6 +325,7 @@ void
325325
TypeCheckItem::visit (HIR::StructStruct &struct_decl)
326326
{
327327
auto lifetime_pin = context->push_clean_lifetime_resolver ();
328+
auto &mappings = Analysis::Mappings::get ();
328329

329330
std::vector<TyTy::SubstitutionParamMapping> substitutions;
330331
if (struct_decl.has_generics ())
@@ -343,6 +344,14 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl)
343344
{
344345
TyTy::BaseType *field_type
345346
= TypeCheckType::Resolve (field.get_field_type ());
347+
auto infer_type = field_type->contains_infer ();
348+
if (infer_type)
349+
{
350+
rust_error_at (mappings.lookup_location (infer_type->get_ref ()),
351+
"the placeholder %<_%> is not allowed within types on "
352+
"item signatures for structs");
353+
return;
354+
}
346355
auto *ty_field
347356
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
348357
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
@@ -763,7 +763,10 @@ BaseType::contains_infer () const
763763
}
764764
else if (auto arr = x->try_as<const ArrayType> ())
765765
{
766-
return arr->get_element_type ()->contains_infer ();
766+
auto type_infer = (arr->get_element_type ()->contains_infer ());
767+
if (type_infer)
768+
return type_infer;
769+
return arr->get_capacity ()->contains_infer ();
767770
}
768771
else if (auto slice = x->try_as<const SliceType> ())
769772
{
@@ -798,6 +801,13 @@ BaseType::contains_infer () const
798801
{
799802
return x;
800803
}
804+
else if (x->get_kind () == TyTy::TypeKind::CONST)
805+
{
806+
if (x->as_const_type ()->const_kind () == BaseConstType::Infer)
807+
{
808+
return x;
809+
}
810+
}
801811

802812
return nullptr;
803813
}
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)