]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: resolve: Fix multiple bindings handling in match
authorMahmoud Mohamed <mahadelr19@gmail.com>
Thu, 16 Mar 2023 11:53:44 +0000 (14:53 +0300)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:21:12 +0000 (18:21 +0100)
gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit):
Push a Product context instead of an Or context.

gcc/testsuite/ChangeLog:

* rust/compile/multiple_bindings2.rs: New test.

Signed-off-by: Mahmoud Mohamed <mahadelr19@gmail.com>
gcc/rust/resolve/rust-ast-resolve-expr.cc
gcc/testsuite/rust/compile/multiple_bindings2.rs [new file with mode: 0644]

index 6b7782ce391eef564ea93fac41b8b00ab96f0dde..a2708f53b2909b979909be6fa74e189057172052 100644 (file)
@@ -207,10 +207,10 @@ ResolveExpr::visit (AST::IfLetExpr &expr)
   resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
   resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
 
-  // FIXME: this declaration should be removed after refactoring
-  // parse_match_arm_patterns output into an AltPattern
+  // We know expr.get_patterns () has one pattern at most
+  // so there's no reason to handle it like an AltPattern.
   std::vector<PatternBinding> bindings
-    = {PatternBinding (PatternBoundCtx::Or, std::set<Identifier> ())};
+    = {PatternBinding (PatternBoundCtx::Product, std::set<Identifier> ())};
 
   for (auto &pattern : expr.get_patterns ())
     {
@@ -522,10 +522,10 @@ ResolveExpr::visit (AST::MatchExpr &expr)
        ResolveExpr::go (arm.get_guard_expr ().get (), prefix,
                         canonical_prefix);
 
-      // FIXME: this declaration should be removed after refactoring
-      // parse_match_arms_patterns output into a single AltPattern
+      // We know expr.get_patterns () has one pattern at most
+      // so there's no reason to handle it like an AltPattern.
       std::vector<PatternBinding> bindings
-       = {PatternBinding (PatternBoundCtx::Or, std::set<Identifier> ())};
+       = {PatternBinding (PatternBoundCtx::Product, std::set<Identifier> ())};
 
       // insert any possible new patterns
       for (auto &pattern : arm.get_patterns ())
diff --git a/gcc/testsuite/rust/compile/multiple_bindings2.rs b/gcc/testsuite/rust/compile/multiple_bindings2.rs
new file mode 100644 (file)
index 0000000..e62b18f
--- /dev/null
@@ -0,0 +1,14 @@
+fn main()
+{
+  match (1, 2) {
+    (a, a) => {},
+  }
+  // { dg-error "identifier .a. is bound more than once in the same pattern .E0416." "" { target *-*-* } .-2 }
+
+  if let (a, a) = (1, 2) {}
+  // { dg-error "identifier .a. is bound more than once in the same pattern .E0416." "" { target *-*-* } .-1 }
+
+  let (a, a) = (1, 2);
+  // { dg-error "identifier .a. is bound more than once in the same pattern .E0416." "" { target *-*-* } .-1 }
+
+}