Skip to content

Commit bb16ad7

Browse files
committed
Extract ast ImplOfTrait
1 parent fc5af18 commit bb16ad7

File tree

23 files changed

+293
-243
lines changed

23 files changed

+293
-243
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<ImplOfTrait>>,
3766+
pub self_ty: P<Ty>,
3767+
pub items: ThinVec<P<AssocItem>>,
3768+
}
3769+
3770+
#[derive(Clone, Encodable, Decodable, Debug)]
3771+
pub struct ImplOfTrait {
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,8 @@ 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);
4148+
static_assert_size!(ImplOfTrait, 80);
41444149
static_assert_size!(Item, 144);
41454150
static_assert_size!(ItemKind, 80);
41464151
static_assert_size!(LitKind, 24);

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 ImplOfTrait { 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: 27 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,31 @@ 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(of_trait) => {
398+
let ImplOfTrait { constness, safety, polarity, defaultness, trait_ref: _ } =
399+
**of_trait;
400+
let constness = self.lower_constness(constness);
401+
let safety = self.lower_safety(safety, hir::Safety::Safe);
402+
let polarity = match polarity {
403+
ImplPolarity::Positive => ImplPolarity::Positive,
404+
ImplPolarity::Negative(s) => ImplPolarity::Negative(self.lower_span(s)),
405+
};
406+
let (defaultness, defaultness_span) =
407+
self.lower_defaultness(defaultness, has_val);
408+
(constness, safety, polarity, defaultness, defaultness_span)
409+
}
410+
None => (
411+
hir::Constness::NotConst,
412+
hir::Safety::Safe,
413+
ImplPolarity::Positive,
414+
hir::Defaultness::Final,
415+
None,
416+
),
404417
};
405418
hir::ItemKind::Impl(self.arena.alloc(hir::Impl {
406-
constness: self.lower_constness(*constness),
407-
safety: self.lower_safety(*safety, hir::Safety::Safe),
419+
constness,
420+
safety,
408421
polarity,
409422
defaultness,
410423
defaultness_span,

compiler/rustc_ast_passes/messages.ftl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,6 @@ ast_passes_generic_default_trailing = generic parameters with a default must be
175175
ast_passes_incompatible_features = `{$f1}` and `{$f2}` are incompatible, using them at the same time is not allowed
176176
.help = remove one of these features
177177
178-
ast_passes_inherent_cannot_be = inherent impls cannot be {$annotation}
179-
.because = {$annotation} because of this
180-
.type = inherent impl for this type
181-
.only_trait = only trait implementations may be annotated with {$annotation}
182-
183178
ast_passes_item_invalid_safety = items outside of `unsafe extern {"{ }"}` cannot be declared with `safe` safety qualifier
184179
.suggestion = remove safe from this item
185180

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -951,13 +951,10 @@ 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 ImplOfTrait { safety, polarity, defaultness: _, constness, trait_ref: t }),
961958
self_ty,
962959
items,
963960
}) => {
@@ -989,46 +986,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
989986
walk_list!(this, visit_assoc_item, items, AssocCtxt::Impl { of_trait: true });
990987
});
991988
}
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-
}) => {
1002-
let error = |annotation_span, annotation, only_trait| errors::InherentImplCannot {
1003-
span: self_ty.span,
1004-
annotation_span,
1005-
annotation,
1006-
self_ty: self_ty.span,
1007-
only_trait,
1008-
};
1009-
989+
ItemKind::Impl(Impl { generics, of_trait: None, self_ty, items }) => {
1010990
self.visit_attrs_vis(&item.attrs, &item.vis);
1011991
self.visibility_not_permitted(
1012992
&item.vis,
1013993
errors::VisibilityNotPermittedNote::IndividualImplItems,
1014994
);
1015-
if let &Safety::Unsafe(span) = safety {
1016-
self.dcx().emit_err(errors::InherentImplCannotUnsafe {
1017-
span: self_ty.span,
1018-
annotation_span: span,
1019-
annotation: "unsafe",
1020-
self_ty: self_ty.span,
1021-
});
1022-
}
1023-
if let &ImplPolarity::Negative(span) = polarity {
1024-
self.dcx().emit_err(error(span, "negative", false));
1025-
}
1026-
if let &Defaultness::Default(def_span) = defaultness {
1027-
self.dcx().emit_err(error(def_span, "`default`", true));
1028-
}
1029-
if let &Const::Yes(span) = constness {
1030-
self.dcx().emit_err(error(span, "`const`", true));
1031-
}
1032995

1033996
self.with_tilde_const(Some(TildeConstReason::Impl { span: item.span }), |this| {
1034997
this.visit_generics(generics)

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -463,32 +463,6 @@ pub(crate) struct UnsafeNegativeImpl {
463463
pub r#unsafe: Span,
464464
}
465465

466-
#[derive(Diagnostic)]
467-
#[diag(ast_passes_inherent_cannot_be)]
468-
pub(crate) struct InherentImplCannot<'a> {
469-
#[primary_span]
470-
pub span: Span,
471-
#[label(ast_passes_because)]
472-
pub annotation_span: Span,
473-
pub annotation: &'a str,
474-
#[label(ast_passes_type)]
475-
pub self_ty: Span,
476-
#[note(ast_passes_only_trait)]
477-
pub only_trait: bool,
478-
}
479-
480-
#[derive(Diagnostic)]
481-
#[diag(ast_passes_inherent_cannot_be, code = E0197)]
482-
pub(crate) struct InherentImplCannotUnsafe<'a> {
483-
#[primary_span]
484-
pub span: Span,
485-
#[label(ast_passes_because)]
486-
pub annotation_span: Span,
487-
pub annotation: &'a str,
488-
#[label(ast_passes_type)]
489-
pub self_ty: Span,
490-
}
491-
492466
#[derive(Diagnostic)]
493467
#[diag(ast_passes_unsafe_item)]
494468
pub(crate) struct UnsafeItem {

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: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -309,39 +309,42 @@ 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");
327315

328-
if generics.params.is_empty() {
329-
self.nbsp();
330-
} else {
331-
self.print_generic_params(&generics.params);
332-
self.space();
333-
}
334-
335-
self.print_constness(*constness);
336-
337-
if let ast::ImplPolarity::Negative(_) = polarity {
338-
self.word("!");
339-
}
316+
let impl_generics = |this: &mut Self| {
317+
this.word("impl");
340318

341-
if let Some(t) = of_trait {
342-
self.print_trait_ref(t);
343-
self.space();
344-
self.word_space("for");
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+
match of_trait {
328+
None => impl_generics(self),
329+
Some(box of_trait) => {
330+
let ast::ImplOfTrait {
331+
defaultness,
332+
safety,
333+
constness,
334+
polarity,
335+
ref trait_ref,
336+
} = *of_trait;
337+
self.print_defaultness(defaultness);
338+
self.print_safety(safety);
339+
impl_generics(self);
340+
self.print_constness(constness);
341+
if let ast::ImplPolarity::Negative(_) = polarity {
342+
self.word("!");
343+
}
344+
self.print_trait_ref(trait_ref);
345+
self.space();
346+
self.word_space("for");
347+
}
345348
}
346349

347350
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::ImplOfTrait {
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::ImplOfTrait {
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
};

0 commit comments

Comments
 (0)