Skip to content

Commit

Permalink
Attempted to access a nonexistent field [E0609]
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
	Add error code and update error message

gcc/testsuite/ChangeLog:

	* rust/compile/nonexistent-field.rs: New test.

Signed-off-by: Muhammad Mahad <[email protected]>
  • Loading branch information
MahadMuhammad authored and CohenArthur committed Aug 16, 2024
1 parent e61d37a commit 0f2062b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ TypeCheckExpr::visit (HIR::TupleIndexExpr &expr)
TupleIndex index = expr.get_tuple_index ();
if ((size_t) index >= tuple->num_fields ())
{
rust_error_at (expr.get_locus (), "unknown field at index %i", index);
rust_error_at (expr.get_locus (), ErrorCode::E0609,
"no field %qi on type %qs", index,
resolved->get_name ().c_str ());
return;
}

Expand Down Expand Up @@ -1078,7 +1080,8 @@ TypeCheckExpr::visit (HIR::FieldAccessExpr &expr)
&lookup, nullptr);
if (!found)
{
rust_error_at (expr.get_locus (), "unknown field [%s] for type [%s]",
rust_error_at (expr.get_locus (), ErrorCode::E0609,
"no field %qs on type %qs",
expr.get_field_name ().as_string ().c_str (),
adt->as_string ().c_str ());
return;
Expand Down
14 changes: 14 additions & 0 deletions gcc/testsuite/rust/compile/nonexistent-field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![allow(unused)]
fn main() {
struct StructWithFields {
x: u32,
}

let s = StructWithFields { x: 0 };
s.foo;
// { dg-error "no field .foo. on type .StructWithFields.StructWithFields .x.u32... .E0609." "" { target *-*-* } .-1 }

let numbers = (1, 2, 3);
numbers.3;
// { dg-error "no field .3. on type ..<integer>, <integer>, <integer>.. .E0609." "" { target *-*-* } .-1 }
}

0 comments on commit 0f2062b

Please sign in to comment.