]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix handling of generic arguments for lifetimes only
authorPhilip Herron <herron.philip@googlemail.com>
Wed, 29 Mar 2023 12:35:53 +0000 (13:35 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:28:42 +0000 (18:28 +0100)
We might have generics that are only for lifetimes this is a bad error
check. We can simply rely on the function to marshall the HIR generics
arguements for the type system to do the error handling for us which will
be more acurate anyway.

Fixes #2043 #2039

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit):
add check for reference marker and type's in self params
(ResolveItem::visit): likewise
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_root_path):
remove bad generics check
(TypeCheckExpr::resolve_segments): likewise
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path): likewise

gcc/testsuite/ChangeLog:

* rust/compile/const_generics_5.rs: update test case
* rust/compile/issue-2043.rs: New test.
* rust/compile/issue-2039.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/resolve/rust-ast-resolve-item.cc
gcc/rust/typecheck/rust-hir-type-check-path.cc
gcc/rust/typecheck/rust-hir-type-check-type.cc
gcc/testsuite/rust/compile/const_generics_5.rs
gcc/testsuite/rust/compile/issue-2039.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/issue-2043.rs [new file with mode: 0644]

index a73c9ee3ecee94a4dbbd59771577d2bc2124dec4..dd8b0a9754141f90281e6756d196e890b85e3713 100644 (file)
@@ -142,13 +142,8 @@ ResolveTraitItems::visit (AST::TraitItemMethod &func)
 
   if (self_param.has_type ())
     {
-      if (self_param.get_has_ref ())
-       {
-         // FIXME is this true?
-         rust_error_at (
-           self_param.get_locus (),
-           "it is not possible to mark self as reference and specify type");
-       }
+      // This shouldn't happen the parser should already error for this
+      rust_assert (!self_param.get_has_ref ());
       ResolveType::go (self_param.get_type ().get ());
     }
   else
@@ -655,13 +650,8 @@ ResolveItem::visit (AST::Method &method)
 
   if (self_param.has_type ())
     {
-      if (self_param.get_has_ref ())
-       {
-         // FIXME is this true?
-         rust_error_at (
-           self_param.get_locus (),
-           "it is not possible to mark self as reference and specify type");
-       }
+      // This shouldn't happen the parser should already error for this
+      rust_assert (!self_param.get_has_ref ());
       ResolveType::go (self_param.get_type ().get ());
     }
   else
index cdd275c9ff257d50fb137bf4013f677f94b63e7d..322d72a883bda47293e1148ee27e228ec231acd5 100644 (file)
@@ -280,14 +280,6 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression &expr, size_t *offset,
       // turbo-fish segment path::<ty>
       if (seg.has_generic_args ())
        {
-         if (!lookup->has_subsititions_defined ())
-           {
-             rust_error_at (expr.get_locus (),
-                            "substitutions not supported for %s",
-                            root_tyty->as_string ().c_str ());
-             return new TyTy::ErrorType (expr.get_mappings ().get_hirid ());
-           }
-
          lookup = SubstMapper::Resolve (lookup, expr.get_locus (),
                                         &seg.get_generic_args ());
          if (lookup->get_kind () == TyTy::TypeKind::ERROR)
@@ -456,13 +448,6 @@ TypeCheckExpr::resolve_segments (NodeId root_resolved_node_id,
 
       if (seg.has_generic_args ())
        {
-         if (!tyseg->has_subsititions_defined ())
-           {
-             rust_error_at (expr_locus, "substitutions not supported for %s",
-                            tyseg->as_string ().c_str ());
-             return;
-           }
-
          tyseg = SubstMapper::Resolve (tyseg, expr_locus,
                                        &seg.get_generic_args ());
          if (tyseg->get_kind () == TyTy::TypeKind::ERROR)
index 981786c9ad5c981bbe47bb3e146cc2dc284d7dc1..c78ecb51fd4aca1032fec9d2d755e223e02f6a77 100644 (file)
@@ -398,17 +398,10 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset,
          HIR::TypePathSegmentGeneric *generic_segment
            = static_cast<HIR::TypePathSegmentGeneric *> (seg.get ());
 
-         if (!lookup->has_subsititions_defined ())
-           {
-             rust_error_at (path.get_locus (),
-                            "TypePath %s declares generic arguments but the "
-                            "type %s does not have any",
-                            path.as_string ().c_str (),
-                            lookup->as_string ().c_str ());
-             return new TyTy::ErrorType (lookup->get_ref ());
-           }
          lookup = SubstMapper::Resolve (lookup, path.get_locus (),
                                         &generic_segment->get_generic_args ());
+         if (lookup->get_kind () == TyTy::TypeKind::ERROR)
+           return new TyTy::ErrorType (seg->get_mappings ().get_hirid ());
        }
       else if (lookup->needs_generic_substitutions ())
        {
@@ -494,13 +487,6 @@ TypeCheckType::resolve_segments (
          HIR::TypePathSegmentGeneric *generic_segment
            = static_cast<HIR::TypePathSegmentGeneric *> (seg.get ());
 
-         if (!tyseg->has_subsititions_defined ())
-           {
-             rust_error_at (expr_locus, "substitutions not supported for %s",
-                            tyseg->as_string ().c_str ());
-             return new TyTy::ErrorType (expr_id);
-           }
-
          tyseg = SubstMapper::Resolve (tyseg, expr_locus,
                                        &generic_segment->get_generic_args ());
          if (tyseg->get_kind () == TyTy::TypeKind::ERROR)
index 46cf6c939687bf3d0d4e4d912ff6de6c335749ee..685229eabd21cbc6e8fab660d58d0ec08dfabf60 100644 (file)
@@ -1,15 +1,12 @@
-// bogus errors but shows the type checking system needs to support const
-// generics with defaults
-
+// { dg-options "-w" }
 struct Foo<const N: usize = { 14 }>;
 
 const M: usize = 15;
-type N = Foo<3>; // { dg-error "TypePath Foo<> declares generic arguments but the type Foo{Foo {}} does not have any" }
+type N = Foo<3>;
 
 fn main() {
-    let _: Foo<15> = Foo; // { dg-error "TypePath Foo<> declares generic arguments but the type Foo{Foo {}} does not have any" }
-    let _: Foo<{ M }> = Foo; // { dg-error "TypePath Foo<> declares generic arguments but the type Foo{Foo {}} does not have any" }
-    let _: Foo<M> = Foo; // { dg-error "TypePath Foo<> declares generic arguments but the type Foo{Foo {}} does not have any" }
-
-    let _: Foo<N> = Foo; // { dg-error "TypePath Foo<N> declares generic arguments but the type Foo{Foo {}} does not have any" }
+    let _: Foo<15> = Foo;
+    let _: Foo<{ M }> = Foo;
+    let _: Foo<M> = Foo;
+    // let _: Foo<N> = Foo; this causes an ICE we need to do const generics
 }
diff --git a/gcc/testsuite/rust/compile/issue-2039.rs b/gcc/testsuite/rust/compile/issue-2039.rs
new file mode 100644 (file)
index 0000000..70eb0ee
--- /dev/null
@@ -0,0 +1,22 @@
+// { dg-options "-w" }
+pub struct Lexer<'a> {
+    input: &'a str,
+}
+
+impl<'a> Lexer<'a> {
+    pub fn new(input: &'a str) -> Lexer<'a> {
+        Lexer { input: input }
+    }
+}
+
+struct Parser<'a> {
+    lexer: &'a mut Lexer<'a>,
+}
+
+impl<'a> Parser<'a> {
+    pub fn new(lexer: &'a mut Lexer) -> Parser<'a> {
+        Parser { lexer: lexer }
+    }
+}
+
+fn main() {}
diff --git a/gcc/testsuite/rust/compile/issue-2043.rs b/gcc/testsuite/rust/compile/issue-2043.rs
new file mode 100644 (file)
index 0000000..efa1ded
--- /dev/null
@@ -0,0 +1,12 @@
+struct Foo<'a> {
+    // { dg-warning "struct is never constructed: .Foo." "" { target *-*-* } .-1 }
+    data: &'a [u8],
+}
+
+impl<'a> Foo<'a> {
+    fn bar(self: &mut Foo<'a>) {}
+    // { dg-warning "associated function is never used: .bar." "" { target *-*-* } .-1 }
+    // { dg-warning "unused name .self." "" { target *-*-* } .-2 }
+}
+
+fn main() {}