-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Support syntax for one-line trait reuse #150130
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
Open
aerooneqq
wants to merge
5
commits into
rust-lang:main
Choose a base branch
from
aerooneqq:delegation-one-line-trait-impl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+799
−34
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
829d1bf
Support syntax for one-line trait reuse
aerooneqq fc3f279
Fix using wrong structs in tests
aerooneqq 591b2d2
Addressing review comments
aerooneqq d27d133
Addressing review comments
aerooneqq 3f81911
Addressing review comments
aerooneqq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -117,6 +117,11 @@ impl<'a> Parser<'a> { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| enum ReuseKind { | ||||||
| Path, | ||||||
| Impl, | ||||||
| } | ||||||
|
|
||||||
| impl<'a> Parser<'a> { | ||||||
| pub fn parse_item(&mut self, force_collect: ForceCollect) -> PResult<'a, Option<Box<Item>>> { | ||||||
| let fn_parse_mode = | ||||||
|
|
@@ -249,9 +254,9 @@ impl<'a> Parser<'a> { | |||||
| } else if self.check_keyword_case(exp!(Trait), case) || self.check_trait_front_matter() { | ||||||
| // TRAIT ITEM | ||||||
| self.parse_item_trait(attrs, lo)? | ||||||
| } else if self.check_impl_frontmatter() { | ||||||
| } else if self.check_impl_frontmatter(0) { | ||||||
| // IMPL ITEM | ||||||
| self.parse_item_impl(attrs, def_())? | ||||||
| self.parse_item_impl(attrs, def_(), false)? | ||||||
| } else if let Const::Yes(const_span) = self.parse_constness(case) { | ||||||
| // CONST ITEM | ||||||
| self.recover_const_mut(const_span); | ||||||
|
|
@@ -265,8 +270,8 @@ impl<'a> Parser<'a> { | |||||
| rhs, | ||||||
| define_opaque: None, | ||||||
| })) | ||||||
| } else if self.is_reuse_path_item() { | ||||||
| self.parse_item_delegation()? | ||||||
| } else if let Some(kind) = self.is_reuse_item() { | ||||||
| self.parse_item_delegation(attrs, def_(), kind)? | ||||||
| } else if self.check_keyword_case(exp!(Mod), case) | ||||||
| || self.check_keyword_case(exp!(Unsafe), case) && self.is_keyword_ahead(1, &[kw::Mod]) | ||||||
| { | ||||||
|
|
@@ -367,16 +372,26 @@ impl<'a> Parser<'a> { | |||||
| /// When parsing a statement, would the start of a path be an item? | ||||||
| pub(super) fn is_path_start_item(&mut self) -> bool { | ||||||
| self.is_kw_followed_by_ident(kw::Union) // no: `union::b`, yes: `union U { .. }` | ||||||
| || self.is_reuse_path_item() | ||||||
| || self.is_reuse_item().is_some() // yes: `reuse impl Trait for Struct { self.0 }`, yes: `reuse some_path::foo;` | ||||||
| || self.check_trait_front_matter() // no: `auto::b`, yes: `auto trait X { .. }` | ||||||
| || self.is_async_fn() // no(2015): `async::b`, yes: `async fn` | ||||||
| || matches!(self.is_macro_rules_item(), IsMacroRulesItem::Yes{..}) // no: `macro_rules::b`, yes: `macro_rules! mac` | ||||||
| } | ||||||
|
|
||||||
| fn is_reuse_path_item(&mut self) -> bool { | ||||||
| // no: `reuse ::path` for compatibility reasons with macro invocations | ||||||
| self.token.is_keyword(kw::Reuse) | ||||||
| && self.look_ahead(1, |t| t.is_path_start() && *t != token::PathSep) | ||||||
| fn is_reuse_item(&mut self) -> Option<ReuseKind> { | ||||||
| self.token | ||||||
| .is_keyword(kw::Reuse) | ||||||
| .then(|| { | ||||||
| // no: `reuse ::path` for compatibility reasons with macro invocations | ||||||
| if self.look_ahead(1, |t| t.is_path_start() && *t != token::PathSep) { | ||||||
| Some(ReuseKind::Path) | ||||||
| } else if self.check_impl_frontmatter(1) { | ||||||
| Some(ReuseKind::Impl) | ||||||
| } else { | ||||||
| None | ||||||
| } | ||||||
| }) | ||||||
| .flatten() | ||||||
| } | ||||||
|
|
||||||
| /// Are we sure this could not possibly be a macro invocation? | ||||||
|
|
@@ -560,6 +575,7 @@ impl<'a> Parser<'a> { | |||||
| &mut self, | ||||||
| attrs: &mut AttrVec, | ||||||
| defaultness: Defaultness, | ||||||
| is_reuse: bool, | ||||||
| ) -> PResult<'a, ItemKind> { | ||||||
| let mut constness = self.parse_constness(Case::Sensitive); | ||||||
| let safety = self.parse_safety(Case::Sensitive); | ||||||
|
|
@@ -628,7 +644,11 @@ impl<'a> Parser<'a> { | |||||
|
|
||||||
| generics.where_clause = self.parse_where_clause()?; | ||||||
|
|
||||||
| let impl_items = self.parse_item_list(attrs, |p| p.parse_impl_item(ForceCollect::No))?; | ||||||
| let impl_items = if is_reuse { | ||||||
| Default::default() | ||||||
| } else { | ||||||
| self.parse_item_list(attrs, |p| p.parse_impl_item(ForceCollect::No))? | ||||||
| }; | ||||||
|
|
||||||
| let (of_trait, self_ty) = match ty_second { | ||||||
| Some(ty_second) => { | ||||||
|
|
@@ -699,10 +719,76 @@ impl<'a> Parser<'a> { | |||||
| Ok(ItemKind::Impl(Impl { generics, of_trait, self_ty, items: impl_items, constness })) | ||||||
| } | ||||||
|
|
||||||
| fn parse_item_delegation(&mut self) -> PResult<'a, ItemKind> { | ||||||
| fn parse_item_delegation( | ||||||
| &mut self, | ||||||
| attrs: &mut AttrVec, | ||||||
| defaultness: Defaultness, | ||||||
| kind: ReuseKind, | ||||||
| ) -> PResult<'a, ItemKind> { | ||||||
| let span = self.token.span; | ||||||
| self.expect_keyword(exp!(Reuse))?; | ||||||
|
|
||||||
| let item_kind = match kind { | ||||||
| ReuseKind::Path => self.parse_path_like_delegation(), | ||||||
| ReuseKind::Impl => self.parse_impl_delegation(span, attrs, defaultness), | ||||||
| }?; | ||||||
|
|
||||||
| self.psess.gated_spans.gate(sym::fn_delegation, span.to(self.prev_token.span)); | ||||||
|
|
||||||
| Ok(item_kind) | ||||||
| } | ||||||
|
|
||||||
| fn parse_delegation_body(&mut self) -> PResult<'a, Option<Box<Block>>> { | ||||||
| Ok(if self.check(exp!(OpenBrace)) { | ||||||
| Some(self.parse_block()?) | ||||||
| } else { | ||||||
| self.expect(exp!(Semi))?; | ||||||
| None | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| fn parse_impl_delegation( | ||||||
| &mut self, | ||||||
| span: Span, | ||||||
| attrs: &mut AttrVec, | ||||||
| defaultness: Defaultness, | ||||||
| ) -> PResult<'a, ItemKind> { | ||||||
| let mut impl_item = self.parse_item_impl(attrs, defaultness, true)?; | ||||||
| let ItemKind::Impl(Impl { items, of_trait, .. }) = &mut impl_item else { unreachable!() }; | ||||||
|
|
||||||
| let until_expr_span = span.to(self.prev_token.span); | ||||||
|
|
||||||
| let Some(of_trait) = of_trait else { | ||||||
| return Err(self | ||||||
| .dcx() | ||||||
| .create_err(errors::ImplReuseInherentImpl { span: until_expr_span })); | ||||||
| }; | ||||||
|
|
||||||
| let body = self.parse_delegation_body()?; | ||||||
| let whole_reuse_span = span.to(self.prev_token.span); | ||||||
|
|
||||||
| items.push(Box::new(AssocItem { | ||||||
| id: DUMMY_NODE_ID, | ||||||
| attrs: Default::default(), | ||||||
| span: whole_reuse_span, | ||||||
| tokens: None, | ||||||
| vis: Visibility { | ||||||
| kind: VisibilityKind::Inherited, | ||||||
| span: whole_reuse_span, | ||||||
| tokens: None, | ||||||
| }, | ||||||
| kind: AssocItemKind::DelegationMac(Box::new(DelegationMac { | ||||||
| qself: None, | ||||||
| prefix: of_trait.trait_ref.path.clone(), | ||||||
| suffixes: None, | ||||||
| body, | ||||||
| })), | ||||||
| })); | ||||||
|
|
||||||
| Ok(impl_item) | ||||||
| } | ||||||
|
|
||||||
| fn parse_path_like_delegation(&mut self) -> PResult<'a, ItemKind> { | ||||||
| let (qself, path) = if self.eat_lt() { | ||||||
| let (qself, path) = self.parse_qpath(PathStyle::Expr)?; | ||||||
| (Some(qself), path) | ||||||
|
|
@@ -713,43 +799,35 @@ impl<'a> Parser<'a> { | |||||
| let rename = |this: &mut Self| { | ||||||
| Ok(if this.eat_keyword(exp!(As)) { Some(this.parse_ident()?) } else { None }) | ||||||
| }; | ||||||
| let body = |this: &mut Self| { | ||||||
| Ok(if this.check(exp!(OpenBrace)) { | ||||||
| Some(this.parse_block()?) | ||||||
| } else { | ||||||
| this.expect(exp!(Semi))?; | ||||||
| None | ||||||
| }) | ||||||
| }; | ||||||
|
|
||||||
| let item_kind = if self.eat_path_sep() { | ||||||
| if self.eat_path_sep() { | ||||||
| let suffixes = if self.eat(exp!(Star)) { | ||||||
| None | ||||||
| } else { | ||||||
| let parse_suffix = |p: &mut Self| Ok((p.parse_path_segment_ident()?, rename(p)?)); | ||||||
| Some(self.parse_delim_comma_seq(exp!(OpenBrace), exp!(CloseBrace), parse_suffix)?.0) | ||||||
| }; | ||||||
| let deleg = DelegationMac { qself, prefix: path, suffixes, body: body(self)? }; | ||||||
| ItemKind::DelegationMac(Box::new(deleg)) | ||||||
|
|
||||||
| Ok(ItemKind::DelegationMac(Box::new(DelegationMac { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| qself, | ||||||
| prefix: path, | ||||||
| suffixes, | ||||||
| body: self.parse_delegation_body()?, | ||||||
| }))) | ||||||
| } else { | ||||||
| let rename = rename(self)?; | ||||||
| let ident = rename.unwrap_or_else(|| path.segments.last().unwrap().ident); | ||||||
| let deleg = Delegation { | ||||||
|
|
||||||
| Ok(ItemKind::Delegation(Box::new(Delegation { | ||||||
| id: DUMMY_NODE_ID, | ||||||
| qself, | ||||||
| path, | ||||||
| ident, | ||||||
| rename, | ||||||
| body: body(self)?, | ||||||
| body: self.parse_delegation_body()?, | ||||||
| from_glob: false, | ||||||
| }; | ||||||
| ItemKind::Delegation(Box::new(deleg)) | ||||||
| }; | ||||||
|
|
||||||
| let span = span.to(self.prev_token.span); | ||||||
| self.psess.gated_spans.gate(sym::fn_delegation, span); | ||||||
|
|
||||||
| Ok(item_kind) | ||||||
| }))) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn parse_item_list<T>( | ||||||
|
|
@@ -2594,7 +2672,7 @@ impl<'a> Parser<'a> { | |||||
| Ok(body) | ||||||
| } | ||||||
|
|
||||||
| fn check_impl_frontmatter(&mut self) -> bool { | ||||||
| fn check_impl_frontmatter(&mut self, look_ahead: usize) -> bool { | ||||||
| const ALL_QUALS: &[Symbol] = &[kw::Const, kw::Unsafe]; | ||||||
| // In contrast to the loop below, this call inserts `impl` into the | ||||||
| // list of expected tokens shown in diagnostics. | ||||||
|
|
@@ -2603,7 +2681,7 @@ impl<'a> Parser<'a> { | |||||
| } | ||||||
| let mut i = 0; | ||||||
| while i < ALL_QUALS.len() { | ||||||
| let action = self.look_ahead(i, |token| { | ||||||
| let action = self.look_ahead(i + look_ahead, |token| { | ||||||
| if token.is_keyword(kw::Impl) { | ||||||
| return Some(true); | ||||||
| } | ||||||
|
|
@@ -2618,6 +2696,7 @@ impl<'a> Parser<'a> { | |||||
| } | ||||||
| i += 1; | ||||||
| } | ||||||
|
|
||||||
| self.is_keyword_ahead(i, &[kw::Impl]) | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #![allow(incomplete_features)] | ||
| #![feature(fn_delegation)] | ||
|
|
||
| mod unresolved { | ||
| struct S; | ||
| reuse impl unresolved for S { self.0 } | ||
| //~^ ERROR failed to resolve: use of unresolved module or unlinked crate `unresolved` | ||
| //~| ERROR cannot find trait `unresolved` in this scope | ||
|
|
||
| trait T {} | ||
| reuse impl T for unresolved { self.0 } | ||
| //~^ ERROR empty glob delegation is not supported | ||
| //~| ERROR cannot find type `unresolved` in this scope | ||
| } | ||
|
|
||
| mod wrong_entities { | ||
| trait T {} | ||
| struct Trait; | ||
| struct S; | ||
|
|
||
| reuse impl Trait for S { self.0 } | ||
| //~^ ERROR expected trait, found struct `Trait` | ||
| //~| ERROR expected trait, found struct `Trait` | ||
|
|
||
| mod TraitModule {} | ||
| reuse impl TraitModule for S { self.0 } | ||
| //~^ ERROR expected trait, found module `TraitModule` | ||
| //~| ERROR expected trait, found module `TraitModule` | ||
| } | ||
|
|
||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| error: empty glob delegation is not supported | ||
| --> $DIR/impl-reuse-bad-path.rs:11:5 | ||
| | | ||
| LL | reuse impl T for unresolved { self.0 } | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| error: expected trait, found struct `Trait` | ||
| --> $DIR/impl-reuse-bad-path.rs:21:16 | ||
| | | ||
| LL | reuse impl Trait for S { self.0 } | ||
| | ^^^^^ not a trait | ||
|
|
||
| error: expected trait, found module `TraitModule` | ||
| --> $DIR/impl-reuse-bad-path.rs:26:16 | ||
| | | ||
| LL | reuse impl TraitModule for S { self.0 } | ||
| | ^^^^^^^^^^^ not a trait | ||
|
|
||
| error[E0433]: failed to resolve: use of unresolved module or unlinked crate `unresolved` | ||
| --> $DIR/impl-reuse-bad-path.rs:6:16 | ||
| | | ||
| LL | reuse impl unresolved for S { self.0 } | ||
| | ^^^^^^^^^^ use of unresolved module or unlinked crate `unresolved` | ||
|
|
||
| error[E0405]: cannot find trait `unresolved` in this scope | ||
| --> $DIR/impl-reuse-bad-path.rs:6:16 | ||
| | | ||
| LL | reuse impl unresolved for S { self.0 } | ||
| | ^^^^^^^^^^ not found in this scope | ||
|
|
||
| error[E0425]: cannot find type `unresolved` in this scope | ||
| --> $DIR/impl-reuse-bad-path.rs:11:22 | ||
| | | ||
| LL | reuse impl T for unresolved { self.0 } | ||
| | ^^^^^^^^^^ not found in this scope | ||
|
|
||
| error[E0404]: expected trait, found struct `Trait` | ||
| --> $DIR/impl-reuse-bad-path.rs:21:16 | ||
| | | ||
| LL | reuse impl Trait for S { self.0 } | ||
| | ^^^^^ not a trait | ||
|
|
||
| error[E0404]: expected trait, found module `TraitModule` | ||
| --> $DIR/impl-reuse-bad-path.rs:26:16 | ||
| | | ||
| LL | reuse impl TraitModule for S { self.0 } | ||
| | ^^^^^^^^^^^ not a trait | ||
|
|
||
| error: aborting due to 8 previous errors | ||
|
|
||
| Some errors have detailed explanations: E0404, E0405, E0425, E0433. | ||
| For more information about an error, try `rustc --explain E0404`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #![allow(incomplete_features)] | ||
| #![feature(fn_delegation)] | ||
|
|
||
| mod empty_glob { | ||
| trait T {} | ||
|
|
||
| struct S; | ||
|
|
||
| reuse impl T for S { self.0 } | ||
| //~^ ERROR empty glob delegation is not supported | ||
| } | ||
|
|
||
|
|
||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| error: empty glob delegation is not supported | ||
| --> $DIR/impl-reuse-empty-glob.rs:9:5 | ||
| | | ||
| LL | reuse impl T for S { self.0 } | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, so you cannot do things like
impl my_trait!() for Type {}.This is good, it means the impl delegation desugaring won't result in duplicating macro calls, and the parsing-time treatment of the feature is not a very bad approximation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Items in the trait path will still get duplicated.
reuse impl Trait<{ struct S; 0 }> for Type {}->
, but that's probably ok because the glob delegation will duplicate the item too (?)
Could you maybe add an AST pretty-printing test for this case?