Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add improved error when a field is redefined in a struct constructor #2797

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions gcc/rust/typecheck/rust-hir-type-check-struct-field.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class TypeCheckStructExpr : public TypeCheckBase
protected:
void resolve (HIR::StructExprStructFields &struct_expr);

void visit (HIR::StructExprFieldIdentifierValue &field);
void visit (HIR::StructExprFieldIndexValue &field);
void visit (HIR::StructExprFieldIdentifier &field);
bool visit (HIR::StructExprFieldIdentifierValue &field);
bool visit (HIR::StructExprFieldIndexValue &field);
bool visit (HIR::StructExprFieldIdentifier &field);

private:
TypeCheckStructExpr (HIR::Expr *e);
Expand Down
90 changes: 61 additions & 29 deletions gcc/rust/typecheck/rust-hir-type-check-struct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,26 @@ TypeCheckStructExpr::resolve (HIR::StructExprStructFields &struct_expr)
switch (field->get_kind ())
{
case HIR::StructExprField::StructExprFieldKind::IDENTIFIER:
visit (static_cast<HIR::StructExprFieldIdentifier &> (*field.get ()));
ok = visit (
static_cast<HIR::StructExprFieldIdentifier &> (*field.get ()));
break;

case HIR::StructExprField::StructExprFieldKind::IDENTIFIER_VALUE:
visit (
ok = visit (
static_cast<HIR::StructExprFieldIdentifierValue &> (*field.get ()));
break;

case HIR::StructExprField::StructExprFieldKind::INDEX_VALUE:
visit (static_cast<HIR::StructExprFieldIndexValue &> (*field.get ()));
ok = visit (
static_cast<HIR::StructExprFieldIndexValue &> (*field.get ()));
break;
}

if (!ok)
{
return;
}

if (resolved_field_value_expr == nullptr)
{
rust_fatal_error (field->get_locus (),
Expand Down Expand Up @@ -238,24 +245,30 @@ TypeCheckStructExpr::resolve (HIR::StructExprStructFields &struct_expr)
resolved = struct_def;
}

void
bool
TypeCheckStructExpr::visit (HIR::StructExprFieldIdentifierValue &field)
{
auto it = fields_assigned.find (field.field_name.as_string ());
if (it != fields_assigned.end ())
{
rust_fatal_error (field.get_locus (), "used more than once");
return;
}

size_t field_index;
TyTy::StructFieldType *field_type;
bool ok = variant->lookup_field (field.field_name.as_string (), &field_type,
&field_index);
if (!ok)
{
rust_error_at (field.get_locus (), "unknown field");
return;
return true;
}

auto it = adtFieldIndexToField.find (field_index);
if (it != adtFieldIndexToField.end ())
{
rich_location repeat_location (line_table, field.get_locus ());
auto prev_field_locus = it->second->get_locus ();
repeat_location.add_range (prev_field_locus);

rust_error_at (repeat_location, ErrorCode::E0062,
"field %<%s%> specified more than once",
field.field_name.as_string ().c_str ());
return false;
}

TyTy::BaseType *value = TypeCheckExpr::Resolve (field.get_value ().get ());
Expand All @@ -273,26 +286,35 @@ TypeCheckStructExpr::visit (HIR::StructExprFieldIdentifierValue &field)
fields_assigned.insert (field.field_name.as_string ());
adtFieldIndexToField[field_index] = &field;
}

return true;
}

void
bool
TypeCheckStructExpr::visit (HIR::StructExprFieldIndexValue &field)
{
std::string field_name (std::to_string (field.get_tuple_index ()));
auto it = fields_assigned.find (field_name);
if (it != fields_assigned.end ())
{
rust_fatal_error (field.get_locus (), "used more than once");
return;
}

size_t field_index;
TyTy::StructFieldType *field_type;
bool ok = variant->lookup_field (field_name, &field_type, &field_index);
if (!ok)
{
rust_error_at (field.get_locus (), "unknown field");
return;
return true;
}

auto it = adtFieldIndexToField.find (field_index);
if (it != adtFieldIndexToField.end ())
{
rich_location repeat_location (line_table, field.get_locus ());
auto prev_field_locus = it->second->get_locus ();
repeat_location.add_range (prev_field_locus);

rust_error_at (repeat_location, ErrorCode::E0062,
"field %<%s%> specified more than once",
field_name.c_str ());
return false;
}

TyTy::BaseType *value = TypeCheckExpr::Resolve (field.get_value ().get ());
Expand All @@ -310,26 +332,34 @@ TypeCheckStructExpr::visit (HIR::StructExprFieldIndexValue &field)
fields_assigned.insert (field_name);
adtFieldIndexToField[field_index] = &field;
}

return true;
}

void
bool
TypeCheckStructExpr::visit (HIR::StructExprFieldIdentifier &field)
{
auto it = fields_assigned.find (field.get_field_name ().as_string ());
if (it != fields_assigned.end ())
{
rust_fatal_error (field.get_locus (), "used more than once");
return;
}

size_t field_index;
TyTy::StructFieldType *field_type;
bool ok = variant->lookup_field (field.get_field_name ().as_string (),
&field_type, &field_index);
if (!ok)
{
rust_error_at (field.get_locus (), "unknown field");
return;
return true;
}

auto it = adtFieldIndexToField.find (field_index);
if (it != adtFieldIndexToField.end ())
{
rich_location repeat_location (line_table, field.get_locus ());
auto prev_field_locus = it->second->get_locus ();
repeat_location.add_range (prev_field_locus);

rust_error_at (repeat_location, ErrorCode::E0062,
"field %<%s%> specified more than once",
field.get_field_name ().as_string ().c_str ());
return false;
}

// we can make the field look like a path expr to take advantage of existing
Expand Down Expand Up @@ -358,6 +388,8 @@ TypeCheckStructExpr::visit (HIR::StructExprFieldIdentifier &field)
fields_assigned.insert (field.get_field_name ().as_string ());
adtFieldIndexToField[field_index] = &field;
}

return true;
}

} // namespace Resolver
Expand Down
10 changes: 10 additions & 0 deletions gcc/testsuite/rust/compile/repeated_constructor_fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
struct Foo {
x: i32,
}

fn main() {
let x = Foo {
x: 0,
x: 0, // { dg-error "field 'x' specified more than once" }
};
}