Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto trait AST validation #2749

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,29 @@ ASTValidation::visit (AST::Function &function)
AST::ContextualASTVisitor::visit (function);
}

void
ASTValidation::visit (AST::Trait &trait)
{
if (trait.is_auto ())
{
if (trait.has_generics ())
rust_error_at (trait.get_generic_params ()[0]->get_locus (),
ErrorCode::E0567,
"auto traits cannot have generic parameters");
if (trait.has_type_param_bounds ())
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);
}

} // namespace Rust
1 change: 1 addition & 0 deletions gcc/rust/checks/errors/rust-ast-validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ASTValidation : public AST::ContextualASTVisitor
virtual void visit (AST::LoopLabel &label);
virtual void visit (AST::ExternalFunctionItem &item);
virtual void visit (AST::Function &function);
virtual void visit (AST::Trait &trait);
};

} // namespace Rust
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 @@ -4983,18 +4983,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 }
4 changes: 4 additions & 0 deletions gcc/testsuite/rust/compile/auto_trait_super_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
trait Cold {}

auto trait IsCool: Cold {}
// { dg-error "auto traits cannot have super traits .E0568." "" { target *-*-* } .-1 }
2 changes: 2 additions & 0 deletions gcc/testsuite/rust/compile/generic_auto_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
auto trait IsCooler<G> {}
// { dg-error "auto traits cannot have generic parameters .E0567." "" { target *-*-* } .-1 }