Skip to content

Commit 7ef7c7b

Browse files
committed
Extract ast TraitImplHeader
1 parent 3dc0602 commit 7ef7c7b

File tree

18 files changed

+187
-184
lines changed

18 files changed

+187
-184
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3761,15 +3761,19 @@ pub struct TyAlias {
37613761

37623762
#[derive(Clone, Encodable, Decodable, Debug)]
37633763
pub struct Impl {
3764+
pub generics: Generics,
3765+
pub of_trait: Option<Box<TraitImplHeader>>,
3766+
pub self_ty: P<Ty>,
3767+
pub items: ThinVec<P<AssocItem>>,
3768+
}
3769+
3770+
#[derive(Clone, Encodable, Decodable, Debug)]
3771+
pub struct TraitImplHeader {
37643772
pub defaultness: Defaultness,
37653773
pub safety: Safety,
3766-
pub generics: Generics,
37673774
pub constness: Const,
37683775
pub polarity: ImplPolarity,
3769-
/// The trait being implemented, if any.
3770-
pub of_trait: Option<TraitRef>,
3771-
pub self_ty: P<Ty>,
3772-
pub items: ThinVec<P<AssocItem>>,
3776+
pub trait_ref: TraitRef,
37733777
}
37743778

37753779
#[derive(Clone, Encodable, Decodable, Debug, Default, Walkable)]
@@ -3893,7 +3897,7 @@ pub enum ItemKind {
38933897
/// An implementation.
38943898
///
38953899
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
3896-
Impl(Box<Impl>),
3900+
Impl(Impl),
38973901
/// A macro invocation.
38983902
///
38993903
/// E.g., `foo!(..)`.
@@ -3980,7 +3984,7 @@ impl ItemKind {
39803984
| Self::Union(_, generics, _)
39813985
| Self::Trait(box Trait { generics, .. })
39823986
| Self::TraitAlias(_, generics, _)
3983-
| Self::Impl(box Impl { generics, .. }) => Some(generics),
3987+
| Self::Impl(Impl { generics, .. }) => Some(generics),
39843988
_ => None,
39853989
}
39863990
}
@@ -4140,7 +4144,7 @@ mod size_asserts {
41404144
static_assert_size!(GenericArg, 24);
41414145
static_assert_size!(GenericBound, 88);
41424146
static_assert_size!(Generics, 40);
4143-
static_assert_size!(Impl, 136);
4147+
static_assert_size!(Impl, 64);
41444148
static_assert_size!(Item, 144);
41454149
static_assert_size!(ItemKind, 80);
41464150
static_assert_size!(LitKind, 24);
@@ -4153,6 +4157,7 @@ mod size_asserts {
41534157
static_assert_size!(PathSegment, 24);
41544158
static_assert_size!(Stmt, 32);
41554159
static_assert_size!(StmtKind, 16);
4160+
static_assert_size!(TraitImplHeader, 80);
41564161
static_assert_size!(Ty, 64);
41574162
static_assert_size!(TyKind, 40);
41584163
// tidy-alphabetical-end

compiler/rustc_ast/src/visit.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,8 +930,13 @@ macro_rules! common_visitor_and_walkers {
930930
}
931931

932932
impl_walkable!(|&$($mut)? $($lt)? self: Impl, vis: &mut V| {
933-
let Impl { defaultness, safety, generics, constness, polarity, of_trait, self_ty, items } = self;
934-
visit_visitable!($($mut)? vis, defaultness, safety, generics, constness, polarity, of_trait, self_ty);
933+
let Impl { generics, of_trait, self_ty, items } = self;
934+
try_visit!(vis.visit_generics(generics));
935+
if let Some(box of_trait) = of_trait {
936+
let TraitImplHeader { defaultness, safety, constness, polarity, trait_ref } = of_trait;
937+
visit_visitable!($($mut)? vis, defaultness, safety, constness, polarity, trait_ref);
938+
}
939+
try_visit!(vis.visit_ty(self_ty));
935940
visit_visitable_with!($($mut)? vis, items, AssocCtxt::Impl { of_trait: of_trait.is_some() });
936941
V::Result::output()
937942
});

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
341341
);
342342
hir::ItemKind::Union(ident, generics, vdata)
343343
}
344-
ItemKind::Impl(box Impl {
345-
safety,
346-
polarity,
347-
defaultness,
348-
constness,
344+
ItemKind::Impl(Impl {
349345
generics: ast_generics,
350-
of_trait: trait_ref,
346+
of_trait,
351347
self_ty: ty,
352348
items: impl_items,
353349
}) => {
@@ -374,10 +370,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
374370
polarity: BoundPolarity::Positive,
375371
};
376372

377-
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
373+
let trait_ref = of_trait.as_ref().map(|of_trait| {
378374
this.lower_trait_ref(
379375
modifiers,
380-
trait_ref,
376+
&of_trait.trait_ref,
381377
ImplTraitContext::Disallowed(ImplTraitPosition::Trait),
382378
)
383379
});
@@ -397,14 +393,35 @@ impl<'hir> LoweringContext<'_, 'hir> {
397393
// `defaultness.has_value()` is never called for an `impl`, always `true` in order
398394
// to not cause an assertion failure inside the `lower_defaultness` function.
399395
let has_val = true;
400-
let (defaultness, defaultness_span) = self.lower_defaultness(*defaultness, has_val);
401-
let polarity = match polarity {
402-
ImplPolarity::Positive => ImplPolarity::Positive,
403-
ImplPolarity::Negative(s) => ImplPolarity::Negative(self.lower_span(*s)),
396+
let (constness, safety, polarity, defaultness, defaultness_span) = match *of_trait {
397+
Some(box TraitImplHeader {
398+
constness,
399+
safety,
400+
polarity,
401+
defaultness,
402+
trait_ref: _,
403+
}) => {
404+
let constness = self.lower_constness(constness);
405+
let safety = self.lower_safety(safety, hir::Safety::Safe);
406+
let polarity = match polarity {
407+
ImplPolarity::Positive => ImplPolarity::Positive,
408+
ImplPolarity::Negative(s) => ImplPolarity::Negative(self.lower_span(s)),
409+
};
410+
let (defaultness, defaultness_span) =
411+
self.lower_defaultness(defaultness, has_val);
412+
(constness, safety, polarity, defaultness, defaultness_span)
413+
}
414+
None => (
415+
hir::Constness::NotConst,
416+
hir::Safety::Safe,
417+
ImplPolarity::Positive,
418+
hir::Defaultness::Final,
419+
None,
420+
),
404421
};
405422
hir::ItemKind::Impl(self.arena.alloc(hir::Impl {
406-
constness: self.lower_constness(*constness),
407-
safety: self.lower_safety(*safety, hir::Safety::Safe),
423+
constness,
424+
safety,
408425
polarity,
409426
defaultness,
410427
defaultness_span,

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -951,13 +951,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
951951
}
952952

953953
match &item.kind {
954-
ItemKind::Impl(box Impl {
955-
safety,
956-
polarity,
957-
defaultness: _,
958-
constness,
954+
ItemKind::Impl(Impl {
959955
generics,
960-
of_trait: Some(t),
956+
of_trait:
957+
Some(box TraitImplHeader {
958+
safety,
959+
polarity,
960+
defaultness: _,
961+
constness,
962+
trait_ref: t,
963+
}),
961964
self_ty,
962965
items,
963966
}) => {
@@ -989,16 +992,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
989992
walk_list!(this, visit_assoc_item, items, AssocCtxt::Impl { of_trait: true });
990993
});
991994
}
992-
ItemKind::Impl(box Impl {
993-
safety: _,
994-
polarity: _,
995-
defaultness: _,
996-
constness: _,
997-
generics,
998-
of_trait: None,
999-
self_ty,
1000-
items,
1001-
}) => {
995+
ItemKind::Impl(Impl { generics, of_trait: None, self_ty, items }) => {
1002996
self.visit_attrs_vis(&item.attrs, &item.vis);
1003997
self.visibility_not_permitted(
1004998
&item.vis,

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,18 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
217217
}
218218
}
219219

220-
ast::ItemKind::Impl(box ast::Impl { polarity, defaultness, of_trait, .. }) => {
221-
if let &ast::ImplPolarity::Negative(span) = polarity {
220+
ast::ItemKind::Impl(ast::Impl { of_trait: Some(of_trait), .. }) => {
221+
if let ast::ImplPolarity::Negative(span) = of_trait.polarity {
222222
gate!(
223223
&self,
224224
negative_impls,
225-
span.to(of_trait.as_ref().map_or(span, |t| t.path.span)),
225+
span.to(of_trait.trait_ref.path.span),
226226
"negative trait bounds are not fully implemented; \
227227
use marker types for now"
228228
);
229229
}
230230

231-
if let ast::Defaultness::Default(_) = defaultness {
231+
if let ast::Defaultness::Default(_) = of_trait.defaultness {
232232
gate!(&self, specialization, i.span, "specialization is unstable");
233233
}
234234
}

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -309,39 +309,41 @@ impl<'a> State<'a> {
309309
let (cb, ib) = self.head(visibility_qualified(&item.vis, "union"));
310310
self.print_struct(struct_def, generics, *ident, item.span, true, cb, ib);
311311
}
312-
ast::ItemKind::Impl(box ast::Impl {
313-
safety,
314-
polarity,
315-
defaultness,
316-
constness,
317-
generics,
318-
of_trait,
319-
self_ty,
320-
items,
321-
}) => {
312+
ast::ItemKind::Impl(ast::Impl { generics, of_trait, self_ty, items }) => {
322313
let (cb, ib) = self.head("");
323314
self.print_visibility(&item.vis);
324-
self.print_defaultness(*defaultness);
325-
self.print_safety(*safety);
326-
self.word("impl");
327-
328-
if generics.params.is_empty() {
329-
self.nbsp();
330-
} else {
331-
self.print_generic_params(&generics.params);
332-
self.space();
333-
}
334315

335-
self.print_constness(*constness);
316+
let impl_generics = |this: &mut Self| {
317+
this.word("impl");
336318

337-
if let ast::ImplPolarity::Negative(_) = polarity {
338-
self.word("!");
339-
}
340-
341-
if let Some(t) = of_trait {
342-
self.print_trait_ref(t);
319+
if generics.params.is_empty() {
320+
this.nbsp();
321+
} else {
322+
this.print_generic_params(&generics.params);
323+
this.space();
324+
}
325+
};
326+
327+
if let Some(box of_trait) = of_trait {
328+
let ast::TraitImplHeader {
329+
defaultness,
330+
safety,
331+
constness,
332+
polarity,
333+
ref trait_ref,
334+
} = *of_trait;
335+
self.print_defaultness(defaultness);
336+
self.print_safety(safety);
337+
impl_generics(self);
338+
self.print_constness(constness);
339+
if let ast::ImplPolarity::Negative(_) = polarity {
340+
self.word("!");
341+
}
342+
self.print_trait_ref(trait_ref);
343343
self.space();
344344
self.word_space("for");
345+
} else {
346+
impl_generics(self);
345347
}
346348

347349
self.print_type(self_ty);

compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ pub(crate) fn expand_deriving_coerce_pointee(
108108
cx.item(
109109
span,
110110
attrs.clone(),
111-
ast::ItemKind::Impl(Box::new(ast::Impl {
112-
safety: ast::Safety::Default,
113-
polarity: ast::ImplPolarity::Positive,
114-
defaultness: ast::Defaultness::Final,
115-
constness: ast::Const::No,
111+
ast::ItemKind::Impl(ast::Impl {
116112
generics: Generics {
117113
params: generics
118114
.params
@@ -137,10 +133,16 @@ pub(crate) fn expand_deriving_coerce_pointee(
137133
where_clause: generics.where_clause.clone(),
138134
span: generics.span,
139135
},
140-
of_trait: Some(trait_ref),
136+
of_trait: Some(Box::new(ast::TraitImplHeader {
137+
safety: ast::Safety::Default,
138+
polarity: ast::ImplPolarity::Positive,
139+
defaultness: ast::Defaultness::Final,
140+
constness: ast::Const::No,
141+
trait_ref,
142+
})),
141143
self_ty: self_type.clone(),
142144
items: ThinVec::new(),
143-
})),
145+
}),
144146
),
145147
));
146148
}
@@ -152,16 +154,18 @@ pub(crate) fn expand_deriving_coerce_pointee(
152154
let item = cx.item(
153155
span,
154156
attrs.clone(),
155-
ast::ItemKind::Impl(Box::new(ast::Impl {
156-
safety: ast::Safety::Default,
157-
polarity: ast::ImplPolarity::Positive,
158-
defaultness: ast::Defaultness::Final,
159-
constness: ast::Const::No,
157+
ast::ItemKind::Impl(ast::Impl {
160158
generics,
161-
of_trait: Some(trait_ref),
159+
of_trait: Some(Box::new(ast::TraitImplHeader {
160+
safety: ast::Safety::Default,
161+
polarity: ast::ImplPolarity::Positive,
162+
defaultness: ast::Defaultness::Final,
163+
constness: ast::Const::No,
164+
trait_ref,
165+
})),
162166
self_ty: self_type.clone(),
163167
items: ThinVec::new(),
164-
})),
168+
}),
165169
);
166170
push(Annotatable::Item(item));
167171
};

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -827,21 +827,25 @@ impl<'a> TraitDef<'a> {
827827
)
828828
}
829829

830-
let opt_trait_ref = Some(trait_ref);
831-
832830
cx.item(
833831
self.span,
834832
attrs,
835-
ast::ItemKind::Impl(Box::new(ast::Impl {
836-
safety: ast::Safety::Default,
837-
polarity: ast::ImplPolarity::Positive,
838-
defaultness: ast::Defaultness::Final,
839-
constness: if self.is_const { ast::Const::Yes(DUMMY_SP) } else { ast::Const::No },
833+
ast::ItemKind::Impl(ast::Impl {
840834
generics: trait_generics,
841-
of_trait: opt_trait_ref,
835+
of_trait: Some(Box::new(ast::TraitImplHeader {
836+
safety: ast::Safety::Default,
837+
polarity: ast::ImplPolarity::Positive,
838+
defaultness: ast::Defaultness::Final,
839+
constness: if self.is_const {
840+
ast::Const::Yes(DUMMY_SP)
841+
} else {
842+
ast::Const::No
843+
},
844+
trait_ref,
845+
})),
842846
self_ty: self_type,
843847
items: methods.into_iter().chain(associated_types).collect(),
844-
})),
848+
}),
845849
)
846850
}
847851

compiler/rustc_lint/src/builtin.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,10 @@ impl EarlyLintPass for UnsafeCode {
270270
self.report_unsafe(cx, it.span, BuiltinUnsafe::UnsafeTrait);
271271
}
272272

273-
ast::ItemKind::Impl(box ast::Impl { safety: ast::Safety::Unsafe(_), .. }) => {
273+
ast::ItemKind::Impl(ast::Impl {
274+
of_trait: Some(box ast::TraitImplHeader { safety: ast::Safety::Unsafe(_), .. }),
275+
..
276+
}) => {
274277
self.report_unsafe(cx, it.span, BuiltinUnsafe::UnsafeImpl);
275278
}
276279

0 commit comments

Comments
 (0)