Skip to content

Commit 5bc23ce

Browse files
committed
Extract ast TraitImplHeader
1 parent 3aa0ac0 commit 5bc23ce

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
@@ -3661,15 +3661,19 @@ pub struct TyAlias {
36613661

36623662
#[derive(Clone, Encodable, Decodable, Debug)]
36633663
pub struct Impl {
3664+
pub generics: Generics,
3665+
pub of_trait: Option<Box<TraitImplHeader>>,
3666+
pub self_ty: Box<Ty>,
3667+
pub items: ThinVec<Box<AssocItem>>,
3668+
}
3669+
3670+
#[derive(Clone, Encodable, Decodable, Debug)]
3671+
pub struct TraitImplHeader {
36643672
pub defaultness: Defaultness,
36653673
pub safety: Safety,
3666-
pub generics: Generics,
36673674
pub constness: Const,
36683675
pub polarity: ImplPolarity,
3669-
/// The trait being implemented, if any.
3670-
pub of_trait: Option<TraitRef>,
3671-
pub self_ty: Box<Ty>,
3672-
pub items: ThinVec<Box<AssocItem>>,
3676+
pub trait_ref: TraitRef,
36733677
}
36743678

36753679
#[derive(Clone, Encodable, Decodable, Debug, Default, Walkable)]
@@ -3793,7 +3797,7 @@ pub enum ItemKind {
37933797
/// An implementation.
37943798
///
37953799
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
3796-
Impl(Box<Impl>),
3800+
Impl(Impl),
37973801
/// A macro invocation.
37983802
///
37993803
/// E.g., `foo!(..)`.
@@ -3880,7 +3884,7 @@ impl ItemKind {
38803884
| Self::Union(_, generics, _)
38813885
| Self::Trait(box Trait { generics, .. })
38823886
| Self::TraitAlias(_, generics, _)
3883-
| Self::Impl(box Impl { generics, .. }) => Some(generics),
3887+
| Self::Impl(Impl { generics, .. }) => Some(generics),
38843888
_ => None,
38853889
}
38863890
}
@@ -4040,7 +4044,7 @@ mod size_asserts {
40404044
static_assert_size!(GenericArg, 24);
40414045
static_assert_size!(GenericBound, 88);
40424046
static_assert_size!(Generics, 40);
4043-
static_assert_size!(Impl, 136);
4047+
static_assert_size!(Impl, 64);
40444048
static_assert_size!(Item, 144);
40454049
static_assert_size!(ItemKind, 80);
40464050
static_assert_size!(LitKind, 24);
@@ -4053,6 +4057,7 @@ mod size_asserts {
40534057
static_assert_size!(PathSegment, 24);
40544058
static_assert_size!(Stmt, 32);
40554059
static_assert_size!(StmtKind, 16);
4060+
static_assert_size!(TraitImplHeader, 80);
40564061
static_assert_size!(Ty, 64);
40574062
static_assert_size!(TyKind, 40);
40584063
// tidy-alphabetical-end

compiler/rustc_ast/src/visit.rs

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

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

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
340340
);
341341
hir::ItemKind::Union(ident, generics, vdata)
342342
}
343-
ItemKind::Impl(box Impl {
344-
safety,
345-
polarity,
346-
defaultness,
347-
constness,
343+
ItemKind::Impl(Impl {
348344
generics: ast_generics,
349-
of_trait: trait_ref,
345+
of_trait,
350346
self_ty: ty,
351347
items: impl_items,
352348
}) => {
@@ -373,10 +369,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
373369
polarity: BoundPolarity::Positive,
374370
};
375371

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

compiler/rustc_ast_passes/src/ast_validation.rs

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

956956
match &item.kind {
957-
ItemKind::Impl(box Impl {
958-
safety,
959-
polarity,
960-
defaultness: _,
961-
constness,
957+
ItemKind::Impl(Impl {
962958
generics,
963-
of_trait: Some(t),
959+
of_trait:
960+
Some(box TraitImplHeader {
961+
safety,
962+
polarity,
963+
defaultness: _,
964+
constness,
965+
trait_ref: t,
966+
}),
964967
self_ty,
965968
items,
966969
}) => {
@@ -992,16 +995,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
992995
walk_list!(this, visit_assoc_item, items, AssocCtxt::Impl { of_trait: true });
993996
});
994997
}
995-
ItemKind::Impl(box Impl {
996-
safety: _,
997-
polarity: _,
998-
defaultness: _,
999-
constness: _,
1000-
generics,
1001-
of_trait: None,
1002-
self_ty,
1003-
items,
1004-
}) => {
998+
ItemKind::Impl(Impl { generics, of_trait: None, self_ty, items }) => {
1005999
self.visit_attrs_vis(&item.attrs, &item.vis);
10061000
self.visibility_not_permitted(
10071001
&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
@@ -308,39 +308,41 @@ impl<'a> State<'a> {
308308
let (cb, ib) = self.head(visibility_qualified(&item.vis, "union"));
309309
self.print_struct(struct_def, generics, *ident, item.span, true, cb, ib);
310310
}
311-
ast::ItemKind::Impl(box ast::Impl {
312-
safety,
313-
polarity,
314-
defaultness,
315-
constness,
316-
generics,
317-
of_trait,
318-
self_ty,
319-
items,
320-
}) => {
311+
ast::ItemKind::Impl(ast::Impl { generics, of_trait, self_ty, items }) => {
321312
let (cb, ib) = self.head("");
322313
self.print_visibility(&item.vis);
323-
self.print_defaultness(*defaultness);
324-
self.print_safety(*safety);
325-
self.word("impl");
326-
327-
if generics.params.is_empty() {
328-
self.nbsp();
329-
} else {
330-
self.print_generic_params(&generics.params);
331-
self.space();
332-
}
333314

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

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

346348
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
@@ -826,21 +826,25 @@ impl<'a> TraitDef<'a> {
826826
)
827827
}
828828

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

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)