Skip to content

Retire hir::*ItemRef. #143357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions compiler/rustc_ast_lowering/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use rustc_errors::ErrorGuaranteed;
use rustc_hir::def_id::DefId;
use rustc_middle::span_bug;
use rustc_middle::ty::{Asyncness, ResolverAstLowering};
use rustc_span::symbol::kw;
use rustc_span::{Ident, Span, Symbol};
use {rustc_ast as ast, rustc_hir as hir};

Expand All @@ -61,21 +62,6 @@ pub(crate) struct DelegationResults<'hir> {
}

impl<'hir> LoweringContext<'_, 'hir> {
/// Defines whether the delegatee is an associated function whose first parameter is `self`.
pub(crate) fn delegatee_is_method(
&self,
item_id: NodeId,
path_id: NodeId,
span: Span,
is_in_trait_impl: bool,
) -> bool {
let sig_id = self.get_delegation_sig_id(item_id, path_id, span, is_in_trait_impl);
let Ok(sig_id) = sig_id else {
return false;
};
self.is_method(sig_id, span)
}

fn is_method(&self, def_id: DefId, span: Span) -> bool {
match self.tcx.def_kind(def_id) {
DefKind::Fn => false,
Expand All @@ -101,10 +87,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
let sig_id = self.get_delegation_sig_id(item_id, delegation.id, span, is_in_trait_impl);
match sig_id {
Ok(sig_id) => {
let is_method = self.is_method(sig_id, span);
let (param_count, c_variadic) = self.param_count(sig_id);
let decl = self.lower_delegation_decl(sig_id, param_count, c_variadic, span);
let sig = self.lower_delegation_sig(sig_id, decl, span);
let body_id = self.lower_delegation_body(delegation, param_count, span);
let body_id = self.lower_delegation_body(delegation, is_method, param_count, span);
let ident = self.lower_ident(delegation.ident);
let generics = self.lower_delegation_generics(span);
DelegationResults { body_id, sig, ident, generics }
Expand Down Expand Up @@ -234,10 +221,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::FnSig { decl, header, span }
}

fn generate_param(&mut self, idx: usize, span: Span) -> (hir::Param<'hir>, NodeId) {
fn generate_param(
&mut self,
is_method: bool,
idx: usize,
span: Span,
) -> (hir::Param<'hir>, NodeId) {
let pat_node_id = self.next_node_id();
let pat_id = self.lower_node_id(pat_node_id);
let ident = Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}")));
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
let name = if is_method && idx == 0 {
kw::SelfLower
} else {
Symbol::intern(&format!("arg{idx}"))
};
let ident = Ident::with_dummy_span(name);
let pat = self.arena.alloc(hir::Pat {
hir_id: pat_id,
kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None),
Expand All @@ -248,9 +246,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
(hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id)
}

fn generate_arg(&mut self, idx: usize, param_id: HirId, span: Span) -> hir::Expr<'hir> {
fn generate_arg(
&mut self,
is_method: bool,
idx: usize,
param_id: HirId,
span: Span,
) -> hir::Expr<'hir> {
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
let name = if is_method && idx == 0 {
kw::SelfLower
} else {
Symbol::intern(&format!("arg{idx}"))
};
let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment {
ident: Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}"))),
ident: Ident::with_dummy_span(name),
hir_id: self.next_id(),
res: Res::Local(param_id),
args: None,
Expand All @@ -264,6 +274,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_delegation_body(
&mut self,
delegation: &Delegation,
is_method: bool,
param_count: usize,
span: Span,
) -> BodyId {
Expand All @@ -274,7 +285,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);

for idx in 0..param_count {
let (param, pat_node_id) = this.generate_param(idx, span);
let (param, pat_node_id) = this.generate_param(is_method, idx, span);
parameters.push(param);

let arg = if let Some(block) = block
Expand All @@ -290,7 +301,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
this.lower_target_expr(&block)
} else {
this.generate_arg(idx, param.pat.hir_id, span)
this.generate_arg(is_method, idx, param.pat.hir_id, span)
};
args.push(arg);
}
Expand Down
24 changes: 6 additions & 18 deletions compiler/rustc_ast_lowering/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,28 +381,16 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
})
}

fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
// Do not visit the duplicate information in TraitItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref.
let TraitItemRef { id, ident: _, kind: _, span: _ } = *ii;

self.visit_nested_trait_item(id);
fn visit_trait_item_ref(&mut self, id: &'hir TraitItemId) {
self.visit_nested_trait_item(*id);
}

fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
// Do not visit the duplicate information in ImplItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref.
let ImplItemRef { id, ident: _, kind: _, span: _, trait_item_def_id: _ } = *ii;

self.visit_nested_impl_item(id);
fn visit_impl_item_ref(&mut self, id: &'hir ImplItemId) {
self.visit_nested_impl_item(*id);
}

fn visit_foreign_item_ref(&mut self, fi: &'hir ForeignItemRef) {
// Do not visit the duplicate information in ForeignItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref.
let ForeignItemRef { id, ident: _, span: _ } = *fi;

self.visit_nested_foreign_item(id);
fn visit_foreign_item_ref(&mut self, id: &'hir ForeignItemId) {
self.visit_nested_foreign_item(*id);
}

fn visit_where_predicate(&mut self, predicate: &'hir WherePredicate<'hir>) {
Expand Down
82 changes: 13 additions & 69 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
(trait_ref, lowered_ty)
});

let new_impl_items = self.arena.alloc_from_iter(
impl_items
.iter()
.map(|item| self.lower_impl_item_ref(item, trait_ref.is_some())),
);
let new_impl_items = self
.arena
.alloc_from_iter(impl_items.iter().map(|item| self.lower_impl_item_ref(item)));

// `defaultness.has_value()` is never called for an `impl`, always `true` in order
// to not cause an assertion failure inside the `lower_defaultness` function.
Expand Down Expand Up @@ -705,14 +703,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.arena.alloc(item)
}

fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef {
hir::ForeignItemRef {
id: hir::ForeignItemId { owner_id: self.owner_id(i.id) },
// `unwrap` is safe because `ForeignItemKind::MacCall` is the only foreign item kind
// without an identifier and it cannot reach here.
ident: self.lower_ident(i.kind.ident().unwrap()),
span: self.lower_span(i.span),
}
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemId {
hir::ForeignItemId { owner_id: self.owner_id(i.id) }
}

fn lower_variant(&mut self, item_kind: &ItemKind, v: &Variant) -> hir::Variant<'hir> {
Expand Down Expand Up @@ -971,32 +963,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.arena.alloc(item)
}

fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
let (ident, kind) = match &i.kind {
AssocItemKind::Const(box ConstItem { ident, .. }) => {
(*ident, hir::AssocItemKind::Const)
}
AssocItemKind::Type(box TyAlias { ident, .. }) => (*ident, hir::AssocItemKind::Type),
AssocItemKind::Fn(box Fn { ident, sig, .. }) => {
(*ident, hir::AssocItemKind::Fn { has_self: sig.decl.has_self() })
}
AssocItemKind::Delegation(box delegation) => (
delegation.ident,
hir::AssocItemKind::Fn {
has_self: self.delegatee_is_method(i.id, delegation.id, i.span, false),
},
),
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
panic!("macros should have been expanded by now")
}
};
let id = hir::TraitItemId { owner_id: self.owner_id(i.id) };
hir::TraitItemRef {
id,
ident: self.lower_ident(ident),
span: self.lower_span(i.span),
kind,
}
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemId {
hir::TraitItemId { owner_id: self.owner_id(i.id) }
}

/// Construct `ExprKind::Err` for the given `span`.
Expand Down Expand Up @@ -1127,41 +1095,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
span: self.lower_span(i.span),
defaultness,
has_delayed_lints: !self.delayed_lints.is_empty(),
};
self.arena.alloc(item)
}

fn lower_impl_item_ref(&mut self, i: &AssocItem, is_in_trait_impl: bool) -> hir::ImplItemRef {
hir::ImplItemRef {
id: hir::ImplItemId { owner_id: self.owner_id(i.id) },
// `unwrap` is safe because `AssocItemKind::{MacCall,DelegationMac}` are the only
// assoc item kinds without an identifier and they cannot reach here.
ident: self.lower_ident(i.kind.ident().unwrap()),
span: self.lower_span(i.span),
kind: match &i.kind {
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
AssocItemKind::Type(..) => hir::AssocItemKind::Type,
AssocItemKind::Fn(box Fn { sig, .. }) => {
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
}
AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn {
has_self: self.delegatee_is_method(
i.id,
delegation.id,
i.span,
is_in_trait_impl,
),
},
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
panic!("macros should have been expanded by now")
}
},
trait_item_def_id: self
.resolver
.get_partial_res(i.id)
.map(|r| r.expect_full_res().opt_def_id())
.unwrap_or(None),
}
};
self.arena.alloc(item)
}

fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemId {
hir::ImplItemId { owner_id: self.owner_id(i.id) }
}

fn lower_defaultness(
Expand Down
46 changes: 15 additions & 31 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,46 +681,30 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
return (false, false, None);
}
let my_def = self.body.source.def_id();
let my_hir = self.infcx.tcx.local_def_id_to_hir_id(my_def.as_local().unwrap());
let Some(td) =
self.infcx.tcx.impl_of_method(my_def).and_then(|x| self.infcx.tcx.trait_id_of_impl(x))
else {
return (false, false, None);
};

let implemented_trait_item = self.infcx.tcx.associated_item(my_def).trait_item_def_id;

(
true,
td.is_local(),
td.as_local().and_then(|tld| match self.infcx.tcx.hir_node_by_def_id(tld) {
Node::Item(hir::Item {
kind: hir::ItemKind::Trait(_, _, _, _, _, items), ..
}) => {
let mut f_in_trait_opt = None;
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
let hi = fi.hir_id();
if !matches!(k, hir::AssocItemKind::Fn { .. }) {
continue;
}
if self.infcx.tcx.hir_name(hi) != self.infcx.tcx.hir_name(my_hir) {
continue;
}
f_in_trait_opt = Some(hi);
break;
}
f_in_trait_opt.and_then(|f_in_trait| {
if let Node::TraitItem(ti) = self.infcx.tcx.hir_node(f_in_trait)
&& let hir::TraitItemKind::Fn(sig, _) = ti.kind
&& let Some(ty) = sig.decl.inputs.get(local.index() - 1)
&& let hir::TyKind::Ref(_, mut_ty) = ty.kind
&& let hir::Mutability::Not = mut_ty.mutbl
&& sig.decl.implicit_self.has_implicit_self()
{
Some(ty.span)
} else {
None
}
})
implemented_trait_item.and_then(|f_in_trait| {
let f_in_trait = f_in_trait.as_local()?;
if let Node::TraitItem(ti) = self.infcx.tcx.hir_node_by_def_id(f_in_trait)
&& let hir::TraitItemKind::Fn(sig, _) = ti.kind
&& let Some(ty) = sig.decl.inputs.get(local.index() - 1)
&& let hir::TyKind::Ref(_, mut_ty) = ty.kind
&& let hir::Mutability::Not = mut_ty.mutbl
&& sig.decl.implicit_self.has_implicit_self()
{
Some(ty.span)
} else {
None
}
_ => None,
}),
)
}
Expand Down
Loading
Loading