Skip to content

Commit

Permalink
Add check for associated items on auto traits
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
P-E-P committed Nov 21, 2023
1 parent 1786c4f commit b1b0de6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
7 changes: 7 additions & 0 deletions gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 0 additions & 12 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions gcc/testsuite/rust/compile/auto_trait_invalid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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 }

0 comments on commit b1b0de6

Please sign in to comment.