-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
829d1bf
fc3f279
591b2d2
d27d133
3f81911
1116ce4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,11 @@ impl<'a> Parser<'a> { | |
| } | ||
| } | ||
|
|
||
| enum ReuseKind { | ||
| Path, | ||
| OneLineTraitReuse, | ||
aerooneqq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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_with_diagnostics() { | ||
| // 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,28 @@ 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(&self) -> Option<ReuseKind> { | ||
| self.token | ||
| .is_keyword(kw::Reuse) | ||
| .then(|| { | ||
| const LOOK_AHEAD_DIST: usize = 1; | ||
|
|
||
| // no: `reuse ::path` for compatibility reasons with macro invocations | ||
| if self.look_ahead(LOOK_AHEAD_DIST, |t| t.is_path_start() && *t != token::PathSep) { | ||
aerooneqq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
aerooneqq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Some(ReuseKind::Path) | ||
| } else if self.look_ahead_check_impl_frontmatter(LOOK_AHEAD_DIST) { | ||
aerooneqq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Some(ReuseKind::OneLineTraitReuse) | ||
| } else { | ||
| None | ||
| } | ||
| }) | ||
| .flatten() | ||
| } | ||
|
|
||
| /// Are we sure this could not possibly be a macro invocation? | ||
|
|
@@ -560,6 +577,7 @@ impl<'a> Parser<'a> { | |
| &mut self, | ||
| attrs: &mut AttrVec, | ||
| defaultness: Defaultness, | ||
| is_one_line_trait_reuse: bool, | ||
aerooneqq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) -> PResult<'a, ItemKind> { | ||
| let mut constness = self.parse_constness(Case::Sensitive); | ||
| let safety = self.parse_safety(Case::Sensitive); | ||
|
|
@@ -628,7 +646,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_one_line_trait_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 +721,84 @@ 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::OneLineTraitReuse => { | ||
| self.parse_one_line_trait_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_one_line_trait_delegation( | ||
aerooneqq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| &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::OneLineReuseAllowedOnlyForTraitsImpls { | ||
|
||
| span: until_expr_span, | ||
| })); | ||
| }; | ||
|
|
||
| if matches!(of_trait.polarity, ImplPolarity::Negative(..)) { | ||
aerooneqq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return Err(self.dcx().create_err( | ||
| errors::OneLineReuseOfNegativeTraitImplsNotAllowed { 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(), | ||
|
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. Oh, so you cannot do things like
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. Items in the trait path will still get duplicated. impl Trait<{ struct S; 0 }> for Type {
reuse Trait<{ struct S; 0 }>::*;
}, but that's probably ok because the glob delegation will duplicate the item too (?) reuse Trait<{ struct S; 0 }>::foo;
reuse Trait<{ struct S; 0 }>::bar;
reuse Trait<{ struct S; 0 }>::baz;Could you maybe add an AST pretty-printing test for this case?
Contributor
Author
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. Added test for this case, maybe we can add tests from
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. UI is enough, IMO. |
||
| 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 +809,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 { | ||
aerooneqq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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,16 +2682,22 @@ impl<'a> Parser<'a> { | |
| Ok(body) | ||
| } | ||
|
|
||
| fn check_impl_frontmatter(&mut self) -> bool { | ||
| const ALL_QUALS: &[Symbol] = &[kw::Const, kw::Unsafe]; | ||
| // In contrast to the loop below, this call inserts `impl` into the | ||
| fn check_impl_frontmatter_with_diagnostics(&mut self) -> bool { | ||
| // In contrast to the method below, this call inserts `impl` into the | ||
| // list of expected tokens shown in diagnostics. | ||
| if self.check_keyword(exp!(Impl)) { | ||
| return true; | ||
| } | ||
|
|
||
| self.look_ahead_check_impl_frontmatter(0) | ||
| } | ||
|
|
||
| fn look_ahead_check_impl_frontmatter(&self, look_ahead_dist: usize) -> bool { | ||
| const ALL_QUALS: &[Symbol] = &[kw::Const, kw::Unsafe]; | ||
|
|
||
| let mut i = 0; | ||
| while i < ALL_QUALS.len() { | ||
| let action = self.look_ahead(i, |token| { | ||
| let action = self.look_ahead(i + look_ahead_dist, |token| { | ||
| if token.is_keyword(kw::Impl) { | ||
| return Some(true); | ||
| } | ||
|
|
@@ -2618,6 +2712,7 @@ impl<'a> Parser<'a> { | |
| } | ||
| i += 1; | ||
| } | ||
|
|
||
| self.is_keyword_ahead(i, &[kw::Impl]) | ||
| } | ||
|
|
||
|
|
||
| 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() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| error: empty glob delegation is not supported | ||
| --> $DIR/one-line-trait-reuse-bad-path.rs:11:5 | ||
| | | ||
| LL | reuse impl T for unresolved { self.0 } | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| error: expected trait, found struct `Trait` | ||
| --> $DIR/one-line-trait-reuse-bad-path.rs:21:16 | ||
| | | ||
| LL | reuse impl Trait for S { self.0 } | ||
| | ^^^^^ not a trait | ||
|
|
||
| error: expected trait, found module `TraitModule` | ||
| --> $DIR/one-line-trait-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/one-line-trait-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/one-line-trait-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/one-line-trait-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/one-line-trait-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/one-line-trait-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`. |
| 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() {} |
Uh oh!
There was an error while loading. Please reload this page.