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
6 changes: 6 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3921,6 +3921,12 @@ pub struct Delegation {
pub from_glob: bool,
}

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

self.path.segments.last().unwrap().ident.span
}
}

#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub enum DelegationSuffixes {
List(ThinVec<(Ident, Option<Ident>)>),
Expand Down
105 changes: 30 additions & 75 deletions compiler/rustc_ast_lowering/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,17 @@ use hir::{BodyId, HirId};
use rustc_abi::ExternAbi;
use rustc_ast as ast;
use rustc_ast::*;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::ErrorGuaranteed;
use rustc_hir::attrs::{AttributeKind, InlineAttr};
use rustc_hir::def_id::DefId;
use rustc_hir::{self as hir, FnDeclFlags};
use rustc_middle::span_bug;
use rustc_middle::ty::Asyncness;
use rustc_span::symbol::kw;
use rustc_span::{Ident, Span, Symbol};
use smallvec::SmallVec;

use crate::delegation::generics::{GenericsGenerationResult, GenericsGenerationResults};
use crate::errors::{CycleInDelegationSignatureResolution, UnresolvedDelegationCallee};
use crate::{
AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
ResolverAstLoweringExt,
};

mod generics;
Expand Down Expand Up @@ -119,13 +114,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
delegation: &Delegation,
item_id: NodeId,
) -> DelegationResults<'hir> {
let span = self.lower_span(delegation.path.segments.last().unwrap().ident.span);
let span = self.lower_span(delegation.span());

let sig_id =
self.tcx.delegations_resolutions(()).get(&self.owner.def_id).copied().flatten();

// Delegation can be unresolved in illegal places such as function bodies in extern blocks (see #151356)
let sig_id = if let Some(delegation_info) = self.resolver.delegation_info(self.owner.def_id)
{
self.get_sig_id(delegation_info.resolution_id, span)
} else {
let Some(sig_id) = sig_id else {
self.dcx().span_delayed_bug(
span,
format!("LoweringContext: the delegation {:?} is unresolved", item_id),
Expand All @@ -134,49 +129,39 @@ impl<'hir> LoweringContext<'_, 'hir> {
return self.generate_delegation_error(span, delegation);
};

match sig_id {
Ok(sig_id) => {
self.add_attrs_if_needed(span, sig_id);
self.add_attrs_if_needed(span, sig_id);

let is_method = self.is_method(sig_id, span);
let is_method = self.is_method(sig_id, span);

let (param_count, c_variadic) = self.param_count(sig_id);
let (param_count, c_variadic) = self.param_count(sig_id);

let mut generics = self.uplift_delegation_generics(delegation, sig_id, is_method);
let mut generics = self.uplift_delegation_generics(delegation, sig_id, is_method);

let (body_id, call_expr_id) = self.lower_delegation_body(
delegation,
is_method,
param_count,
&mut generics,
span,
);
let (body_id, call_expr_id) =
self.lower_delegation_body(delegation, is_method, param_count, &mut generics, span);

let decl = self.lower_delegation_decl(
sig_id,
param_count,
c_variadic,
span,
&generics,
delegation.id,
call_expr_id,
);
let decl = self.lower_delegation_decl(
sig_id,
param_count,
c_variadic,
span,
&generics,
delegation.id,
call_expr_id,
);

let sig = self.lower_delegation_sig(sig_id, decl, span);
let ident = self.lower_ident(delegation.ident);
let sig = self.lower_delegation_sig(sig_id, decl, span);
let ident = self.lower_ident(delegation.ident);

let generics = self.arena.alloc(hir::Generics {
has_where_clause_predicates: false,
params: self.arena.alloc_from_iter(generics.all_params()),
predicates: self.arena.alloc_from_iter(generics.all_predicates()),
span,
where_clause_span: span,
});
let generics = self.arena.alloc(hir::Generics {
has_where_clause_predicates: false,
params: self.arena.alloc_from_iter(generics.all_params()),
predicates: self.arena.alloc_from_iter(generics.all_predicates()),
span,
where_clause_span: span,
});

DelegationResults { body_id, sig, ident, generics }
}
Err(_) => self.generate_delegation_error(span, delegation),
}
DelegationResults { body_id, sig, ident, generics }
}

fn add_attrs_if_needed(&mut self, span: Span, sig_id: DefId) {
Expand Down Expand Up @@ -230,36 +215,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
.collect::<Vec<_>>()
}

fn get_sig_id(&self, mut def_id: DefId, span: Span) -> Result<DefId, ErrorGuaranteed> {
let mut visited: FxHashSet<DefId> = Default::default();
let mut path: SmallVec<[DefId; 1]> = Default::default();

loop {
visited.insert(def_id);

path.push(def_id);

// If def_id is in local crate and it corresponds to another delegation
// it means that we refer to another delegation as a callee, so in order to obtain
// a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
if let Some(local_id) = def_id.as_local()
&& let Some(delegation_info) = self.resolver.delegation_info(local_id)
{
def_id = delegation_info.resolution_id;
if visited.contains(&def_id) {
// We encountered a cycle in the resolution, or delegation callee refers to non-existent
// entity, in this case emit an error.
return Err(match visited.len() {
1 => self.dcx().emit_err(UnresolvedDelegationCallee { span }),
_ => self.dcx().emit_err(CycleInDelegationSignatureResolution { span }),
});
}
} else {
return Ok(path[0]);
}
}
}

fn get_resolution_id(&self, node_id: NodeId) -> Option<DefId> {
self.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id())
}
Expand Down
90 changes: 80 additions & 10 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ use rustc_ast::visit::Visitor;
use rustc_ast::{self as ast, *};
use rustc_attr_parsing::{AttributeParser, OmitDoc, Recovery, ShouldEmit};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxIndexSet;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::sorted_map::SortedMap;
use rustc_data_structures::stable_hash::{StableHash, StableHasher};
use rustc_data_structures::steal::Steal;
use rustc_data_structures::tagged_ptr::TaggedRef;
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle};
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE, LocalDefId};
use rustc_hir::definitions::PerParentDisambiguatorState;
use rustc_hir::lints::DelayedLint;
use rustc_hir::{
Expand All @@ -69,7 +69,10 @@ use smallvec::SmallVec;
use thin_vec::ThinVec;
use tracing::{debug, instrument, trace};

use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait};
use crate::errors::{
AssocTyParentheses, AssocTyParenthesesSub, CycleInDelegationSignatureResolution,
MisplacedImplTrait, UnresolvedDelegationCallee,
};
use crate::item::Owners;

macro_rules! arena_vec {
Expand Down Expand Up @@ -541,13 +544,11 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> mid_hir::Crate<'_> {
let mut delayed_ids: FxIndexSet<LocalDefId> = Default::default();

for def_id in ast_index.indices() {
match &ast_index[def_id] {
AstOwner::Item(Item { kind: ItemKind::Delegation { .. }, .. })
| AstOwner::AssocItem(Item { kind: AssocItemKind::Delegation { .. }, .. }, _) => {
delayed_ids.insert(def_id);
}
_ => lowerer.lower_node(def_id),
};
if opt_delegation(&ast_index[def_id]).is_some() {
delayed_ids.insert(def_id);
} else {
lowerer.lower_node(def_id);
}
}

// Don't hash unless necessary, because it's expensive.
Expand All @@ -558,6 +559,16 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> mid_hir::Crate<'_> {
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

}
_ => None,
}
}

/// Lowers an AST owner corresponding to `def_id`, now only delegations are lowered this way.
pub fn lower_delayed_owner(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let krate = tcx.hir_crate(());
Expand All @@ -583,6 +594,65 @@ pub fn lower_delayed_owner(tcx: TyCtxt<'_>, def_id: LocalDefId) {
}
}

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

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

let krate = tcx.hir_crate(());

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

let ast_index = index_crate(resolver, ast_crate);

let mut result = FxIndexMap::<LocalDefId, Option<DefId>>::default();

for &def_id in &krate.delayed_ids {
let delegation = opt_delegation(&ast_index[def_id]).expect("processing delegations");
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

.and_then(|info| resolve_delegation(tcx, info.resolution_id, span));

result.insert(def_id, res_id);
}

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

let mut visited: FxHashSet<DefId> = Default::default();
let mut path: SmallVec<[DefId; 1]> = Default::default();

let (resolver, _) = &*tcx.hir_crate(()).delayed_resolver.borrow();

loop {
visited.insert(def_id);

path.push(def_id);

// If def_id is in local crate and it corresponds to another delegation
// it means that we refer to another delegation as a callee, so in order to obtain
// a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
if let Some(local_id) = def_id.as_local()
&& let Some(delegation_info) = resolver.delegation_info(local_id)
{
def_id = delegation_info.resolution_id;
if visited.contains(&def_id) {
// We encountered a cycle in the resolution, or delegation callee refers to non-existent
// entity, in this case emit an error.
match visited.len() {
1 => tcx.dcx().emit_err(UnresolvedDelegationCallee { span }),
_ => tcx.dcx().emit_err(CycleInDelegationSignatureResolution { span }),
};

return None;
}
} else {
return Some(path[0]);
}
}
}

#[derive(Copy, Clone, PartialEq, Debug)]
enum ParamMode {
/// Any path in a type context.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
providers.queries.analysis = analysis;
providers.queries.hir_crate = rustc_ast_lowering::lower_to_hir;
providers.queries.lower_delayed_owner = rustc_ast_lowering::lower_delayed_owner;
providers.queries.delegations_resolutions = rustc_ast_lowering::delegations_resolutions;
// `hir_delayed_owner` is fed during `lower_delayed_owner`, by default it returns phantom,
// as if this query was not fed it means that `MaybeOwner` does not exist for provided LocalDefId.
providers.queries.hir_delayed_owner = |_, _| MaybeOwner::Phantom;
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/hir/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,8 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalModDefId) -> Mod
}

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


let krate = tcx.hir_crate(());
for &id in &krate.delayed_ids {
tcx.ensure_done().lower_delayed_owner(id);
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,12 @@ rustc_queries! {
desc { "inheriting delegation signature" }
}

query delegations_resolutions(_: ()) -> &'tcx FxIndexMap<LocalDefId, Option<DefId>> {
arena_cache
eval_always
desc { "getting delegations resolutions" }
}

/// Does lifetime resolution on items. Importantly, we can't resolve
/// lifetimes directly on things like trait methods, because of trait params.
/// See `rustc_resolve::late::lifetimes` for details.
Expand Down
Loading