Skip to content

Commit c4bbe9c

Browse files
committed
Alias TraitItem & ImplItem.
Allow defaultness on trait items syntactically.
1 parent 3eebe05 commit c4bbe9c

File tree

13 files changed

+90
-27
lines changed

13 files changed

+90
-27
lines changed

src/librustc_parse/parser/item.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,9 +714,9 @@ impl<'a> Parser<'a> {
714714
id: DUMMY_NODE_ID,
715715
span: lo.to(self.prev_span),
716716
ident: name,
717+
attrs,
717718
vis,
718719
defaultness,
719-
attrs,
720720
generics,
721721
kind,
722722
tokens: None,
@@ -882,6 +882,7 @@ impl<'a> Parser<'a> {
882882
) -> PResult<'a, TraitItem> {
883883
let lo = self.token.span;
884884
let vis = self.parse_visibility(FollowedByType::No)?;
885+
let defaultness = self.parse_defaultness();
885886
let (name, kind, generics) = if self.eat_keyword(kw::Type) {
886887
self.parse_trait_item_assoc_ty()?
887888
} else if self.is_const_item() {
@@ -895,12 +896,13 @@ impl<'a> Parser<'a> {
895896

896897
Ok(TraitItem {
897898
id: DUMMY_NODE_ID,
899+
span: lo.to(self.prev_span),
898900
ident: name,
899901
attrs,
900902
vis,
903+
defaultness,
901904
generics,
902905
kind,
903-
span: lo.to(self.prev_span),
904906
tokens: None,
905907
})
906908
}

src/librustc_passes/ast_validation.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ impl<'a> AstValidator<'a> {
271271
forbid, and warn are the only allowed built-in attributes in function parameters")
272272
});
273273
}
274+
275+
fn check_defaultness(&self, span: Span, defaultness: Defaultness) {
276+
if let Defaultness::Default = defaultness {
277+
self.err_handler()
278+
.struct_span_err(span, "`default` is only allowed on items in `impl` definitions")
279+
.emit();
280+
}
281+
}
274282
}
275283

276284
enum GenericPosition {
@@ -746,6 +754,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
746754

747755
fn visit_trait_item(&mut self, ti: &'a TraitItem) {
748756
self.invalid_visibility(&ti.vis, None);
757+
self.check_defaultness(ti.span, ti.defaultness);
749758
visit::walk_trait_item(self, ti);
750759
}
751760
}

src/libsyntax/ast.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,24 +1603,12 @@ pub struct FnSig {
16031603
pub decl: P<FnDecl>,
16041604
}
16051605

1606-
/// Represents an item declaration within a trait declaration,
1606+
pub type TraitItem = ImplItem<TraitItemKind>;
1607+
1608+
/// Represents the kind of an item declaration within a trait declaration,
16071609
/// possibly including a default implementation. A trait item is
16081610
/// either required (meaning it doesn't have an implementation, just a
16091611
/// signature) or provided (meaning it has a default implementation).
1610-
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1611-
pub struct TraitItem {
1612-
pub attrs: Vec<Attribute>,
1613-
pub id: NodeId,
1614-
pub span: Span,
1615-
pub vis: Visibility,
1616-
pub ident: Ident,
1617-
1618-
pub generics: Generics,
1619-
pub kind: TraitItemKind,
1620-
/// See `Item::tokens` for what this is.
1621-
pub tokens: Option<TokenStream>,
1622-
}
1623-
16241612
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
16251613
pub enum TraitItemKind {
16261614
Const(P<Ty>, Option<P<Expr>>),
@@ -1631,7 +1619,7 @@ pub enum TraitItemKind {
16311619

16321620
/// Represents anything within an `impl` block.
16331621
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1634-
pub struct ImplItem {
1622+
pub struct ImplItem<K = ImplItemKind> {
16351623
pub attrs: Vec<Attribute>,
16361624
pub id: NodeId,
16371625
pub span: Span,
@@ -1640,7 +1628,7 @@ pub struct ImplItem {
16401628

16411629
pub defaultness: Defaultness,
16421630
pub generics: Generics,
1643-
pub kind: ImplItemKind,
1631+
pub kind: K,
16441632
/// See `Item::tokens` for what this is.
16451633
pub tokens: Option<TokenStream>,
16461634
}

src/libsyntax/mut_visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,8 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
939939
pub fn noop_flat_map_trait_item<T: MutVisitor>(mut item: TraitItem, visitor: &mut T)
940940
-> SmallVec<[TraitItem; 1]>
941941
{
942-
let TraitItem { id, ident, vis, attrs, generics, kind, span, tokens: _ } = &mut item;
942+
let TraitItem { id, ident, vis, defaultness: _, attrs, generics, kind, span, tokens: _ } =
943+
&mut item;
943944
visitor.visit_id(id);
944945
visitor.visit_ident(ident);
945946
visitor.visit_vis(vis);

src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,7 @@ impl<'a> State<'a> {
15501550
self.hardbreak_if_not_bol();
15511551
self.maybe_print_comment(ti.span.lo());
15521552
self.print_outer_attributes(&ti.attrs);
1553+
self.print_defaultness(ti.defaultness);
15531554
match ti.kind {
15541555
ast::TraitItemKind::Const(ref ty, ref default) => {
15551556
self.print_associated_const(

src/libsyntax_expand/placeholders.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId, vis: Option<ast::Visi
5353
AstFragmentKind::TraitItems => AstFragment::TraitItems(smallvec![ast::TraitItem {
5454
id, span, ident, vis, attrs, generics,
5555
kind: ast::TraitItemKind::Macro(mac_placeholder()),
56+
defaultness: ast::Defaultness::Final,
5657
tokens: None,
5758
}]),
5859
AstFragmentKind::ImplItems => AstFragment::ImplItems(smallvec![ast::ImplItem {

src/test/ui/issues/issue-60075.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: expected one of `.`, `;`, `?`, `else`, or an operator, found `}`
44
LL | });
55
| ^ expected one of `.`, `;`, `?`, `else`, or an operator
66

7-
error: expected one of `async`, `const`, `crate`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `;`
7+
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `;`
88
--> $DIR/issue-60075.rs:6:11
99
|
1010
LL | fn qux() -> Option<usize> {

src/test/ui/parser/issue-32446.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `async`, `const`, `crate`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `...`
1+
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `...`
22
--> $DIR/issue-32446.rs:4:11
33
|
44
LL | trait T { ... }
5-
| ^^^ expected one of 9 possible tokens
5+
| ^^^ expected one of 10 possible tokens
66

77
error: aborting due to previous error
88

src/test/ui/parser/macro/trait-non-item-macros.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `async`, `const`, `crate`, `extern`, `fn`, `pub`, `type`, or `unsafe`, found `2`
1+
error: expected one of `async`, `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, or `unsafe`, found `2`
22
--> $DIR/trait-non-item-macros.rs:2:19
33
|
44
LL | ($a:expr) => ($a)
5-
| ^^ expected one of 8 possible tokens
5+
| ^^ expected one of 9 possible tokens
66
...
77
LL | bah!(2);
88
| -------- in this macro invocation

src/test/ui/parser/mismatched-braces/missing-close-brace-in-trait.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | trait T {
77
LL | fn main() {}
88
| ^
99

10-
error: expected one of `async`, `const`, `extern`, `fn`, `type`, or `unsafe`, found keyword `struct`
10+
error: expected one of `async`, `const`, `default`, `extern`, `fn`, `type`, or `unsafe`, found keyword `struct`
1111
--> $DIR/missing-close-brace-in-trait.rs:5:12
1212
|
1313
LL | pub(crate) struct Bar<T>();
14-
| ^^^^^^ expected one of `async`, `const`, `extern`, `fn`, `type`, or `unsafe`
14+
| ^^^^^^ expected one of 7 possible tokens
1515

1616
error[E0601]: `main` function not found in crate `missing_close_brace_in_trait`
1717
--> $DIR/missing-close-brace-in-trait.rs:1:1

0 commit comments

Comments
 (0)