From: Philip Herron <[email protected]>
We were wrongly adding the assertion that this must not be an enum but
this is a pointless assertion we only care that there are variant in the
ADT and if the field exists in the first variant.
Fixes Rust-GCC#3581
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): fix bad
assertion
gcc/testsuite/ChangeLog:
* rust/compile/nonexistent-field.rs: fix bad error message
* rust/compile/issue-3581-1.rs: New test.
* rust/compile/issue-3581-2.rs: New test.
Signed-off-by: Philip Herron <[email protected]>
---
gcc/rust/typecheck/rust-hir-type-check-expr.cc | 18 ++++++++----------
gcc/testsuite/rust/compile/issue-3581-1.rs | 12 ++++++++++++
gcc/testsuite/rust/compile/issue-3581-2.rs | 9 +++++++++
.../rust/compile/nonexistent-field.rs | 2 +-
4 files changed, 30 insertions(+), 11 deletions(-)
create mode 100644 gcc/testsuite/rust/compile/issue-3581-1.rs
create mode 100644 gcc/testsuite/rust/compile/issue-3581-2.rs
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
index 7ddcebc731d..cbf529a77eb 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -1142,27 +1142,25 @@ TypeCheckExpr::visit (HIR::FieldAccessExpr &expr)
bool is_valid_type = struct_base->get_kind () == TyTy::TypeKind::ADT;
if (!is_valid_type)
{
- rust_error_at (expr.get_locus (),
- "expected algebraic data type got: [%s]",
- struct_base->as_string ().c_str ());
+ rust_error_at (expr.get_locus (), "expected algebraic data type got %qs",
+ struct_base->get_name ().c_str ());
return;
}
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (struct_base);
- rust_assert (!adt->is_enum ());
- rust_assert (adt->number_of_variants () == 1);
-
+ rust_assert (adt->number_of_variants () > 0);
TyTy::VariantDef *vaiant = adt->get_variants ().at (0);
TyTy::StructFieldType *lookup = nullptr;
bool found = vaiant->lookup_field (expr.get_field_name ().as_string (),
&lookup, nullptr);
- if (!found)
+ if (!found || adt->is_enum ())
{
- rust_error_at (expr.get_locus (), ErrorCode::E0609,
- "no field %qs on type %qs",
+ rich_location r (line_table, expr.get_locus ());
+ r.add_range (expr.get_field_name ().get_locus ());
+ rust_error_at (r, ErrorCode::E0609, "no field %qs on type %qs",
expr.get_field_name ().as_string ().c_str (),
- adt->as_string ().c_str ());
+ adt->get_name ().c_str ());
return;
}
diff --git a/gcc/testsuite/rust/compile/issue-3581-1.rs
b/gcc/testsuite/rust/compile/issue-3581-1.rs
new file mode 100644
index 00000000000..eb2f5f033d5
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3581-1.rs
@@ -0,0 +1,12 @@
+enum Foo {
+ Bar,
+}
+
+struct Baz;
+
+fn main() {
+ Foo::Bar.a;
+ // { dg-error "no field .a. on type .Foo. .E0609." "" { target *-*-* } .-1
}
+ Baz.a;
+ // { dg-error "no field .a. on type .Baz. .E0609." "" { target *-*-* } .-1
}
+}
diff --git a/gcc/testsuite/rust/compile/issue-3581-2.rs
b/gcc/testsuite/rust/compile/issue-3581-2.rs
new file mode 100644
index 00000000000..50597844465
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3581-2.rs
@@ -0,0 +1,9 @@
+enum A {
+ X { inner: i32 },
+ Y,
+}
+
+pub fn test() {
+ let _ = A::Y.inner;
+ // { dg-error "no field .inner. on type .A. .E0609." "" { target *-*-* }
.-1 }
+}
diff --git a/gcc/testsuite/rust/compile/nonexistent-field.rs
b/gcc/testsuite/rust/compile/nonexistent-field.rs
index e20c49d3ebf..9bcfb2fe84f 100644
--- a/gcc/testsuite/rust/compile/nonexistent-field.rs
+++ b/gcc/testsuite/rust/compile/nonexistent-field.rs
@@ -6,7 +6,7 @@ fn main() {
let s = StructWithFields { x: 0 };
s.foo;
- // { dg-error "no field .foo. on type .StructWithFields.StructWithFields
.x.u32... .E0609." "" { target *-*-* } .-1 }
+ // { dg-error "no field .foo. on type .StructWithFields. .E0609." "" {
target *-*-* } .-1 }
let numbers = (1, 2, 3);
numbers.3;
--
2.49.0