From c4bbe9cbbe9921646cdedb856e34dc951641ed96 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 30 Nov 2019 18:25:44 +0100 Subject: Alias `TraitItem` & `ImplItem`. Allow defaultness on trait items syntactically. --- src/libsyntax/ast.rs | 22 +++++----------------- src/libsyntax/mut_visit.rs | 3 ++- src/libsyntax/print/pprust.rs | 1 + 3 files changed, 8 insertions(+), 18 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 92ba071a03d..964acfa92b9 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1603,24 +1603,12 @@ pub struct FnSig { pub decl: P, } -/// Represents an item declaration within a trait declaration, +pub type TraitItem = ImplItem; + +/// Represents the kind of an item declaration within a trait declaration, /// possibly including a default implementation. A trait item is /// either required (meaning it doesn't have an implementation, just a /// signature) or provided (meaning it has a default implementation). -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] -pub struct TraitItem { - pub attrs: Vec, - pub id: NodeId, - pub span: Span, - pub vis: Visibility, - pub ident: Ident, - - pub generics: Generics, - pub kind: TraitItemKind, - /// See `Item::tokens` for what this is. - pub tokens: Option, -} - #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub enum TraitItemKind { Const(P, Option>), @@ -1631,7 +1619,7 @@ pub enum TraitItemKind { /// Represents anything within an `impl` block. #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] -pub struct ImplItem { +pub struct ImplItem { pub attrs: Vec, pub id: NodeId, pub span: Span, @@ -1640,7 +1628,7 @@ pub struct ImplItem { pub defaultness: Defaultness, pub generics: Generics, - pub kind: ImplItemKind, + pub kind: K, /// See `Item::tokens` for what this is. pub tokens: Option, } diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index f8795d885d2..9d0a29f2951 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -939,7 +939,8 @@ pub fn noop_visit_item_kind(kind: &mut ItemKind, vis: &mut T) { pub fn noop_flat_map_trait_item(mut item: TraitItem, visitor: &mut T) -> SmallVec<[TraitItem; 1]> { - let TraitItem { id, ident, vis, attrs, generics, kind, span, tokens: _ } = &mut item; + let TraitItem { id, ident, vis, defaultness: _, attrs, generics, kind, span, tokens: _ } = + &mut item; visitor.visit_id(id); visitor.visit_ident(ident); visitor.visit_vis(vis); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index f0c5fb32fb1..2e3ea5e2444 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1550,6 +1550,7 @@ impl<'a> State<'a> { self.hardbreak_if_not_bol(); self.maybe_print_comment(ti.span.lo()); self.print_outer_attributes(&ti.attrs); + self.print_defaultness(ti.defaultness); match ti.kind { ast::TraitItemKind::Const(ref ty, ref default) => { self.print_associated_const( -- cgit 1.4.1-3-g733a5 From 73557faed23678fc443f3fa52727b5f200f597d2 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 1 Dec 2019 04:12:28 +0100 Subject: Use `Option` in `ImplItemKind::Const`. --- src/librustc/hir/lowering/item.rs | 8 ++++++- src/librustc_parse/parser/item.rs | 17 ++++++++------ src/librustc_passes/ast_validation.rs | 26 +++++++++++++++++----- src/librustc_save_analysis/dump_visitor.rs | 2 +- src/libsyntax/ast.rs | 4 ++-- src/libsyntax/mut_visit.rs | 2 +- src/libsyntax/print/pprust.rs | 2 +- src/libsyntax/visit.rs | 2 +- src/test/ui/parser/impl-item-const-pass.rs | 8 +++++++ .../ui/parser/impl-item-const-semantic-fail.rs | 7 ++++++ .../ui/parser/impl-item-const-semantic-fail.stderr | 8 +++++++ 11 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 src/test/ui/parser/impl-item-const-pass.rs create mode 100644 src/test/ui/parser/impl-item-const-semantic-fail.rs create mode 100644 src/test/ui/parser/impl-item-const-semantic-fail.stderr (limited to 'src/libsyntax') diff --git a/src/librustc/hir/lowering/item.rs b/src/librustc/hir/lowering/item.rs index ff9d8c85df8..32b36d2021b 100644 --- a/src/librustc/hir/lowering/item.rs +++ b/src/librustc/hir/lowering/item.rs @@ -899,7 +899,13 @@ impl LoweringContext<'_> { self.lower_generics(&i.generics, ImplTraitContext::disallowed()), hir::ImplItemKind::Const( self.lower_ty(ty, ImplTraitContext::disallowed()), - self.lower_const_body(expr), + match expr { + Some(expr) => self.lower_const_body(expr), + None => self.lower_body(|this| ( + hir_vec![], + this.expr(i.span, hir::ExprKind::Err, ThinVec::new()), + )), + } ), ), ImplItemKind::Method(ref sig, ref body) => { diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index c159fb66d50..68fdfd24d61 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -756,13 +756,16 @@ impl<'a> Parser<'a> { /// ImplItemConst = "const" Ident ":" Ty "=" Expr ";" fn parse_impl_const(&mut self) -> PResult<'a, (Ident, ImplItemKind, Generics)> { self.expect_keyword(kw::Const)?; - let name = self.parse_ident()?; + let ident = self.parse_ident()?; self.expect(&token::Colon)?; - let typ = self.parse_ty()?; - self.expect(&token::Eq)?; - let expr = self.parse_expr()?; + let ty = self.parse_ty()?; + let expr = if self.eat(&token::Eq) { + Some(self.parse_expr()?) + } else { + None + }; self.expect_semi()?; - Ok((name, ImplItemKind::Const(typ, expr), Generics::default())) + Ok((ident, ImplItemKind::Const(ty, expr), Generics::default())) } /// Parses `auto? trait Foo { ... }` or `trait Foo = Bar;`. @@ -912,13 +915,13 @@ impl<'a> Parser<'a> { let ident = self.parse_ident()?; self.expect(&token::Colon)?; let ty = self.parse_ty()?; - let default = if self.eat(&token::Eq) { + let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None }; self.expect_semi()?; - Ok((ident, TraitItemKind::Const(ty, default), Generics::default())) + Ok((ident, TraitItemKind::Const(ty, expr), Generics::default())) } /// Parses the following grammar: diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index a078a36db7a..ba0e6b100ee 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -259,12 +259,12 @@ impl<'a> AstValidator<'a> { !arr.contains(&attr.name_or_empty()) && attr::is_builtin_attr(attr) }) .for_each(|attr| if attr.is_doc_comment() { - let mut err = self.err_handler().struct_span_err( + self.err_handler().struct_span_err( attr.span, "documentation comments cannot be applied to function parameters" - ); - err.span_label(attr.span, "doc comments are not allowed here"); - err.emit(); + ) + .span_label(attr.span, "doc comments are not allowed here") + .emit(); } else { self.err_handler().span_err(attr.span, "allow, cfg, cfg_attr, deny, \ @@ -746,8 +746,22 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } fn visit_impl_item(&mut self, ii: &'a ImplItem) { - if let ImplItemKind::Method(ref sig, _) = ii.kind { - self.check_fn_decl(&sig.decl); + match &ii.kind { + ImplItemKind::Const(ty, None) => { + self.err_handler() + .struct_span_err(ii.span, "associated constant in `impl` without body") + .span_suggestion( + ii.span, + "provide a definition for the constant", + format!("const {}: {} = ;", ii.ident, pprust::ty_to_string(ty)), + Applicability::HasPlaceholders, + ) + .emit(); + } + ImplItemKind::Method(sig, _) => { + self.check_fn_decl(&sig.decl); + } + _ => {} } visit::walk_impl_item(self, ii); } diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index 396d9484339..97cbcb6401c 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -1110,7 +1110,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { impl_item.id, impl_item.ident, &ty, - Some(expr), + expr.as_deref(), impl_id, impl_item.vis.clone(), &impl_item.attrs, diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 964acfa92b9..17a57387da7 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1635,8 +1635,8 @@ pub struct ImplItem { /// Represents various kinds of content within an `impl`. #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] -pub enum ImplItemKind { - Const(P, P), +pub enum ImplItemKind { + Const(P, Option>), Method(FnSig, P), TyAlias(P), Macro(Mac), diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 9d0a29f2951..14701455013 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -981,7 +981,7 @@ pub fn noop_flat_map_impl_item(mut item: ImplItem, visitor: &mut match kind { ImplItemKind::Const(ty, expr) => { visitor.visit_ty(ty); - visitor.visit_expr(expr); + visit_opt(expr, |expr| visitor.visit_expr(expr)); } ImplItemKind::Method(sig, body) => { visit_fn_sig(sig, visitor); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 2e3ea5e2444..c1405e15819 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1599,7 +1599,7 @@ impl<'a> State<'a> { self.print_defaultness(ii.defaultness); match ii.kind { ast::ImplItemKind::Const(ref ty, ref expr) => { - self.print_associated_const(ii.ident, ty, Some(expr), &ii.vis); + self.print_associated_const(ii.ident, ty, expr.as_deref(), &ii.vis); } ast::ImplItemKind::Method(ref sig, ref body) => { self.head(""); diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 4ee09b4b87a..0b7a7d993aa 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -617,7 +617,7 @@ pub fn walk_impl_item<'a, V: Visitor<'a>>(visitor: &mut V, impl_item: &'a ImplIt match impl_item.kind { ImplItemKind::Const(ref ty, ref expr) => { visitor.visit_ty(ty); - visitor.visit_expr(expr); + walk_list!(visitor, visit_expr, expr); } ImplItemKind::Method(ref sig, ref body) => { visitor.visit_fn(FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis), body), diff --git a/src/test/ui/parser/impl-item-const-pass.rs b/src/test/ui/parser/impl-item-const-pass.rs new file mode 100644 index 00000000000..d1124561374 --- /dev/null +++ b/src/test/ui/parser/impl-item-const-pass.rs @@ -0,0 +1,8 @@ +// check-pass + +fn main() {} + +#[cfg(FALSE)] +impl X { + const Y: u8; +} diff --git a/src/test/ui/parser/impl-item-const-semantic-fail.rs b/src/test/ui/parser/impl-item-const-semantic-fail.rs new file mode 100644 index 00000000000..5d4692f9f14 --- /dev/null +++ b/src/test/ui/parser/impl-item-const-semantic-fail.rs @@ -0,0 +1,7 @@ +fn main() {} + +struct X; + +impl X { + const Y: u8; //~ ERROR associated constant in `impl` without body +} diff --git a/src/test/ui/parser/impl-item-const-semantic-fail.stderr b/src/test/ui/parser/impl-item-const-semantic-fail.stderr new file mode 100644 index 00000000000..31a15f8e80e --- /dev/null +++ b/src/test/ui/parser/impl-item-const-semantic-fail.stderr @@ -0,0 +1,8 @@ +error: associated constant in `impl` without body + --> $DIR/impl-item-const-semantic-fail.rs:6:5 + | +LL | const Y: u8; + | ^^^^^^^^^^^^ help: provide a definition for the constant: `const Y: u8 = ;` + +error: aborting due to previous error + -- cgit 1.4.1-3-g733a5 From f6403c6c766c704569564b9021071c4917d45a25 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 1 Dec 2019 07:24:07 +0100 Subject: Use `Option` in `ImplItemKind::Method`. --- src/librustc/hir/lowering.rs | 4 +- src/librustc/hir/lowering/item.rs | 71 +++++++++++++++------- src/librustc_parse/parser/item.rs | 12 ++-- src/librustc_passes/ast_validation.rs | 31 ++++++---- src/librustc_resolve/def_collector.rs | 10 +-- src/librustc_save_analysis/dump_visitor.rs | 2 +- src/libsyntax/ast.rs | 2 +- src/libsyntax/mut_visit.rs | 2 +- src/libsyntax/print/pprust.rs | 26 ++++---- src/libsyntax/visit.rs | 12 ++-- src/libsyntax_ext/deriving/generic/mod.rs | 2 +- src/test/ui/issues/issue-58856-1.rs | 2 + src/test/ui/issues/issue-58856-1.stderr | 17 +++++- .../ui/parser/impl-item-const-semantic-fail.stderr | 4 +- src/test/ui/parser/impl-item-fn-no-body-pass.rs | 8 +++ .../parser/impl-item-fn-no-body-semantic-fail.rs | 7 +++ .../impl-item-fn-no-body-semantic-fail.stderr | 10 +++ 17 files changed, 149 insertions(+), 73 deletions(-) create mode 100644 src/test/ui/parser/impl-item-fn-no-body-pass.rs create mode 100644 src/test/ui/parser/impl-item-fn-no-body-semantic-fail.rs create mode 100644 src/test/ui/parser/impl-item-fn-no-body-semantic-fail.stderr (limited to 'src/libsyntax') diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index e13f6cabb52..a82febba38a 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -1211,7 +1211,7 @@ impl<'a> LoweringContext<'a> { let ct = self.with_new_scopes(|this| { hir::AnonConst { hir_id: this.lower_node_id(node_id), - body: this.lower_const_body(&path_expr), + body: this.lower_const_body(path_expr.span, Some(&path_expr)), } }); return GenericArg::Const(ConstArg { @@ -3003,7 +3003,7 @@ impl<'a> LoweringContext<'a> { self.with_new_scopes(|this| { hir::AnonConst { hir_id: this.lower_node_id(c.id), - body: this.lower_const_body(&c.value), + body: this.lower_const_body(c.value.span, Some(&c.value)), } }) } diff --git a/src/librustc/hir/lowering/item.rs b/src/librustc/hir/lowering/item.rs index 32b36d2021b..7e231cd6b59 100644 --- a/src/librustc/hir/lowering/item.rs +++ b/src/librustc/hir/lowering/item.rs @@ -250,7 +250,7 @@ impl LoweringContext<'_> { return None; } - let kind = self.lower_item_kind(i.id, &mut ident, &attrs, &mut vis, &i.kind); + let kind = self.lower_item_kind(i.span, i.id, &mut ident, &attrs, &mut vis, &i.kind); Some(hir::Item { hir_id: self.lower_node_id(i.id), @@ -264,6 +264,7 @@ impl LoweringContext<'_> { fn lower_item_kind( &mut self, + span: Span, id: NodeId, ident: &mut Ident, attrs: &hir::HirVec, @@ -292,7 +293,7 @@ impl LoweringContext<'_> { } ), m, - self.lower_const_body(e), + self.lower_const_body(span, Some(e)), ) } ItemKind::Const(ref t, ref e) => { @@ -305,7 +306,7 @@ impl LoweringContext<'_> { ImplTraitContext::Disallowed(ImplTraitPosition::Binding) } ), - self.lower_const_body(e) + self.lower_const_body(span, Some(e)) ) } ItemKind::Fn(FnSig { ref decl, header }, ref generics, ref body) => { @@ -317,7 +318,12 @@ impl LoweringContext<'_> { // `impl Future` here because lower_body // only cares about the input argument patterns in the function // declaration (decl), not the return types. - let body_id = this.lower_maybe_async_body(&decl, header.asyncness.node, body); + let body_id = this.lower_maybe_async_body( + span, + &decl, + header.asyncness.node, + Some(body), + ); let (generics, decl) = this.add_in_band_defs( generics, @@ -817,7 +823,7 @@ impl LoweringContext<'_> { self.lower_ty(ty, ImplTraitContext::disallowed()), default .as_ref() - .map(|x| self.lower_const_body(x)), + .map(|x| self.lower_const_body(i.span, Some(x))), ), ), TraitItemKind::Method(ref sig, None) => { @@ -832,7 +838,7 @@ impl LoweringContext<'_> { (generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Required(names))) } TraitItemKind::Method(ref sig, Some(ref body)) => { - let body_id = self.lower_fn_body_block(&sig.decl, body); + let body_id = self.lower_fn_body_block(i.span, &sig.decl, Some(body)); let (generics, sig) = self.lower_method_sig( &i.generics, sig, @@ -891,6 +897,11 @@ impl LoweringContext<'_> { } } + /// Construct `ExprKind::Err` for the given `span`. + fn expr_err(&mut self, span: Span) -> hir::Expr { + self.expr(span, hir::ExprKind::Err, ThinVec::new()) + } + fn lower_impl_item(&mut self, i: &ImplItem) -> hir::ImplItem { let impl_item_def_id = self.resolver.definitions().local_def_id(i.id); @@ -899,19 +910,16 @@ impl LoweringContext<'_> { self.lower_generics(&i.generics, ImplTraitContext::disallowed()), hir::ImplItemKind::Const( self.lower_ty(ty, ImplTraitContext::disallowed()), - match expr { - Some(expr) => self.lower_const_body(expr), - None => self.lower_body(|this| ( - hir_vec![], - this.expr(i.span, hir::ExprKind::Err, ThinVec::new()), - )), - } + self.lower_const_body(i.span, expr.as_deref()), ), ), ImplItemKind::Method(ref sig, ref body) => { self.current_item = Some(i.span); let body_id = self.lower_maybe_async_body( - &sig.decl, sig.header.asyncness.node, body + i.span, + &sig.decl, + sig.header.asyncness.node, + body.as_deref(), ); let impl_trait_return_allow = !self.is_in_trait_impl; let (generics, sig) = self.lower_method_sig( @@ -1069,23 +1077,39 @@ impl LoweringContext<'_> { )) } - fn lower_fn_body_block(&mut self, decl: &FnDecl, body: &Block) -> hir::BodyId { - self.lower_fn_body(decl, |this| this.lower_block_expr(body)) + fn lower_fn_body_block( + &mut self, + span: Span, + decl: &FnDecl, + body: Option<&Block>, + ) -> hir::BodyId { + self.lower_fn_body(decl, |this| this.lower_block_expr_opt(span, body)) + } + + fn lower_block_expr_opt(&mut self, span: Span, block: Option<&Block>) -> hir::Expr { + match block { + Some(block) => self.lower_block_expr(block), + None => self.expr_err(span), + } } - pub(super) fn lower_const_body(&mut self, expr: &Expr) -> hir::BodyId { - self.lower_body(|this| (hir_vec![], this.lower_expr(expr))) + pub(super) fn lower_const_body(&mut self, span: Span, expr: Option<&Expr>) -> hir::BodyId { + self.lower_body(|this| (hir_vec![], match expr { + Some(expr) => this.lower_expr(expr), + None => this.expr_err(span), + })) } fn lower_maybe_async_body( &mut self, + span: Span, decl: &FnDecl, asyncness: IsAsync, - body: &Block, + body: Option<&Block>, ) -> hir::BodyId { let closure_id = match asyncness { IsAsync::Async { closure_id, .. } => closure_id, - IsAsync::NotAsync => return self.lower_fn_body_block(decl, body), + IsAsync::NotAsync => return self.lower_fn_body_block(span, decl, body), }; self.lower_body(|this| { @@ -1219,15 +1243,16 @@ impl LoweringContext<'_> { parameters.push(new_parameter); } + let body_span = body.map_or(span, |b| b.span); let async_expr = this.make_async_expr( CaptureBy::Value, closure_id, None, - body.span, + body_span, hir::AsyncGeneratorKind::Fn, |this| { // Create a block from the user's function body: - let user_body = this.lower_block_expr(body); + let user_body = this.lower_block_expr_opt(body_span, body); // Transform into `drop-temps { }`, an expression: let desugared_span = this.mark_span_with_reason( @@ -1257,7 +1282,7 @@ impl LoweringContext<'_> { ); this.expr_block(P(body), ThinVec::new()) }); - (HirVec::from(parameters), this.expr(body.span, async_expr, ThinVec::new())) + (HirVec::from(parameters), this.expr(body_span, async_expr, ThinVec::new())) }) } diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index 68fdfd24d61..053502c43dc 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -705,9 +705,7 @@ impl<'a> Parser<'a> { // FIXME: code copied from `parse_macro_use_or_failure` -- use abstraction! (Ident::invalid(), ast::ImplItemKind::Macro(mac), Generics::default()) } else { - let (name, inner_attrs, generics, kind) = self.parse_impl_method(at_end)?; - attrs.extend(inner_attrs); - (name, kind, generics) + self.parse_impl_method(at_end, &mut attrs)? }; Ok(ImplItem { @@ -1842,11 +1840,11 @@ impl<'a> Parser<'a> { fn parse_impl_method( &mut self, at_end: &mut bool, - ) -> PResult<'a, (Ident, Vec, Generics, ImplItemKind)> { + attrs: &mut Vec, + ) -> PResult<'a, (Ident, ImplItemKind, Generics)> { let (ident, sig, generics) = self.parse_method_sig(|_| true)?; - *at_end = true; - let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; - Ok((ident, inner_attrs, generics, ast::ImplItemKind::Method(sig, body))) + let body = self.parse_trait_method_body(at_end, attrs)?; + Ok((ident, ast::ImplItemKind::Method(sig, body), generics)) } fn parse_trait_item_method( diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index ba0e6b100ee..78866dc9cc9 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -279,6 +279,22 @@ impl<'a> AstValidator<'a> { .emit(); } } + + fn check_impl_item_provided(&self, sp: Span, body: &Option, ctx: &str, sugg: &str) { + if body.is_some() { + return; + } + + self.err_handler() + .struct_span_err(sp, &format!("associated {} in `impl` without body", ctx)) + .span_suggestion( + self.session.source_map().end_point(sp), + &format!("provide a definition for the {}", ctx), + sugg.to_string(), + Applicability::HasPlaceholders, + ) + .emit(); + } } enum GenericPosition { @@ -747,18 +763,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> { fn visit_impl_item(&mut self, ii: &'a ImplItem) { match &ii.kind { - ImplItemKind::Const(ty, None) => { - self.err_handler() - .struct_span_err(ii.span, "associated constant in `impl` without body") - .span_suggestion( - ii.span, - "provide a definition for the constant", - format!("const {}: {} = ;", ii.ident, pprust::ty_to_string(ty)), - Applicability::HasPlaceholders, - ) - .emit(); + ImplItemKind::Const(_, body) => { + self.check_impl_item_provided(ii.span, body, "constant", " = ;"); } - ImplItemKind::Method(sig, _) => { + ImplItemKind::Method(sig, body) => { + self.check_impl_item_provided(ii.span, body, "function", " { }"); self.check_fn_decl(&sig.decl); } _ => {} diff --git a/src/librustc_resolve/def_collector.rs b/src/librustc_resolve/def_collector.rs index dd6b1d2119e..76a52bb7f7f 100644 --- a/src/librustc_resolve/def_collector.rs +++ b/src/librustc_resolve/def_collector.rs @@ -50,7 +50,7 @@ impl<'a> DefCollector<'a> { header: &FnHeader, generics: &'a Generics, decl: &'a FnDecl, - body: &'a Block, + body: Option<&'a Block>, ) { let (closure_id, return_impl_trait_id) = match header.asyncness.node { IsAsync::Async { @@ -74,7 +74,9 @@ impl<'a> DefCollector<'a> { closure_id, DefPathData::ClosureExpr, span, ); this.with_parent(closure_def, |this| { - visit::walk_block(this, body); + if let Some(body) = body { + visit::walk_block(this, body); + } }) }) } @@ -123,7 +125,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { &sig.header, generics, &sig.decl, - body, + Some(body), ) } ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) => @@ -237,7 +239,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { header, &ii.generics, decl, - body, + body.as_deref(), ) } ImplItemKind::Method(..) | diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index 97cbcb6401c..99f9c3b1f2e 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -1119,7 +1119,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { ast::ImplItemKind::Method(ref sig, ref body) => { self.process_method( sig, - Some(body), + body.as_deref(), impl_item.id, impl_item.ident, &impl_item.generics, diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 17a57387da7..f6af5d8637e 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1637,7 +1637,7 @@ pub struct ImplItem { #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub enum ImplItemKind { Const(P, Option>), - Method(FnSig, P), + Method(FnSig, Option>), TyAlias(P), Macro(Mac), } diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 14701455013..0fa4dcf3ad9 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -985,7 +985,7 @@ pub fn noop_flat_map_impl_item(mut item: ImplItem, visitor: &mut } ImplItemKind::Method(sig, body) => { visit_fn_sig(sig, visitor); - visitor.visit_block(body); + visit_opt(body, |body| visitor.visit_block(body)); } ImplItemKind::TyAlias(ty) => visitor.visit_ty(ty), ImplItemKind::Macro(mac) => visitor.visit_mac(mac), diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index c1405e15819..34097841b4a 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1553,23 +1553,13 @@ impl<'a> State<'a> { self.print_defaultness(ti.defaultness); match ti.kind { ast::TraitItemKind::Const(ref ty, ref default) => { - self.print_associated_const( - ti.ident, - ty, - default.as_ref().map(|expr| &**expr), - &source_map::respan(ti.span.shrink_to_lo(), ast::VisibilityKind::Inherited), - ); + self.print_associated_const(ti.ident, ty, default.as_deref(), &ti.vis); } ast::TraitItemKind::Method(ref sig, ref body) => { if body.is_some() { self.head(""); } - self.print_method_sig( - ti.ident, - &ti.generics, - sig, - &source_map::respan(ti.span.shrink_to_lo(), ast::VisibilityKind::Inherited), - ); + self.print_method_sig(ti.ident, &ti.generics, sig, &ti.vis); if let Some(ref body) = *body { self.nbsp(); self.print_block_with_attrs(body, &ti.attrs); @@ -1602,10 +1592,16 @@ impl<'a> State<'a> { self.print_associated_const(ii.ident, ty, expr.as_deref(), &ii.vis); } ast::ImplItemKind::Method(ref sig, ref body) => { - self.head(""); + if body.is_some() { + self.head(""); + } self.print_method_sig(ii.ident, &ii.generics, sig, &ii.vis); - self.nbsp(); - self.print_block_with_attrs(body, &ii.attrs); + if let Some(body) = body { + self.nbsp(); + self.print_block_with_attrs(body, &ii.attrs); + } else { + self.s.word(";"); + } } ast::ImplItemKind::TyAlias(ref ty) => { self.print_associated_type(ii.ident, None, Some(ty)); diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 0b7a7d993aa..bdf70ec46f7 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -25,7 +25,7 @@ pub enum FnKind<'a> { ItemFn(Ident, &'a FnHeader, &'a Visibility, &'a Block), /// E.g., `fn foo(&self)`. - Method(Ident, &'a FnSig, Option<&'a Visibility>, &'a Block), + Method(Ident, &'a FnSig, &'a Visibility, &'a Block), /// E.g., `|x, y| body`. Closure(&'a Expr), @@ -596,7 +596,7 @@ pub fn walk_trait_item<'a, V: Visitor<'a>>(visitor: &mut V, trait_item: &'a Trai walk_fn_decl(visitor, &sig.decl); } TraitItemKind::Method(ref sig, Some(ref body)) => { - visitor.visit_fn(FnKind::Method(trait_item.ident, sig, None, body), + visitor.visit_fn(FnKind::Method(trait_item.ident, sig, &trait_item.vis, body), &sig.decl, trait_item.span, trait_item.id); } TraitItemKind::Type(ref bounds, ref default) => { @@ -619,8 +619,12 @@ pub fn walk_impl_item<'a, V: Visitor<'a>>(visitor: &mut V, impl_item: &'a ImplIt visitor.visit_ty(ty); walk_list!(visitor, visit_expr, expr); } - ImplItemKind::Method(ref sig, ref body) => { - visitor.visit_fn(FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis), body), + ImplItemKind::Method(ref sig, None) => { + visitor.visit_fn_header(&sig.header); + walk_fn_decl(visitor, &sig.decl); + } + ImplItemKind::Method(ref sig, Some(ref body)) => { + visitor.visit_fn(FnKind::Method(impl_item.ident, sig, &impl_item.vis, body), &sig.decl, impl_item.span, impl_item.id); } ImplItemKind::TyAlias(ref ty) => { diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index 5bd84b43a78..e8c4f993d4f 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -956,7 +956,7 @@ impl<'a> MethodDef<'a> { vis: respan(trait_lo_sp, ast::VisibilityKind::Inherited), defaultness: ast::Defaultness::Final, ident: method_ident, - kind: ast::ImplItemKind::Method(sig, body_block), + kind: ast::ImplItemKind::Method(sig, Some(body_block)), tokens: None, } } diff --git a/src/test/ui/issues/issue-58856-1.rs b/src/test/ui/issues/issue-58856-1.rs index db3984cd189..8b1a39a94e6 100644 --- a/src/test/ui/issues/issue-58856-1.rs +++ b/src/test/ui/issues/issue-58856-1.rs @@ -1,6 +1,8 @@ impl A { + //~^ ERROR cannot find type `A` in this scope fn b(self> //~^ ERROR expected one of `)`, `,`, or `:`, found `>` + //~| ERROR expected `;` or `{`, found `>` } fn main() {} diff --git a/src/test/ui/issues/issue-58856-1.stderr b/src/test/ui/issues/issue-58856-1.stderr index 58ab0a142d6..0ea6b017548 100644 --- a/src/test/ui/issues/issue-58856-1.stderr +++ b/src/test/ui/issues/issue-58856-1.stderr @@ -1,10 +1,23 @@ error: expected one of `)`, `,`, or `:`, found `>` - --> $DIR/issue-58856-1.rs:2:14 + --> $DIR/issue-58856-1.rs:3:14 | LL | fn b(self> | - ^ help: `)` may belong here | | | unclosed delimiter -error: aborting due to previous error +error: expected `;` or `{`, found `>` + --> $DIR/issue-58856-1.rs:3:14 + | +LL | fn b(self> + | ^ expected `;` or `{` + +error[E0412]: cannot find type `A` in this scope + --> $DIR/issue-58856-1.rs:1:6 + | +LL | impl A { + | ^ not found in this scope + +error: aborting due to 3 previous errors +For more information about this error, try `rustc --explain E0412`. diff --git a/src/test/ui/parser/impl-item-const-semantic-fail.stderr b/src/test/ui/parser/impl-item-const-semantic-fail.stderr index 31a15f8e80e..ec3bee0ce68 100644 --- a/src/test/ui/parser/impl-item-const-semantic-fail.stderr +++ b/src/test/ui/parser/impl-item-const-semantic-fail.stderr @@ -2,7 +2,9 @@ error: associated constant in `impl` without body --> $DIR/impl-item-const-semantic-fail.rs:6:5 | LL | const Y: u8; - | ^^^^^^^^^^^^ help: provide a definition for the constant: `const Y: u8 = ;` + | ^^^^^^^^^^^- + | | + | help: provide a definition for the constant: `= ;` error: aborting due to previous error diff --git a/src/test/ui/parser/impl-item-fn-no-body-pass.rs b/src/test/ui/parser/impl-item-fn-no-body-pass.rs new file mode 100644 index 00000000000..16b09d64e8c --- /dev/null +++ b/src/test/ui/parser/impl-item-fn-no-body-pass.rs @@ -0,0 +1,8 @@ +// check-pass + +fn main() {} + +#[cfg(FALSE)] +impl X { + fn f(); +} diff --git a/src/test/ui/parser/impl-item-fn-no-body-semantic-fail.rs b/src/test/ui/parser/impl-item-fn-no-body-semantic-fail.rs new file mode 100644 index 00000000000..cb183db5964 --- /dev/null +++ b/src/test/ui/parser/impl-item-fn-no-body-semantic-fail.rs @@ -0,0 +1,7 @@ +fn main() {} + +struct X; + +impl X { + fn f(); //~ ERROR associated function in `impl` without body +} diff --git a/src/test/ui/parser/impl-item-fn-no-body-semantic-fail.stderr b/src/test/ui/parser/impl-item-fn-no-body-semantic-fail.stderr new file mode 100644 index 00000000000..1acb727368b --- /dev/null +++ b/src/test/ui/parser/impl-item-fn-no-body-semantic-fail.stderr @@ -0,0 +1,10 @@ +error: associated function in `impl` without body + --> $DIR/impl-item-fn-no-body-semantic-fail.rs:6:5 + | +LL | fn f(); + | ^^^^^^- + | | + | help: provide a definition for the function: `{ }` + +error: aborting due to previous error + -- cgit 1.4.1-3-g733a5 From c02fd3130284921f7077f78271b5501b402ec469 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 1 Dec 2019 08:41:35 +0100 Subject: `TraitItemKind::Type` -> `TraitItemKind::TyAlias`. --- src/librustc/hir/lowering/item.rs | 4 ++-- src/librustc_parse/parser/item.rs | 2 +- src/librustc_resolve/build_reduced_graph.rs | 2 +- src/librustc_resolve/def_collector.rs | 2 +- src/librustc_resolve/late.rs | 4 ++-- src/librustc_save_analysis/dump_visitor.rs | 2 +- src/libsyntax/ast.rs | 2 +- src/libsyntax/feature_gate/check.rs | 2 +- src/libsyntax/mut_visit.rs | 2 +- src/libsyntax/print/pprust.rs | 2 +- src/libsyntax/visit.rs | 2 +- 11 files changed, 13 insertions(+), 13 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/lowering/item.rs b/src/librustc/hir/lowering/item.rs index 7e231cd6b59..ec78bcf1403 100644 --- a/src/librustc/hir/lowering/item.rs +++ b/src/librustc/hir/lowering/item.rs @@ -848,7 +848,7 @@ impl LoweringContext<'_> { ); (generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Provided(body_id))) } - TraitItemKind::Type(ref bounds, ref default) => { + TraitItemKind::TyAlias(ref bounds, ref default) => { let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed()); let kind = hir::TraitItemKind::Type( self.lower_param_bounds(bounds, ImplTraitContext::disallowed()), @@ -877,7 +877,7 @@ impl LoweringContext<'_> { TraitItemKind::Const(_, ref default) => { (hir::AssocItemKind::Const, default.is_some()) } - TraitItemKind::Type(_, ref default) => { + TraitItemKind::TyAlias(_, ref default) => { (hir::AssocItemKind::Type, default.is_some()) } TraitItemKind::Method(ref sig, ref default) => ( diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index 053502c43dc..5bfecf78e71 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -944,7 +944,7 @@ impl<'a> Parser<'a> { }; self.expect_semi()?; - Ok((ident, TraitItemKind::Type(bounds, default), generics)) + Ok((ident, TraitItemKind::TyAlias(bounds, default), generics)) } /// Parses a `UseTree`. diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index e2578d67e73..d2d5a33ec7a 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -1182,7 +1182,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> { } (Res::Def(DefKind::Method, item_def_id), ValueNS) } - TraitItemKind::Type(..) => (Res::Def(DefKind::AssocTy, item_def_id), TypeNS), + TraitItemKind::TyAlias(..) => (Res::Def(DefKind::AssocTy, item_def_id), TypeNS), TraitItemKind::Macro(_) => bug!(), // handled above }; diff --git a/src/librustc_resolve/def_collector.rs b/src/librustc_resolve/def_collector.rs index 76a52bb7f7f..471e2634b8a 100644 --- a/src/librustc_resolve/def_collector.rs +++ b/src/librustc_resolve/def_collector.rs @@ -216,7 +216,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { let def_data = match ti.kind { TraitItemKind::Method(..) | TraitItemKind::Const(..) => DefPathData::ValueNs(ti.ident.name), - TraitItemKind::Type(..) => { + TraitItemKind::TyAlias(..) => { DefPathData::TypeNs(ti.ident.name) }, TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id), diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index 4f95d6fe70f..d32a6a4b3e6 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -821,7 +821,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { TraitItemKind::Method(_, _) => { visit::walk_trait_item(this, trait_item) } - TraitItemKind::Type(..) => { + TraitItemKind::TyAlias(..) => { visit::walk_trait_item(this, trait_item) } TraitItemKind::Macro(_) => { @@ -995,7 +995,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { let trait_assoc_types = replace( &mut self.diagnostic_metadata.current_trait_assoc_types, trait_items.iter().filter_map(|item| match &item.kind { - TraitItemKind::Type(bounds, _) if bounds.len() == 0 => Some(item.ident), + TraitItemKind::TyAlias(bounds, _) if bounds.len() == 0 => Some(item.ident), _ => None, }).collect(), ); diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index 99f9c3b1f2e..bd7851296dd 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -1056,7 +1056,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { trait_item.span, ); } - ast::TraitItemKind::Type(ref bounds, ref default_ty) => { + ast::TraitItemKind::TyAlias(ref bounds, ref default_ty) => { // FIXME do something with _bounds (for type refs) let name = trait_item.ident.name.to_string(); let qualname = format!("::{}", diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index f6af5d8637e..4e2f78e8ab8 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1613,7 +1613,7 @@ pub type TraitItem = ImplItem; pub enum TraitItemKind { Const(P, Option>), Method(FnSig, Option>), - Type(GenericBounds, Option>), + TyAlias(GenericBounds, Option>), Macro(Mac), } diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index 3d2c3b1d4f9..10f6bbb5949 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -585,7 +585,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { gate_feature_post!(&self, const_fn, ti.span, "const fn is unstable"); } } - ast::TraitItemKind::Type(_, ref default) => { + ast::TraitItemKind::TyAlias(_, ref default) => { if let Some(ty) = default { self.check_impl_trait(ty); gate_feature_post!(&self, associated_type_defaults, ti.span, diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 0fa4dcf3ad9..66cac0f917d 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -955,7 +955,7 @@ pub fn noop_flat_map_trait_item(mut item: TraitItem, visitor: &mu visit_fn_sig(sig, visitor); visit_opt(body, |body| visitor.visit_block(body)); } - TraitItemKind::Type(bounds, default) => { + TraitItemKind::TyAlias(bounds, default) => { visit_bounds(bounds, visitor); visit_opt(default, |default| visitor.visit_ty(default)); } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 34097841b4a..00dcd7e8d0b 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1567,7 +1567,7 @@ impl<'a> State<'a> { self.s.word(";"); } } - ast::TraitItemKind::Type(ref bounds, ref default) => { + ast::TraitItemKind::TyAlias(ref bounds, ref default) => { self.print_associated_type(ti.ident, Some(bounds), default.as_ref().map(|ty| &**ty)); } diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index bdf70ec46f7..f96290ec4f8 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -599,7 +599,7 @@ pub fn walk_trait_item<'a, V: Visitor<'a>>(visitor: &mut V, trait_item: &'a Trai visitor.visit_fn(FnKind::Method(trait_item.ident, sig, &trait_item.vis, body), &sig.decl, trait_item.span, trait_item.id); } - TraitItemKind::Type(ref bounds, ref default) => { + TraitItemKind::TyAlias(ref bounds, ref default) => { walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_ty, default); } -- cgit 1.4.1-3-g733a5 From 39073767a483d10f8b4b2ac2f32bc9573d9dabbf Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 1 Dec 2019 10:25:45 +0100 Subject: Unify `{Trait,Impl}ItemKind::TyAlias` structures. --- src/librustc/hir/lowering.rs | 37 +++++------ src/librustc/hir/lowering/item.rs | 24 ++++--- src/librustc_parse/parser/item.rs | 30 ++++++++- src/librustc_passes/ast_validation.rs | 15 +++++ src/librustc_passes/lib.rs | 1 + src/librustc_resolve/late.rs | 3 +- src/librustc_save_analysis/dump_visitor.rs | 3 +- src/libsyntax/ast.rs | 2 +- src/libsyntax/feature_gate/check.rs | 6 +- src/libsyntax/mut_visit.rs | 5 +- src/libsyntax/print/pprust.rs | 22 +++---- src/libsyntax/visit.rs | 5 +- src/libsyntax_ext/deriving/generic/mod.rs | 4 +- src/test/ui/parser/impl-item-type-no-body-pass.rs | 11 ++++ .../parser/impl-item-type-no-body-semantic-fail.rs | 22 +++++++ .../impl-item-type-no-body-semantic-fail.stderr | 73 ++++++++++++++++++++++ 16 files changed, 209 insertions(+), 54 deletions(-) create mode 100644 src/test/ui/parser/impl-item-type-no-body-pass.rs create mode 100644 src/test/ui/parser/impl-item-type-no-body-semantic-fail.rs create mode 100644 src/test/ui/parser/impl-item-type-no-body-semantic-fail.stderr (limited to 'src/libsyntax') diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index a82febba38a..54ff1f56eec 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -1253,6 +1253,14 @@ impl<'a> LoweringContext<'a> { ty } + fn ty(&mut self, span: Span, kind: hir::TyKind) -> hir::Ty { + hir::Ty { hir_id: self.next_id(), kind, span } + } + + fn ty_tup(&mut self, span: Span, tys: HirVec) -> hir::Ty { + self.ty(span, hir::TyKind::Tup(tys)) + } + fn lower_ty_direct(&mut self, t: &Ty, mut itctx: ImplTraitContext<'_>) -> hir::Ty { let kind = match t.kind { TyKind::Infer => hir::TyKind::Infer, @@ -2084,12 +2092,9 @@ impl<'a> LoweringContext<'a> { .iter() .map(|ty| this.lower_ty_direct(ty, ImplTraitContext::disallowed())) .collect(); - let mk_tup = |this: &mut Self, tys, span| { - hir::Ty { kind: hir::TyKind::Tup(tys), hir_id: this.next_id(), span } - }; ( hir::GenericArgs { - args: hir_vec![GenericArg::Type(mk_tup(this, inputs, span))], + args: hir_vec![GenericArg::Type(this.ty_tup(span, inputs))], bindings: hir_vec![ hir::TypeBinding { hir_id: this.next_id(), @@ -2102,7 +2107,7 @@ impl<'a> LoweringContext<'a> { ImplTraitContext::disallowed() )) .unwrap_or_else(|| - P(mk_tup(this, hir::HirVec::new(), span)) + P(this.ty_tup(span, hir::HirVec::new())) ), }, span: output.as_ref().map_or(span, |ty| ty.span), @@ -2474,17 +2479,13 @@ impl<'a> LoweringContext<'a> { }) ); - // Create the `Foo<...>` refernece itself. Note that the `type + // Create the `Foo<...>` reference itself. Note that the `type // Foo = impl Trait` is, internally, created as a child of the // async fn, so the *type parameters* are inherited. It's // only the lifetime parameters that we must supply. let opaque_ty_ref = hir::TyKind::Def(hir::ItemId { id: opaque_ty_id }, generic_args.into()); - - hir::FunctionRetTy::Return(P(hir::Ty { - kind: opaque_ty_ref, - span: opaque_ty_span, - hir_id: self.next_id(), - })) + let opaque_ty = self.ty(opaque_ty_span, opaque_ty_ref); + hir::FunctionRetTy::Return(P(opaque_ty)) } /// Transforms `-> T` into `Future` @@ -2496,16 +2497,8 @@ impl<'a> LoweringContext<'a> { ) -> hir::GenericBound { // Compute the `T` in `Future` from the return type. let output_ty = match output { - FunctionRetTy::Ty(ty) => { - self.lower_ty(ty, ImplTraitContext::OpaqueTy(Some(fn_def_id))) - } - FunctionRetTy::Default(ret_ty_span) => { - P(hir::Ty { - hir_id: self.next_id(), - kind: hir::TyKind::Tup(hir_vec![]), - span: *ret_ty_span, - }) - } + FunctionRetTy::Ty(ty) => self.lower_ty(ty, ImplTraitContext::OpaqueTy(Some(fn_def_id))), + FunctionRetTy::Default(ret_ty_span) => P(self.ty_tup(*ret_ty_span, hir_vec![])), }; // "" diff --git a/src/librustc/hir/lowering/item.rs b/src/librustc/hir/lowering/item.rs index ec78bcf1403..f77523e6382 100644 --- a/src/librustc/hir/lowering/item.rs +++ b/src/librustc/hir/lowering/item.rs @@ -932,16 +932,21 @@ impl LoweringContext<'_> { (generics, hir::ImplItemKind::Method(sig, body_id)) } - ImplItemKind::TyAlias(ref ty) => { + ImplItemKind::TyAlias(_, ref ty) => { let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed()); - let kind = match ty.kind.opaque_top_hack() { + let kind = match ty { None => { - let ty = self.lower_ty(ty, ImplTraitContext::disallowed()); - hir::ImplItemKind::TyAlias(ty) + hir::ImplItemKind::TyAlias(P(self.ty(i.span, hir::TyKind::Err))) } - Some(bs) => { - let bounds = self.lower_param_bounds(bs, ImplTraitContext::disallowed()); - hir::ImplItemKind::OpaqueTy(bounds) + Some(ty) => match ty.kind.opaque_top_hack() { + None => { + let ty = self.lower_ty(ty, ImplTraitContext::disallowed()); + hir::ImplItemKind::TyAlias(ty) + } + Some(bs) => { + let bs = self.lower_param_bounds(bs, ImplTraitContext::disallowed()); + hir::ImplItemKind::OpaqueTy(bs) + } } }; (generics, kind) @@ -972,7 +977,10 @@ impl LoweringContext<'_> { defaultness: self.lower_defaultness(i.defaultness, true /* [1] */), kind: match &i.kind { ImplItemKind::Const(..) => hir::AssocItemKind::Const, - ImplItemKind::TyAlias(ty) => match ty.kind.opaque_top_hack() { + ImplItemKind::TyAlias(_, ty) => match ty + .as_deref() + .and_then(|ty| ty.kind.opaque_top_hack()) + { None => hir::AssocItemKind::Type, Some(_) => hir::AssocItemKind::OpaqueTy, }, diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index 5bfecf78e71..302fcba4cf8 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -697,8 +697,7 @@ impl<'a> Parser<'a> { let vis = self.parse_visibility(FollowedByType::No)?; let defaultness = self.parse_defaultness(); let (name, kind, generics) = if self.eat_keyword(kw::Type) { - let (name, ty, generics) = self.parse_type_alias()?; - (name, ast::ImplItemKind::TyAlias(ty), generics) + self.parse_impl_assoc_ty()? } else if self.is_const_item() { self.parse_impl_const()? } else if let Some(mac) = self.parse_assoc_macro_invoc("impl", Some(&vis), at_end)? { @@ -766,6 +765,31 @@ impl<'a> Parser<'a> { Ok((ident, ImplItemKind::Const(ty, expr), Generics::default())) } + /// Parses the following grammar: + /// + /// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty] + fn parse_impl_assoc_ty(&mut self) -> PResult<'a, (Ident, ImplItemKind, Generics)> { + let ident = self.parse_ident()?; + let mut generics = self.parse_generics()?; + + // Parse optional colon and param bounds. + let bounds = if self.eat(&token::Colon) { + self.parse_generic_bounds(None)? + } else { + Vec::new() + }; + generics.where_clause = self.parse_where_clause()?; + + let default = if self.eat(&token::Eq) { + Some(self.parse_ty()?) + } else { + None + }; + self.expect_semi()?; + + Ok((ident, ImplItemKind::TyAlias(bounds, default), generics)) + } + /// Parses `auto? trait Foo { ... }` or `trait Foo = Bar;`. fn parse_item_trait(&mut self, lo: Span, unsafety: Unsafety) -> PResult<'a, ItemInfo> { // Parse optional `auto` prefix. @@ -924,7 +948,7 @@ impl<'a> Parser<'a> { /// Parses the following grammar: /// - /// TraitItemAssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty] + /// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty] fn parse_trait_item_assoc_ty(&mut self) -> PResult<'a, (Ident, TraitItemKind, Generics)> { let ident = self.parse_ident()?; let mut generics = self.parse_generics()?; diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index 78866dc9cc9..f4b28077e9f 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -295,6 +295,17 @@ impl<'a> AstValidator<'a> { ) .emit(); } + + fn check_impl_assoc_type_no_bounds(&self, bounds: &[GenericBound]) { + let span = match bounds { + [] => return, + [b0] => b0.span(), + [b0, .., bl] => b0.span().to(bl.span()), + }; + self.err_handler() + .struct_span_err(span, "bounds on associated `type`s in `impl`s have no effect") + .emit(); + } } enum GenericPosition { @@ -770,6 +781,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.check_impl_item_provided(ii.span, body, "function", " { }"); self.check_fn_decl(&sig.decl); } + ImplItemKind::TyAlias(bounds, body) => { + self.check_impl_item_provided(ii.span, body, "type", " = ;"); + self.check_impl_assoc_type_no_bounds(bounds); + } _ => {} } visit::walk_impl_item(self, ii); diff --git a/src/librustc_passes/lib.rs b/src/librustc_passes/lib.rs index 81f06a14d95..f01867f32c6 100644 --- a/src/librustc_passes/lib.rs +++ b/src/librustc_passes/lib.rs @@ -8,6 +8,7 @@ #![feature(in_band_lifetimes)] #![feature(nll)] +#![feature(slice_patterns)] #![recursion_limit="256"] diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index d32a6a4b3e6..33e24c8cfd4 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -1119,7 +1119,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { visit::walk_impl_item(this, impl_item); } - ImplItemKind::TyAlias(ref ty) => { + ImplItemKind::TyAlias(_, Some(ref ty)) => { // If this is a trait impl, ensure the type // exists in trait this.check_trait_item(impl_item.ident, @@ -1129,6 +1129,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { this.visit_ty(ty); } + ImplItemKind::TyAlias(_, None) => {} ImplItemKind::Macro(_) => panic!("unexpanded macro in resolve!"), } diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index bd7851296dd..d63a9df8d9b 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -1127,7 +1127,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { impl_item.span, ); } - ast::ImplItemKind::TyAlias(ref ty) => { + ast::ImplItemKind::TyAlias(_, None) => {} + ast::ImplItemKind::TyAlias(_, Some(ref ty)) => { // FIXME: uses of the assoc type should ideally point to this // 'def' and the name here should be a ref to the def in the // trait. diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 4e2f78e8ab8..89868a9cd29 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1638,7 +1638,7 @@ pub struct ImplItem { pub enum ImplItemKind { Const(P, Option>), Method(FnSig, Option>), - TyAlias(P), + TyAlias(GenericBounds, Option>), Macro(Mac), } diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index 10f6bbb5949..f786de6401a 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -612,8 +612,10 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { "C-variadic functions are unstable"); } } - ast::ImplItemKind::TyAlias(ref ty) => { - self.check_impl_trait(ty); + ast::ImplItemKind::TyAlias(_, ref ty) => { + if let Some(ty) = ty { + self.check_impl_trait(ty); + } self.check_gat(&ii.generics, ii.span); } _ => {} diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 66cac0f917d..bb0462c19cd 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -987,7 +987,10 @@ pub fn noop_flat_map_impl_item(mut item: ImplItem, visitor: &mut visit_fn_sig(sig, visitor); visit_opt(body, |body| visitor.visit_block(body)); } - ImplItemKind::TyAlias(ty) => visitor.visit_ty(ty), + ImplItemKind::TyAlias(bounds, ty) => { + visit_bounds(bounds, visitor); + visit_opt(ty, |ty| visitor.visit_ty(ty)); + } ImplItemKind::Macro(mac) => visitor.visit_mac(mac), } visitor.visit_span(span); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 00dcd7e8d0b..03e394b8c7e 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1128,16 +1128,15 @@ impl<'a> State<'a> { self.s.word(";") } - fn print_associated_type(&mut self, - ident: ast::Ident, - bounds: Option<&ast::GenericBounds>, - ty: Option<&ast::Ty>) - { + fn print_associated_type( + &mut self, + ident: ast::Ident, + bounds: &ast::GenericBounds, + ty: Option<&ast::Ty>, + ) { self.word_space("type"); self.print_ident(ident); - if let Some(bounds) = bounds { - self.print_type_bounds(":", bounds); - } + self.print_type_bounds(":", bounds); if let Some(ty) = ty { self.s.space(); self.word_space("="); @@ -1568,8 +1567,7 @@ impl<'a> State<'a> { } } ast::TraitItemKind::TyAlias(ref bounds, ref default) => { - self.print_associated_type(ti.ident, Some(bounds), - default.as_ref().map(|ty| &**ty)); + self.print_associated_type(ti.ident, bounds, default.as_deref()); } ast::TraitItemKind::Macro(ref mac) => { self.print_mac(mac); @@ -1603,8 +1601,8 @@ impl<'a> State<'a> { self.s.word(";"); } } - ast::ImplItemKind::TyAlias(ref ty) => { - self.print_associated_type(ii.ident, None, Some(ty)); + ast::ImplItemKind::TyAlias(ref bounds, ref ty) => { + self.print_associated_type(ii.ident, bounds, ty.as_deref()); } ast::ImplItemKind::Macro(ref mac) => { self.print_mac(mac); diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index f96290ec4f8..7cc1a769e52 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -627,8 +627,9 @@ pub fn walk_impl_item<'a, V: Visitor<'a>>(visitor: &mut V, impl_item: &'a ImplIt visitor.visit_fn(FnKind::Method(impl_item.ident, sig, &impl_item.vis, body), &sig.decl, impl_item.span, impl_item.id); } - ImplItemKind::TyAlias(ref ty) => { - visitor.visit_ty(ty); + ImplItemKind::TyAlias(ref bounds, ref ty) => { + walk_list!(visitor, visit_param_bound, bounds); + walk_list!(visitor, visit_ty, ty); } ImplItemKind::Macro(ref mac) => { visitor.visit_mac(mac); diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index e8c4f993d4f..d51fcf315a6 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -519,7 +519,9 @@ impl<'a> TraitDef<'a> { attrs: Vec::new(), generics: Generics::default(), kind: ast::ImplItemKind::TyAlias( - type_def.to_ty(cx, self.span, type_ident, generics)), + Vec::new(), + Some(type_def.to_ty(cx, self.span, type_ident, generics)), + ), tokens: None, } }); diff --git a/src/test/ui/parser/impl-item-type-no-body-pass.rs b/src/test/ui/parser/impl-item-type-no-body-pass.rs new file mode 100644 index 00000000000..74a9c6ab7e8 --- /dev/null +++ b/src/test/ui/parser/impl-item-type-no-body-pass.rs @@ -0,0 +1,11 @@ +// check-pass + +fn main() {} + +#[cfg(FALSE)] +impl X { + type Y; + type Z: Ord; + type W: Ord where Self: Eq; + type W where Self: Eq; +} diff --git a/src/test/ui/parser/impl-item-type-no-body-semantic-fail.rs b/src/test/ui/parser/impl-item-type-no-body-semantic-fail.rs new file mode 100644 index 00000000000..71c7d4ba21d --- /dev/null +++ b/src/test/ui/parser/impl-item-type-no-body-semantic-fail.rs @@ -0,0 +1,22 @@ +#![feature(generic_associated_types)] +//~^ WARN the feature `generic_associated_types` is incomplete + +fn main() {} + +struct X; + +impl X { + type Y; + //~^ ERROR associated type in `impl` without body + //~| ERROR associated types are not yet supported in inherent impls + type Z: Ord; + //~^ ERROR associated type in `impl` without body + //~| ERROR bounds on associated `type`s in `impl`s have no effect + //~| ERROR associated types are not yet supported in inherent impls + type W: Ord where Self: Eq; + //~^ ERROR associated type in `impl` without body + //~| ERROR bounds on associated `type`s in `impl`s have no effect + //~| ERROR associated types are not yet supported in inherent impls + type W where Self: Eq; + //~^ ERROR associated type in `impl` without body +} diff --git a/src/test/ui/parser/impl-item-type-no-body-semantic-fail.stderr b/src/test/ui/parser/impl-item-type-no-body-semantic-fail.stderr new file mode 100644 index 00000000000..6f1439c8f0b --- /dev/null +++ b/src/test/ui/parser/impl-item-type-no-body-semantic-fail.stderr @@ -0,0 +1,73 @@ +error: associated type in `impl` without body + --> $DIR/impl-item-type-no-body-semantic-fail.rs:9:5 + | +LL | type Y; + | ^^^^^^- + | | + | help: provide a definition for the type: `= ;` + +error: associated type in `impl` without body + --> $DIR/impl-item-type-no-body-semantic-fail.rs:12:5 + | +LL | type Z: Ord; + | ^^^^^^^^^^^- + | | + | help: provide a definition for the type: `= ;` + +error: bounds on associated `type`s in `impl`s have no effect + --> $DIR/impl-item-type-no-body-semantic-fail.rs:12:13 + | +LL | type Z: Ord; + | ^^^ + +error: associated type in `impl` without body + --> $DIR/impl-item-type-no-body-semantic-fail.rs:16:5 + | +LL | type W: Ord where Self: Eq; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^- + | | + | help: provide a definition for the type: `= ;` + +error: bounds on associated `type`s in `impl`s have no effect + --> $DIR/impl-item-type-no-body-semantic-fail.rs:16:13 + | +LL | type W: Ord where Self: Eq; + | ^^^ + +error: associated type in `impl` without body + --> $DIR/impl-item-type-no-body-semantic-fail.rs:20:5 + | +LL | type W where Self: Eq; + | ^^^^^^^^^^^^^^^^^^^^^- + | | + | help: provide a definition for the type: `= ;` + +warning: the feature `generic_associated_types` is incomplete and may cause the compiler to crash + --> $DIR/impl-item-type-no-body-semantic-fail.rs:1:12 + | +LL | #![feature(generic_associated_types)] + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + +error[E0202]: associated types are not yet supported in inherent impls (see #8995) + --> $DIR/impl-item-type-no-body-semantic-fail.rs:9:5 + | +LL | type Y; + | ^^^^^^^ + +error[E0202]: associated types are not yet supported in inherent impls (see #8995) + --> $DIR/impl-item-type-no-body-semantic-fail.rs:12:5 + | +LL | type Z: Ord; + | ^^^^^^^^^^^^ + +error[E0202]: associated types are not yet supported in inherent impls (see #8995) + --> $DIR/impl-item-type-no-body-semantic-fail.rs:16:5 + | +LL | type W: Ord where Self: Eq; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 9 previous errors + +For more information about this error, try `rustc --explain E0202`. -- cgit 1.4.1-3-g733a5 From 92a372b0204a5f18286ff94648b98e4c0d6d27d2 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 1 Dec 2019 10:46:06 +0100 Subject: Unify `{Impl,Trait}Item` as `AssocItem`. --- src/libsyntax/ast.rs | 41 ++++++++++++++++++++++++----------------- src/libsyntax/attr/mod.rs | 2 +- 2 files changed, 25 insertions(+), 18 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 89868a9cd29..dd8a7fa8665 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1603,23 +1603,16 @@ pub struct FnSig { pub decl: P, } -pub type TraitItem = ImplItem; +// FIXME(Centril): Remove all of these. +pub type TraitItem = AssocItem; +pub type TraitItemKind = AssocItemKind; +pub type ImplItem = AssocItem; +pub type ImplItemKind = AssocItemKind; -/// Represents the kind of an item declaration within a trait declaration, -/// possibly including a default implementation. A trait item is -/// either required (meaning it doesn't have an implementation, just a -/// signature) or provided (meaning it has a default implementation). +/// Represents associated items. +/// These include items in `impl` and `trait` definitions. #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] -pub enum TraitItemKind { - Const(P, Option>), - Method(FnSig, Option>), - TyAlias(GenericBounds, Option>), - Macro(Mac), -} - -/// Represents anything within an `impl` block. -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] -pub struct ImplItem { +pub struct AssocItem { pub attrs: Vec, pub id: NodeId, pub span: Span, @@ -1634,11 +1627,25 @@ pub struct ImplItem { } /// Represents various kinds of content within an `impl`. -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] -pub enum ImplItemKind { +/// +/// The term "provided" in the variants below refers to the item having a default +/// definition / body. Meanwhile, a "required" item lacks a definition / body. +/// In an implementation, all items must be provided. +/// The `Option`s below denote the bodies, where `Some(_)` +/// means "provided" and conversely `None` means "required". +#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] +pub enum AssocItemKind { + /// An associated constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`. + /// If `def` is parsed, then the associated constant is provided, and otherwise required. Const(P, Option>), + + /// An associated function. Method(FnSig, Option>), + + /// An associated type. TyAlias(GenericBounds, Option>), + + /// A macro expanding to an associated item. Macro(Mac), } diff --git a/src/libsyntax/attr/mod.rs b/src/libsyntax/attr/mod.rs index 079a0f6fafa..13a9ed5f215 100644 --- a/src/libsyntax/attr/mod.rs +++ b/src/libsyntax/attr/mod.rs @@ -749,6 +749,6 @@ macro_rules! derive_has_attrs { } derive_has_attrs! { - Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::ImplItem, ast::TraitItem, ast::Arm, + Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::AssocItem, ast::Arm, ast::Field, ast::FieldPat, ast::Variant, ast::Param } -- cgit 1.4.1-3-g733a5 From 7672bff3780ef0e7ba5313bf23644465644e19e6 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 1 Dec 2019 11:07:49 +0100 Subject: Unify associated function parsing. --- src/librustc_parse/parser/item.rs | 65 ++++++++++++++------------------------- src/libsyntax/ast.rs | 1 + 2 files changed, 24 insertions(+), 42 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index 3fc4cafc0da..32cc156783c 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -6,7 +6,7 @@ use crate::maybe_whole; use rustc_errors::{PResult, Applicability, DiagnosticBuilder, StashKey}; use rustc_error_codes::*; use syntax::ast::{self, DUMMY_NODE_ID, Ident, Attribute, AttrKind, AttrStyle, AnonConst, Item}; -use syntax::ast::{ItemKind, ImplItem, ImplItemKind, TraitItem, TraitItemKind, UseTree, UseTreeKind}; +use syntax::ast::{ItemKind, ImplItem, TraitItem, TraitItemKind, UseTree, UseTreeKind}; use syntax::ast::{AssocItemKind}; use syntax::ast::{PathSegment, IsAuto, Constness, IsAsync, Unsafety, Defaultness, Extern, StrLit}; use syntax::ast::{Visibility, VisibilityKind, Mutability, FnHeader, ForeignItem, ForeignItemKind}; @@ -705,7 +705,7 @@ impl<'a> Parser<'a> { // FIXME: code copied from `parse_macro_use_or_failure` -- use abstraction! (Ident::invalid(), ast::ImplItemKind::Macro(mac), Generics::default()) } else { - self.parse_impl_method(at_end, &mut attrs)? + self.parse_assoc_fn(at_end, &mut attrs, |_| true)? }; Ok(ImplItem { @@ -876,7 +876,11 @@ impl<'a> Parser<'a> { // trait item macro. (Ident::invalid(), TraitItemKind::Macro(mac), Generics::default()) } else { - self.parse_trait_item_method(at_end, &mut attrs)? + // This is somewhat dubious; We don't want to allow + // param names to be left off if there is a definition... + // + // We don't allow param names to be left off in edition 2018. + self.parse_assoc_fn(at_end, &mut attrs, |t| t.span.rust_2018())? }; Ok(TraitItem { @@ -1823,48 +1827,40 @@ impl<'a> Parser<'a> { }) } - /// Parses a method or a macro invocation in a trait impl. - fn parse_impl_method( - &mut self, - at_end: &mut bool, - attrs: &mut Vec, - ) -> PResult<'a, (Ident, ImplItemKind, Generics)> { - let (ident, sig, generics) = self.parse_method_sig(|_| true)?; - let body = self.parse_trait_method_body(at_end, attrs)?; - Ok((ident, ast::ImplItemKind::Method(sig, body), generics)) - } - - fn parse_trait_item_method( + fn parse_assoc_fn( &mut self, at_end: &mut bool, attrs: &mut Vec, - ) -> PResult<'a, (Ident, TraitItemKind, Generics)> { - // This is somewhat dubious; We don't want to allow - // argument names to be left off if there is a definition... - // - // We don't allow argument names to be left off in edition 2018. - let (ident, sig, generics) = self.parse_method_sig(|t| t.span.rust_2018())?; - let body = self.parse_trait_method_body(at_end, attrs)?; - Ok((ident, TraitItemKind::Method(sig, body), generics)) + is_name_required: fn(&token::Token) -> bool, + ) -> PResult<'a, (Ident, AssocItemKind, Generics)> { + let header = self.parse_fn_front_matter()?; + let (ident, decl, generics) = self.parse_fn_sig(ParamCfg { + is_self_allowed: true, + allow_c_variadic: false, + is_name_required, + })?; + let sig = FnSig { header, decl }; + let body = self.parse_assoc_fn_body(at_end, attrs)?; + Ok((ident, AssocItemKind::Method(sig, body), generics)) } - /// Parse the "body" of a method in a trait item definition. + /// Parse the "body" of a method in an associated item definition. /// This can either be `;` when there's no body, /// or e.g. a block when the method is a provided one. - fn parse_trait_method_body( + fn parse_assoc_fn_body( &mut self, at_end: &mut bool, attrs: &mut Vec, ) -> PResult<'a, Option>> { Ok(match self.token.kind { token::Semi => { - debug!("parse_trait_method_body(): parsing required method"); + debug!("parse_assoc_fn_body(): parsing required method"); self.bump(); *at_end = true; None } token::OpenDelim(token::Brace) => { - debug!("parse_trait_method_body(): parsing provided method"); + debug!("parse_assoc_fn_body(): parsing provided method"); *at_end = true; let (inner_attrs, body) = self.parse_inner_attrs_and_block()?; attrs.extend(inner_attrs.iter().cloned()); @@ -1885,21 +1881,6 @@ impl<'a> Parser<'a> { }) } - /// Parse the "signature", including the identifier, parameters, and generics - /// of a method. The body is not parsed as that differs between `trait`s and `impl`s. - fn parse_method_sig( - &mut self, - is_name_required: fn(&token::Token) -> bool, - ) -> PResult<'a, (Ident, FnSig, Generics)> { - let header = self.parse_fn_front_matter()?; - let (ident, decl, generics) = self.parse_fn_sig(ParamCfg { - is_self_allowed: true, - allow_c_variadic: false, - is_name_required, - })?; - Ok((ident, FnSig { header, decl }, generics)) - } - /// Parses all the "front matter" for a `fn` declaration, up to /// and including the `fn` keyword: /// diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index dd8a7fa8665..5866f9db078 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1640,6 +1640,7 @@ pub enum AssocItemKind { Const(P, Option>), /// An associated function. + /// FIXME(Centril): Rename to `Fn`. Method(FnSig, Option>), /// An associated type. -- cgit 1.4.1-3-g733a5 From 34d91709b672d91ea9623ae4bc2275e8b003fc2c Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 1 Dec 2019 16:00:08 +0100 Subject: parse: refactor fun ret ty & param ty --- src/librustc/hir/lowering.rs | 34 +++++--------- src/librustc_interface/util.rs | 8 ++-- src/librustc_parse/parser/expr.rs | 2 +- src/librustc_parse/parser/item.rs | 6 +-- src/librustc_parse/parser/path.rs | 6 +-- src/librustc_parse/parser/ty.rs | 26 +++++++++-- src/librustc_passes/ast_validation.rs | 4 +- src/librustc_save_analysis/dump_visitor.rs | 9 ++-- src/libsyntax/ast.rs | 4 +- src/libsyntax/mut_visit.rs | 8 +++- src/libsyntax/print/pprust.rs | 75 ++++++++---------------------- src/libsyntax/visit.rs | 2 +- 12 files changed, 77 insertions(+), 107 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 54ff1f56eec..e2c99f456e9 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -2092,29 +2092,19 @@ impl<'a> LoweringContext<'a> { .iter() .map(|ty| this.lower_ty_direct(ty, ImplTraitContext::disallowed())) .collect(); + let output_ty = match output { + FunctionRetTy::Ty(ty) => this.lower_ty(&ty, ImplTraitContext::disallowed()), + FunctionRetTy::Default(_) => P(this.ty_tup(span, hir::HirVec::new())), + }; + let args = hir_vec![GenericArg::Type(this.ty_tup(span, inputs))]; + let binding = hir::TypeBinding { + hir_id: this.next_id(), + ident: Ident::with_dummy_span(FN_OUTPUT_NAME), + span: output_ty.span, + kind: hir::TypeBindingKind::Equality { ty: output_ty }, + }; ( - hir::GenericArgs { - args: hir_vec![GenericArg::Type(this.ty_tup(span, inputs))], - bindings: hir_vec![ - hir::TypeBinding { - hir_id: this.next_id(), - ident: Ident::with_dummy_span(FN_OUTPUT_NAME), - kind: hir::TypeBindingKind::Equality { - ty: output - .as_ref() - .map(|ty| this.lower_ty( - &ty, - ImplTraitContext::disallowed() - )) - .unwrap_or_else(|| - P(this.ty_tup(span, hir::HirVec::new())) - ), - }, - span: output.as_ref().map_or(span, |ty| ty.span), - } - ], - parenthesized: true, - }, + hir::GenericArgs { args, bindings: hir_vec![binding], parenthesized: true }, false, ) } diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index 8c225b83f40..ca7c4ba8786 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -712,8 +712,8 @@ impl<'a, 'b> ReplaceBodyWithLoop<'a, 'b> { ret } - fn should_ignore_fn(ret_ty: &ast::FnDecl) -> bool { - if let ast::FunctionRetTy::Ty(ref ty) = ret_ty.output { + fn should_ignore_fn(ret_ty: &ast::FunctionRetTy) -> bool { + if let ast::FunctionRetTy::Ty(ref ty) = ret_ty { fn involves_impl_trait(ty: &ast::Ty) -> bool { match ty.kind { ast::TyKind::ImplTrait(..) => true, @@ -742,7 +742,7 @@ impl<'a, 'b> ReplaceBodyWithLoop<'a, 'b> { }, Some(&ast::GenericArgs::Parenthesized(ref data)) => { any_involves_impl_trait(data.inputs.iter()) || - any_involves_impl_trait(data.output.iter()) + ReplaceBodyWithLoop::should_ignore_fn(&data.output) } } }), @@ -762,7 +762,7 @@ impl<'a, 'b> ReplaceBodyWithLoop<'a, 'b> { fn is_sig_const(sig: &ast::FnSig) -> bool { sig.header.constness.node == ast::Constness::Const || - ReplaceBodyWithLoop::should_ignore_fn(&sig.decl) + ReplaceBodyWithLoop::should_ignore_fn(&sig.decl.output) } } diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index 3cd4988ce0b..e4dff07e92c 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -1381,7 +1381,7 @@ impl<'a> Parser<'a> { args } }; - let output = self.parse_ret_ty(true)?; + let output = self.parse_ret_ty(true, true)?; Ok(P(FnDecl { inputs: inputs_captures, diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index dbc0163eef6..f391eda976c 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -1900,7 +1900,7 @@ impl<'a> Parser<'a> { ) -> PResult<'a, P> { Ok(P(FnDecl { inputs: self.parse_fn_params(cfg)?, - output: self.parse_ret_ty(ret_allow_plus)?, + output: self.parse_ret_ty(ret_allow_plus, true)?, })) } @@ -2002,12 +2002,12 @@ impl<'a> Parser<'a> { } self.eat_incorrect_doc_comment_for_param_type(); - (pat, self.parse_ty_common(true, true, cfg.allow_c_variadic)?) + (pat, self.parse_ty_for_param(cfg.allow_c_variadic)?) } else { debug!("parse_param_general ident_to_pat"); let parser_snapshot_before_ty = self.clone(); self.eat_incorrect_doc_comment_for_param_type(); - let mut ty = self.parse_ty_common(true, true, cfg.allow_c_variadic); + let mut ty = self.parse_ty_for_param(cfg.allow_c_variadic); if ty.is_ok() && self.token != token::Comma && self.token != token::CloseDelim(token::Paren) { // This wasn't actually a type, but a pattern looking like a type, diff --git a/src/librustc_parse/parser/path.rs b/src/librustc_parse/parser/path.rs index 5334fc485e7..aeba6dd2f67 100644 --- a/src/librustc_parse/parser/path.rs +++ b/src/librustc_parse/parser/path.rs @@ -182,11 +182,7 @@ impl<'a> Parser<'a> { // `(T, U) -> R` let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?; let span = ident.span.to(self.prev_span); - let output = if self.eat(&token::RArrow) { - Some(self.parse_ty_common(false, false, false)?) - } else { - None - }; + let output = self.parse_ret_ty(false, false)?; ParenthesizedArgs { inputs, output, span }.into() }; diff --git a/src/librustc_parse/parser/ty.rs b/src/librustc_parse/parser/ty.rs index 33d7c878e88..1dffa6c94a8 100644 --- a/src/librustc_parse/parser/ty.rs +++ b/src/librustc_parse/parser/ty.rs @@ -30,6 +30,13 @@ impl<'a> Parser<'a> { self.parse_ty_common(true, true, false) } + /// Parse a type suitable for a function or function pointer parameter. + /// The difference from `parse_ty` is that this version allows `...` + /// (`CVarArgs`) at the top level of the the type. + pub(super) fn parse_ty_for_param(&mut self, allow_c_variadic: bool) -> PResult<'a, P> { + self.parse_ty_common(true, true, allow_c_variadic) + } + /// Parses a type in restricted contexts where `+` is not permitted. /// /// Example 1: `&'a TYPE` @@ -41,17 +48,26 @@ impl<'a> Parser<'a> { } /// Parses an optional return type `[ -> TY ]` in a function declaration. - pub(super) fn parse_ret_ty(&mut self, allow_plus: bool) -> PResult<'a, FunctionRetTy> { + pub(super) fn parse_ret_ty( + &mut self, + allow_plus: bool, + allow_qpath_recovery: bool, + ) -> PResult<'a, FunctionRetTy> { Ok(if self.eat(&token::RArrow) { // FIXME(Centril): Can we unconditionally `allow_plus`? - FunctionRetTy::Ty(self.parse_ty_common(allow_plus, true, false)?) + FunctionRetTy::Ty(self.parse_ty_common(allow_plus, allow_qpath_recovery, false)?) } else { FunctionRetTy::Default(self.token.span.shrink_to_lo()) }) } - pub(super) fn parse_ty_common(&mut self, allow_plus: bool, allow_qpath_recovery: bool, - allow_c_variadic: bool) -> PResult<'a, P> { + fn parse_ty_common( + &mut self, + allow_plus: bool, + allow_qpath_recovery: bool, + // Is `...` (`CVarArgs`) legal in the immediate top level call? + allow_c_variadic: bool, + ) -> PResult<'a, P> { maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery); maybe_whole!(self, NtTy, |x| x); @@ -198,6 +214,8 @@ impl<'a> Parser<'a> { self.eat(&token::DotDotDot); TyKind::CVarArgs } else { + // FIXME(Centril): Should we just allow `...` syntactically + // anywhere in a type and use semantic restrictions instead? return Err(struct_span_fatal!( self.sess.span_diagnostic, self.token.span, diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index f4b28077e9f..8be97155d8c 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -674,10 +674,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } GenericArgs::Parenthesized(ref data) => { walk_list!(self, visit_ty, &data.inputs); - if let Some(ref type_) = data.output { + if let FunctionRetTy::Ty(ty) = &data.output { // `-> Foo` syntax is essentially an associated type binding, // so it is also allowed to contain nested `impl Trait`. - self.with_impl_trait(None, |this| this.visit_ty(type_)); + self.with_impl_trait(None, |this| this.visit_ty(ty)); } } } diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index d63a9df8d9b..cc0f3c512f5 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -811,9 +811,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { match **generic_args { ast::GenericArgs::AngleBracketed(ref data) => { for arg in &data.args { - match arg { - ast::GenericArg::Type(ty) => self.visit_ty(ty), - _ => {} + if let ast::GenericArg::Type(ty) = arg { + self.visit_ty(ty); } } } @@ -821,8 +820,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { for t in &data.inputs { self.visit_ty(t); } - if let Some(ref t) = data.output { - self.visit_ty(t); + if let ast::FunctionRetTy::Ty(ty) = &data.output { + self.visit_ty(ty); } } } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 5866f9db078..d90d74d7a26 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -254,7 +254,7 @@ pub struct ParenthesizedArgs { pub inputs: Vec>, /// `C` - pub output: Option>, + pub output: FunctionRetTy, } impl ParenthesizedArgs { @@ -2185,7 +2185,7 @@ impl fmt::Debug for ImplPolarity { } #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] -pub enum FunctionRetTy { +pub enum FunctionRetTy { // FIXME(Centril): Rename to `FnRetTy` and in HIR also. /// Returns type is not specified. /// /// Functions default to `()` and closures default to inference. diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index bb0462c19cd..97a85b0fe7e 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -553,7 +553,7 @@ pub fn noop_visit_parenthesized_parameter_data(args: &mut Parenth vis: &mut T) { let ParenthesizedArgs { inputs, output, span } = args; visit_vec(inputs, |input| vis.visit_ty(input)); - visit_opt(output, |output| vis.visit_ty(output)); + noop_visit_fn_ret_ty(output, vis); vis.visit_span(span); } @@ -742,7 +742,11 @@ pub fn noop_visit_asyncness(asyncness: &mut IsAsync, vis: &mut T) pub fn noop_visit_fn_decl(decl: &mut P, vis: &mut T) { let FnDecl { inputs, output } = decl.deref_mut(); inputs.flat_map_in_place(|param| vis.flat_map_param(param)); - match output { + noop_visit_fn_ret_ty(output, vis); +} + +pub fn noop_visit_fn_ret_ty(fn_ret_ty: &mut FunctionRetTy, vis: &mut T) { + match fn_ret_ty { FunctionRetTy::Default(span) => vis.visit_span(span), FunctionRetTy::Ty(ty) => vis.visit_ty(ty), } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 03e394b8c7e..a141d4d71bb 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -887,17 +887,9 @@ impl<'a> PrintState<'a> for State<'a> { ast::GenericArgs::Parenthesized(ref data) => { self.s.word("("); - self.commasep( - Inconsistent, - &data.inputs, - |s, ty| s.print_type(ty)); + self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(ty)); self.s.word(")"); - - if let Some(ref ty) = data.output { - self.space_if_not_bol(); - self.word_space("->"); - self.print_type(ty); - } + self.print_fn_ret_ty(&data.output); } } } @@ -1579,6 +1571,7 @@ impl<'a> State<'a> { self.ann.post(self, AnnNode::SubItem(ti.id)) } + // FIXME(Centril): merge with function above. crate fn print_impl_item(&mut self, ii: &ast::ImplItem) { self.ann.pre(self, AnnNode::SubItem(ii.id)); self.hardbreak_if_not_bol(); @@ -2104,7 +2097,7 @@ impl<'a> State<'a> { self.print_asyncness(asyncness); self.print_capture_clause(capture_clause); - self.print_fn_block_params(decl); + self.print_fn_params_and_ret(decl, true); self.s.space(); self.print_expr(body); self.end(); // need to close a box @@ -2535,36 +2528,16 @@ impl<'a> State<'a> { self.print_ident(name); } self.print_generic_params(&generics.params); - self.print_fn_params_and_ret(decl); + self.print_fn_params_and_ret(decl, false); self.print_where_clause(&generics.where_clause) } - crate fn print_fn_params_and_ret(&mut self, decl: &ast::FnDecl) { - self.popen(); - self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, false)); - self.pclose(); - - self.print_fn_output(decl) - } - - crate fn print_fn_block_params(&mut self, decl: &ast::FnDecl) { - self.s.word("|"); - self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, true)); - self.s.word("|"); - - if let ast::FunctionRetTy::Default(..) = decl.output { - return; - } - - self.space_if_not_bol(); - self.word_space("->"); - match decl.output { - ast::FunctionRetTy::Ty(ref ty) => { - self.print_type(ty); - self.maybe_print_comment(ty.span.lo()) - } - ast::FunctionRetTy::Default(..) => unreachable!(), - } + crate fn print_fn_params_and_ret(&mut self, decl: &ast::FnDecl, is_closure: bool) { + let (open, close) = if is_closure { ("|", "|") } else { ("(", ")") }; + self.word(open); + self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, is_closure)); + self.word(close); + self.print_fn_ret_ty(&decl.output) } crate fn print_movability(&mut self, movability: ast::Movability) { @@ -2786,24 +2759,14 @@ impl<'a> State<'a> { self.end(); } - crate fn print_fn_output(&mut self, decl: &ast::FnDecl) { - if let ast::FunctionRetTy::Default(..) = decl.output { - return; - } - - self.space_if_not_bol(); - self.ibox(INDENT_UNIT); - self.word_space("->"); - match decl.output { - ast::FunctionRetTy::Default(..) => unreachable!(), - ast::FunctionRetTy::Ty(ref ty) => - self.print_type(ty), - } - self.end(); - - match decl.output { - ast::FunctionRetTy::Ty(ref output) => self.maybe_print_comment(output.span.lo()), - _ => {} + crate fn print_fn_ret_ty(&mut self, fn_ret_ty: &ast::FunctionRetTy) { + if let ast::FunctionRetTy::Ty(ty) = fn_ret_ty { + self.space_if_not_bol(); + self.ibox(INDENT_UNIT); + self.word_space("->"); + self.print_type(ty); + self.end(); + self.maybe_print_comment(ty.span.lo()); } } diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 7cc1a769e52..74df808b37e 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -420,7 +420,7 @@ pub fn walk_generic_args<'a, V>(visitor: &mut V, } GenericArgs::Parenthesized(ref data) => { walk_list!(visitor, visit_ty, &data.inputs); - walk_list!(visitor, visit_ty, &data.output); + walk_fn_ret_ty(visitor, &data.output); } } } -- cgit 1.4.1-3-g733a5 From 9193d7a07e3c70f5f41c8a92aa34c120c9fdba0d Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 1 Dec 2019 16:15:10 +0100 Subject: Unify associated item pretty printing. --- src/libsyntax/print/pprust.rs | 96 ++++++++++--------------------------------- 1 file changed, 21 insertions(+), 75 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index a141d4d71bb..9e4615f60c0 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -335,8 +335,8 @@ pub fn nonterminal_to_string(nt: &Nonterminal) -> String { token::NtLifetime(e) => e.to_string(), token::NtLiteral(ref e) => expr_to_string(e), token::NtTT(ref tree) => tt_to_string(tree.clone()), - token::NtImplItem(ref e) => impl_item_to_string(e), - token::NtTraitItem(ref e) => trait_item_to_string(e), + // FIXME(Centril): merge these variants. + token::NtImplItem(ref e) | token::NtTraitItem(ref e) => assoc_item_to_string(e), token::NtVis(ref e) => vis_to_string(e), token::NtForeignItem(ref e) => foreign_item_to_string(e), } @@ -374,12 +374,8 @@ pub fn item_to_string(i: &ast::Item) -> String { to_string(|s| s.print_item(i)) } -fn impl_item_to_string(i: &ast::ImplItem) -> String { - to_string(|s| s.print_impl_item(i)) -} - -fn trait_item_to_string(i: &ast::TraitItem) -> String { - to_string(|s| s.print_trait_item(i)) +fn assoc_item_to_string(i: &ast::AssocItem) -> String { + to_string(|s| s.print_assoc_item(i)) } pub fn generic_params_to_string(generic_params: &[ast::GenericParam]) -> String { @@ -1301,7 +1297,7 @@ impl<'a> State<'a> { self.bopen(); self.print_inner_attributes(&item.attrs); for impl_item in impl_items { - self.print_impl_item(impl_item); + self.print_assoc_item(impl_item); } self.bclose(item.span); } @@ -1328,7 +1324,7 @@ impl<'a> State<'a> { self.s.word(" "); self.bopen(); for trait_item in trait_items { - self.print_trait_item(trait_item); + self.print_assoc_item(trait_item); } self.bclose(item.span); } @@ -1522,89 +1518,39 @@ impl<'a> State<'a> { } } - crate fn print_method_sig(&mut self, - ident: ast::Ident, - generics: &ast::Generics, - m: &ast::FnSig, - vis: &ast::Visibility) - { - self.print_fn(&m.decl, - m.header, - Some(ident), - &generics, - vis) - } - - crate fn print_trait_item(&mut self, ti: &ast::TraitItem) - { - self.ann.pre(self, AnnNode::SubItem(ti.id)); + crate fn print_assoc_item(&mut self, item: &ast::AssocItem) { + self.ann.pre(self, AnnNode::SubItem(item.id)); self.hardbreak_if_not_bol(); - self.maybe_print_comment(ti.span.lo()); - self.print_outer_attributes(&ti.attrs); - self.print_defaultness(ti.defaultness); - match ti.kind { - ast::TraitItemKind::Const(ref ty, ref default) => { - self.print_associated_const(ti.ident, ty, default.as_deref(), &ti.vis); - } - ast::TraitItemKind::Method(ref sig, ref body) => { - if body.is_some() { - self.head(""); - } - self.print_method_sig(ti.ident, &ti.generics, sig, &ti.vis); - if let Some(ref body) = *body { - self.nbsp(); - self.print_block_with_attrs(body, &ti.attrs); - } else { - self.s.word(";"); - } - } - ast::TraitItemKind::TyAlias(ref bounds, ref default) => { - self.print_associated_type(ti.ident, bounds, default.as_deref()); - } - ast::TraitItemKind::Macro(ref mac) => { - self.print_mac(mac); - if mac.args.need_semicolon() { - self.s.word(";"); - } + self.maybe_print_comment(item.span.lo()); + self.print_outer_attributes(&item.attrs); + self.print_defaultness(item.defaultness); + match &item.kind { + ast::AssocItemKind::Const(ty, expr) => { + self.print_associated_const(item.ident, ty, expr.as_deref(), &item.vis); } - } - self.ann.post(self, AnnNode::SubItem(ti.id)) - } - - // FIXME(Centril): merge with function above. - crate fn print_impl_item(&mut self, ii: &ast::ImplItem) { - self.ann.pre(self, AnnNode::SubItem(ii.id)); - self.hardbreak_if_not_bol(); - self.maybe_print_comment(ii.span.lo()); - self.print_outer_attributes(&ii.attrs); - self.print_defaultness(ii.defaultness); - match ii.kind { - ast::ImplItemKind::Const(ref ty, ref expr) => { - self.print_associated_const(ii.ident, ty, expr.as_deref(), &ii.vis); - } - ast::ImplItemKind::Method(ref sig, ref body) => { + ast::AssocItemKind::Method(sig, body) => { if body.is_some() { self.head(""); } - self.print_method_sig(ii.ident, &ii.generics, sig, &ii.vis); + self.print_fn(&sig.decl, sig.header, Some(item.ident), &item.generics, &item.vis); if let Some(body) = body { self.nbsp(); - self.print_block_with_attrs(body, &ii.attrs); + self.print_block_with_attrs(body, &item.attrs); } else { self.s.word(";"); } } - ast::ImplItemKind::TyAlias(ref bounds, ref ty) => { - self.print_associated_type(ii.ident, bounds, ty.as_deref()); + ast::AssocItemKind::TyAlias(bounds, ty) => { + self.print_associated_type(item.ident, bounds, ty.as_deref()); } - ast::ImplItemKind::Macro(ref mac) => { + ast::AssocItemKind::Macro(mac) => { self.print_mac(mac); if mac.args.need_semicolon() { self.s.word(";"); } } } - self.ann.post(self, AnnNode::SubItem(ii.id)) + self.ann.post(self, AnnNode::SubItem(item.id)) } crate fn print_stmt(&mut self, st: &ast::Stmt) { -- cgit 1.4.1-3-g733a5 From 76576d401c3fc33bab78cf022d0c4901d9b89548 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 1 Dec 2019 16:58:37 +0100 Subject: Unify associated item mut visitors. --- src/librustc_interface/util.rs | 4 +-- src/librustc_parse/config.rs | 8 +++--- src/libsyntax/mut_visit.rs | 54 ++++++++---------------------------- src/libsyntax_expand/expand.rs | 16 +++++------ src/libsyntax_expand/placeholders.rs | 12 ++++---- 5 files changed, 31 insertions(+), 63 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index ca7c4ba8786..026cb6eab1c 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -782,7 +782,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> { ast::TraitItemKind::Method(ref sig, _) => Self::is_sig_const(sig), _ => false, }; - self.run(is_const, |s| noop_flat_map_trait_item(i, s)) + self.run(is_const, |s| noop_flat_map_assoc_item(i, s)) } fn flat_map_impl_item(&mut self, i: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> { @@ -791,7 +791,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> { ast::ImplItemKind::Method(ref sig, _) => Self::is_sig_const(sig), _ => false, }; - self.run(is_const, |s| noop_flat_map_impl_item(i, s)) + self.run(is_const, |s| noop_flat_map_assoc_item(i, s)) } fn visit_anon_const(&mut self, c: &mut ast::AnonConst) { diff --git a/src/librustc_parse/config.rs b/src/librustc_parse/config.rs index 1e9203f377f..38ae7050abe 100644 --- a/src/librustc_parse/config.rs +++ b/src/librustc_parse/config.rs @@ -344,12 +344,12 @@ impl<'a> MutVisitor for StripUnconfigured<'a> { noop_flat_map_item(configure!(self, item), self) } - fn flat_map_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> { - noop_flat_map_impl_item(configure!(self, item), self) + fn flat_map_impl_item(&mut self, item: ast::AssocItem) -> SmallVec<[ast::AssocItem; 1]> { + noop_flat_map_assoc_item(configure!(self, item), self) } - fn flat_map_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> { - noop_flat_map_trait_item(configure!(self, item), self) + fn flat_map_trait_item(&mut self, item: ast::AssocItem) -> SmallVec<[ast::AssocItem; 1]> { + noop_flat_map_assoc_item(configure!(self, item), self) } fn visit_mac(&mut self, _mac: &mut ast::Mac) { diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 97a85b0fe7e..18d42f9dd66 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -103,12 +103,12 @@ pub trait MutVisitor: Sized { noop_visit_item_kind(i, self); } - fn flat_map_trait_item(&mut self, i: TraitItem) -> SmallVec<[TraitItem; 1]> { - noop_flat_map_trait_item(i, self) + fn flat_map_trait_item(&mut self, i: AssocItem) -> SmallVec<[AssocItem; 1]> { + noop_flat_map_assoc_item(i, self) } - fn flat_map_impl_item(&mut self, i: ImplItem) -> SmallVec<[ImplItem; 1]> { - noop_flat_map_impl_item(i, self) + fn flat_map_impl_item(&mut self, i: AssocItem) -> SmallVec<[AssocItem; 1]> { + noop_flat_map_assoc_item(i, self) } fn visit_fn_decl(&mut self, d: &mut P) { @@ -940,42 +940,10 @@ pub fn noop_visit_item_kind(kind: &mut ItemKind, vis: &mut T) { } } -pub fn noop_flat_map_trait_item(mut item: TraitItem, visitor: &mut T) - -> SmallVec<[TraitItem; 1]> +pub fn noop_flat_map_assoc_item(mut item: AssocItem, visitor: &mut T) + -> SmallVec<[AssocItem; 1]> { - let TraitItem { id, ident, vis, defaultness: _, attrs, generics, kind, span, tokens: _ } = - &mut item; - visitor.visit_id(id); - visitor.visit_ident(ident); - visitor.visit_vis(vis); - visit_attrs(attrs, visitor); - visitor.visit_generics(generics); - match kind { - TraitItemKind::Const(ty, default) => { - visitor.visit_ty(ty); - visit_opt(default, |default| visitor.visit_expr(default)); - } - TraitItemKind::Method(sig, body) => { - visit_fn_sig(sig, visitor); - visit_opt(body, |body| visitor.visit_block(body)); - } - TraitItemKind::TyAlias(bounds, default) => { - visit_bounds(bounds, visitor); - visit_opt(default, |default| visitor.visit_ty(default)); - } - TraitItemKind::Macro(mac) => { - visitor.visit_mac(mac); - } - } - visitor.visit_span(span); - - smallvec![item] -} - -pub fn noop_flat_map_impl_item(mut item: ImplItem, visitor: &mut T) - -> SmallVec<[ImplItem; 1]> -{ - let ImplItem { id, ident, vis, defaultness: _, attrs, generics, kind, span, tokens: _ } = + let AssocItem { id, ident, vis, defaultness: _, attrs, generics, kind, span, tokens: _ } = &mut item; visitor.visit_id(id); visitor.visit_ident(ident); @@ -983,19 +951,19 @@ pub fn noop_flat_map_impl_item(mut item: ImplItem, visitor: &mut visit_attrs(attrs, visitor); visitor.visit_generics(generics); match kind { - ImplItemKind::Const(ty, expr) => { + AssocItemKind::Const(ty, expr) => { visitor.visit_ty(ty); visit_opt(expr, |expr| visitor.visit_expr(expr)); } - ImplItemKind::Method(sig, body) => { + AssocItemKind::Method(sig, body) => { visit_fn_sig(sig, visitor); visit_opt(body, |body| visitor.visit_block(body)); } - ImplItemKind::TyAlias(bounds, ty) => { + AssocItemKind::TyAlias(bounds, ty) => { visit_bounds(bounds, visitor); visit_opt(ty, |ty| visitor.visit_ty(ty)); } - ImplItemKind::Macro(mac) => visitor.visit_mac(mac), + AssocItemKind::Macro(mac) => visitor.visit_mac(mac), } visitor.visit_span(span); diff --git a/src/libsyntax_expand/expand.rs b/src/libsyntax_expand/expand.rs index 9bfedb3b617..2eae6d494d0 100644 --- a/src/libsyntax_expand/expand.rs +++ b/src/libsyntax_expand/expand.rs @@ -1317,7 +1317,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> { } } - fn flat_map_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> { + fn flat_map_trait_item(&mut self, item: ast::AssocItem) -> SmallVec<[ast::AssocItem; 1]> { let mut item = configure!(self, item); let (attr, traits, after_derive) = self.classify_item(&mut item); @@ -1327,16 +1327,16 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> { } match item.kind { - ast::TraitItemKind::Macro(mac) => { - let ast::TraitItem { attrs, span, .. } = item; + ast::AssocItemKind::Macro(mac) => { + let ast::AssocItem { attrs, span, .. } = item; self.check_attributes(&attrs); self.collect_bang(mac, span, AstFragmentKind::TraitItems).make_trait_items() } - _ => noop_flat_map_trait_item(item, self), + _ => noop_flat_map_assoc_item(item, self), } } - fn flat_map_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> { + fn flat_map_impl_item(&mut self, item: ast::AssocItem) -> SmallVec<[ast::AssocItem; 1]> { let mut item = configure!(self, item); let (attr, traits, after_derive) = self.classify_item(&mut item); @@ -1346,12 +1346,12 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> { } match item.kind { - ast::ImplItemKind::Macro(mac) => { - let ast::ImplItem { attrs, span, .. } = item; + ast::AssocItemKind::Macro(mac) => { + let ast::AssocItem { attrs, span, .. } = item; self.check_attributes(&attrs); self.collect_bang(mac, span, AstFragmentKind::ImplItems).make_impl_items() } - _ => noop_flat_map_impl_item(item, self), + _ => noop_flat_map_assoc_item(item, self), } } diff --git a/src/libsyntax_expand/placeholders.rs b/src/libsyntax_expand/placeholders.rs index 6057be9826a..18acbf2cc13 100644 --- a/src/libsyntax_expand/placeholders.rs +++ b/src/libsyntax_expand/placeholders.rs @@ -253,17 +253,17 @@ impl<'a, 'b> MutVisitor for PlaceholderExpander<'a, 'b> { noop_flat_map_item(item, self) } - fn flat_map_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> { + fn flat_map_trait_item(&mut self, item: ast::AssocItem) -> SmallVec<[ast::AssocItem; 1]> { match item.kind { - ast::TraitItemKind::Macro(_) => self.remove(item.id).make_trait_items(), - _ => noop_flat_map_trait_item(item, self), + ast::AssocItemKind::Macro(_) => self.remove(item.id).make_trait_items(), + _ => noop_flat_map_assoc_item(item, self), } } - fn flat_map_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> { + fn flat_map_impl_item(&mut self, item: ast::AssocItem) -> SmallVec<[ast::AssocItem; 1]> { match item.kind { - ast::ImplItemKind::Macro(_) => self.remove(item.id).make_impl_items(), - _ => noop_flat_map_impl_item(item, self), + ast::AssocItemKind::Macro(_) => self.remove(item.id).make_impl_items(), + _ => noop_flat_map_assoc_item(item, self), } } -- cgit 1.4.1-3-g733a5 From 0d8a9d74e3bbeed1ad787e1b563e6884496728b2 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 1 Dec 2019 17:11:07 +0100 Subject: Unify associated item visitor. --- src/librustc/hir/lowering.rs | 6 +-- src/librustc/hir/lowering/item.rs | 4 +- src/librustc/lint/context.rs | 4 +- src/librustc_passes/ast_validation.rs | 14 +++---- src/librustc_passes/hir_stats.rs | 4 +- src/librustc_resolve/build_reduced_graph.rs | 4 +- src/librustc_resolve/def_collector.rs | 4 +- src/librustc_resolve/late.rs | 8 ++-- src/libsyntax/feature_gate/check.rs | 4 +- src/libsyntax/util/node_count.rs | 4 +- src/libsyntax/visit.rs | 58 ++++++++--------------------- 11 files changed, 43 insertions(+), 71 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index e2c99f456e9..1ee02dcf0c1 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -484,11 +484,11 @@ impl<'a> LoweringContext<'a> { TraitItemKind::Method(_, None) => { // Ignore patterns in trait methods without bodies self.with_hir_id_owner(None, |this| { - visit::walk_trait_item(this, item) + visit::walk_assoc_item(this, item) }); } _ => self.with_hir_id_owner(Some(item.id), |this| { - visit::walk_trait_item(this, item); + visit::walk_assoc_item(this, item); }) } } @@ -496,7 +496,7 @@ impl<'a> LoweringContext<'a> { fn visit_impl_item(&mut self, item: &'tcx ImplItem) { self.lctx.allocate_hir_id_counter(item.id); self.with_hir_id_owner(Some(item.id), |this| { - visit::walk_impl_item(this, item); + visit::walk_assoc_item(this, item); }); } diff --git a/src/librustc/hir/lowering/item.rs b/src/librustc/hir/lowering/item.rs index f77523e6382..c3d2ed6b39c 100644 --- a/src/librustc/hir/lowering/item.rs +++ b/src/librustc/hir/lowering/item.rs @@ -94,7 +94,7 @@ impl<'tcx, 'interner> Visitor<'tcx> for ItemLowerer<'tcx, 'interner> { lctx.modules.get_mut(&lctx.current_module).unwrap().trait_items.insert(id); }); - visit::walk_trait_item(self, item); + visit::walk_assoc_item(self, item); } fn visit_impl_item(&mut self, item: &'tcx ImplItem) { @@ -104,7 +104,7 @@ impl<'tcx, 'interner> Visitor<'tcx> for ItemLowerer<'tcx, 'interner> { lctx.impl_items.insert(id, hir_item); lctx.modules.get_mut(&lctx.current_module).unwrap().impl_items.insert(id); }); - visit::walk_impl_item(self, item); + visit::walk_assoc_item(self, item); } } diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index 7f72154e42c..e0ec9e62645 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -1252,7 +1252,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> fn visit_trait_item(&mut self, trait_item: &'a ast::TraitItem) { self.with_lint_attrs(trait_item.id, &trait_item.attrs, |cx| { run_early_pass!(cx, check_trait_item, trait_item); - ast_visit::walk_trait_item(cx, trait_item); + ast_visit::walk_assoc_item(cx, trait_item); run_early_pass!(cx, check_trait_item_post, trait_item); }); } @@ -1260,7 +1260,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> fn visit_impl_item(&mut self, impl_item: &'a ast::ImplItem) { self.with_lint_attrs(impl_item.id, &impl_item.attrs, |cx| { run_early_pass!(cx, check_impl_item, impl_item); - ast_visit::walk_impl_item(cx, impl_item); + ast_visit::walk_assoc_item(cx, impl_item); run_early_pass!(cx, check_impl_item_post, impl_item); }); } diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index 8be97155d8c..34534d6cca5 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -772,28 +772,28 @@ impl<'a> Visitor<'a> for AstValidator<'a> { |this| visit::walk_enum_def(this, enum_definition, generics, item_id)) } - fn visit_impl_item(&mut self, ii: &'a ImplItem) { + fn visit_impl_item(&mut self, ii: &'a AssocItem) { match &ii.kind { - ImplItemKind::Const(_, body) => { + AssocItemKind::Const(_, body) => { self.check_impl_item_provided(ii.span, body, "constant", " = ;"); } - ImplItemKind::Method(sig, body) => { + AssocItemKind::Method(sig, body) => { self.check_impl_item_provided(ii.span, body, "function", " { }"); self.check_fn_decl(&sig.decl); } - ImplItemKind::TyAlias(bounds, body) => { + AssocItemKind::TyAlias(bounds, body) => { self.check_impl_item_provided(ii.span, body, "type", " = ;"); self.check_impl_assoc_type_no_bounds(bounds); } _ => {} } - visit::walk_impl_item(self, ii); + visit::walk_assoc_item(self, ii); } - fn visit_trait_item(&mut self, ti: &'a TraitItem) { + fn visit_trait_item(&mut self, ti: &'a AssocItem) { self.invalid_visibility(&ti.vis, None); self.check_defaultness(ti.span, ti.defaultness); - visit::walk_trait_item(self, ti); + visit::walk_assoc_item(self, ti); } } diff --git a/src/librustc_passes/hir_stats.rs b/src/librustc_passes/hir_stats.rs index a5924efefc2..071ed0db870 100644 --- a/src/librustc_passes/hir_stats.rs +++ b/src/librustc_passes/hir_stats.rs @@ -316,12 +316,12 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> { fn visit_trait_item(&mut self, ti: &'v ast::TraitItem) { self.record("TraitItem", Id::None, ti); - ast_visit::walk_trait_item(self, ti) + ast_visit::walk_assoc_item(self, ti) } fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) { self.record("ImplItem", Id::None, ii); - ast_visit::walk_impl_item(self, ii) + ast_visit::walk_assoc_item(self, ii) } fn visit_param_bound(&mut self, bounds: &'v ast::GenericBound) { diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index d2d5a33ec7a..abed4b326a5 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -1190,7 +1190,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> { let expansion = self.parent_scope.expansion; self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion)); - visit::walk_trait_item(self, item); + visit::walk_assoc_item(self, item); } fn visit_impl_item(&mut self, item: &'b ast::ImplItem) { @@ -1198,7 +1198,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> { self.visit_invoc(item.id); } else { self.resolve_visibility(&item.vis); - visit::walk_impl_item(self, item); + visit::walk_assoc_item(self, item); } } diff --git a/src/librustc_resolve/def_collector.rs b/src/librustc_resolve/def_collector.rs index 471e2634b8a..248027a91ab 100644 --- a/src/librustc_resolve/def_collector.rs +++ b/src/librustc_resolve/def_collector.rs @@ -223,7 +223,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { }; let def = self.create_def(ti.id, def_data, ti.span); - self.with_parent(def, |this| visit::walk_trait_item(this, ti)); + self.with_parent(def, |this| visit::walk_assoc_item(this, ti)); } fn visit_impl_item(&mut self, ii: &'a ImplItem) { @@ -249,7 +249,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { }; let def = self.create_def(ii.id, def_data, ii.span); - self.with_parent(def, |this| visit::walk_impl_item(this, ii)); + self.with_parent(def, |this| visit::walk_assoc_item(this, ii)); } fn visit_pat(&mut self, pat: &'a Pat) { diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index 33e24c8cfd4..682ddc421c2 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -819,10 +819,10 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { } } TraitItemKind::Method(_, _) => { - visit::walk_trait_item(this, trait_item) + visit::walk_assoc_item(this, trait_item) } TraitItemKind::TyAlias(..) => { - visit::walk_trait_item(this, trait_item) + visit::walk_assoc_item(this, trait_item) } TraitItemKind::Macro(_) => { panic!("unexpanded macro in resolve!") @@ -1106,7 +1106,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { ); this.with_constant_rib(|this| { - visit::walk_impl_item(this, impl_item) + visit::walk_assoc_item(this, impl_item) }); } ImplItemKind::Method(..) => { @@ -1117,7 +1117,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { impl_item.span, |n, s| MethodNotMemberOfTrait(n, s)); - visit::walk_impl_item(this, impl_item); + visit::walk_assoc_item(this, impl_item); } ImplItemKind::TyAlias(_, Some(ref ty)) => { // If this is a trait impl, ensure the type diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index f786de6401a..226719c7460 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -595,7 +595,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } _ => {} } - visit::walk_trait_item(self, ti) + visit::walk_assoc_item(self, ti) } fn visit_impl_item(&mut self, ii: &'a ast::ImplItem) { @@ -620,7 +620,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } _ => {} } - visit::walk_impl_item(self, ii) + visit::walk_assoc_item(self, ii) } fn visit_vis(&mut self, vis: &'a ast::Visibility) { diff --git a/src/libsyntax/util/node_count.rs b/src/libsyntax/util/node_count.rs index a64fec70961..14ca7a7eff3 100644 --- a/src/libsyntax/util/node_count.rs +++ b/src/libsyntax/util/node_count.rs @@ -75,11 +75,11 @@ impl<'ast> Visitor<'ast> for NodeCounter { } fn visit_trait_item(&mut self, ti: &TraitItem) { self.count += 1; - walk_trait_item(self, ti) + walk_assoc_item(self, ti) } fn visit_impl_item(&mut self, ii: &ImplItem) { self.count += 1; - walk_impl_item(self, ii) + walk_assoc_item(self, ii) } fn visit_trait_ref(&mut self, t: &TraitRef) { self.count += 1; diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 74df808b37e..8fe8e136c10 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -83,8 +83,8 @@ pub trait Visitor<'ast>: Sized { fn visit_fn(&mut self, fk: FnKind<'ast>, fd: &'ast FnDecl, s: Span, _: NodeId) { walk_fn(self, fk, fd, s) } - fn visit_trait_item(&mut self, ti: &'ast TraitItem) { walk_trait_item(self, ti) } - fn visit_impl_item(&mut self, ii: &'ast ImplItem) { walk_impl_item(self, ii) } + fn visit_trait_item(&mut self, i: &'ast AssocItem) { walk_assoc_item(self, i) } + fn visit_impl_item(&mut self, i: &'ast AssocItem) { walk_assoc_item(self, i) } fn visit_trait_ref(&mut self, t: &'ast TraitRef) { walk_trait_ref(self, t) } fn visit_param_bound(&mut self, bounds: &'ast GenericBound) { walk_param_bound(self, bounds) @@ -581,57 +581,29 @@ pub fn walk_fn<'a, V>(visitor: &mut V, kind: FnKind<'a>, declaration: &'a FnDecl } } -pub fn walk_trait_item<'a, V: Visitor<'a>>(visitor: &mut V, trait_item: &'a TraitItem) { - visitor.visit_vis(&trait_item.vis); - visitor.visit_ident(trait_item.ident); - walk_list!(visitor, visit_attribute, &trait_item.attrs); - visitor.visit_generics(&trait_item.generics); - match trait_item.kind { - TraitItemKind::Const(ref ty, ref default) => { - visitor.visit_ty(ty); - walk_list!(visitor, visit_expr, default); - } - TraitItemKind::Method(ref sig, None) => { - visitor.visit_fn_header(&sig.header); - walk_fn_decl(visitor, &sig.decl); - } - TraitItemKind::Method(ref sig, Some(ref body)) => { - visitor.visit_fn(FnKind::Method(trait_item.ident, sig, &trait_item.vis, body), - &sig.decl, trait_item.span, trait_item.id); - } - TraitItemKind::TyAlias(ref bounds, ref default) => { - walk_list!(visitor, visit_param_bound, bounds); - walk_list!(visitor, visit_ty, default); - } - TraitItemKind::Macro(ref mac) => { - visitor.visit_mac(mac); - } - } -} - -pub fn walk_impl_item<'a, V: Visitor<'a>>(visitor: &mut V, impl_item: &'a ImplItem) { - visitor.visit_vis(&impl_item.vis); - visitor.visit_ident(impl_item.ident); - walk_list!(visitor, visit_attribute, &impl_item.attrs); - visitor.visit_generics(&impl_item.generics); - match impl_item.kind { - ImplItemKind::Const(ref ty, ref expr) => { +pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem) { + visitor.visit_vis(&item.vis); + visitor.visit_ident(item.ident); + walk_list!(visitor, visit_attribute, &item.attrs); + visitor.visit_generics(&item.generics); + match item.kind { + AssocItemKind::Const(ref ty, ref expr) => { visitor.visit_ty(ty); walk_list!(visitor, visit_expr, expr); } - ImplItemKind::Method(ref sig, None) => { + AssocItemKind::Method(ref sig, None) => { visitor.visit_fn_header(&sig.header); walk_fn_decl(visitor, &sig.decl); } - ImplItemKind::Method(ref sig, Some(ref body)) => { - visitor.visit_fn(FnKind::Method(impl_item.ident, sig, &impl_item.vis, body), - &sig.decl, impl_item.span, impl_item.id); + AssocItemKind::Method(ref sig, Some(ref body)) => { + visitor.visit_fn(FnKind::Method(item.ident, sig, &item.vis, body), + &sig.decl, item.span, item.id); } - ImplItemKind::TyAlias(ref bounds, ref ty) => { + AssocItemKind::TyAlias(ref bounds, ref ty) => { walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_ty, ty); } - ImplItemKind::Macro(ref mac) => { + AssocItemKind::Macro(ref mac) => { visitor.visit_mac(mac); } } -- cgit 1.4.1-3-g733a5 From b499a88dfc484f077ec7264bd7fae7d7c60d9fb8 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Mon, 2 Dec 2019 02:03:31 +0100 Subject: Unify assoc item visitors more. --- src/librustc/hir/lowering.rs | 6 ++-- src/librustc/lint/context.rs | 4 +-- src/librustc_passes/ast_validation.rs | 4 +-- src/librustc_passes/hir_stats.rs | 4 +-- src/librustc_resolve/build_reduced_graph.rs | 4 +-- src/librustc_resolve/def_collector.rs | 4 +-- src/libsyntax/feature_gate/check.rs | 32 ++++++++++------------ src/libsyntax/util/node_count.rs | 6 +--- src/libsyntax/visit.rs | 13 +++++++-- .../feature-gate-type_alias_impl_trait.stderr | 18 ++++++------ .../trait-item-with-defaultness-fail-semantic.rs | 2 ++ ...rait-item-with-defaultness-fail-semantic.stderr | 12 ++++---- 12 files changed, 56 insertions(+), 53 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 1ee02dcf0c1..e2c99f456e9 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -484,11 +484,11 @@ impl<'a> LoweringContext<'a> { TraitItemKind::Method(_, None) => { // Ignore patterns in trait methods without bodies self.with_hir_id_owner(None, |this| { - visit::walk_assoc_item(this, item) + visit::walk_trait_item(this, item) }); } _ => self.with_hir_id_owner(Some(item.id), |this| { - visit::walk_assoc_item(this, item); + visit::walk_trait_item(this, item); }) } } @@ -496,7 +496,7 @@ impl<'a> LoweringContext<'a> { fn visit_impl_item(&mut self, item: &'tcx ImplItem) { self.lctx.allocate_hir_id_counter(item.id); self.with_hir_id_owner(Some(item.id), |this| { - visit::walk_assoc_item(this, item); + visit::walk_impl_item(this, item); }); } diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index e0ec9e62645..7f72154e42c 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -1252,7 +1252,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> fn visit_trait_item(&mut self, trait_item: &'a ast::TraitItem) { self.with_lint_attrs(trait_item.id, &trait_item.attrs, |cx| { run_early_pass!(cx, check_trait_item, trait_item); - ast_visit::walk_assoc_item(cx, trait_item); + ast_visit::walk_trait_item(cx, trait_item); run_early_pass!(cx, check_trait_item_post, trait_item); }); } @@ -1260,7 +1260,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> fn visit_impl_item(&mut self, impl_item: &'a ast::ImplItem) { self.with_lint_attrs(impl_item.id, &impl_item.attrs, |cx| { run_early_pass!(cx, check_impl_item, impl_item); - ast_visit::walk_assoc_item(cx, impl_item); + ast_visit::walk_impl_item(cx, impl_item); run_early_pass!(cx, check_impl_item_post, impl_item); }); } diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index 34534d6cca5..ad6c99494a6 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -787,13 +787,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } _ => {} } - visit::walk_assoc_item(self, ii); + visit::walk_impl_item(self, ii); } fn visit_trait_item(&mut self, ti: &'a AssocItem) { self.invalid_visibility(&ti.vis, None); self.check_defaultness(ti.span, ti.defaultness); - visit::walk_assoc_item(self, ti); + visit::walk_trait_item(self, ti); } } diff --git a/src/librustc_passes/hir_stats.rs b/src/librustc_passes/hir_stats.rs index 071ed0db870..a5924efefc2 100644 --- a/src/librustc_passes/hir_stats.rs +++ b/src/librustc_passes/hir_stats.rs @@ -316,12 +316,12 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> { fn visit_trait_item(&mut self, ti: &'v ast::TraitItem) { self.record("TraitItem", Id::None, ti); - ast_visit::walk_assoc_item(self, ti) + ast_visit::walk_trait_item(self, ti) } fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) { self.record("ImplItem", Id::None, ii); - ast_visit::walk_assoc_item(self, ii) + ast_visit::walk_impl_item(self, ii) } fn visit_param_bound(&mut self, bounds: &'v ast::GenericBound) { diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index abed4b326a5..d2d5a33ec7a 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -1190,7 +1190,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> { let expansion = self.parent_scope.expansion; self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion)); - visit::walk_assoc_item(self, item); + visit::walk_trait_item(self, item); } fn visit_impl_item(&mut self, item: &'b ast::ImplItem) { @@ -1198,7 +1198,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> { self.visit_invoc(item.id); } else { self.resolve_visibility(&item.vis); - visit::walk_assoc_item(self, item); + visit::walk_impl_item(self, item); } } diff --git a/src/librustc_resolve/def_collector.rs b/src/librustc_resolve/def_collector.rs index 248027a91ab..471e2634b8a 100644 --- a/src/librustc_resolve/def_collector.rs +++ b/src/librustc_resolve/def_collector.rs @@ -223,7 +223,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { }; let def = self.create_def(ti.id, def_data, ti.span); - self.with_parent(def, |this| visit::walk_assoc_item(this, ti)); + self.with_parent(def, |this| visit::walk_trait_item(this, ti)); } fn visit_impl_item(&mut self, ii: &'a ImplItem) { @@ -249,7 +249,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { }; let def = self.create_def(ii.id, def_data, ii.span); - self.with_parent(def, |this| visit::walk_assoc_item(this, ii)); + self.with_parent(def, |this| visit::walk_impl_item(this, ii)); } fn visit_pat(&mut self, pat: &'a Pat) { diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index 226719c7460..c27fcb6a68e 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -577,42 +577,38 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { if block.is_none() { self.check_extern(sig.header.ext); } - if sig.decl.c_variadic() { - gate_feature_post!(&self, c_variadic, ti.span, - "C-variadic functions are unstable"); - } if sig.header.constness.node == ast::Constness::Const { gate_feature_post!(&self, const_fn, ti.span, "const fn is unstable"); } } ast::TraitItemKind::TyAlias(_, ref default) => { - if let Some(ty) = default { - self.check_impl_trait(ty); - gate_feature_post!(&self, associated_type_defaults, ti.span, - "associated type defaults are unstable"); + if let Some(_) = default { + gate_feature_post!( + &self, associated_type_defaults, ti.span, + "associated type defaults are unstable" + ); } - self.check_gat(&ti.generics, ti.span); } _ => {} } - visit::walk_assoc_item(self, ti) + visit::walk_trait_item(self, ti) } - fn visit_impl_item(&mut self, ii: &'a ast::ImplItem) { + fn visit_assoc_item(&mut self, ii: &'a ast::AssocItem) { if ii.defaultness == ast::Defaultness::Default { - gate_feature_post!(&self, specialization, - ii.span, - "specialization is unstable"); + gate_feature_post!(&self, specialization, ii.span, "specialization is unstable"); } match ii.kind { - ast::ImplItemKind::Method(ref sig, _) => { + ast::AssocItemKind::Method(ref sig, _) => { if sig.decl.c_variadic() { - gate_feature_post!(&self, c_variadic, ii.span, - "C-variadic functions are unstable"); + gate_feature_post!( + &self, c_variadic, ii.span, + "C-variadic functions are unstable" + ); } } - ast::ImplItemKind::TyAlias(_, ref ty) => { + ast::AssocItemKind::TyAlias(_, ref ty) => { if let Some(ty) = ty { self.check_impl_trait(ty); } diff --git a/src/libsyntax/util/node_count.rs b/src/libsyntax/util/node_count.rs index 14ca7a7eff3..3db9955d304 100644 --- a/src/libsyntax/util/node_count.rs +++ b/src/libsyntax/util/node_count.rs @@ -73,14 +73,10 @@ impl<'ast> Visitor<'ast> for NodeCounter { self.count += 1; walk_fn(self, fk, fd, s) } - fn visit_trait_item(&mut self, ti: &TraitItem) { + fn visit_assoc_item(&mut self, ti: &AssocItem) { self.count += 1; walk_assoc_item(self, ti) } - fn visit_impl_item(&mut self, ii: &ImplItem) { - self.count += 1; - walk_assoc_item(self, ii) - } fn visit_trait_ref(&mut self, t: &TraitRef) { self.count += 1; walk_trait_ref(self, t) diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 8fe8e136c10..e2c0ee61467 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -83,8 +83,9 @@ pub trait Visitor<'ast>: Sized { fn visit_fn(&mut self, fk: FnKind<'ast>, fd: &'ast FnDecl, s: Span, _: NodeId) { walk_fn(self, fk, fd, s) } - fn visit_trait_item(&mut self, i: &'ast AssocItem) { walk_assoc_item(self, i) } - fn visit_impl_item(&mut self, i: &'ast AssocItem) { walk_assoc_item(self, i) } + fn visit_trait_item(&mut self, i: &'ast AssocItem) { walk_trait_item(self, i) } + fn visit_impl_item(&mut self, i: &'ast AssocItem) { walk_impl_item(self, i) } + fn visit_assoc_item(&mut self, i: &'ast AssocItem) { walk_assoc_item(self, i) } fn visit_trait_ref(&mut self, t: &'ast TraitRef) { walk_trait_ref(self, t) } fn visit_param_bound(&mut self, bounds: &'ast GenericBound) { walk_param_bound(self, bounds) @@ -581,6 +582,14 @@ pub fn walk_fn<'a, V>(visitor: &mut V, kind: FnKind<'a>, declaration: &'a FnDecl } } +pub fn walk_impl_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem) { + visitor.visit_assoc_item(item); +} + +pub fn walk_trait_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem) { + visitor.visit_assoc_item(item); +} + pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem) { visitor.visit_vis(&item.vis); visitor.visit_ident(item.ident); diff --git a/src/test/ui/feature-gates/feature-gate-type_alias_impl_trait.stderr b/src/test/ui/feature-gates/feature-gate-type_alias_impl_trait.stderr index d9ebcdecb9b..1f61473c9d2 100644 --- a/src/test/ui/feature-gates/feature-gate-type_alias_impl_trait.stderr +++ b/src/test/ui/feature-gates/feature-gate-type_alias_impl_trait.stderr @@ -16,15 +16,6 @@ LL | type Baa = impl Debug; = note: for more information, see https://github.com/rust-lang/rust/issues/63063 = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable -error[E0658]: `impl Trait` in type aliases is unstable - --> $DIR/feature-gate-type_alias_impl_trait.rs:18:18 - | -LL | type Assoc = impl Debug; - | ^^^^^^^^^^ - | - = note: for more information, see https://github.com/rust-lang/rust/issues/63063 - = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable - error[E0658]: associated type defaults are unstable --> $DIR/feature-gate-type_alias_impl_trait.rs:18:5 | @@ -34,6 +25,15 @@ LL | type Assoc = impl Debug; = note: for more information, see https://github.com/rust-lang/rust/issues/29661 = help: add `#![feature(associated_type_defaults)]` to the crate attributes to enable +error[E0658]: `impl Trait` in type aliases is unstable + --> $DIR/feature-gate-type_alias_impl_trait.rs:18:18 + | +LL | type Assoc = impl Debug; + | ^^^^^^^^^^ + | + = note: for more information, see https://github.com/rust-lang/rust/issues/63063 + = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable + error[E0658]: `impl Trait` in type aliases is unstable --> $DIR/feature-gate-type_alias_impl_trait.rs:24:24 | diff --git a/src/test/ui/parser/trait-item-with-defaultness-fail-semantic.rs b/src/test/ui/parser/trait-item-with-defaultness-fail-semantic.rs index b67e30637aa..09f967f161e 100644 --- a/src/test/ui/parser/trait-item-with-defaultness-fail-semantic.rs +++ b/src/test/ui/parser/trait-item-with-defaultness-fail-semantic.rs @@ -1,3 +1,5 @@ +#![feature(specialization)] + fn main() {} trait X { diff --git a/src/test/ui/parser/trait-item-with-defaultness-fail-semantic.stderr b/src/test/ui/parser/trait-item-with-defaultness-fail-semantic.stderr index 48b502a1506..54111df3423 100644 --- a/src/test/ui/parser/trait-item-with-defaultness-fail-semantic.stderr +++ b/src/test/ui/parser/trait-item-with-defaultness-fail-semantic.stderr @@ -1,35 +1,35 @@ error: `default` is only allowed on items in `impl` definitions - --> $DIR/trait-item-with-defaultness-fail-semantic.rs:4:5 + --> $DIR/trait-item-with-defaultness-fail-semantic.rs:6:5 | LL | default const A: u8; | ^^^^^^^^^^^^^^^^^^^^ error: `default` is only allowed on items in `impl` definitions - --> $DIR/trait-item-with-defaultness-fail-semantic.rs:5:5 + --> $DIR/trait-item-with-defaultness-fail-semantic.rs:7:5 | LL | default const B: u8 = 0; | ^^^^^^^^^^^^^^^^^^^^^^^^ error: `default` is only allowed on items in `impl` definitions - --> $DIR/trait-item-with-defaultness-fail-semantic.rs:6:5 + --> $DIR/trait-item-with-defaultness-fail-semantic.rs:8:5 | LL | default type D; | ^^^^^^^^^^^^^^^ error: `default` is only allowed on items in `impl` definitions - --> $DIR/trait-item-with-defaultness-fail-semantic.rs:7:5 + --> $DIR/trait-item-with-defaultness-fail-semantic.rs:9:5 | LL | default type C: Ord; | ^^^^^^^^^^^^^^^^^^^^ error: `default` is only allowed on items in `impl` definitions - --> $DIR/trait-item-with-defaultness-fail-semantic.rs:8:5 + --> $DIR/trait-item-with-defaultness-fail-semantic.rs:10:5 | LL | default fn f1(); | ^^^^^^^^^^^^^^^^ error: `default` is only allowed on items in `impl` definitions - --> $DIR/trait-item-with-defaultness-fail-semantic.rs:9:5 + --> $DIR/trait-item-with-defaultness-fail-semantic.rs:11:5 | LL | default fn f2() {} | ^^^^^^^^^^^^^^^^^^ -- cgit 1.4.1-3-g733a5 From abf2e7aa959a4611bc2f607bc7e9200b8b81c089 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 8 Dec 2019 00:08:09 +0100 Subject: Remove `ast::{Impl,Trait}{Item,ItemKind}`. --- src/librustc/hir/lowering.rs | 6 ++-- src/librustc/hir/lowering/item.rs | 46 ++++++++++++++--------------- src/librustc/lint/context.rs | 4 +-- src/librustc/lint/mod.rs | 8 ++--- src/librustc_interface/util.rs | 15 ++++------ src/librustc_lint/builtin.rs | 8 ++--- src/librustc_passes/ast_validation.rs | 2 +- src/librustc_passes/hir_stats.rs | 4 +-- src/librustc_resolve/build_reduced_graph.rs | 18 +++++------ src/librustc_resolve/def_collector.rs | 20 ++++++------- src/librustc_resolve/late.rs | 26 ++++++++-------- src/librustc_save_analysis/dump_visitor.rs | 26 ++++++++-------- src/libsyntax/ast.rs | 14 +++------ src/libsyntax/feature_gate/check.rs | 6 ++-- src/libsyntax/token.rs | 4 +-- src/libsyntax_expand/base.rs | 24 +++++++-------- src/libsyntax_expand/expand.rs | 4 +-- src/libsyntax_expand/placeholders.rs | 8 ++--- src/libsyntax_ext/deriving/generic/mod.rs | 14 ++++----- 19 files changed, 123 insertions(+), 134 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 58225e87f26..78df14adc5d 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -477,11 +477,11 @@ impl<'a> LoweringContext<'a> { }); } - fn visit_trait_item(&mut self, item: &'tcx TraitItem) { + fn visit_trait_item(&mut self, item: &'tcx AssocItem) { self.lctx.allocate_hir_id_counter(item.id); match item.kind { - TraitItemKind::Method(_, None) => { + AssocItemKind::Method(_, None) => { // Ignore patterns in trait methods without bodies self.with_hir_id_owner(None, |this| { visit::walk_trait_item(this, item) @@ -493,7 +493,7 @@ impl<'a> LoweringContext<'a> { } } - fn visit_impl_item(&mut self, item: &'tcx ImplItem) { + fn visit_impl_item(&mut self, item: &'tcx AssocItem) { self.lctx.allocate_hir_id_counter(item.id); self.with_hir_id_owner(Some(item.id), |this| { visit::walk_impl_item(this, item); diff --git a/src/librustc/hir/lowering/item.rs b/src/librustc/hir/lowering/item.rs index c3d2ed6b39c..f25714f741b 100644 --- a/src/librustc/hir/lowering/item.rs +++ b/src/librustc/hir/lowering/item.rs @@ -86,7 +86,7 @@ impl<'tcx, 'interner> Visitor<'tcx> for ItemLowerer<'tcx, 'interner> { } } - fn visit_trait_item(&mut self, item: &'tcx TraitItem) { + fn visit_trait_item(&mut self, item: &'tcx AssocItem) { self.lctx.with_hir_id_owner(item.id, |lctx| { let hir_item = lctx.lower_trait_item(item); let id = hir::TraitItemId { hir_id: hir_item.hir_id }; @@ -97,7 +97,7 @@ impl<'tcx, 'interner> Visitor<'tcx> for ItemLowerer<'tcx, 'interner> { visit::walk_assoc_item(self, item); } - fn visit_impl_item(&mut self, item: &'tcx ImplItem) { + fn visit_impl_item(&mut self, item: &'tcx AssocItem) { self.lctx.with_hir_id_owner(item.id, |lctx| { let hir_item = lctx.lower_impl_item(item); let id = hir::ImplItemId { hir_id: hir_item.hir_id }; @@ -813,11 +813,11 @@ impl LoweringContext<'_> { } } - fn lower_trait_item(&mut self, i: &TraitItem) -> hir::TraitItem { + fn lower_trait_item(&mut self, i: &AssocItem) -> hir::TraitItem { let trait_item_def_id = self.resolver.definitions().local_def_id(i.id); let (generics, kind) = match i.kind { - TraitItemKind::Const(ref ty, ref default) => ( + AssocItemKind::Const(ref ty, ref default) => ( self.lower_generics(&i.generics, ImplTraitContext::disallowed()), hir::TraitItemKind::Const( self.lower_ty(ty, ImplTraitContext::disallowed()), @@ -826,7 +826,7 @@ impl LoweringContext<'_> { .map(|x| self.lower_const_body(i.span, Some(x))), ), ), - TraitItemKind::Method(ref sig, None) => { + AssocItemKind::Method(ref sig, None) => { let names = self.lower_fn_params_to_names(&sig.decl); let (generics, sig) = self.lower_method_sig( &i.generics, @@ -837,7 +837,7 @@ impl LoweringContext<'_> { ); (generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Required(names))) } - TraitItemKind::Method(ref sig, Some(ref body)) => { + AssocItemKind::Method(ref sig, Some(ref body)) => { let body_id = self.lower_fn_body_block(i.span, &sig.decl, Some(body)); let (generics, sig) = self.lower_method_sig( &i.generics, @@ -848,7 +848,7 @@ impl LoweringContext<'_> { ); (generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Provided(body_id))) } - TraitItemKind::TyAlias(ref bounds, ref default) => { + AssocItemKind::TyAlias(ref bounds, ref default) => { let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed()); let kind = hir::TraitItemKind::Type( self.lower_param_bounds(bounds, ImplTraitContext::disallowed()), @@ -859,7 +859,7 @@ impl LoweringContext<'_> { (generics, kind) }, - TraitItemKind::Macro(..) => bug!("macro item shouldn't exist at this point"), + AssocItemKind::Macro(..) => bug!("macro item shouldn't exist at this point"), }; hir::TraitItem { @@ -872,21 +872,21 @@ impl LoweringContext<'_> { } } - fn lower_trait_item_ref(&mut self, i: &TraitItem) -> hir::TraitItemRef { + fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef { let (kind, has_default) = match i.kind { - TraitItemKind::Const(_, ref default) => { + AssocItemKind::Const(_, ref default) => { (hir::AssocItemKind::Const, default.is_some()) } - TraitItemKind::TyAlias(_, ref default) => { + AssocItemKind::TyAlias(_, ref default) => { (hir::AssocItemKind::Type, default.is_some()) } - TraitItemKind::Method(ref sig, ref default) => ( + AssocItemKind::Method(ref sig, ref default) => ( hir::AssocItemKind::Method { has_self: sig.decl.has_self(), }, default.is_some(), ), - TraitItemKind::Macro(..) => unimplemented!(), + AssocItemKind::Macro(..) => unimplemented!(), }; hir::TraitItemRef { id: hir::TraitItemId { hir_id: self.lower_node_id(i.id) }, @@ -902,18 +902,18 @@ impl LoweringContext<'_> { self.expr(span, hir::ExprKind::Err, ThinVec::new()) } - fn lower_impl_item(&mut self, i: &ImplItem) -> hir::ImplItem { + fn lower_impl_item(&mut self, i: &AssocItem) -> hir::ImplItem { let impl_item_def_id = self.resolver.definitions().local_def_id(i.id); let (generics, kind) = match i.kind { - ImplItemKind::Const(ref ty, ref expr) => ( + AssocItemKind::Const(ref ty, ref expr) => ( self.lower_generics(&i.generics, ImplTraitContext::disallowed()), hir::ImplItemKind::Const( self.lower_ty(ty, ImplTraitContext::disallowed()), self.lower_const_body(i.span, expr.as_deref()), ), ), - ImplItemKind::Method(ref sig, ref body) => { + AssocItemKind::Method(ref sig, ref body) => { self.current_item = Some(i.span); let body_id = self.lower_maybe_async_body( i.span, @@ -932,7 +932,7 @@ impl LoweringContext<'_> { (generics, hir::ImplItemKind::Method(sig, body_id)) } - ImplItemKind::TyAlias(_, ref ty) => { + AssocItemKind::TyAlias(_, ref ty) => { let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed()); let kind = match ty { None => { @@ -951,7 +951,7 @@ impl LoweringContext<'_> { }; (generics, kind) }, - ImplItemKind::Macro(..) => bug!("`TyMac` should have been expanded by now"), + AssocItemKind::Macro(..) => bug!("`TyMac` should have been expanded by now"), }; hir::ImplItem { @@ -968,7 +968,7 @@ impl LoweringContext<'_> { // [1] since `default impl` is not yet implemented, this is always true in impls } - fn lower_impl_item_ref(&mut self, i: &ImplItem) -> hir::ImplItemRef { + fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef { hir::ImplItemRef { id: hir::ImplItemId { hir_id: self.lower_node_id(i.id) }, ident: i.ident, @@ -976,18 +976,18 @@ impl LoweringContext<'_> { vis: self.lower_visibility(&i.vis, Some(i.id)), defaultness: self.lower_defaultness(i.defaultness, true /* [1] */), kind: match &i.kind { - ImplItemKind::Const(..) => hir::AssocItemKind::Const, - ImplItemKind::TyAlias(_, ty) => match ty + AssocItemKind::Const(..) => hir::AssocItemKind::Const, + AssocItemKind::TyAlias(_, ty) => match ty .as_deref() .and_then(|ty| ty.kind.opaque_top_hack()) { None => hir::AssocItemKind::Type, Some(_) => hir::AssocItemKind::OpaqueTy, }, - ImplItemKind::Method(sig, _) => hir::AssocItemKind::Method { + AssocItemKind::Method(sig, _) => hir::AssocItemKind::Method { has_self: sig.decl.has_self(), }, - ImplItemKind::Macro(..) => unimplemented!(), + AssocItemKind::Macro(..) => unimplemented!(), }, } diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index 7f72154e42c..0fdd509a0bb 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -1249,7 +1249,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> ast_visit::walk_poly_trait_ref(self, t, m); } - fn visit_trait_item(&mut self, trait_item: &'a ast::TraitItem) { + fn visit_trait_item(&mut self, trait_item: &'a ast::AssocItem) { self.with_lint_attrs(trait_item.id, &trait_item.attrs, |cx| { run_early_pass!(cx, check_trait_item, trait_item); ast_visit::walk_trait_item(cx, trait_item); @@ -1257,7 +1257,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> }); } - fn visit_impl_item(&mut self, impl_item: &'a ast::ImplItem) { + fn visit_impl_item(&mut self, impl_item: &'a ast::AssocItem) { self.with_lint_attrs(impl_item.id, &impl_item.attrs, |cx| { run_early_pass!(cx, check_impl_item, impl_item); ast_visit::walk_impl_item(cx, impl_item); diff --git a/src/librustc/lint/mod.rs b/src/librustc/lint/mod.rs index a8d88686679..0054f149f8c 100644 --- a/src/librustc/lint/mod.rs +++ b/src/librustc/lint/mod.rs @@ -258,10 +258,10 @@ macro_rules! early_lint_methods { c: Span, d: ast::NodeId ); - fn check_trait_item(a: &ast::TraitItem); - fn check_trait_item_post(a: &ast::TraitItem); - fn check_impl_item(a: &ast::ImplItem); - fn check_impl_item_post(a: &ast::ImplItem); + fn check_trait_item(a: &ast::AssocItem); + fn check_trait_item_post(a: &ast::AssocItem); + fn check_impl_item(a: &ast::AssocItem); + fn check_impl_item_post(a: &ast::AssocItem); fn check_struct_def(a: &ast::VariantData); fn check_struct_def_post(a: &ast::VariantData); fn check_struct_field(a: &ast::StructField); diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index 026cb6eab1c..da8eae6028e 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -776,22 +776,17 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> { self.run(is_const, |s| noop_visit_item_kind(i, s)) } - fn flat_map_trait_item(&mut self, i: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> { + fn flat_map_trait_item(&mut self, i: ast::AssocItem) -> SmallVec<[ast::AssocItem; 1]> { let is_const = match i.kind { - ast::TraitItemKind::Const(..) => true, - ast::TraitItemKind::Method(ref sig, _) => Self::is_sig_const(sig), + ast::AssocItemKind::Const(..) => true, + ast::AssocItemKind::Method(ref sig, _) => Self::is_sig_const(sig), _ => false, }; self.run(is_const, |s| noop_flat_map_assoc_item(i, s)) } - fn flat_map_impl_item(&mut self, i: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> { - let is_const = match i.kind { - ast::ImplItemKind::Const(..) => true, - ast::ImplItemKind::Method(ref sig, _) => Self::is_sig_const(sig), - _ => false, - }; - self.run(is_const, |s| noop_flat_map_assoc_item(i, s)) + fn flat_map_impl_item(&mut self, i: ast::AssocItem) -> SmallVec<[ast::AssocItem; 1]> { + self.flat_map_trait_item(i) } fn visit_anon_const(&mut self, c: &mut ast::AnonConst) { diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 10b00d35d9b..8916fc08838 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -268,8 +268,8 @@ impl EarlyLintPass for UnsafeCode { } } - fn check_trait_item(&mut self, cx: &EarlyContext<'_>, item: &ast::TraitItem) { - if let ast::TraitItemKind::Method(ref sig, None) = item.kind { + fn check_trait_item(&mut self, cx: &EarlyContext<'_>, item: &ast::AssocItem) { + if let ast::AssocItemKind::Method(ref sig, None) = item.kind { if sig.header.unsafety == ast::Unsafety::Unsafe { self.report_unsafe(cx, item.span, "declaration of an `unsafe` method") } @@ -615,9 +615,9 @@ declare_lint_pass!( ); impl EarlyLintPass for AnonymousParameters { - fn check_trait_item(&mut self, cx: &EarlyContext<'_>, it: &ast::TraitItem) { + fn check_trait_item(&mut self, cx: &EarlyContext<'_>, it: &ast::AssocItem) { match it.kind { - ast::TraitItemKind::Method(ref sig, _) => { + ast::AssocItemKind::Method(ref sig, _) => { for arg in sig.decl.inputs.iter() { match arg.pat.kind { ast::PatKind::Ident(_, ident, None) => { diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index c75bd996e10..609885cb254 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -544,7 +544,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } for impl_item in impl_items { self.invalid_visibility(&impl_item.vis, None); - if let ImplItemKind::Method(ref sig, _) = impl_item.kind { + if let AssocItemKind::Method(ref sig, _) = impl_item.kind { self.check_trait_fn_not_const(sig.header.constness); self.check_trait_fn_not_async(impl_item.span, sig.header.asyncness.node); } diff --git a/src/librustc_passes/hir_stats.rs b/src/librustc_passes/hir_stats.rs index a5924efefc2..66ceb4212c8 100644 --- a/src/librustc_passes/hir_stats.rs +++ b/src/librustc_passes/hir_stats.rs @@ -314,12 +314,12 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> { ast_visit::walk_fn(self, fk, fd, s) } - fn visit_trait_item(&mut self, ti: &'v ast::TraitItem) { + fn visit_trait_item(&mut self, ti: &'v ast::AssocItem) { self.record("TraitItem", Id::None, ti); ast_visit::walk_trait_item(self, ti) } - fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) { + fn visit_impl_item(&mut self, ii: &'v ast::AssocItem) { self.record("ImplItem", Id::None, ii); ast_visit::walk_impl_item(self, ii) } diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index d2d5a33ec7a..dc301375440 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -30,7 +30,7 @@ use errors::Applicability; use syntax::ast::{Name, Ident}; use syntax::attr; use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind, NodeId}; -use syntax::ast::{MetaItemKind, StmtKind, TraitItem, TraitItemKind}; +use syntax::ast::{MetaItemKind, StmtKind, AssocItem, AssocItemKind}; use syntax::token::{self, Token}; use syntax::span_err; use syntax::source_map::{respan, Spanned}; @@ -1164,10 +1164,10 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> { self.parent_scope.legacy = orig_current_legacy_scope; } - fn visit_trait_item(&mut self, item: &'b TraitItem) { + fn visit_trait_item(&mut self, item: &'b AssocItem) { let parent = self.parent_scope.module; - if let TraitItemKind::Macro(_) = item.kind { + if let AssocItemKind::Macro(_) = item.kind { self.visit_invoc(item.id); return } @@ -1175,15 +1175,15 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> { // Add the item to the trait info. let item_def_id = self.r.definitions.local_def_id(item.id); let (res, ns) = match item.kind { - TraitItemKind::Const(..) => (Res::Def(DefKind::AssocConst, item_def_id), ValueNS), - TraitItemKind::Method(ref sig, _) => { + AssocItemKind::Const(..) => (Res::Def(DefKind::AssocConst, item_def_id), ValueNS), + AssocItemKind::Method(ref sig, _) => { if sig.decl.has_self() { self.r.has_self.insert(item_def_id); } (Res::Def(DefKind::Method, item_def_id), ValueNS) } - TraitItemKind::TyAlias(..) => (Res::Def(DefKind::AssocTy, item_def_id), TypeNS), - TraitItemKind::Macro(_) => bug!(), // handled above + AssocItemKind::TyAlias(..) => (Res::Def(DefKind::AssocTy, item_def_id), TypeNS), + AssocItemKind::Macro(_) => bug!(), // handled above }; let vis = ty::Visibility::Public; @@ -1193,8 +1193,8 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> { visit::walk_trait_item(self, item); } - fn visit_impl_item(&mut self, item: &'b ast::ImplItem) { - if let ast::ImplItemKind::Macro(..) = item.kind { + fn visit_impl_item(&mut self, item: &'b ast::AssocItem) { + if let ast::AssocItemKind::Macro(..) = item.kind { self.visit_invoc(item.id); } else { self.resolve_visibility(&item.vis); diff --git a/src/librustc_resolve/def_collector.rs b/src/librustc_resolve/def_collector.rs index 471e2634b8a..6e26553d82f 100644 --- a/src/librustc_resolve/def_collector.rs +++ b/src/librustc_resolve/def_collector.rs @@ -212,23 +212,23 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { visit::walk_generic_param(self, param); } - fn visit_trait_item(&mut self, ti: &'a TraitItem) { + fn visit_trait_item(&mut self, ti: &'a AssocItem) { let def_data = match ti.kind { - TraitItemKind::Method(..) | TraitItemKind::Const(..) => + AssocItemKind::Method(..) | AssocItemKind::Const(..) => DefPathData::ValueNs(ti.ident.name), - TraitItemKind::TyAlias(..) => { + AssocItemKind::TyAlias(..) => { DefPathData::TypeNs(ti.ident.name) }, - TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id), + AssocItemKind::Macro(..) => return self.visit_macro_invoc(ti.id), }; let def = self.create_def(ti.id, def_data, ti.span); self.with_parent(def, |this| visit::walk_trait_item(this, ti)); } - fn visit_impl_item(&mut self, ii: &'a ImplItem) { + fn visit_impl_item(&mut self, ii: &'a AssocItem) { let def_data = match ii.kind { - ImplItemKind::Method(FnSig { + AssocItemKind::Method(FnSig { ref header, ref decl, }, ref body) if header.asyncness.node.is_async() => { @@ -242,10 +242,10 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { body.as_deref(), ) } - ImplItemKind::Method(..) | - ImplItemKind::Const(..) => DefPathData::ValueNs(ii.ident.name), - ImplItemKind::TyAlias(..) => DefPathData::TypeNs(ii.ident.name), - ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id), + AssocItemKind::Method(..) | + AssocItemKind::Const(..) => DefPathData::ValueNs(ii.ident.name), + AssocItemKind::TyAlias(..) => DefPathData::TypeNs(ii.ident.name), + AssocItemKind::Macro(..) => return self.visit_macro_invoc(ii.id), }; let def = self.create_def(ii.id, def_data, ii.span); diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index 682ddc421c2..ad29fd9e1db 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -806,7 +806,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { this.with_generic_param_rib(&trait_item.generics, AssocItemRibKind, |this| { match trait_item.kind { - TraitItemKind::Const(ref ty, ref default) => { + AssocItemKind::Const(ref ty, ref default) => { this.visit_ty(ty); // Only impose the restrictions of @@ -818,13 +818,13 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { }); } } - TraitItemKind::Method(_, _) => { + AssocItemKind::Method(_, _) => { visit::walk_assoc_item(this, trait_item) } - TraitItemKind::TyAlias(..) => { + AssocItemKind::TyAlias(..) => { visit::walk_assoc_item(this, trait_item) } - TraitItemKind::Macro(_) => { + AssocItemKind::Macro(_) => { panic!("unexpanded macro in resolve!") } }; @@ -989,13 +989,13 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { /// When evaluating a `trait` use its associated types' idents for suggestionsa in E0412. fn with_trait_items( &mut self, - trait_items: &Vec, + trait_items: &Vec, f: impl FnOnce(&mut Self) -> T, ) -> T { let trait_assoc_types = replace( &mut self.diagnostic_metadata.current_trait_assoc_types, trait_items.iter().filter_map(|item| match &item.kind { - TraitItemKind::TyAlias(bounds, _) if bounds.len() == 0 => Some(item.ident), + AssocItemKind::TyAlias(bounds, _) if bounds.len() == 0 => Some(item.ident), _ => None, }).collect(), ); @@ -1063,7 +1063,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { opt_trait_reference: &Option, self_type: &Ty, item_id: NodeId, - impl_items: &[ImplItem]) { + impl_items: &[AssocItem]) { debug!("resolve_implementation"); // If applicable, create a rib for the type parameters. self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| { @@ -1092,9 +1092,9 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { |this| { use crate::ResolutionError::*; match impl_item.kind { - ImplItemKind::Const(..) => { + AssocItemKind::Const(..) => { debug!( - "resolve_implementation ImplItemKind::Const", + "resolve_implementation AssocItemKind::Const", ); // If this is a trait impl, ensure the const // exists in trait @@ -1109,7 +1109,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { visit::walk_assoc_item(this, impl_item) }); } - ImplItemKind::Method(..) => { + AssocItemKind::Method(..) => { // If this is a trait impl, ensure the method // exists in trait this.check_trait_item(impl_item.ident, @@ -1119,7 +1119,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { visit::walk_assoc_item(this, impl_item); } - ImplItemKind::TyAlias(_, Some(ref ty)) => { + AssocItemKind::TyAlias(_, Some(ref ty)) => { // If this is a trait impl, ensure the type // exists in trait this.check_trait_item(impl_item.ident, @@ -1129,8 +1129,8 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { this.visit_ty(ty); } - ImplItemKind::TyAlias(_, None) => {} - ImplItemKind::Macro(_) => + AssocItemKind::TyAlias(_, None) => {} + AssocItemKind::Macro(_) => panic!("unexpanded macro in resolve!"), } }); diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index cc0f3c512f5..3b36c1c70f9 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -676,7 +676,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { generics: &'l ast::Generics, trait_ref: &'l Option, typ: &'l ast::Ty, - impl_items: &'l [ast::ImplItem], + impl_items: &'l [ast::AssocItem], ) { if let Some(impl_data) = self.save_ctxt.get_item_data(item) { if !self.span.filter_generated(item.span) { @@ -707,7 +707,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { item: &'l ast::Item, generics: &'l ast::Generics, trait_refs: &'l ast::GenericBounds, - methods: &'l [ast::TraitItem], + methods: &'l [ast::AssocItem], ) { let name = item.ident.to_string(); let qualname = format!("::{}", @@ -1029,11 +1029,11 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { // } } - fn process_trait_item(&mut self, trait_item: &'l ast::TraitItem, trait_id: DefId) { + fn process_trait_item(&mut self, trait_item: &'l ast::AssocItem, trait_id: DefId) { self.process_macro_use(trait_item.span); let vis_span = trait_item.span.shrink_to_lo(); match trait_item.kind { - ast::TraitItemKind::Const(ref ty, ref expr) => { + ast::AssocItemKind::Const(ref ty, ref expr) => { self.process_assoc_const( trait_item.id, trait_item.ident, @@ -1044,7 +1044,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { &trait_item.attrs, ); } - ast::TraitItemKind::Method(ref sig, ref body) => { + ast::AssocItemKind::Method(ref sig, ref body) => { self.process_method( sig, body.as_ref().map(|x| &**x), @@ -1055,7 +1055,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { trait_item.span, ); } - ast::TraitItemKind::TyAlias(ref bounds, ref default_ty) => { + ast::AssocItemKind::TyAlias(ref bounds, ref default_ty) => { // FIXME do something with _bounds (for type refs) let name = trait_item.ident.name.to_string(); let qualname = format!("::{}", @@ -1097,14 +1097,14 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { self.visit_ty(default_ty) } } - ast::TraitItemKind::Macro(_) => {} + ast::AssocItemKind::Macro(_) => {} } } - fn process_impl_item(&mut self, impl_item: &'l ast::ImplItem, impl_id: DefId) { + fn process_impl_item(&mut self, impl_item: &'l ast::AssocItem, impl_id: DefId) { self.process_macro_use(impl_item.span); match impl_item.kind { - ast::ImplItemKind::Const(ref ty, ref expr) => { + ast::AssocItemKind::Const(ref ty, ref expr) => { self.process_assoc_const( impl_item.id, impl_item.ident, @@ -1115,7 +1115,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { &impl_item.attrs, ); } - ast::ImplItemKind::Method(ref sig, ref body) => { + ast::AssocItemKind::Method(ref sig, ref body) => { self.process_method( sig, body.as_deref(), @@ -1126,14 +1126,14 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { impl_item.span, ); } - ast::ImplItemKind::TyAlias(_, None) => {} - ast::ImplItemKind::TyAlias(_, Some(ref ty)) => { + ast::AssocItemKind::TyAlias(_, None) => {} + ast::AssocItemKind::TyAlias(_, Some(ref ty)) => { // FIXME: uses of the assoc type should ideally point to this // 'def' and the name here should be a ref to the def in the // trait. self.visit_ty(ty) } - ast::ImplItemKind::Macro(_) => {} + ast::AssocItemKind::Macro(_) => {} } } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index d90d74d7a26..1b729ebaf43 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1603,16 +1603,10 @@ pub struct FnSig { pub decl: P, } -// FIXME(Centril): Remove all of these. -pub type TraitItem = AssocItem; -pub type TraitItemKind = AssocItemKind; -pub type ImplItem = AssocItem; -pub type ImplItemKind = AssocItemKind; - /// Represents associated items. /// These include items in `impl` and `trait` definitions. #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] -pub struct AssocItem { +pub struct AssocItem { pub attrs: Vec, pub id: NodeId, pub span: Span, @@ -1621,7 +1615,7 @@ pub struct AssocItem { pub defaultness: Defaultness, pub generics: Generics, - pub kind: K, + pub kind: AssocItemKind, /// See `Item::tokens` for what this is. pub tokens: Option, } @@ -2598,7 +2592,7 @@ pub enum ItemKind { /// A trait declaration (`trait`). /// /// E.g., `trait Foo { .. }`, `trait Foo { .. }` or `auto trait Foo {}`. - Trait(IsAuto, Unsafety, Generics, GenericBounds, Vec), + Trait(IsAuto, Unsafety, Generics, GenericBounds, Vec), /// Trait alias /// /// E.g., `trait Foo = Bar + Quux;`. @@ -2613,7 +2607,7 @@ pub enum ItemKind { Generics, Option, // (optional) trait this impl implements P, // self - Vec, + Vec, ), /// A macro invocation. /// diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index c27fcb6a68e..fcce9d4e95f 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -571,9 +571,9 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { visit::walk_assoc_ty_constraint(self, constraint) } - fn visit_trait_item(&mut self, ti: &'a ast::TraitItem) { + fn visit_trait_item(&mut self, ti: &'a ast::AssocItem) { match ti.kind { - ast::TraitItemKind::Method(ref sig, ref block) => { + ast::AssocItemKind::Method(ref sig, ref block) => { if block.is_none() { self.check_extern(sig.header.ext); } @@ -581,7 +581,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { gate_feature_post!(&self, const_fn, ti.span, "const fn is unstable"); } } - ast::TraitItemKind::TyAlias(_, ref default) => { + ast::AssocItemKind::TyAlias(_, ref default) => { if let Some(_) = default { gate_feature_post!( &self, associated_type_defaults, ti.span, diff --git a/src/libsyntax/token.rs b/src/libsyntax/token.rs index 6f45211ac5f..263f8192241 100644 --- a/src/libsyntax/token.rs +++ b/src/libsyntax/token.rs @@ -685,8 +685,8 @@ pub enum Nonterminal { // Used only for passing items to proc macro attributes (they are not // strictly necessary for that, `Annotatable` can be converted into // tokens directly, but doing that naively regresses pretty-printing). - NtTraitItem(ast::TraitItem), - NtImplItem(ast::ImplItem), + NtTraitItem(ast::AssocItem), + NtImplItem(ast::AssocItem), NtForeignItem(ast::ForeignItem), } diff --git a/src/libsyntax_expand/base.rs b/src/libsyntax_expand/base.rs index a4449ca5b1d..2cfcbd3161f 100644 --- a/src/libsyntax_expand/base.rs +++ b/src/libsyntax_expand/base.rs @@ -31,8 +31,8 @@ crate use syntax_pos::hygiene::MacroKind; #[derive(Debug,Clone)] pub enum Annotatable { Item(P), - TraitItem(P), - ImplItem(P), + TraitItem(P), + ImplItem(P), ForeignItem(P), Stmt(P), Expr(P), @@ -137,14 +137,14 @@ impl Annotatable { } } - pub fn expect_trait_item(self) -> ast::TraitItem { + pub fn expect_trait_item(self) -> ast::AssocItem { match self { Annotatable::TraitItem(i) => i.into_inner(), _ => panic!("expected Item") } } - pub fn expect_impl_item(self) -> ast::ImplItem { + pub fn expect_impl_item(self) -> ast::AssocItem { match self { Annotatable::ImplItem(i) => i.into_inner(), _ => panic!("expected Item") @@ -382,12 +382,12 @@ pub trait MacResult { } /// Creates zero or more impl items. - fn make_impl_items(self: Box) -> Option> { + fn make_impl_items(self: Box) -> Option> { None } /// Creates zero or more trait items. - fn make_trait_items(self: Box) -> Option> { + fn make_trait_items(self: Box) -> Option> { None } @@ -468,8 +468,8 @@ make_MacEager! { expr: P, pat: P, items: SmallVec<[P; 1]>, - impl_items: SmallVec<[ast::ImplItem; 1]>, - trait_items: SmallVec<[ast::TraitItem; 1]>, + impl_items: SmallVec<[ast::AssocItem; 1]>, + trait_items: SmallVec<[ast::AssocItem; 1]>, foreign_items: SmallVec<[ast::ForeignItem; 1]>, stmts: SmallVec<[ast::Stmt; 1]>, ty: P, @@ -484,11 +484,11 @@ impl MacResult for MacEager { self.items } - fn make_impl_items(self: Box) -> Option> { + fn make_impl_items(self: Box) -> Option> { self.impl_items } - fn make_trait_items(self: Box) -> Option> { + fn make_trait_items(self: Box) -> Option> { self.trait_items } @@ -588,11 +588,11 @@ impl MacResult for DummyResult { Some(SmallVec::new()) } - fn make_impl_items(self: Box) -> Option> { + fn make_impl_items(self: Box) -> Option> { Some(SmallVec::new()) } - fn make_trait_items(self: Box) -> Option> { + fn make_trait_items(self: Box) -> Option> { Some(SmallVec::new()) } diff --git a/src/libsyntax_expand/expand.rs b/src/libsyntax_expand/expand.rs index 2eae6d494d0..d4223af27ab 100644 --- a/src/libsyntax_expand/expand.rs +++ b/src/libsyntax_expand/expand.rs @@ -155,10 +155,10 @@ ast_fragments! { Items(SmallVec<[P; 1]>) { "item"; many fn flat_map_item; fn visit_item; fn make_items; } - TraitItems(SmallVec<[ast::TraitItem; 1]>) { + TraitItems(SmallVec<[ast::AssocItem; 1]>) { "trait item"; many fn flat_map_trait_item; fn visit_trait_item; fn make_trait_items; } - ImplItems(SmallVec<[ast::ImplItem; 1]>) { + ImplItems(SmallVec<[ast::AssocItem; 1]>) { "impl item"; many fn flat_map_impl_item; fn visit_impl_item; fn make_impl_items; } ForeignItems(SmallVec<[ast::ForeignItem; 1]>) { diff --git a/src/libsyntax_expand/placeholders.rs b/src/libsyntax_expand/placeholders.rs index 18acbf2cc13..22e99baae5b 100644 --- a/src/libsyntax_expand/placeholders.rs +++ b/src/libsyntax_expand/placeholders.rs @@ -50,15 +50,15 @@ pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId, vis: Option AstFragment::TraitItems(smallvec![ast::TraitItem { + AstFragmentKind::TraitItems => AstFragment::TraitItems(smallvec![ast::AssocItem { id, span, ident, vis, attrs, generics, - kind: ast::TraitItemKind::Macro(mac_placeholder()), + kind: ast::AssocItemKind::Macro(mac_placeholder()), defaultness: ast::Defaultness::Final, tokens: None, }]), - AstFragmentKind::ImplItems => AstFragment::ImplItems(smallvec![ast::ImplItem { + AstFragmentKind::ImplItems => AstFragment::ImplItems(smallvec![ast::AssocItem { id, span, ident, vis, attrs, generics, - kind: ast::ImplItemKind::Macro(mac_placeholder()), + kind: ast::AssocItemKind::Macro(mac_placeholder()), defaultness: ast::Defaultness::Final, tokens: None, }]), diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index d51fcf315a6..1c1fcc4f301 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -504,13 +504,13 @@ impl<'a> TraitDef<'a> { type_ident: Ident, generics: &Generics, field_tys: Vec>, - methods: Vec) + methods: Vec) -> P { let trait_path = self.path.to_path(cx, self.span, type_ident, generics); - // Transform associated types from `deriving::ty::Ty` into `ast::ImplItem` + // Transform associated types from `deriving::ty::Ty` into `ast::AssocItem` let associated_types = self.associated_types.iter().map(|&(ident, ref type_def)| { - ast::ImplItem { + ast::AssocItem { id: ast::DUMMY_NODE_ID, span: self.span, ident, @@ -518,7 +518,7 @@ impl<'a> TraitDef<'a> { defaultness: ast::Defaultness::Final, attrs: Vec::new(), generics: Generics::default(), - kind: ast::ImplItemKind::TyAlias( + kind: ast::AssocItemKind::TyAlias( Vec::new(), Some(type_def.to_ty(cx, self.span, type_ident, generics)), ), @@ -912,7 +912,7 @@ impl<'a> MethodDef<'a> { explicit_self: Option, arg_types: Vec<(Ident, P)>, body: P) - -> ast::ImplItem { + -> ast::AssocItem { // Create the generics that aren't for `Self`. let fn_generics = self.generics.to_generics(cx, trait_.span, type_ident, generics); @@ -950,7 +950,7 @@ impl<'a> MethodDef<'a> { }; // Create the method. - ast::ImplItem { + ast::AssocItem { id: ast::DUMMY_NODE_ID, attrs: self.attributes.clone(), generics: fn_generics, @@ -958,7 +958,7 @@ impl<'a> MethodDef<'a> { vis: respan(trait_lo_sp, ast::VisibilityKind::Inherited), defaultness: ast::Defaultness::Final, ident: method_ident, - kind: ast::ImplItemKind::Method(sig, Some(body_block)), + kind: ast::AssocItemKind::Method(sig, Some(body_block)), tokens: None, } } -- cgit 1.4.1-3-g733a5 From e52f902a8ab3a1abbb200607db4766d95b27bc8e Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 8 Dec 2019 00:13:59 +0100 Subject: `AssocImplKind::{Method -> Fn}`. --- src/librustc/hir/lowering.rs | 2 +- src/librustc/hir/lowering/item.rs | 10 +++++----- src/librustc_interface/util.rs | 2 +- src/librustc_lint/builtin.rs | 4 ++-- src/librustc_parse/parser/item.rs | 2 +- src/librustc_passes/ast_validation.rs | 8 ++++---- src/librustc_resolve/build_reduced_graph.rs | 2 +- src/librustc_resolve/def_collector.rs | 11 ++++------- src/librustc_resolve/late.rs | 4 ++-- src/librustc_save_analysis/dump_visitor.rs | 4 ++-- src/libsyntax/ast.rs | 3 +-- src/libsyntax/feature_gate/check.rs | 4 ++-- src/libsyntax/mut_visit.rs | 2 +- src/libsyntax/print/pprust.rs | 2 +- src/libsyntax/visit.rs | 4 ++-- src/libsyntax_ext/deriving/generic/mod.rs | 2 +- 16 files changed, 31 insertions(+), 35 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 78df14adc5d..50733512fae 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -481,7 +481,7 @@ impl<'a> LoweringContext<'a> { self.lctx.allocate_hir_id_counter(item.id); match item.kind { - AssocItemKind::Method(_, None) => { + AssocItemKind::Fn(_, None) => { // Ignore patterns in trait methods without bodies self.with_hir_id_owner(None, |this| { visit::walk_trait_item(this, item) diff --git a/src/librustc/hir/lowering/item.rs b/src/librustc/hir/lowering/item.rs index f25714f741b..f0543b9057d 100644 --- a/src/librustc/hir/lowering/item.rs +++ b/src/librustc/hir/lowering/item.rs @@ -826,7 +826,7 @@ impl LoweringContext<'_> { .map(|x| self.lower_const_body(i.span, Some(x))), ), ), - AssocItemKind::Method(ref sig, None) => { + AssocItemKind::Fn(ref sig, None) => { let names = self.lower_fn_params_to_names(&sig.decl); let (generics, sig) = self.lower_method_sig( &i.generics, @@ -837,7 +837,7 @@ impl LoweringContext<'_> { ); (generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Required(names))) } - AssocItemKind::Method(ref sig, Some(ref body)) => { + AssocItemKind::Fn(ref sig, Some(ref body)) => { let body_id = self.lower_fn_body_block(i.span, &sig.decl, Some(body)); let (generics, sig) = self.lower_method_sig( &i.generics, @@ -880,7 +880,7 @@ impl LoweringContext<'_> { AssocItemKind::TyAlias(_, ref default) => { (hir::AssocItemKind::Type, default.is_some()) } - AssocItemKind::Method(ref sig, ref default) => ( + AssocItemKind::Fn(ref sig, ref default) => ( hir::AssocItemKind::Method { has_self: sig.decl.has_self(), }, @@ -913,7 +913,7 @@ impl LoweringContext<'_> { self.lower_const_body(i.span, expr.as_deref()), ), ), - AssocItemKind::Method(ref sig, ref body) => { + AssocItemKind::Fn(ref sig, ref body) => { self.current_item = Some(i.span); let body_id = self.lower_maybe_async_body( i.span, @@ -984,7 +984,7 @@ impl LoweringContext<'_> { None => hir::AssocItemKind::Type, Some(_) => hir::AssocItemKind::OpaqueTy, }, - AssocItemKind::Method(sig, _) => hir::AssocItemKind::Method { + AssocItemKind::Fn(sig, _) => hir::AssocItemKind::Method { has_self: sig.decl.has_self(), }, AssocItemKind::Macro(..) => unimplemented!(), diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index da8eae6028e..e84ac82942c 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -779,7 +779,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> { fn flat_map_trait_item(&mut self, i: ast::AssocItem) -> SmallVec<[ast::AssocItem; 1]> { let is_const = match i.kind { ast::AssocItemKind::Const(..) => true, - ast::AssocItemKind::Method(ref sig, _) => Self::is_sig_const(sig), + ast::AssocItemKind::Fn(ref sig, _) => Self::is_sig_const(sig), _ => false, }; self.run(is_const, |s| noop_flat_map_assoc_item(i, s)) diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 8916fc08838..1fc89961889 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -269,7 +269,7 @@ impl EarlyLintPass for UnsafeCode { } fn check_trait_item(&mut self, cx: &EarlyContext<'_>, item: &ast::AssocItem) { - if let ast::AssocItemKind::Method(ref sig, None) = item.kind { + if let ast::AssocItemKind::Fn(ref sig, None) = item.kind { if sig.header.unsafety == ast::Unsafety::Unsafe { self.report_unsafe(cx, item.span, "declaration of an `unsafe` method") } @@ -617,7 +617,7 @@ declare_lint_pass!( impl EarlyLintPass for AnonymousParameters { fn check_trait_item(&mut self, cx: &EarlyContext<'_>, it: &ast::AssocItem) { match it.kind { - ast::AssocItemKind::Method(ref sig, _) => { + ast::AssocItemKind::Fn(ref sig, _) => { for arg in sig.decl.inputs.iter() { match arg.pat.kind { ast::PatKind::Ident(_, ident, None) => { diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index a7c98886622..0840a1551db 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -1790,7 +1790,7 @@ impl<'a> Parser<'a> { })?; let sig = FnSig { header, decl }; let body = self.parse_assoc_fn_body(at_end, attrs)?; - Ok((ident, AssocItemKind::Method(sig, body), generics)) + Ok((ident, AssocItemKind::Fn(sig, body), generics)) } /// Parse the "body" of a method in an associated item definition. diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index 609885cb254..e90231f984b 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -544,7 +544,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } for impl_item in impl_items { self.invalid_visibility(&impl_item.vis, None); - if let AssocItemKind::Method(ref sig, _) = impl_item.kind { + if let AssocItemKind::Fn(ref sig, _) = impl_item.kind { self.check_trait_fn_not_const(sig.header.constness); self.check_trait_fn_not_async(impl_item.span, sig.header.asyncness.node); } @@ -795,7 +795,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { AssocItemKind::Const(_, body) => { self.check_impl_item_provided(ii.span, body, "constant", " = ;"); } - AssocItemKind::Method(sig, body) => { + AssocItemKind::Fn(sig, body) => { self.check_impl_item_provided(ii.span, body, "function", " { }"); self.check_fn_decl(&sig.decl); } @@ -812,7 +812,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.invalid_visibility(&ti.vis, None); self.check_defaultness(ti.span, ti.defaultness); - if let AssocItemKind::Method(sig, block) = &ti.kind { + if let AssocItemKind::Fn(sig, block) = &ti.kind { self.check_fn_decl(&sig.decl); self.check_trait_fn_not_async(ti.span, sig.header.asyncness.node); self.check_trait_fn_not_const(sig.header.constness); @@ -838,7 +838,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } fn visit_assoc_item(&mut self, item: &'a AssocItem) { - if let AssocItemKind::Method(sig, _) = &item.kind { + if let AssocItemKind::Fn(sig, _) = &item.kind { self.check_c_varadic_type(&sig.decl); } visit::walk_assoc_item(self, item); diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index dc301375440..e94e0dc695c 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -1176,7 +1176,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> { let item_def_id = self.r.definitions.local_def_id(item.id); let (res, ns) = match item.kind { AssocItemKind::Const(..) => (Res::Def(DefKind::AssocConst, item_def_id), ValueNS), - AssocItemKind::Method(ref sig, _) => { + AssocItemKind::Fn(ref sig, _) => { if sig.decl.has_self() { self.r.has_self.insert(item_def_id); } diff --git a/src/librustc_resolve/def_collector.rs b/src/librustc_resolve/def_collector.rs index 6e26553d82f..9bae339f80e 100644 --- a/src/librustc_resolve/def_collector.rs +++ b/src/librustc_resolve/def_collector.rs @@ -214,11 +214,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { fn visit_trait_item(&mut self, ti: &'a AssocItem) { let def_data = match ti.kind { - AssocItemKind::Method(..) | AssocItemKind::Const(..) => - DefPathData::ValueNs(ti.ident.name), - AssocItemKind::TyAlias(..) => { - DefPathData::TypeNs(ti.ident.name) - }, + AssocItemKind::Fn(..) | AssocItemKind::Const(..) => DefPathData::ValueNs(ti.ident.name), + AssocItemKind::TyAlias(..) => DefPathData::TypeNs(ti.ident.name), AssocItemKind::Macro(..) => return self.visit_macro_invoc(ti.id), }; @@ -228,7 +225,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { fn visit_impl_item(&mut self, ii: &'a AssocItem) { let def_data = match ii.kind { - AssocItemKind::Method(FnSig { + AssocItemKind::Fn(FnSig { ref header, ref decl, }, ref body) if header.asyncness.node.is_async() => { @@ -242,7 +239,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { body.as_deref(), ) } - AssocItemKind::Method(..) | + AssocItemKind::Fn(..) | AssocItemKind::Const(..) => DefPathData::ValueNs(ii.ident.name), AssocItemKind::TyAlias(..) => DefPathData::TypeNs(ii.ident.name), AssocItemKind::Macro(..) => return self.visit_macro_invoc(ii.id), diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index ad29fd9e1db..ec9c2a5b75d 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -818,7 +818,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { }); } } - AssocItemKind::Method(_, _) => { + AssocItemKind::Fn(_, _) => { visit::walk_assoc_item(this, trait_item) } AssocItemKind::TyAlias(..) => { @@ -1109,7 +1109,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { visit::walk_assoc_item(this, impl_item) }); } - AssocItemKind::Method(..) => { + AssocItemKind::Fn(..) => { // If this is a trait impl, ensure the method // exists in trait this.check_trait_item(impl_item.ident, diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index 3b36c1c70f9..df6ad51d104 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -1044,7 +1044,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { &trait_item.attrs, ); } - ast::AssocItemKind::Method(ref sig, ref body) => { + ast::AssocItemKind::Fn(ref sig, ref body) => { self.process_method( sig, body.as_ref().map(|x| &**x), @@ -1115,7 +1115,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { &impl_item.attrs, ); } - ast::AssocItemKind::Method(ref sig, ref body) => { + ast::AssocItemKind::Fn(ref sig, ref body) => { self.process_method( sig, body.as_deref(), diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 1b729ebaf43..f7f84333857 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1634,8 +1634,7 @@ pub enum AssocItemKind { Const(P, Option>), /// An associated function. - /// FIXME(Centril): Rename to `Fn`. - Method(FnSig, Option>), + Fn(FnSig, Option>), /// An associated type. TyAlias(GenericBounds, Option>), diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index fcce9d4e95f..871ec2c008e 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -573,7 +573,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { fn visit_trait_item(&mut self, ti: &'a ast::AssocItem) { match ti.kind { - ast::AssocItemKind::Method(ref sig, ref block) => { + ast::AssocItemKind::Fn(ref sig, ref block) => { if block.is_none() { self.check_extern(sig.header.ext); } @@ -600,7 +600,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } match ii.kind { - ast::AssocItemKind::Method(ref sig, _) => { + ast::AssocItemKind::Fn(ref sig, _) => { if sig.decl.c_variadic() { gate_feature_post!( &self, c_variadic, ii.span, diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 18d42f9dd66..2a6cff5971c 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -955,7 +955,7 @@ pub fn noop_flat_map_assoc_item(mut item: AssocItem, visitor: &mu visitor.visit_ty(ty); visit_opt(expr, |expr| visitor.visit_expr(expr)); } - AssocItemKind::Method(sig, body) => { + AssocItemKind::Fn(sig, body) => { visit_fn_sig(sig, visitor); visit_opt(body, |body| visitor.visit_block(body)); } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 9e4615f60c0..87f6ae85b69 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1528,7 +1528,7 @@ impl<'a> State<'a> { ast::AssocItemKind::Const(ty, expr) => { self.print_associated_const(item.ident, ty, expr.as_deref(), &item.vis); } - ast::AssocItemKind::Method(sig, body) => { + ast::AssocItemKind::Fn(sig, body) => { if body.is_some() { self.head(""); } diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index e2c0ee61467..51e7fa1eb38 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -600,11 +600,11 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem) visitor.visit_ty(ty); walk_list!(visitor, visit_expr, expr); } - AssocItemKind::Method(ref sig, None) => { + AssocItemKind::Fn(ref sig, None) => { visitor.visit_fn_header(&sig.header); walk_fn_decl(visitor, &sig.decl); } - AssocItemKind::Method(ref sig, Some(ref body)) => { + AssocItemKind::Fn(ref sig, Some(ref body)) => { visitor.visit_fn(FnKind::Method(item.ident, sig, &item.vis, body), &sig.decl, item.span, item.id); } diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index 1c1fcc4f301..b7707bfb8e5 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -958,7 +958,7 @@ impl<'a> MethodDef<'a> { vis: respan(trait_lo_sp, ast::VisibilityKind::Inherited), defaultness: ast::Defaultness::Final, ident: method_ident, - kind: ast::AssocItemKind::Method(sig, Some(body_block)), + kind: ast::AssocItemKind::Fn(sig, Some(body_block)), tokens: None, } } -- cgit 1.4.1-3-g733a5 From a74911662e8de2c024ea188e4dcac6a494c74455 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Mon, 2 Dec 2019 22:20:35 +0000 Subject: Fix comment ordering --- src/libsyntax/ast.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 92ba071a03d..274e19ec3e4 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -728,13 +728,13 @@ impl Mutability { #[derive(Clone, Copy, PartialEq, Eq, Debug)] #[derive(RustcEncodable, RustcDecodable, HashStable_Generic)] pub enum BorrowKind { - /// A raw borrow, `&raw const $expr` or `&raw mut $expr`. - /// The resulting type is either `*const T` or `*mut T` - /// where `T = typeof($expr)`. - Ref, /// A normal borrow, `&$expr` or `&mut $expr`. /// The resulting type is either `&'a T` or `&'a mut T` /// where `T = typeof($expr)` and `'a` is some lifetime. + Ref, + /// A raw borrow, `&raw const $expr` or `&raw mut $expr`. + /// The resulting type is either `*const T` or `*mut T` + /// where `T = typeof($expr)`. Raw, } -- cgit 1.4.1-3-g733a5