compiler/rustc_ast_lowering/src/delegation.rs RUST 773 lines View on github.com → Search inside
1//! This module implements expansion of delegation items with early resolved paths.2//! It includes a delegation to a free functions:3//!4//! ```ignore (illustrative)5//! reuse module::name { target_expr_template }6//! ```7//!8//! And delegation to a trait methods:9//!10//! ```ignore (illustrative)11//! reuse <Type as Trait>::name { target_expr_template }12//! ```13//!14//! After expansion for both cases we get:15//!16//! ```ignore (illustrative)17//! fn name(18//!     arg0: InferDelegation(sig_id, Input(0)),19//!     arg1: InferDelegation(sig_id, Input(1)),20//!     ...,21//!     argN: InferDelegation(sig_id, Input(N)),22//! ) -> InferDelegation(sig_id, Output) {23//!     callee_path(target_expr_template(arg0), arg1, ..., argN)24//! }25//! ```26//!27//! Where `callee_path` is a path in delegation item e.g. `<Type as Trait>::name`.28//! `sig_id` is a id of item from which the signature is inherited. It may be a delegation29//! item id (`item_id`) in case of impl trait or path resolution id (`path_id`) otherwise.30//!31//! Since we do not have a proper way to obtain function type information by path resolution32//! in AST, we mark each function parameter type as `InferDelegation` and inherit it during33//! HIR ty lowering.34//!35//! Similarly generics, predicates and header are set to the "default" values.36//! In case of discrepancy with callee function the `UnsupportedDelegation` error will37//! also be emitted during HIR ty lowering.3839use std::iter;40use std::ops::ControlFlow;4142use ast::visit::Visitor;43use hir::def::{DefKind, Res};44use hir::{BodyId, HirId};45use rustc_abi::ExternAbi;46use rustc_ast as ast;47use rustc_ast::node_id::NodeMap;48use rustc_ast::*;49use rustc_data_structures::fx::FxHashSet;50use rustc_hir::attrs::{AttributeKind, InlineAttr};51use rustc_hir::{self as hir, FnDeclFlags};52use rustc_middle::span_bug;53use rustc_middle::ty::{Asyncness, PerOwnerResolverData};54use rustc_span::def_id::{DefId, LocalDefId};55use rustc_span::symbol::kw;56use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};5758use crate::delegation::generics::{GenericsGenerationResult, GenericsGenerationResults};59use crate::diagnostics::{60    CycleInDelegationSignatureResolution, DelegationAttemptedBlockWithDefsDeletion,61    DelegationBlockSpecifiedWhenNoParams, UnresolvedDelegationCallee,62};63use crate::{64    AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,65};6667mod generics;6869pub(crate) struct DelegationResults<'hir> {70    pub body_id: hir::BodyId,71    pub sig: hir::FnSig<'hir>,72    pub ident: Ident,73    pub generics: &'hir hir::Generics<'hir>,74}7576struct AttrAdditionInfo {77    pub equals: fn(&hir::Attribute) -> bool,78    pub kind: AttrAdditionKind,79}8081enum AttrAdditionKind {82    Default { factory: fn(Span) -> hir::Attribute },83    Inherit { factory: fn(Span, &hir::Attribute) -> hir::Attribute },84}8586/// Summary info about function parameters.87#[derive(Debug, Clone, Copy, Eq, PartialEq)]88struct ParamInfo {89    /// The number of function parameters, including any C variadic `...` parameter.90    pub param_count: usize,9192    /// Whether the function arguments end in a C variadic `...` parameter.93    pub c_variadic: bool,9495    /// The index of the splatted parameter, if any.96    pub splatted: Option<u16>,97}9899const PARENT_ID: hir::ItemLocalId = hir::ItemLocalId::ZERO;100101static ATTRS_ADDITIONS: &[AttrAdditionInfo] = &[102    AttrAdditionInfo {103        equals: |a| matches!(a, hir::Attribute::Parsed(AttributeKind::MustUse { .. })),104        kind: AttrAdditionKind::Inherit {105            factory: |span, original_attr| {106                let reason = match original_attr {107                    hir::Attribute::Parsed(AttributeKind::MustUse { reason, .. }) => *reason,108                    _ => None,109                };110111                hir::Attribute::Parsed(AttributeKind::MustUse { span, reason })112            },113        },114    },115    AttrAdditionInfo {116        equals: |a| matches!(a, hir::Attribute::Parsed(AttributeKind::Inline(..))),117        kind: AttrAdditionKind::Default {118            factory: |span| hir::Attribute::Parsed(AttributeKind::Inline(InlineAttr::Hint, span)),119        },120    },121];122123impl<'hir> LoweringContext<'_, 'hir> {124    fn is_method(&self, def_id: DefId, span: Span) -> bool {125        match self.tcx.def_kind(def_id) {126            DefKind::Fn => false,127            DefKind::AssocFn => self.tcx.associated_item(def_id).is_method(),128            _ => span_bug!(span, "unexpected DefKind for delegation item"),129        }130    }131132    fn check_for_cycles(&self, mut def_id: DefId, span: Span) -> Result<(), ErrorGuaranteed> {133        let mut visited: FxHashSet<DefId> = Default::default();134135        loop {136            visited.insert(def_id);137138            // If def_id is in local crate and it corresponds to another delegation139            // it means that we refer to another delegation as a callee, so in order to obtain140            // a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.141            if let Some(local_id) = def_id.as_local()142                && let Some(info) = self.tcx.resolutions(()).delegation_infos.get(&local_id)143                && let Ok(id) = info.resolution_id144            {145                def_id = id;146                if visited.contains(&def_id) {147                    return Err(match visited.len() {148                        1 => self.dcx().emit_err(UnresolvedDelegationCallee { span }),149                        _ => self.dcx().emit_err(CycleInDelegationSignatureResolution { span }),150                    });151                }152            } else {153                return Ok(());154            }155        }156    }157158    pub(crate) fn lower_delegation(159        &mut self,160        delegation: &Delegation,161        item_id: NodeId,162    ) -> DelegationResults<'hir> {163        let span = self.lower_span(delegation.last_segment_span());164165        let Some(info) = self.tcx.resolutions(()).delegation_infos.get(&self.owner.def_id) else {166            self.dcx().span_delayed_bug(167                span,168                format!("delegation resolution record was not found for {:?}", self.owner.def_id),169            );170171            return self.generate_delegation_error(span, delegation);172        };173174        let sig_id = info.resolution_id.and_then(|id| self.check_for_cycles(id, span).map(|_| id));175176        // Delegation can be missing from the `delegations_resolutions` table177        // in illegal places such as function bodies in extern blocks (see #151356).178        let Ok(sig_id) = sig_id else {179            self.dcx().span_delayed_bug(180                span,181                format!("LoweringContext: the delegation {:?} is unresolved", item_id),182            );183184            return self.generate_delegation_error(span, delegation);185        };186187        self.add_attrs_if_needed(span, sig_id);188189        let is_method = self.is_method(sig_id, span);190191        let param_info = self.param_info(sig_id);192193        if !self.check_block_soundness(delegation, sig_id, is_method, param_info.param_count) {194            return self.generate_delegation_error(span, delegation);195        }196197        let mut generics = self.uplift_delegation_generics(delegation, sig_id, is_method);198199        let (body_id, call_expr_id, unused_target_expr) = self.lower_delegation_body(200            delegation,201            sig_id,202            param_info.param_count,203            &mut generics,204            span,205        );206207        let decl = self.lower_delegation_decl(208            delegation.source,209            sig_id,210            param_info,211            span,212            &generics,213            delegation.id,214            call_expr_id,215            unused_target_expr,216        );217218        let sig = self.lower_delegation_sig(sig_id, decl, span);219        let ident = self.lower_ident(delegation.ident);220221        let generics = self.arena.alloc(hir::Generics {222            has_where_clause_predicates: false,223            params: self.arena.alloc_from_iter(generics.all_params()),224            predicates: self.arena.alloc_from_iter(generics.all_predicates()),225            span,226            where_clause_span: span,227        });228229        DelegationResults { body_id, sig, ident, generics }230    }231232    fn check_block_soundness(233        &self,234        delegation: &Delegation,235        sig_id: DefId,236        is_method: bool,237        param_count: usize,238    ) -> bool {239        let Some(block) = delegation.body.as_ref() else { return true };240        let should_generate_block = self.should_generate_block(delegation, sig_id, is_method);241242        // Report an error if user has explicitly specified delegation's target expression243        // in a single delegation when reused function has no params.244        if param_count == 0 && should_generate_block {245            self.dcx().emit_err(DelegationBlockSpecifiedWhenNoParams { span: block.span });246            return false;247        }248249        struct DefinitionsFinder<'a> {250            all_owners: &'a NodeMap<PerOwnerResolverData<'a>>,251            // `self.owner.node_id_to_def_id`252            nested_def_ids: &'a NodeMap<LocalDefId>,253        }254255        impl<'a> ast::visit::Visitor<'a> for DefinitionsFinder<'a> {256            type Result = ControlFlow<()>;257258            fn visit_id(&mut self, id: NodeId) -> Self::Result {259                /*260                    (from `tests\ui\delegation\target-expr-removal-defs-inside.rs`):261                    ```rust262                        reuse impl Trait for S1 {263                            some::path::<{ fn foo() {} }>::xd();264                            fn foo() {}265                            self.0266                        }267                    ```268269                    Constant from unresolved path will be in `nested_owners`,270                    `fn foo() {}` will not be in `nested_owners` but will be in `owners`,271                    both have `LocalDefId`, so we check those two maps.272                */273                match self.all_owners.contains_key(&id) || self.nested_def_ids.contains_key(&id) {274                    true => ControlFlow::Break(()),275                    false => ControlFlow::Continue(()),276                }277            }278        }279280        let mut collector = DefinitionsFinder {281            all_owners: &self.resolver.owners,282            nested_def_ids: &self.owner.node_id_to_def_id,283        };284285        let contains_defs = collector.visit_block(block).is_break();286287        // If there are definitions inside and we can't delete target expression, so report an error.288        // FIXME(fn_delegation): support deletion of target expression with defs inside.289        if !should_generate_block && contains_defs {290            self.dcx().emit_err(DelegationAttemptedBlockWithDefsDeletion { span: block.span });291            return false;292        }293294        true295    }296297    fn should_generate_block(298        &self,299        delegation: &Delegation,300        sig_id: DefId,301        is_method: bool,302    ) -> bool {303        is_method304            || matches!(self.tcx.def_kind(sig_id), DefKind::Fn)305            || matches!(delegation.source, DelegationSource::Single)306    }307308    fn add_attrs_if_needed(&mut self, span: Span, sig_id: DefId) {309        let new_attrs =310            self.create_new_attrs(ATTRS_ADDITIONS, span, sig_id, self.attrs.get(&PARENT_ID));311312        if new_attrs.is_empty() {313            return;314        }315316        let new_arena_allocated_attrs = match self.attrs.get(&PARENT_ID) {317            Some(existing_attrs) => self.arena.alloc_from_iter(318                existing_attrs.iter().map(|a| a.clone()).chain(new_attrs.into_iter()),319            ),320            None => self.arena.alloc_from_iter(new_attrs.into_iter()),321        };322323        self.attrs.insert(PARENT_ID, new_arena_allocated_attrs);324    }325326    fn create_new_attrs(327        &self,328        candidate_additions: &[AttrAdditionInfo],329        span: Span,330        sig_id: DefId,331        existing_attrs: Option<&&[hir::Attribute]>,332    ) -> Vec<hir::Attribute> {333        candidate_additions334            .iter()335            .filter_map(|addition_info| {336                if let Some(existing_attrs) = existing_attrs337                    && existing_attrs338                        .iter()339                        .any(|existing_attr| (addition_info.equals)(existing_attr))340                {341                    return None;342                }343344                match addition_info.kind {345                    AttrAdditionKind::Default { factory } => Some(factory(span)),346                    AttrAdditionKind::Inherit { factory, .. } =>347                    {348                        #[allow(deprecated)]349                        self.tcx350                            .get_all_attrs(sig_id)351                            .iter()352                            .find_map(|a| (addition_info.equals)(a).then(|| factory(span, a)))353                    }354                }355            })356            .collect::<Vec<_>>()357    }358359    fn get_resolution_id(&self, node_id: NodeId) -> Option<DefId> {360        self.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id())361    }362363    /// Returns function parameter info, including C variadic `...` and `#[splat]` if present.364    fn param_info(&self, def_id: DefId) -> ParamInfo {365        let sig = self.tcx.fn_sig(def_id).skip_binder().skip_binder();366367        // FIXME(splat): use `sig.splatted()` once FnSig has it368        ParamInfo {369            param_count: sig.inputs().len() + usize::from(sig.c_variadic()),370            c_variadic: sig.c_variadic(),371            splatted: None,372        }373    }374375    fn lower_delegation_decl(376        &mut self,377        source: DelegationSource,378        sig_id: DefId,379        param_info: ParamInfo,380        span: Span,381        generics: &GenericsGenerationResults<'hir>,382        call_path_node_id: NodeId,383        call_expr_id: HirId,384        unused_target_expr: bool,385    ) -> &'hir hir::FnDecl<'hir> {386        let ParamInfo { param_count, c_variadic, splatted } = param_info;387388        // The last parameter in C variadic functions is skipped in the signature,389        // like during regular lowering.390        let decl_param_count = param_count - c_variadic as usize;391        let inputs = self.arena.alloc_from_iter((0..decl_param_count).map(|arg| hir::Ty {392            hir_id: self.next_id(),393            kind: hir::TyKind::InferDelegation(hir::InferDelegation::Sig(394                sig_id,395                hir::InferDelegationSig::Input(arg),396            )),397            span,398        }));399400        let output = self.arena.alloc(hir::Ty {401            hir_id: self.next_id(),402            kind: hir::TyKind::InferDelegation(hir::InferDelegation::Sig(403                sig_id,404                hir::InferDelegationSig::Output(self.arena.alloc(hir::DelegationInfo {405                    call_expr_id,406                    call_path_res: self.get_resolution_id(call_path_node_id),407                    child_args_segment_id: generics.child.args_segment_id,408                    parent_args_segment_id: generics.parent.args_segment_id,409                    self_ty_id: generics.self_ty_id,410                    propagate_self_ty: generics.propagate_self_ty,411                    group_id: {412                        let id = match source {413                            DelegationSource::Single => None,414                            DelegationSource::List(expn_id) => Some(expn_id),415                            DelegationSource::Glob => {416                                Some(self.tcx.expn_that_defined(self.owner.def_id).expect_local())417                            }418                        };419420                        id.map(|id| (id, unused_target_expr))421                    },422                })),423            )),424            span,425        });426427        self.arena.alloc(hir::FnDecl {428            inputs,429            output: hir::FnRetTy::Return(output),430            fn_decl_kind: FnDeclFlags::default()431                .set_lifetime_elision_allowed(true)432                .set_c_variadic(c_variadic)433                .set_splatted(splatted, inputs.len())434                .unwrap(),435        })436    }437438    fn lower_delegation_sig(439        &mut self,440        sig_id: DefId,441        decl: &'hir hir::FnDecl<'hir>,442        span: Span,443    ) -> hir::FnSig<'hir> {444        let sig = self.tcx.fn_sig(sig_id).skip_binder().skip_binder();445        let asyncness = match self.tcx.asyncness(sig_id) {446            Asyncness::Yes => hir::IsAsync::Async(span),447            Asyncness::No => hir::IsAsync::NotAsync,448        };449450        let header = hir::FnHeader {451            safety: if self.tcx.codegen_fn_attrs(sig_id).safe_target_features {452                hir::HeaderSafety::SafeTargetFeatures453            } else {454                hir::HeaderSafety::Normal(sig.safety())455            },456            constness: self.tcx.constness(sig_id),457            asyncness,458            abi: sig.abi(),459        };460461        hir::FnSig { decl, header, span }462    }463464    fn generate_param(465        &mut self,466        is_method: bool,467        idx: usize,468        span: Span,469    ) -> (hir::Param<'hir>, NodeId) {470        let pat_node_id = self.next_node_id();471        let pat_id = self.lower_node_id(pat_node_id);472        // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.473        let name = if is_method && idx == 0 {474            kw::SelfLower475        } else {476            Symbol::intern(&format!("arg{idx}"))477        };478        let ident = Ident::with_dummy_span(name);479        let pat = self.arena.alloc(hir::Pat {480            hir_id: pat_id,481            kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None),482            span,483            default_binding_modes: false,484        });485486        (hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id)487    }488489    fn generate_arg(490        &mut self,491        is_method: bool,492        idx: usize,493        param_id: HirId,494        span: Span,495    ) -> hir::Expr<'hir> {496        // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.497        let name = if is_method && idx == 0 {498            kw::SelfLower499        } else {500            Symbol::intern(&format!("arg{idx}"))501        };502503        let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment {504            ident: Ident::with_dummy_span(name),505            hir_id: self.next_id(),506            res: Res::Local(param_id),507            args: None,508            infer_args: false,509        }));510511        let path = self.arena.alloc(hir::Path { span, res: Res::Local(param_id), segments });512        self.mk_expr(hir::ExprKind::Path(hir::QPath::Resolved(None, path)), span)513    }514515    fn lower_delegation_body(516        &mut self,517        delegation: &Delegation,518        sig_id: DefId,519        param_count: usize,520        generics: &mut GenericsGenerationResults<'hir>,521        span: Span,522    ) -> (BodyId, HirId, bool) {523        let block = delegation.body.as_deref();524        let mut call_expr_id = HirId::INVALID;525        let mut unused_target_expr = false;526527        let block_id = self.lower_body(|this| {528            let mut parameters: Vec<hir::Param<'_>> = Vec::with_capacity(param_count);529            let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);530            let mut stmts: &[hir::Stmt<'hir>] = &[];531532            let is_method = this.is_method(sig_id, span);533            let should_generate_block = this.should_generate_block(delegation, sig_id, is_method);534535            // Consider non-specified target expression as generated,536            // as we do not want to emit error when target expression is537            // not specified.538            unused_target_expr = block.is_some() && (param_count == 0 || !should_generate_block);539540            for idx in 0..param_count {541                let (param, pat_node_id) = this.generate_param(is_method, idx, span);542                parameters.push(param);543544                let generate_arg =545                    |this: &mut Self| this.generate_arg(is_method, idx, param.pat.hir_id, span);546547                let arg = if let Some(block) = block548                    && idx == 0549                    && should_generate_block550                {551                    let mut self_resolver = SelfResolver {552                        ctxt: this,553                        path_id: delegation.id,554                        self_param_id: pat_node_id,555                    };556                    self_resolver.visit_block(block);557                    // Target expr needs to lower `self` path.558                    this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);559560                    // Lower with `HirId::INVALID` as we will use only expr and stmts.561                    // FIXME(fn_delegation): Alternatives for target expression lowering:562                    // https://github.com/rust-lang/rfcs/pull/3530#issuecomment-2197170600.563                    let block = this.lower_block_noalloc(HirId::INVALID, block, false);564565                    stmts = block.stmts;566567                    // The behavior of the delegation's target expression differs from the568                    // behavior of the usual block, where if there is no final expression569                    // the `()` is returned. In case of the similar situation in delegation570                    // (no final expression) we propagate first argument instead of replacing571                    // it with `()`.572                    if let Some(&expr) = block.expr { expr } else { generate_arg(this) }573                } else {574                    generate_arg(this)575                };576577                args.push(arg);578            }579580            let (final_expr, hir_id) =581                this.finalize_body_lowering(delegation, stmts, args, generics, span);582583            call_expr_id = hir_id;584585            (this.arena.alloc_from_iter(parameters), final_expr)586        });587588        debug_assert_ne!(call_expr_id, HirId::INVALID);589590        (block_id, call_expr_id, unused_target_expr)591    }592593    fn finalize_body_lowering(594        &mut self,595        delegation: &Delegation,596        stmts: &'hir [hir::Stmt<'hir>],597        args: Vec<hir::Expr<'hir>>,598        generics: &mut GenericsGenerationResults<'hir>,599        span: Span,600    ) -> (hir::Expr<'hir>, HirId) {601        let path = self.lower_qpath(602            delegation.id,603            &delegation.qself,604            &delegation.path,605            ParamMode::Optional,606            AllowReturnTypeNotation::No,607            ImplTraitContext::Disallowed(ImplTraitPosition::Path),608            None,609        );610611        let new_path = match path {612            hir::QPath::Resolved(ty, path) => {613                let mut new_path = path.clone();614                let len = new_path.segments.len();615616                new_path.segments = self.arena.alloc_from_iter(617                    new_path.segments.iter().enumerate().map(|(idx, segment)| {618                        if idx + 2 == len {619                            self.process_segment(span, segment, &mut generics.parent)620                        } else if idx + 1 == len {621                            self.process_segment(span, segment, &mut generics.child)622                        } else {623                            segment.clone()624                        }625                    }),626                );627628                hir::QPath::Resolved(ty, self.arena.alloc(new_path))629            }630            hir::QPath::TypeRelative(ty, segment) => {631                let segment = self.process_segment(span, segment, &mut generics.child);632633                hir::QPath::TypeRelative(ty, self.arena.alloc(segment))634            }635        };636637        generics.self_ty_id = match new_path {638            hir::QPath::Resolved(ty, _) => ty,639            hir::QPath::TypeRelative(ty, _) => Some(ty),640        }641        .map(|ty| ty.hir_id);642643        let callee_path = self.arena.alloc(self.mk_expr(hir::ExprKind::Path(new_path), span));644        let args = self.arena.alloc_from_iter(args);645        let call = self.arena.alloc(self.mk_expr(hir::ExprKind::Call(callee_path, args), span));646647        let block = self.arena.alloc(hir::Block {648            stmts,649            expr: Some(call),650            hir_id: self.next_id(),651            rules: hir::BlockCheckMode::DefaultBlock,652            span,653            targeted_by_break: false,654        });655656        (self.mk_expr(hir::ExprKind::Block(block, None), span), call.hir_id)657    }658659    fn process_segment(660        &mut self,661        span: Span,662        segment: &hir::PathSegment<'hir>,663        result: &mut GenericsGenerationResult<'hir>,664    ) -> hir::PathSegment<'hir> {665        let details = result.generics.args_propagation_details();666667        // Always uplift generic params, because if they are not empty then they668        // should be generated in delegation.669        let generics = result.generics.into_hir_generics(self, span);670        let segment = if details.should_propagate {671            let args = generics.into_generic_args(self, span);672673            // Needed for better error messages (`trait-impl-wrong-args-count.rs` test).674            let args = if args.is_empty() { None } else { Some(args) };675676            hir::PathSegment { args, ..segment.clone() }677        } else {678            segment.clone()679        };680681        if details.use_args_in_sig_inheritance {682            result.args_segment_id = Some(segment.hir_id);683        }684685        segment686    }687688    fn generate_delegation_error(689        &mut self,690        span: Span,691        delegation: &Delegation,692    ) -> DelegationResults<'hir> {693        let decl = self.arena.alloc(hir::FnDecl::dummy(span));694695        let header = self.generate_header_error();696        let sig = hir::FnSig { decl, header, span };697698        let ident = self.lower_ident(delegation.ident);699700        let body_id = self.lower_body(|this| {701            let path = this.lower_qpath(702                delegation.id,703                &delegation.qself,704                &delegation.path,705                ParamMode::Optional,706                AllowReturnTypeNotation::No,707                ImplTraitContext::Disallowed(ImplTraitPosition::Path),708                None,709            );710711            let callee_path = this.arena.alloc(this.mk_expr(hir::ExprKind::Path(path), span));712            let args = if let Some(block) = delegation.body.as_ref() {713                this.arena.alloc_slice(&[this.lower_block_expr(block)])714            } else {715                &mut []716            };717718            let call = this.arena.alloc(this.mk_expr(hir::ExprKind::Call(callee_path, args), span));719720            let block = this.arena.alloc(hir::Block {721                stmts: &[],722                expr: Some(call),723                hir_id: this.next_id(),724                rules: hir::BlockCheckMode::DefaultBlock,725                span,726                targeted_by_break: false,727            });728729            (&[], this.mk_expr(hir::ExprKind::Block(block, None), span))730        });731732        let generics = hir::Generics::empty();733        DelegationResults { ident, generics, body_id, sig }734    }735736    fn generate_header_error(&self) -> hir::FnHeader {737        hir::FnHeader {738            safety: hir::Safety::Safe.into(),739            constness: hir::Constness::NotConst,740            asyncness: hir::IsAsync::NotAsync,741            abi: ExternAbi::Rust,742        }743    }744745    #[inline]746    fn mk_expr(&mut self, kind: hir::ExprKind<'hir>, span: Span) -> hir::Expr<'hir> {747        hir::Expr { hir_id: self.next_id(), kind, span }748    }749}750751struct SelfResolver<'a, 'b, 'hir> {752    ctxt: &'a mut LoweringContext<'b, 'hir>,753    path_id: NodeId,754    self_param_id: NodeId,755}756757impl SelfResolver<'_, '_, '_> {758    fn try_replace_id(&mut self, id: NodeId) {759        if let Some(res) = self.ctxt.get_partial_res(id)760            && let Some(Res::Local(sig_id)) = res.full_res()761            && sig_id == self.path_id762        {763            self.ctxt.partial_res_overrides.insert(id, self.self_param_id);764        }765    }766}767768impl<'ast> Visitor<'ast> for SelfResolver<'_, '_, '_> {769    fn visit_id(&mut self, id: NodeId) {770        self.try_replace_id(id);771    }772}

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.