Skip to content

delegation: split resolution and lowering#157296

Open
aerooneqq wants to merge 1 commit into
rust-lang:mainfrom
aerooneqq:delegation-extract-resolve
Open

delegation: split resolution and lowering#157296
aerooneqq wants to merge 1 commit into
rust-lang:mainfrom
aerooneqq:delegation-extract-resolve

Conversation

@aerooneqq
Copy link
Copy Markdown
Contributor

@aerooneqq aerooneqq commented Jun 2, 2026

This PR splits delegation's AST -> HIR lowering and its resolution. Now we resolve all delegations and then lower them. This should have benefits:

Part of #118212.
r? @petrochenkov

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 2, 2026
@aerooneqq aerooneqq changed the title delegation: split delegation resolution and lowering delegation: split resolution and lowering Jun 2, 2026
@petrochenkov petrochenkov added the F-fn_delegation `#![feature(fn_delegation)]` label Jun 2, 2026
}

impl Delegation {
pub fn span(&self) -> Span {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Jun 2, 2026

Choose a reason for hiding this comment

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

Suggested change
pub fn span(&self) -> Span {
pub fn last_segment_span(&self) -> Span {

Nit: the name is too generic, this is some very specific span inside the delegation, not the whole delegation span.

View changes since the review

mid_hir::Crate::new(owners, delayed_ids, delayed_resolver, opt_hir_hash)
}

fn opt_delegation<'a>(ast_owner: &'a AstOwner<'a>) -> Option<&'a Delegation> {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Jun 2, 2026

Choose a reason for hiding this comment

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

This could be a method on AstOwner.
Also could rename this to just delegation because there's no non-opt version.

View changes since the review

match ast_owner {
AstOwner::Item(Item { kind: ItemKind::Delegation(d), .. })
| AstOwner::AssocItem(Item { kind: AssocItemKind::Delegation(d), .. }, _) => {
Some(d.as_ref())
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Jun 2, 2026

Choose a reason for hiding this comment

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

Suggested change
Some(d.as_ref())
Some(d)

Unnecessary?

View changes since the review

let (resolver, ast_crate) = &*krate.delayed_resolver.borrow();

// FIXME!!!(fn_delegation): make ast index lifetime same as resolver,
// as it is too bad to reindex whole crate on each delegation lowering.
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Jun 2, 2026

Choose a reason for hiding this comment

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

Sigh, we are doing it again.

View changes since the review

}
}

pub fn delegations_resolutions(tcx: TyCtxt<'_>, _: ()) -> FxIndexMap<LocalDefId, Option<DefId>> {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Jun 2, 2026

Choose a reason for hiding this comment

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

Suggested change
pub fn delegations_resolutions(tcx: TyCtxt<'_>, _: ()) -> FxIndexMap<LocalDefId, Option<DefId>> {
pub fn delegations_resolutions(tcx: TyCtxt<'_>, _: ()) -> FxIndexMap<LocalDefId, Result<DefId, ErrorGuaranteed>> {

Otherwise it's confusing why the Nones are in the table at all.
Can we get the semantics "not in the table - not delegation, Err in the table - unsuccessfully resolved delegation" here?

View changes since the review

let span = delegation.span();

let res_id = resolver
.delegation_info(def_id)
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Jun 2, 2026

Choose a reason for hiding this comment

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

Same with delegation_infos, can we keep Result<..., ErrorGuaranteed>s in them?
Otherwise it's not clear what not being in the table means.

View changes since the review

}
}

pub fn delegations_resolutions(tcx: TyCtxt<'_>, _: ()) -> FxIndexMap<LocalDefId, Option<DefId>> {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Jun 2, 2026

Choose a reason for hiding this comment

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

It's probably better to move this function to its old place in delegation.rs.

View changes since the review

result
}

fn resolve_delegation(tcx: TyCtxt<'_>, mut def_id: DefId, span: Span) -> Option<DefId> {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Jun 2, 2026

Choose a reason for hiding this comment

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

Suggested change
fn resolve_delegation(tcx: TyCtxt<'_>, mut def_id: DefId, span: Span) -> Option<DefId> {
fn resolve_delegation(tcx: TyCtxt<'_>, mut def_id: DefId, span: Span) -> Result<DefId, ErrorGuaranteed> {

Same as above.

View changes since the review

}

fn force_delayed_owners_lowering(tcx: TyCtxt<'_>) {
tcx.ensure_done().delegations_resolutions(());
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Jun 2, 2026

Choose a reason for hiding this comment

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

Is it necessary to ensure this here?
What happens if delegations_resolutions is first called from some lower_delayed_owner below?

View changes since the review

@petrochenkov petrochenkov 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 Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-fn_delegation `#![feature(fn_delegation)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants