diff options
author | Nikos Alexandris <nikos-alexandris@protonmail.com> | 2023-03-03 23:55:59 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-01-16 18:21:08 +0100 |
commit | b0416339c07bb583a96d3a3fa7ec375b3de6d8a1 (patch) | |
tree | 2f8e15c348fc6064095fbc15113c2532829bb4e1 /gcc | |
parent | 29fed556263557d0a933d55159d2f434f43e2c8f (diff) | |
download | gcc-b0416339c07bb583a96d3a3fa7ec375b3de6d8a1.zip gcc-b0416339c07bb583a96d3a3fa7ec375b3de6d8a1.tar.gz gcc-b0416339c07bb583a96d3a3fa7ec375b3de6d8a1.tar.bz2 |
gccrs: 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>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-pattern.cc | 46 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-pattern.h | 4 | ||||
-rw-r--r-- | 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 e57ac58..89366e3 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 c1ec49e..fc99576 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 0000000..fbeb4b2 --- /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 +} |