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

Handle async qualifier inside trait #2779

Merged
merged 2 commits into from
Dec 27, 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
5 changes: 5 additions & 0 deletions gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ ASTValidation::visit (AST::Function &function)
rust_error_at (function.get_locus (), ErrorCode::E0379,
"functions in traits cannot be declared const");

// may change soon
if (qualifiers.is_async () && context.back () == Context::TRAIT_IMPL)
rust_error_at (function.get_locus (), ErrorCode::E0706,
"functions in traits cannot be declared %<async%>");
Comment on lines +110 to +113
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great :)


if (valid_context.find (context.back ()) == valid_context.end ()
&& function.has_self_param ())
rust_error_at (
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5784,6 +5784,8 @@ Parser<ManagedTokenSource>::parse_trait_impl_item ()
// function or method
return parse_trait_impl_function_or_method (visibility,
std::move (outer_attrs));
case ASYNC:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const is handled this way because inside a trait, you could have a const variable or a constant function:

impl Foo for u32 {
    const TRAIT_ASSOC_CST: u32 = 15;
    
    const fn constant_fn() -> u32 { 14 }
}

for async, we can only have async functions - so we don't need to bundle the cases for CONST and ASYNC together.

Instead, this is what I would like to see:

Suggested change
case ASYNC:
case ASYNC:
return parse_async_item (visibility, std::move(outer_attrs))
case CONST:
/* snip */

return parse_async_item (visibility, std::move (outer_attrs));
case CONST:
// lookahead to resolve production - could be function/method or const
// item
Expand Down
13 changes: 13 additions & 0 deletions gcc/testsuite/rust/compile/issue-2767.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// { dg-additional-options "-frust-edition=2018" }
trait Foo {
fn f() -> u32;
}

impl Foo for u32 {
async fn f() -> u32 {
// { dg-error "functions in traits cannot be declared .async." "" { target *-*-* } .-1 }
22
}
}

fn main() {}