Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3636,6 +3636,8 @@ impl Item {
| ItemKind::DelegationMac(_)
| ItemKind::MacroDef(..) => None,
ItemKind::Static(_) => None,
ItemKind::AutoImpl(ai) => Some(&ai.generics),
ItemKind::ExternImpl(ei) => Some(&ei.generics),
ItemKind::Const(i) => Some(&i.generics),
ItemKind::Fn(i) => Some(&i.generics),
ItemKind::TyAlias(i) => Some(&i.generics),
Expand Down Expand Up @@ -3779,6 +3781,20 @@ pub struct Impl {
pub items: ThinVec<Box<AssocItem>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct AutoImpl {
pub generics: Generics,
pub constness: Const,
pub of_trait: Box<TraitImplHeader>,
pub items: ThinVec<Box<AssocItem>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct ExternImpl {
pub generics: Generics,
pub of_trait: Box<TraitImplHeader>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct TraitImplHeader {
pub defaultness: Defaultness,
Expand Down Expand Up @@ -3975,6 +3991,10 @@ pub enum ItemKind {
///
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
Impl(Impl),
/// An `auto impl` implementation, such as a supertrait `auto impl`.
AutoImpl(Box<AutoImpl>),
/// An `extern impl` directive
ExternImpl(Box<ExternImpl>),
/// A macro invocation.
///
/// E.g., `foo!(..)`.
Expand Down Expand Up @@ -4013,6 +4033,8 @@ impl ItemKind {
| ItemKind::ForeignMod(_)
| ItemKind::GlobalAsm(_)
| ItemKind::Impl(_)
| ItemKind::AutoImpl(_)
| ItemKind::ExternImpl(_)
| ItemKind::MacCall(_)
| ItemKind::DelegationMac(_) => None,
}
Expand All @@ -4025,7 +4047,13 @@ impl ItemKind {
Use(..) | Static(..) | Const(..) | ConstBlock(..) | Fn(..) | Mod(..)
| GlobalAsm(..) | TyAlias(..) | Struct(..) | Union(..) | Trait(..) | TraitAlias(..)
| MacroDef(..) | Delegation(..) | DelegationMac(..) => "a",
ExternCrate(..) | ForeignMod(..) | MacCall(..) | Enum(..) | Impl { .. } => "an",
ExternCrate(..)
| ForeignMod(..)
| MacCall(..)
| Enum(..)
| Impl { .. }
| AutoImpl(..)
| ExternImpl(..) => "an",
}
}

Expand All @@ -4049,6 +4077,8 @@ impl ItemKind {
ItemKind::MacCall(..) => "item macro invocation",
ItemKind::MacroDef(..) => "macro definition",
ItemKind::Impl { .. } => "implementation",
ItemKind::AutoImpl { .. } => "`auto` implementation",
ItemKind::ExternImpl { .. } => "`extern` implementation",
ItemKind::Delegation(..) => "delegated function",
ItemKind::DelegationMac(..) => "delegation",
}
Expand All @@ -4064,6 +4094,8 @@ impl ItemKind {
| Self::Union(_, generics, _)
| Self::Trait(box Trait { generics, .. })
| Self::TraitAlias(box TraitAlias { generics, .. })
| Self::AutoImpl(box AutoImpl { generics, .. })
| Self::ExternImpl(box ExternImpl { generics, .. })
| Self::Impl(Impl { generics, .. }) => Some(generics),

Self::ExternCrate(..)
Expand Down Expand Up @@ -4107,6 +4139,10 @@ pub enum AssocItemKind {
Delegation(Box<Delegation>),
/// An associated list or glob delegation item.
DelegationMac(Box<DelegationMac>),
/// An `auto impl` item
AutoImpl(Box<AutoImpl>),
/// An `extern impl` item
ExternImpl(Box<ExternImpl>),
}

impl AssocItemKind {
Expand All @@ -4117,15 +4153,26 @@ impl AssocItemKind {
| AssocItemKind::Type(box TyAlias { ident, .. })
| AssocItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),

AssocItemKind::MacCall(_) | AssocItemKind::DelegationMac(_) => None,
AssocItemKind::MacCall(_)
| AssocItemKind::DelegationMac(_)
| AssocItemKind::AutoImpl(_)
| AssocItemKind::ExternImpl(_) => None,
}
}

pub fn defaultness(&self) -> Defaultness {
match *self {
Self::Const(box ConstItem { defaultness, .. })
| Self::Fn(box Fn { defaultness, .. })
| Self::Type(box TyAlias { defaultness, .. }) => defaultness,
| Self::Type(box TyAlias { defaultness, .. })
| Self::AutoImpl(box AutoImpl {
of_trait: box TraitImplHeader { defaultness, .. },
..
})
| Self::ExternImpl(box ExternImpl {
of_trait: box TraitImplHeader { defaultness, .. },
..
}) => defaultness,
Self::MacCall(..) | Self::Delegation(..) | Self::DelegationMac(..) => {
Defaultness::Final
}
Expand All @@ -4142,6 +4189,8 @@ impl From<AssocItemKind> for ItemKind {
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
AssocItemKind::Delegation(delegation) => ItemKind::Delegation(delegation),
AssocItemKind::DelegationMac(delegation) => ItemKind::DelegationMac(delegation),
AssocItemKind::AutoImpl(ai) => ItemKind::AutoImpl(ai),
AssocItemKind::ExternImpl(ei) => ItemKind::ExternImpl(ei),
}
}
}
Expand All @@ -4157,6 +4206,8 @@ impl TryFrom<ItemKind> for AssocItemKind {
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
ItemKind::Delegation(d) => AssocItemKind::Delegation(d),
ItemKind::DelegationMac(d) => AssocItemKind::DelegationMac(d),
ItemKind::AutoImpl(ai) => AssocItemKind::AutoImpl(ai),
ItemKind::ExternImpl(ei) => AssocItemKind::ExternImpl(ei),
_ => return Err(item_kind),
})
}
Expand Down
31 changes: 31 additions & 0 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ macro_rules! common_visitor_and_walkers {
//fn visit_assoc_item(AssocItem, _ctxt: AssocCtxt);
fn visit_assoc_item_constraint(AssocItemConstraint);
fn visit_attribute(Attribute);
fn visit_auto_impl(AutoImpl);
fn visit_extern_impl(ExternImpl);
fn visit_block(Block);
//fn visit_nested_use_tree((UseTree, NodeId));
fn visit_capture_by(CaptureBy);
Expand Down Expand Up @@ -845,6 +847,10 @@ macro_rules! common_visitor_and_walkers {
visit_visitable!($($mut)? vis, ident, generics, variant_data),
ItemKind::Impl(impl_) =>
visit_visitable!($($mut)? vis, impl_),
ItemKind::AutoImpl(auto_impl_) =>
visit_visitable!($($mut)? vis, auto_impl_),
ItemKind::ExternImpl(extern_impl_) =>
visit_visitable!($($mut)? vis, extern_impl_),
ItemKind::Trait(trait_) =>
visit_visitable!($($mut)? vis, trait_),
ItemKind::TraitAlias(box TraitAlias { constness, ident, generics, bounds}) => {
Expand Down Expand Up @@ -890,6 +896,10 @@ macro_rules! common_visitor_and_walkers {
visit_visitable!($($mut)? vis, delegation),
AssocItemKind::DelegationMac(dm) =>
visit_visitable!($($mut)? vis, dm),
AssocItemKind::AutoImpl(ai) =>
visit_visitable!($($mut)? vis, ai),
AssocItemKind::ExternImpl(ei) =>
visit_visitable!($($mut)? vis, ei),
}
V::Result::output()
}
Expand Down Expand Up @@ -954,6 +964,27 @@ macro_rules! common_visitor_and_walkers {
V::Result::output()
});

impl_walkable!(|&$($mut)? $($lt)? self: AutoImpl, vis: &mut V| {
let AutoImpl { generics, of_trait, items, constness } = self;
let TraitImplHeader { defaultness, safety, polarity, trait_ref } = &$($mut)? **of_trait;

try_visit!(vis.visit_generics(generics));
visit_visitable!($($mut)? vis, defaultness, safety, constness, polarity, trait_ref);

visit_visitable_with!($($mut)? vis, items, AssocCtxt::Impl { of_trait: true });
V::Result::output()
});

impl_walkable!(|&$($mut)? $($lt)? self: ExternImpl, vis: &mut V| {
let ExternImpl { generics, of_trait } = self;
let TraitImplHeader { defaultness, safety, polarity, trait_ref } = &$($mut)? **of_trait;

try_visit!(vis.visit_generics(generics));
visit_visitable!($($mut)? vis, defaultness, safety, polarity, trait_ref);

V::Result::output()
});

// Special case to call `visit_method_receiver_expr`.
impl_walkable!(|&$($mut)? $($lt)? self: MethodCall, vis: &mut V| {
let MethodCall { seg, receiver, args, span } = self;
Expand Down
75 changes: 74 additions & 1 deletion compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_abi::ExternAbi;
use rustc_ast::visit::AssocCtxt;
use rustc_ast::*;
use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
use rustc_errors::{E0570, ErrorGuaranteed, FatalError, struct_span_code_err};
use rustc_hir::attrs::{AttributeKind, EiiImplResolution};
use rustc_hir::def::{DefKind, PerNS, Res};
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
Expand All @@ -11,6 +11,7 @@ use rustc_hir::{
use rustc_index::{IndexSlice, IndexVec};
use rustc_middle::span_bug;
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
use rustc_session::parse::feature_err;
use rustc_span::def_id::DefId;
use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
Expand Down Expand Up @@ -89,6 +90,12 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
}
AstOwner::AssocItem(item, ctxt) => {
if matches!(item.kind, AssocItemKind::ExternImpl(_)) {
// We do not lower `extern unsafe? impl` items.
// They just discharge the `auto impl` obligation from the current
// `impl` block.
return;
}
self.with_lctx(item.id, |lctx| lctx.lower_assoc_item(item, ctxt))
}
AstOwner::ForeignItem(item) => self.with_lctx(item.id, |lctx| {
Expand Down Expand Up @@ -216,6 +223,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
| ItemKind::Trait(..)
| ItemKind::TraitAlias(..)
| ItemKind::Impl(..)
| ItemKind::AutoImpl(..)
| ItemKind::ExternImpl(..)
| ItemKind::MacCall(..)
| ItemKind::MacroDef(..)
| ItemKind::Delegation(..)
Expand Down Expand Up @@ -508,6 +517,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
constness,
})
}
ItemKind::AutoImpl(..) | ItemKind::ExternImpl(..) => {
feature_err(
self.tcx.sess,
sym::supertrait_auto_impl,
span,
"feature is under construction",
)
.emit();
FatalError.raise()
}
ItemKind::Trait(box Trait {
constness,
is_auto,
Expand Down Expand Up @@ -1028,6 +1047,50 @@ impl<'hir> LoweringContext<'_, 'hir> {
true,
)
}
AssocItemKind::AutoImpl(ai) => {
let tcx = self.tcx;
if !tcx.features().supertrait_auto_impl() {
feature_err(
&tcx.sess,
sym::supertrait_auto_impl,
i.span,
"feature is under construction",
)
.emit();
FatalError.raise();
}
let generics = tcx.arena.alloc(hir::Generics {
has_where_clause_predicates: false,
params: &[],
predicates: &[],
where_clause_span: DUMMY_SP,
span: DUMMY_SP,
});
(
Ident::dummy(),
&*generics,
hir::TraitItemKind::AutoImpl(
tcx.arena.alloc(self.lower_poly_trait_ref_inner(
&ai.generics.params,
&TraitBoundModifiers {
constness: BoundConstness::Never,
asyncness: BoundAsyncness::Normal,
polarity: match ai.of_trait.polarity {
ImplPolarity::Positive => BoundPolarity::Positive,
ImplPolarity::Negative(span) => BoundPolarity::Negative(span),
},
},
&ai.of_trait.trait_ref,
ai.of_trait.trait_ref.path.span,
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::SuperTrait),
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
)),
&[],
),
false,
)
}
AssocItemKind::ExternImpl(..) => unreachable!("we should never lower ast::ExternImpl"),
AssocItemKind::Type(box TyAlias {
ident,
generics,
Expand Down Expand Up @@ -1233,6 +1296,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
),
)
}
AssocItemKind::AutoImpl(_) | AssocItemKind::ExternImpl(_) => {
feature_err(
&self.tcx.sess,
sym::supertrait_auto_impl,
i.span,
"feature is under construction",
)
.emit();
FatalError.raise();
}
AssocItemKind::Delegation(box delegation) => {
let delegation_results = self.lower_delegation(delegation, i.id);
(
Expand Down
24 changes: 22 additions & 2 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2110,21 +2110,41 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
PolyTraitRef { bound_generic_params, modifiers, trait_ref, span, parens: _ }: &PolyTraitRef,
rbp: RelaxedBoundPolicy<'_>,
itctx: ImplTraitContext,
) -> hir::PolyTraitRef<'hir> {
self.lower_poly_trait_ref_inner(
bound_generic_params,
modifiers,
trait_ref,
*span,
rbp,
itctx,
)
}

#[instrument(level = "debug", skip(self))]
fn lower_poly_trait_ref_inner(
&mut self,
bound_generic_params: &[GenericParam],
modifiers: &TraitBoundModifiers,
trait_ref: &TraitRef,
span: Span,
rbp: RelaxedBoundPolicy<'_>,
itctx: ImplTraitContext,
) -> hir::PolyTraitRef<'hir> {
let bound_generic_params =
self.lower_lifetime_binder(trait_ref.ref_id, bound_generic_params);
let trait_ref = self.lower_trait_ref(*modifiers, trait_ref, itctx);
let modifiers = self.lower_trait_bound_modifiers(*modifiers);

if let ast::BoundPolarity::Maybe(_) = modifiers.polarity {
self.validate_relaxed_bound(trait_ref, *span, rbp);
self.validate_relaxed_bound(trait_ref, span, rbp);
}

hir::PolyTraitRef {
bound_generic_params,
modifiers,
trait_ref,
span: self.lower_span(*span),
span: self.lower_span(span),
}
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_ast_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ ast_passes_auto_generic = auto traits cannot have generic parameters
.label = auto trait cannot have generic parameters
.suggestion = remove the parameters

ast_passes_auto_impl_outside_trait_or_impl_trait =
`auto impl` is outside a `trait` block or trait-`impl` block

ast_passes_auto_items = auto traits cannot have associated items
.label = {ast_passes_auto_items}
.suggestion = remove the associated items
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}
visit::walk_item(self, item);
}

ItemKind::AutoImpl(..) => {
self.dcx().emit_err(errors::AutoImplOutsideTraitOrImplTrait { span: item.span });
}

_ => visit::walk_item(self, item),
}

Expand Down
Loading
Loading