]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Add check for associated items on auto traits
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Mon, 20 Nov 2023 13:59:56 +0000 (14:59 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 30 Jan 2024 11:36:44 +0000 (12:36 +0100)
Reject rust code with associated items on auto traits.

gcc/rust/ChangeLog:

* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add auto
trait associated item check in AST validation pass.
* parse/rust-parse-impl.h: Remove old error emission done during
parsing pass.

gcc/testsuite/ChangeLog:

* rust/compile/auto_trait_invalid.rs: Update old test with updated
error message.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/checks/errors/rust-ast-validation.cc
gcc/rust/parse/rust-parse-impl.h
gcc/testsuite/rust/compile/auto_trait_invalid.rs

index aeae6035db8f708a7b69253922d31c8c23af33aa..673290959f4bd46ded89f5d28db134a847db1a2c 100644 (file)
@@ -109,6 +109,13 @@ ASTValidation::visit (AST::Trait &trait)
        rust_error_at (trait.get_type_param_bounds ()[0]->get_locus (),
                       ErrorCode::E0568,
                       "auto traits cannot have super traits");
+      if (trait.has_trait_items ())
+       {
+         rust_error_at (trait.get_identifier ().get_locus (), ErrorCode::E0380,
+                        "auto traits cannot have methods or associated items");
+         for (const auto &item : trait.get_trait_items ())
+           Error::Hint (item->get_locus (), "remove this item").emit ();
+       }
     }
 
   AST::ContextualASTVisitor::visit (trait);
index de17412c3b6848fee214080eda939c29017605c1..45c72e495c27429cd0ff8d859d86a75bd6b0400d 100644 (file)
@@ -4989,18 +4989,6 @@ Parser<ManagedTokenSource>::parse_trait (AST::Visibility vis,
       return nullptr;
     }
 
-  if (is_auto_trait && !trait_items.empty ())
-    {
-      add_error (Error (locus, ErrorCode::E0380,
-                       "auto traits cannot have associated items"));
-
-      // FIXME: unsure if this should be done at parsing time or not
-      for (const auto &item : trait_items)
-       add_error (Error::Hint (item->get_locus (), "remove this item"));
-
-      return nullptr;
-    }
-
   trait_items.shrink_to_fit ();
   return std::unique_ptr<AST::Trait> (
     new AST::Trait (std::move (ident), is_unsafe, is_auto_trait,
index 66e45531f5d808a8274c774cb85335f58a03a656..3be2acbb53b6355c409e3307227e06d3f9702417 100644 (file)
@@ -2,7 +2,9 @@
 
 #![feature(optin_builtin_traits)]
 
-unsafe auto trait Invalid { // { dg-error "auto traits cannot have associated items" }
+auto trait Invalid {
+    // { dg-error "auto traits cannot have methods or associated items" "" { target *-*-* } .-1 }
+
     fn foo(); // { dg-message "remove this item" }
 
     fn bar() {} // { dg-message "remove this item" }
@@ -13,4 +15,3 @@ unsafe auto trait Invalid { // { dg-error "auto traits cannot have associated it
 
     const BAR: i32 = 15; // { dg-message "remove this item" }
 }
-// { dg-error "failed to parse item in crate" "" {target *-*-* } .+1 }