Skip to content

Commit 0f2062b

Browse files
MahadMuhammadCohenArthur
authored andcommitted
Attempted to access a nonexistent field [E0609]
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]>
1 parent e61d37a commit 0f2062b

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ TypeCheckExpr::visit (HIR::TupleIndexExpr &expr)
8585
TupleIndex index = expr.get_tuple_index ();
8686
if ((size_t) index >= tuple->num_fields ())
8787
{
88-
rust_error_at (expr.get_locus (), "unknown field at index %i", index);
88+
rust_error_at (expr.get_locus (), ErrorCode::E0609,
89+
"no field %qi on type %qs", index,
90+
resolved->get_name ().c_str ());
8991
return;
9092
}
9193

@@ -1078,7 +1080,8 @@ TypeCheckExpr::visit (HIR::FieldAccessExpr &expr)
10781080
&lookup, nullptr);
10791081
if (!found)
10801082
{
1081-
rust_error_at (expr.get_locus (), "unknown field [%s] for type [%s]",
1083+
rust_error_at (expr.get_locus (), ErrorCode::E0609,
1084+
"no field %qs on type %qs",
10821085
expr.get_field_name ().as_string ().c_str (),
10831086
adt->as_string ().c_str ());
10841087
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![allow(unused)]
2+
fn main() {
3+
struct StructWithFields {
4+
x: u32,
5+
}
6+
7+
let s = StructWithFields { x: 0 };
8+
s.foo;
9+
// { dg-error "no field .foo. on type .StructWithFields.StructWithFields .x.u32... .E0609." "" { target *-*-* } .-1 }
10+
11+
let numbers = (1, 2, 3);
12+
numbers.3;
13+
// { dg-error "no field .3. on type ..<integer>, <integer>, <integer>.. .E0609." "" { target *-*-* } .-1 }
14+
}

0 commit comments

Comments
 (0)