Skip to content

Conversation

@dingxiangfei2009
Copy link
Contributor

@dingxiangfei2009 dingxiangfei2009 commented Nov 25, 2025

Tracking:

This patch introduce AST elements for auto impl inside the trait and impl block.

This patch does not handle the name resolution, yet. It will be handled in the next patch series.

RFC: rust-lang/rfcs#3851

cc

As a tracking issue is pending, I will link rust-lang/rust-project-goals#393 for more context.

@rustbot
Copy link
Collaborator

rustbot commented Nov 25, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a

HIR ty lowering was modified

cc @fmease

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue. labels Nov 25, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 25, 2025

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

This patch introduce AST elements for `auto impl`
inside the `trait` and `impl` block.

This patch does not handle the name resolution, yet.
It will be handled in the next patch series.

Signed-off-by: Xiangfei Ding <[email protected]>
Copy link
Contributor

@ytmimi ytmimi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a simple formatting test for the AutoImpl changes added to rustfmt.

View changes since this review

@dingxiangfei2009
Copy link
Contributor Author

I think the priority here is to first get parser code proof-read.

r? @petrochenkov

@rustbot rustbot assigned petrochenkov and unassigned fee1-dead Dec 1, 2025
AssocKind::Const { .. } => Const,
AssocKind::Type { .. } => Type,
AssocKind::Fn { .. } => Fn,
AssocKind::AutoImpl => todo!(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really comfortable with a todo!() here. Please add a new variant to SourceItemOrderingTraitAssocItemKind

@petrochenkov
Copy link
Contributor

petrochenkov commented Dec 2, 2025

It's probably more a language-related comment, than compiler-related, but the placement of auto impls doesn't seem right.

Auto impls are not associated items, they could very well live as free items, and placed into the traits just for the proximity.
Ideologically it would be similar if we supported placing inherent impls for structs into the struct body, C++ style (which is requested sometimes). We do not do that to support separation of data and implementation, here it would make sense to support separation of interfaces and implementation as well.
There are many comments on the RFC, so I'm not sure if it was already discussed.

Even from the compiler point of view in HIR and below we'd now need to separate real associated items from the things that just live there in source code, and may break some other assumptions across the compiler about only one level of associated item nesting existing, making building the prototype harder.

@petrochenkov
Copy link
Contributor

The semicolon in auto impl SuperTrait {} -> auto impl SuperTrait; is an entirely orthogonal and optional feature.
It's not specific to auto impls and was proposed for regular impls before, but rejected.
I'd rather not even put this sugar into the RFC at this stage.

@petrochenkov
Copy link
Contributor

Could you add some tests executing all the supported syntax, including const, of_trait and generics?

| ItemKind::DelegationMac(_)
| ItemKind::MacroDef(..) => None,
ItemKind::Static(_) => None,
ItemKind::AutoImpl(_) => None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are generics in AutoImpl, but this returns None.

self.print_assoc_item(impl_item);
}
let empty = item.attrs.is_empty() && items.is_empty();
self.bclose(item.span, empty, cb);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print_assoc_auto_impl was supposed to be reused here?

/// Allows `super let` statements.
(unstable, super_let, "1.88.0", Some(139076)),
/// Allows supertraits to be implemented with automatic defaults
(unstable, supertrait_auto_impl, "CURRENT_RUSTC_VERSION", Some(99999)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no tracking issue for the experiment?
Probably need a lang team approval and a tracking issue to start.

Copy link
Contributor

@traviscross traviscross Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @petrochenkov for raising this. You're right; this should have a tracking issue documenting the lang experiment and its champion. Talked with @dingxiangfei2009, after he pinged me, and he's filing the tracking issue for this, which we'll nominate and discuss in a meeting.

self.r
.tcx()
.dcx()
.emit_err(errors::AutoImplOutsideTraitOrImplTrait { span: item.span });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This belongs to ast_validation.rs, or AST lowering, or something like that.

}
AssocItemKind::AutoImpl(_ai) => {
let tcx = self.r.tcx();
if !tcx.features().supertrait_auto_impl() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a new syntax, feature gating needs to be done in parser using gated_spans.

@petrochenkov
Copy link
Contributor

(I'll continue the review tomorrow.)

constness,
})
}
ItemKind::AutoImpl(box AutoImpl { .. }) => todo!("we should implement lowering to HIR"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you report a regular error instead of an ICE here and for AssocItemKind::AutoImpl?
Upd: and in other crates too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or report a fatal error in lowering and turn todo!s into unreachable!s in the later compilation stages.

AssocItemKind::AutoImpl(ai) => {
let tcx = self.tcx;
if !tcx.features().supertrait_auto_impl() {
feature_err(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feature gating once in the parser should be enough.

// Re. GATs and GACs (generic_const_items), we're not bound by backward compatibility.
ImplItemKind::Const(..) | ImplItemKind::Type(..) => ParamDefaultPolicy::Forbidden,
ImplItemKind::Fn(..) => ParamDefaultPolicy::FutureCompatForbidden,
ImplItemKind::AutoImpl(..) => ParamDefaultPolicy::Forbidden,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ImplItemKind::AutoImpl(..) => ParamDefaultPolicy::Forbidden,
ImplItemKind::AutoImpl(..) | ImplItemKind::ExternImpl(..) => ParamDefaultPolicy::Forbidden,

hir::ImplItemKind::AutoImpl(poly_trait_ref, impl_items) => {
let (cb, ib) = self.head("");
self.word_nbsp("auto");
self.word_nbsp("impl");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe factor out the common parts like in AST printing?

/// An `auto impl` implementation
AutoImpl(&'hir PolyTraitRef<'hir>, &'hir [ImplItem<'hir>]),
/// An `extern impl` directive
ExternImpl(&'hir PolyTraitRef<'hir>),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is ExternImpl actually generated?
I don't see being produced by AST lowering.

let (check_ty, is_assoc_ty) = match item.kind {
ty::AssocKind::Const { .. } | ty::AssocKind::Fn { .. } => (true, false),
ty::AssocKind::Type { .. } => (item.defaultness(self.tcx).has_value(), true),
ty::AssocKind::AutoImpl => return,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure the auto impls should be privacy-checked here too.

if ctxt == AssocCtxt::Trait {
if let AssocItemKind::AutoImpl(_) = item.kind {
// We do not define any local names.
// We will now recursively descend into `auto impl`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? This doesn't seem right.

self.bump(); // `union`
self.parse_item_union()?
} else if self.check_keyword(exp!(Unsafe))
&& self.is_keyword_ahead(1, &[kw::Auto, kw::Impl])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This catches non-auto unsafe impl.

@petrochenkov
Copy link
Contributor

After reading the PR I'd again suggest to implement these impls as free items first, because you'll have a whole host of issues just from trying to move them from free to associated items.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 3, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 3, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@BoxyUwU BoxyUwU self-assigned this Dec 7, 2025
}
ty::AssocKind::Const { .. } => None,
// This does not change judgement on GAT
ty::AssocKind::AutoImpl => continue,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want the type system to have to care about a new assoc kind. Also please get types approval before having this merged (I've assigned myself). Why is this PR doing more than just parser stuff like the title and PR description indicates?

@traviscross traviscross added the I-lang-radar Items that are on lang's radar and will need eventual work or consideration. label Dec 10, 2025
@bors
Copy link
Collaborator

bors commented Dec 14, 2025

☔ The latest upstream changes (presumably #146348) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) I-lang-radar Items that are on lang's radar and will need eventual work or consideration. PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants