public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Add length checking for tuple patterns
@ 2023-03-08 9:56 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-03-08 9:56 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:914b938884f9903681305184534a2e9869760f84
commit 914b938884f9903681305184534a2e9869760f84
Author: Nikos Alexandris <nikos-alexandris@protonmail.com>
Date: Fri Mar 3 23:55:59 2023 +0200
Add length checking for tuple patterns
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit):
Add length checking for tuple patterns.
(TypeCheckPattern::emit_pattern_size_error): New function.
* typecheck/rust-hir-type-check-pattern.h: New function
emit_pattern_size_error.
gcc/testsuite/ChangeLog:
* rust/compile/tuple_mismatch.rs: New test.
Signed-off-by: Nikos Alexandris <nikos-alexandris@protonmail.com>
Diff:
---
gcc/rust/typecheck/rust-hir-type-check-pattern.cc | 46 +++++++++++++++++++----
gcc/rust/typecheck/rust-hir-type-check-pattern.h | 4 ++
gcc/testsuite/rust/compile/tuple_mismatch.rs | 13 +++++++
3 files changed, 55 insertions(+), 8 deletions(-)
diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
index 65a3e673d95..b7412bc405d 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
@@ -262,16 +262,30 @@ TypeCheckPattern::visit (HIR::TuplePattern &pattern)
= *static_cast<HIR::TuplePatternItemsMultiple *> (
pattern.get_items ().get ());
+ if (parent->get_kind () != TyTy::TUPLE)
+ {
+ rust_error_at (pattern.get_locus (), "expected %s, found tuple",
+ parent->as_string ().c_str ());
+ break;
+ }
+
+ const auto &patterns = ref.get_patterns ();
+ size_t nitems_to_resolve = patterns.size ();
+
+ TyTy::TupleType &par = *static_cast<TyTy::TupleType *> (parent);
+ if (patterns.size () != par.get_fields ().size ())
+ {
+ emit_pattern_size_error (pattern, par.get_fields ().size (),
+ patterns.size ());
+ nitems_to_resolve
+ = std::min (nitems_to_resolve, par.get_fields ().size ());
+ }
+
std::vector<TyTy::TyVar> pattern_elems;
- for (size_t i = 0; i < ref.get_patterns ().size (); i++)
+ for (size_t i = 0; i < nitems_to_resolve; i++)
{
- auto &p = ref.get_patterns ()[i];
- TyTy::BaseType *par_type = parent;
- if (parent->get_kind () == TyTy::TUPLE)
- {
- TyTy::TupleType &par = *static_cast<TyTy::TupleType *> (parent);
- par_type = par.get_field (i);
- }
+ auto &p = patterns[i];
+ TyTy::BaseType *par_type = par.get_field (i);
TyTy::BaseType *elem
= TypeCheckPattern::Resolve (p.get (), par_type);
@@ -407,5 +421,21 @@ TypeCheckPattern::visit (HIR::SlicePattern &pattern)
"type checking qualified path patterns not supported");
}
+void
+TypeCheckPattern::emit_pattern_size_error (const HIR::Pattern &pattern,
+ size_t expected_field_count,
+ size_t got_field_count)
+{
+ RichLocation r (pattern.get_locus ());
+ r.add_range (mappings->lookup_location (parent->get_ref ()));
+ rust_error_at (r,
+ "expected a tuple with %lu %s, found one "
+ "with %lu %s",
+ (unsigned long) expected_field_count,
+ expected_field_count == 1 ? "element" : "elements",
+ (unsigned long) got_field_count,
+ got_field_count == 1 ? "element" : "elements");
+}
+
} // namespace Resolver
} // namespace Rust
diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.h b/gcc/rust/typecheck/rust-hir-type-check-pattern.h
index e7550071548..a4fbf888213 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-pattern.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.h
@@ -51,6 +51,10 @@ private:
Analysis::NodeMapping mappings,
Location locus);
+ void emit_pattern_size_error (const HIR::Pattern &pattern,
+ size_t expected_field_count,
+ size_t got_field_count);
+
TyTy::BaseType *parent;
TyTy::BaseType *infered;
};
diff --git a/gcc/testsuite/rust/compile/tuple_mismatch.rs b/gcc/testsuite/rust/compile/tuple_mismatch.rs
new file mode 100644
index 00000000000..fbeb4b27fd4
--- /dev/null
+++ b/gcc/testsuite/rust/compile/tuple_mismatch.rs
@@ -0,0 +1,13 @@
+fn main() {
+ let (_,) = 1; // { dg-error "expected <integer>, found tuple" }
+ let (_,) = (1, 2); // { dg-error "expected a tuple with 2 elements, found one with 1 element" }
+ let (_, _) = (1, 2, 3); // { dg-error "expected a tuple with 3 elements, found one with 2 elements" }
+ let (_, _) = (1,); // { dg-error "expected a tuple with 1 element, found one with 2 elements" }
+}
+
+// The lhs and rhs sizes don't match, but we still resolve 'a' to be bool, we don't
+// error out immediately once we notice the size mismatch.
+fn foo() -> i32 { // { dg-error "expected .i32. got .bool." }
+ let (a, _) = (true, 2, 3); // { dg-error "expected a tuple with 3 elements, found one with 2 elements" }
+ a
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-03-08 9:56 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-08 9:56 [gcc/devel/rust/master] Add length checking for tuple patterns Thomas Schwinge
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).