Skip to content

Commit 2fd1544

Browse files
committed
ast: move Generics into AssocItemKinds
1 parent dbef353 commit 2fd1544

File tree

17 files changed

+155
-163
lines changed

17 files changed

+155
-163
lines changed

src/librustc_ast_lowering/item.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -760,31 +760,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
760760

761761
let (generics, kind) = match i.kind {
762762
AssocItemKind::Const(ref ty, ref default) => {
763-
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
764763
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
765-
(
766-
generics,
767-
hir::TraitItemKind::Const(
768-
ty,
769-
default.as_ref().map(|x| self.lower_const_body(i.span, Some(x))),
770-
),
771-
)
764+
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
765+
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body))
772766
}
773-
AssocItemKind::Fn(ref sig, None) => {
767+
AssocItemKind::Fn(ref sig, ref generics, None) => {
774768
let names = self.lower_fn_params_to_names(&sig.decl);
775769
let (generics, sig) =
776-
self.lower_method_sig(&i.generics, sig, trait_item_def_id, false, None);
770+
self.lower_method_sig(generics, sig, trait_item_def_id, false, None);
777771
(generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Required(names)))
778772
}
779-
AssocItemKind::Fn(ref sig, Some(ref body)) => {
773+
AssocItemKind::Fn(ref sig, ref generics, Some(ref body)) => {
780774
let body_id = self.lower_fn_body_block(i.span, &sig.decl, Some(body));
781775
let (generics, sig) =
782-
self.lower_method_sig(&i.generics, sig, trait_item_def_id, false, None);
776+
self.lower_method_sig(generics, sig, trait_item_def_id, false, None);
783777
(generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Provided(body_id)))
784778
}
785-
AssocItemKind::TyAlias(ref bounds, ref default) => {
779+
AssocItemKind::TyAlias(ref generics, ref bounds, ref default) => {
786780
let ty = default.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::disallowed()));
787-
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
781+
let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
788782
let kind = hir::TraitItemKind::Type(
789783
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
790784
ty,
@@ -806,10 +800,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
806800
}
807801

808802
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
809-
let (kind, has_default) = match i.kind {
810-
AssocItemKind::Const(_, ref default) => (hir::AssocItemKind::Const, default.is_some()),
811-
AssocItemKind::TyAlias(_, ref default) => (hir::AssocItemKind::Type, default.is_some()),
812-
AssocItemKind::Fn(ref sig, ref default) => {
803+
let (kind, has_default) = match &i.kind {
804+
AssocItemKind::Const(_, default) => (hir::AssocItemKind::Const, default.is_some()),
805+
AssocItemKind::TyAlias(_, _, default) => (hir::AssocItemKind::Type, default.is_some()),
806+
AssocItemKind::Fn(sig, _, default) => {
813807
(hir::AssocItemKind::Method { has_self: sig.decl.has_self() }, default.is_some())
814808
}
815809
AssocItemKind::Macro(..) => unimplemented!(),
@@ -833,21 +827,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
833827

834828
let (generics, kind) = match i.kind {
835829
AssocItemKind::Const(ref ty, ref expr) => {
836-
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
837830
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
838831
(
839-
generics,
832+
hir::Generics::empty(),
840833
hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())),
841834
)
842835
}
843-
AssocItemKind::Fn(ref sig, ref body) => {
836+
AssocItemKind::Fn(ref sig, ref generics, ref body) => {
844837
self.current_item = Some(i.span);
845838
let asyncness = sig.header.asyncness;
846839
let body_id =
847840
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, body.as_deref());
848841
let impl_trait_return_allow = !self.is_in_trait_impl;
849842
let (generics, sig) = self.lower_method_sig(
850-
&i.generics,
843+
generics,
851844
sig,
852845
impl_item_def_id,
853846
impl_trait_return_allow,
@@ -856,8 +849,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
856849

857850
(generics, hir::ImplItemKind::Method(sig, body_id))
858851
}
859-
AssocItemKind::TyAlias(_, ref ty) => {
860-
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
852+
AssocItemKind::TyAlias(ref generics, _, ref ty) => {
853+
let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
861854
let kind = match ty {
862855
None => {
863856
let ty = self.arena.alloc(self.ty(i.span, hir::TyKind::Err));
@@ -902,13 +895,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
902895
defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
903896
kind: match &i.kind {
904897
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
905-
AssocItemKind::TyAlias(_, ty) => {
898+
AssocItemKind::TyAlias(_, _, ty) => {
906899
match ty.as_deref().and_then(|ty| ty.kind.opaque_top_hack()) {
907900
None => hir::AssocItemKind::Type,
908901
Some(_) => hir::AssocItemKind::OpaqueTy,
909902
}
910903
}
911-
AssocItemKind::Fn(sig, _) => {
904+
AssocItemKind::Fn(sig, _, _) => {
912905
hir::AssocItemKind::Method { has_self: sig.decl.has_self() }
913906
}
914907
AssocItemKind::Macro(..) => unimplemented!(),

src/librustc_ast_lowering/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
490490
self.lctx.allocate_hir_id_counter(item.id);
491491
let owner = match (&item.kind, ctxt) {
492492
// Ignore patterns in trait methods without bodies.
493-
(AssocItemKind::Fn(_, None), AssocCtxt::Trait) => None,
493+
(AssocItemKind::Fn(_, _, None), AssocCtxt::Trait) => None,
494494
_ => Some(item.id),
495495
};
496496
self.with_hir_id_owner(owner, |this| visit::walk_assoc_item(this, item, ctxt));

src/librustc_ast_passes/ast_validation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,10 +1154,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11541154
AssocItemKind::Const(_, body) => {
11551155
self.check_impl_item_provided(item.span, body, "constant", " = <expr>;");
11561156
}
1157-
AssocItemKind::Fn(_, body) => {
1157+
AssocItemKind::Fn(_, _, body) => {
11581158
self.check_impl_item_provided(item.span, body, "function", " { <body> }");
11591159
}
1160-
AssocItemKind::TyAlias(bounds, body) => {
1160+
AssocItemKind::TyAlias(_, bounds, body) => {
11611161
self.check_impl_item_provided(item.span, body, "type", " = <type>;");
11621162
self.check_impl_assoc_type_no_bounds(bounds);
11631163
}
@@ -1167,7 +1167,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11671167

11681168
if ctxt == AssocCtxt::Trait || self.in_trait_impl {
11691169
self.invalid_visibility(&item.vis, None);
1170-
if let AssocItemKind::Fn(sig, _) = &item.kind {
1170+
if let AssocItemKind::Fn(sig, _, _) = &item.kind {
11711171
self.check_trait_fn_not_const(sig.header.constness);
11721172
self.check_trait_fn_not_async(item.span, sig.header.asyncness);
11731173
}

src/librustc_ast_passes/feature_gate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,12 +548,12 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
548548
}
549549

550550
match i.kind {
551-
ast::AssocItemKind::Fn(ref sig, _) => {
551+
ast::AssocItemKind::Fn(ref sig, _, _) => {
552552
if let (ast::Const::Yes(_), AssocCtxt::Trait) = (sig.header.constness, ctxt) {
553553
gate_feature_post!(&self, const_fn, i.span, "const fn is unstable");
554554
}
555555
}
556-
ast::AssocItemKind::TyAlias(_, ref ty) => {
556+
ast::AssocItemKind::TyAlias(ref generics, _, ref ty) => {
557557
if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) {
558558
gate_feature_post!(
559559
&self,
@@ -565,7 +565,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
565565
if let Some(ty) = ty {
566566
self.check_impl_trait(ty);
567567
}
568-
self.check_gat(&i.generics, i.span);
568+
self.check_gat(generics, i.span);
569569
}
570570
_ => {}
571571
}

src/librustc_ast_pretty/pprust.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,12 +1473,12 @@ impl<'a> State<'a> {
14731473
ast::AssocItemKind::Const(ty, expr) => {
14741474
self.print_associated_const(item.ident, ty, expr.as_deref(), &item.vis);
14751475
}
1476-
ast::AssocItemKind::Fn(sig, body) => {
1476+
ast::AssocItemKind::Fn(sig, generics, body) => {
14771477
let body = body.as_deref();
1478-
self.print_fn_full(sig, item.ident, &item.generics, &item.vis, body, &item.attrs);
1478+
self.print_fn_full(sig, item.ident, generics, &item.vis, body, &item.attrs);
14791479
}
1480-
ast::AssocItemKind::TyAlias(bounds, ty) => {
1481-
self.print_associated_type(item.ident, &item.generics, bounds, ty.as_deref());
1480+
ast::AssocItemKind::TyAlias(generics, bounds, ty) => {
1481+
self.print_associated_type(item.ident, generics, bounds, ty.as_deref());
14821482
}
14831483
ast::AssocItemKind::Macro(mac) => {
14841484
self.print_mac(mac);

src/librustc_builtin_macros/deriving/generic/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,8 @@ impl<'a> TraitDef<'a> {
544544
vis: respan(self.span.shrink_to_lo(), ast::VisibilityKind::Inherited),
545545
defaultness: ast::Defaultness::Final,
546546
attrs: Vec::new(),
547-
generics: Generics::default(),
548547
kind: ast::AssocItemKind::TyAlias(
548+
Generics::default(),
549549
Vec::new(),
550550
Some(type_def.to_ty(cx, self.span, type_ident, generics)),
551551
),
@@ -973,12 +973,11 @@ impl<'a> MethodDef<'a> {
973973
P(ast::AssocItem {
974974
id: ast::DUMMY_NODE_ID,
975975
attrs: self.attributes.clone(),
976-
generics: fn_generics,
977976
span: trait_.span,
978977
vis: respan(trait_lo_sp, ast::VisibilityKind::Inherited),
979978
defaultness: ast::Defaultness::Final,
980979
ident: method_ident,
981-
kind: ast::AssocItemKind::Fn(sig, Some(body_block)),
980+
kind: ast::AssocItemKind::Fn(sig, fn_generics, Some(body_block)),
982981
tokens: None,
983982
})
984983
}

src/librustc_expand/placeholders.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub fn placeholder(
2525

2626
let ident = ast::Ident::invalid();
2727
let attrs = Vec::new();
28-
let generics = ast::Generics::default();
2928
let vis = vis.unwrap_or_else(|| dummy_spanned(ast::VisibilityKind::Inherited));
3029
let span = DUMMY_SP;
3130
let expr_placeholder = || {
@@ -57,7 +56,6 @@ pub fn placeholder(
5756
ident,
5857
vis,
5958
attrs,
60-
generics,
6159
kind: ast::AssocItemKind::Macro(mac_placeholder()),
6260
defaultness: ast::Defaultness::Final,
6361
tokens: None,
@@ -68,7 +66,6 @@ pub fn placeholder(
6866
ident,
6967
vis,
7068
attrs,
71-
generics,
7269
kind: ast::AssocItemKind::Macro(mac_placeholder()),
7370
defaultness: ast::Defaultness::Final,
7471
tokens: None,

src/librustc_interface/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
686686
fn flat_map_trait_item(&mut self, i: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
687687
let is_const = match i.kind {
688688
ast::AssocItemKind::Const(..) => true,
689-
ast::AssocItemKind::Fn(ref sig, _) => Self::is_sig_const(sig),
689+
ast::AssocItemKind::Fn(ref sig, _, _) => Self::is_sig_const(sig),
690690
_ => false,
691691
};
692692
self.run(is_const, |s| noop_flat_map_assoc_item(i, s))

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ declare_lint_pass!(
640640
impl EarlyLintPass for AnonymousParameters {
641641
fn check_trait_item(&mut self, cx: &EarlyContext<'_>, it: &ast::AssocItem) {
642642
match it.kind {
643-
ast::AssocItemKind::Fn(ref sig, _) => {
643+
ast::AssocItemKind::Fn(ref sig, _, _) => {
644644
for arg in sig.decl.inputs.iter() {
645645
match arg.pat.kind {
646646
ast::PatKind::Ident(_, ident, None) => {

src/librustc_parse/parser/item.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -665,46 +665,46 @@ impl<'a> Parser<'a> {
665665
let vis = self.parse_visibility(FollowedByType::No)?;
666666
let defaultness = self.parse_defaultness();
667667

668-
let (ident, kind, generics) = if self.eat_keyword(kw::Type) {
668+
let (ident, kind) = if self.eat_keyword(kw::Type) {
669669
self.parse_assoc_ty()?
670670
} else if self.check_fn_front_matter() {
671671
let (ident, sig, generics, body) = self.parse_fn(at_end, &mut attrs, req_name)?;
672-
(ident, AssocItemKind::Fn(sig, body), generics)
672+
(ident, AssocItemKind::Fn(sig, generics, body))
673673
} else if self.check_keyword(kw::Const) {
674674
self.parse_assoc_const()?
675675
} else if self.isnt_macro_invocation() {
676676
return Err(self.missing_assoc_item_kind_err("associated", self.prev_span));
677677
} else if self.token.is_path_start() {
678678
let mac = self.parse_item_macro(&vis)?;
679679
*at_end = true;
680-
(Ident::invalid(), AssocItemKind::Macro(mac), Generics::default())
680+
(Ident::invalid(), AssocItemKind::Macro(mac))
681681
} else {
682682
self.recover_attrs_no_item(&attrs)?;
683683
self.unexpected()?
684684
};
685685

686686
let span = lo.to(self.prev_span);
687687
let id = DUMMY_NODE_ID;
688-
Ok(AssocItem { id, span, ident, attrs, vis, defaultness, generics, kind, tokens: None })
688+
Ok(AssocItem { id, span, ident, attrs, vis, defaultness, kind, tokens: None })
689689
}
690690

691691
/// This parses the grammar:
692692
///
693693
/// AssocConst = "const" Ident ":" Ty "=" Expr ";"
694-
fn parse_assoc_const(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
694+
fn parse_assoc_const(&mut self) -> PResult<'a, (Ident, AssocItemKind)> {
695695
self.expect_keyword(kw::Const)?;
696696
let ident = self.parse_ident()?;
697697
self.expect(&token::Colon)?;
698698
let ty = self.parse_ty()?;
699699
let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None };
700700
self.expect_semi()?;
701-
Ok((ident, AssocItemKind::Const(ty, expr), Generics::default()))
701+
Ok((ident, AssocItemKind::Const(ty, expr)))
702702
}
703703

704704
/// Parses the following grammar:
705705
///
706706
/// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
707-
fn parse_assoc_ty(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
707+
fn parse_assoc_ty(&mut self) -> PResult<'a, (Ident, AssocItemKind)> {
708708
let ident = self.parse_ident()?;
709709
let mut generics = self.parse_generics()?;
710710

@@ -716,7 +716,7 @@ impl<'a> Parser<'a> {
716716
let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
717717
self.expect_semi()?;
718718

719-
Ok((ident, AssocItemKind::TyAlias(bounds, default), generics))
719+
Ok((ident, AssocItemKind::TyAlias(generics, bounds, default)))
720720
}
721721

722722
/// Parses a `UseTree`.

0 commit comments

Comments
 (0)