]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: fix bad type inference on local patterns
authorPhilip Herron <herron.philip@googlemail.com>
Mon, 4 Nov 2024 14:43:25 +0000 (14:43 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 21 Mar 2025 11:32:55 +0000 (12:32 +0100)
We do not need to inject inference variables on generic patterns
with generic blocks. This will just cause unconstrained inference
variables as they may not unify against something.

Fixes Rust-GCC#2323

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_root_path): dont infer here

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: nr2 cant handle this
* rust/compile/issue-2323.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-hir-type-check-path.cc
gcc/testsuite/rust/compile/issue-2323.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/nr2/exclude

index 231ddd604db2289132ec7aa016752ce2a1639222..7b934b38eb37a62c4717ed4d58af17b454ec2580 100644 (file)
@@ -245,6 +245,7 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression &expr, size_t *offset,
 
       auto seg_is_module = mappings.lookup_module (ref).has_value ();
       auto seg_is_crate = mappings.is_local_hirid_crate (ref);
+      auto seg_is_pattern = mappings.lookup_hir_pattern (ref).has_value ();
       if (seg_is_module || seg_is_crate)
        {
          // A::B::C::this_is_a_module::D::E::F
@@ -321,7 +322,7 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression &expr, size_t *offset,
          if (lookup->get_kind () == TyTy::TypeKind::ERROR)
            return new TyTy::ErrorType (expr.get_mappings ().get_hirid ());
        }
-      else if (lookup->needs_generic_substitutions ())
+      else if (lookup->needs_generic_substitutions () && !seg_is_pattern)
        {
          lookup = SubstMapper::InferSubst (lookup, expr.get_locus ());
        }
diff --git a/gcc/testsuite/rust/compile/issue-2323.rs b/gcc/testsuite/rust/compile/issue-2323.rs
new file mode 100644 (file)
index 0000000..02a3f90
--- /dev/null
@@ -0,0 +1,9 @@
+#[lang = "sized"]
+trait Sized {}
+
+pub struct S<T>(T);
+
+pub fn foo<T>(x: T) {
+    let y = S(x);
+    y.0;
+}
index 4ba27d31f88b3492c82721c3b88b0158165e7453..eaa2a1e0d0bc2c5d743a5cd01f4b9258947ade9d 100644 (file)
@@ -222,4 +222,5 @@ if_let_expr_simple.rs
 iflet.rs
 issue-3033.rs
 issue-3009.rs
+issue-2323.rs
 # please don't delete the trailing newline