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

internal: Parse (nightly) const and async trait bounds #16588

Merged
merged 2 commits into from
Feb 16, 2024

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Feb 16, 2024

Both of these bound modifiers were added recently:

The latter will certainly will not do the right thing; namely, async Fn needs to be mapped to the AsyncFn trait. IDK how to do that, so advice would be appreciated, though perhaps we could land this first so the parser isn't complaining about these bounds?

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 16, 2024
Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

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

Can you adjust the grammar file here?

TypeBound =
Lifetime
| ('?' | '~' 'const')? Type

Something like the following should be fine (ungrammar is not an exact representation of the AST)

TypeBound =
  Lifetime
| ('?' | '~')? ('const' | 'async')? Type

Then run the -p syntax tests to regenerate the AST.

@Veykril
Copy link
Member

Veykril commented Feb 16, 2024

IDK how to do that, so advice would be appreciated, though perhaps we could land this first so the parser isn't complaining about these bounds?

Its fine to land the parser changes only, getting rid of the parser errors is the main concern here. The initial lowering of the bounds happen here

impl TypeBound {
pub(crate) fn from_ast(ctx: &LowerCtx<'_>, node: ast::TypeBound) -> Self {
let lower_path_type = |path_type: ast::PathType| ctx.lower_path(path_type.path()?);
match node.kind() {
ast::TypeBoundKind::PathType(path_type) => {
let m = match node.question_mark_token() {
Some(_) => TraitBoundModifier::Maybe,
None => TraitBoundModifier::None,
};
lower_path_type(path_type)
.map(|p| TypeBound::Path(p, m))
.unwrap_or(TypeBound::Error)
}
ast::TypeBoundKind::ForType(for_type) => {
let lt_refs = match for_type.generic_param_list() {
Some(gpl) => gpl
.lifetime_params()
.flat_map(|lp| lp.lifetime().map(|lt| Name::new_lifetime(&lt)))
.collect(),
None => Box::default(),
};
let path = for_type.ty().and_then(|ty| match ty {
ast::Type::PathType(path_type) => lower_path_type(path_type),
_ => None,
});
match path {
Some(p) => TypeBound::ForLifetime(lt_refs, p),
None => TypeBound::Error,
}
}
ast::TypeBoundKind::Lifetime(lifetime) => {
TypeBound::Lifetime(LifetimeRef::new(&lifetime))
}
}
}
, from there you should be able to follow anything else relevant by usages.

Bounds are CONSTNESS ASYNCNESS POLARITY
@compiler-errors
Copy link
Member Author

👍

@@ -614,7 +614,7 @@ TypeBoundList =

TypeBound =
Lifetime
| ('?' | '~' 'const')? Type
| ('~' 'const' | 'const')? 'async'? '?'? Type
Copy link
Member Author

@compiler-errors compiler-errors Feb 16, 2024

Choose a reason for hiding this comment

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

opted for this because it's a bit more accurate representation of the real grammar rustc parses. I can change it if desired, though. I understand the parser modification up in generic_params.rs doesn't reflect all of the combinations that this one does.

Copy link
Member

Choose a reason for hiding this comment

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

No that's fine as long as the possible tokens are represented.

@Veykril
Copy link
Member

Veykril commented Feb 16, 2024

Thanks!
@bors r+

@bors
Copy link
Contributor

bors commented Feb 16, 2024

📌 Commit 36020bb has been approved by Veykril

It is now in the queue for this repository.

@Veykril Veykril changed the title Add support for (nightly) const and async trait bounds internal: Parse (nightly) const and async trait bounds Feb 16, 2024
@bors
Copy link
Contributor

bors commented Feb 16, 2024

⌛ Testing commit 36020bb with merge 0932f89...

@bors
Copy link
Contributor

bors commented Feb 16, 2024

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 0932f89 to master...

@bors bors merged commit 0932f89 into rust-lang:master Feb 16, 2024
11 checks passed
@compiler-errors compiler-errors deleted the async-and-const-bounds branch February 16, 2024 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants