Skip to content

Commit e071e73

Browse files
Auto merge of #144386 - camsteffen:imploftrait, r=<try>
Extract ImplOfTrait in AST/HIR
2 parents fc5af18 + 2d27737 commit e071e73

File tree

81 files changed

+580
-518
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+580
-518
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: 45 additions & 34 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
}) => {
@@ -365,54 +361,69 @@ impl<'hir> LoweringContext<'_, 'hir> {
365361
// lifetime to be added, but rather a reference to a
366362
// parent lifetime.
367363
let itctx = ImplTraitContext::Universal;
368-
let (generics, (trait_ref, lowered_ty)) =
364+
let (generics, (of_trait, lowered_ty)) =
369365
self.lower_generics(ast_generics, id, itctx, |this| {
370-
let modifiers = TraitBoundModifiers {
371-
constness: BoundConstness::Never,
372-
asyncness: BoundAsyncness::Normal,
373-
// we don't use this in bound lowering
374-
polarity: BoundPolarity::Positive,
375-
};
376-
377-
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
378-
this.lower_trait_ref(
366+
let of_trait = of_trait.as_deref().map(|of_trait| {
367+
let ImplOfTrait {
368+
constness,
369+
safety,
370+
polarity,
371+
defaultness,
372+
ref trait_ref,
373+
} = *of_trait;
374+
let constness = this.lower_constness(constness);
375+
let safety = this.lower_safety(safety, hir::Safety::Safe);
376+
let polarity = match polarity {
377+
ImplPolarity::Positive => ImplPolarity::Positive,
378+
ImplPolarity::Negative(s) => {
379+
ImplPolarity::Negative(this.lower_span(s))
380+
}
381+
};
382+
// `defaultness.has_value()` is never called for an `impl`, always `true` in order
383+
// to not cause an assertion failure inside the `lower_defaultness` function.
384+
let has_val = true;
385+
let (defaultness, defaultness_span) =
386+
this.lower_defaultness(defaultness, has_val);
387+
let modifiers = TraitBoundModifiers {
388+
constness: BoundConstness::Never,
389+
asyncness: BoundAsyncness::Normal,
390+
// we don't use this in bound lowering
391+
polarity: BoundPolarity::Positive,
392+
};
393+
let trait_ref = this.lower_trait_ref(
379394
modifiers,
380395
trait_ref,
381396
ImplTraitContext::Disallowed(ImplTraitPosition::Trait),
382-
)
397+
);
398+
399+
&*this.arena.alloc(hir::ImplOfTrait {
400+
constness,
401+
safety,
402+
polarity,
403+
defaultness,
404+
defaultness_span,
405+
trait_ref,
406+
})
383407
});
384408

385409
let lowered_ty = this.lower_ty(
386410
ty,
387411
ImplTraitContext::Disallowed(ImplTraitPosition::ImplSelf),
388412
);
389413

390-
(trait_ref, lowered_ty)
414+
(of_trait, lowered_ty)
391415
});
392416

393417
let new_impl_items = self
394418
.arena
395419
.alloc_from_iter(impl_items.iter().map(|item| self.lower_impl_item_ref(item)));
396420

397-
// `defaultness.has_value()` is never called for an `impl`, always `true` in order
398-
// to not cause an assertion failure inside the `lower_defaultness` function.
399-
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)),
404-
};
405-
hir::ItemKind::Impl(self.arena.alloc(hir::Impl {
406-
constness: self.lower_constness(*constness),
407-
safety: self.lower_safety(*safety, hir::Safety::Safe),
408-
polarity,
409-
defaultness,
410-
defaultness_span,
421+
hir::ItemKind::Impl(hir::Impl {
411422
generics,
412-
of_trait: trait_ref,
423+
of_trait,
413424
self_ty: lowered_ty,
414425
items: new_impl_items,
415-
}))
426+
})
416427
}
417428
ItemKind::Trait(box Trait {
418429
constness,

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

0 commit comments

Comments
 (0)