Skip to content

Commit c2dbafe

Browse files
committed
Merge branch 'no-assoc-item-kind'
2 parents ea7c3ba + c233ef1 commit c2dbafe

File tree

82 files changed

+654
-883
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+654
-883
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use rustc_errors::ErrorGuaranteed;
4747
use rustc_hir::def_id::DefId;
4848
use rustc_middle::span_bug;
4949
use rustc_middle::ty::{Asyncness, ResolverAstLowering};
50+
use rustc_span::symbol::kw;
5051
use rustc_span::{Ident, Span, Symbol};
5152
use {rustc_ast as ast, rustc_hir as hir};
5253

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

6364
impl<'hir> LoweringContext<'_, 'hir> {
64-
/// Defines whether the delegatee is an associated function whose first parameter is `self`.
65-
pub(crate) fn delegatee_is_method(
66-
&self,
67-
item_id: NodeId,
68-
path_id: NodeId,
69-
span: Span,
70-
is_in_trait_impl: bool,
71-
) -> bool {
72-
let sig_id = self.get_delegation_sig_id(item_id, path_id, span, is_in_trait_impl);
73-
let Ok(sig_id) = sig_id else {
74-
return false;
75-
};
76-
self.is_method(sig_id, span)
77-
}
78-
7965
fn is_method(&self, def_id: DefId, span: Span) -> bool {
8066
match self.tcx.def_kind(def_id) {
8167
DefKind::Fn => false,
@@ -101,10 +87,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
10187
let sig_id = self.get_delegation_sig_id(item_id, delegation.id, span, is_in_trait_impl);
10288
match sig_id {
10389
Ok(sig_id) => {
90+
let is_method = self.is_method(sig_id, span);
10491
let (param_count, c_variadic) = self.param_count(sig_id);
10592
let decl = self.lower_delegation_decl(sig_id, param_count, c_variadic, span);
10693
let sig = self.lower_delegation_sig(sig_id, decl, span);
107-
let body_id = self.lower_delegation_body(delegation, param_count, span);
94+
let body_id = self.lower_delegation_body(delegation, is_method, param_count, span);
10895
let ident = self.lower_ident(delegation.ident);
10996
let generics = self.lower_delegation_generics(span);
11097
DelegationResults { body_id, sig, ident, generics }
@@ -234,10 +221,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
234221
hir::FnSig { decl, header, span }
235222
}
236223

237-
fn generate_param(&mut self, idx: usize, span: Span) -> (hir::Param<'hir>, NodeId) {
224+
fn generate_param(
225+
&mut self,
226+
is_method: bool,
227+
idx: usize,
228+
span: Span,
229+
) -> (hir::Param<'hir>, NodeId) {
238230
let pat_node_id = self.next_node_id();
239231
let pat_id = self.lower_node_id(pat_node_id);
240-
let ident = Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}")));
232+
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
233+
let name = if is_method && idx == 0 {
234+
kw::SelfLower
235+
} else {
236+
Symbol::intern(&format!("arg{idx}"))
237+
};
238+
let ident = Ident::with_dummy_span(name);
241239
let pat = self.arena.alloc(hir::Pat {
242240
hir_id: pat_id,
243241
kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None),
@@ -248,9 +246,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
248246
(hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id)
249247
}
250248

251-
fn generate_arg(&mut self, idx: usize, param_id: HirId, span: Span) -> hir::Expr<'hir> {
249+
fn generate_arg(
250+
&mut self,
251+
is_method: bool,
252+
idx: usize,
253+
param_id: HirId,
254+
span: Span,
255+
) -> hir::Expr<'hir> {
256+
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
257+
let name = if is_method && idx == 0 {
258+
kw::SelfLower
259+
} else {
260+
Symbol::intern(&format!("arg{idx}"))
261+
};
252262
let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment {
253-
ident: Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}"))),
263+
ident: Ident::with_dummy_span(name),
254264
hir_id: self.next_id(),
255265
res: Res::Local(param_id),
256266
args: None,
@@ -264,6 +274,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
264274
fn lower_delegation_body(
265275
&mut self,
266276
delegation: &Delegation,
277+
is_method: bool,
267278
param_count: usize,
268279
span: Span,
269280
) -> BodyId {
@@ -274,7 +285,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
274285
let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);
275286

276287
for idx in 0..param_count {
277-
let (param, pat_node_id) = this.generate_param(idx, span);
288+
let (param, pat_node_id) = this.generate_param(is_method, idx, span);
278289
parameters.push(param);
279290

280291
let arg = if let Some(block) = block
@@ -290,7 +301,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
290301
this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
291302
this.lower_target_expr(&block)
292303
} else {
293-
this.generate_arg(idx, param.pat.hir_id, span)
304+
this.generate_arg(is_method, idx, param.pat.hir_id, span)
294305
};
295306
args.push(arg);
296307
}

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -381,28 +381,16 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
381381
})
382382
}
383383

384-
fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
385-
// Do not visit the duplicate information in TraitItemRef. We want to
386-
// map the actual nodes, not the duplicate ones in the *Ref.
387-
let TraitItemRef { id, ident: _, kind: _, span: _ } = *ii;
388-
389-
self.visit_nested_trait_item(id);
384+
fn visit_trait_item_ref(&mut self, id: &'hir TraitItemId) {
385+
self.visit_nested_trait_item(*id);
390386
}
391387

392-
fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
393-
// Do not visit the duplicate information in ImplItemRef. We want to
394-
// map the actual nodes, not the duplicate ones in the *Ref.
395-
let ImplItemRef { id, ident: _, kind: _, span: _, trait_item_def_id: _ } = *ii;
396-
397-
self.visit_nested_impl_item(id);
388+
fn visit_impl_item_ref(&mut self, id: &'hir ImplItemId) {
389+
self.visit_nested_impl_item(*id);
398390
}
399391

400-
fn visit_foreign_item_ref(&mut self, fi: &'hir ForeignItemRef) {
401-
// Do not visit the duplicate information in ForeignItemRef. We want to
402-
// map the actual nodes, not the duplicate ones in the *Ref.
403-
let ForeignItemRef { id, ident: _, span: _ } = *fi;
404-
405-
self.visit_nested_foreign_item(id);
392+
fn visit_foreign_item_ref(&mut self, id: &'hir ForeignItemId) {
393+
self.visit_nested_foreign_item(*id);
406394
}
407395

408396
fn visit_where_predicate(&mut self, predicate: &'hir WherePredicate<'hir>) {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 13 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
393393
(trait_ref, lowered_ty)
394394
});
395395

396-
let new_impl_items = self.arena.alloc_from_iter(
397-
impl_items
398-
.iter()
399-
.map(|item| self.lower_impl_item_ref(item, trait_ref.is_some())),
400-
);
396+
let new_impl_items = self
397+
.arena
398+
.alloc_from_iter(impl_items.iter().map(|item| self.lower_impl_item_ref(item)));
401399

402400
// `defaultness.has_value()` is never called for an `impl`, always `true` in order
403401
// to not cause an assertion failure inside the `lower_defaultness` function.
@@ -706,14 +704,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
706704
self.arena.alloc(item)
707705
}
708706

709-
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef {
710-
hir::ForeignItemRef {
711-
id: hir::ForeignItemId { owner_id: self.owner_id(i.id) },
712-
// `unwrap` is safe because `ForeignItemKind::MacCall` is the only foreign item kind
713-
// without an identifier and it cannot reach here.
714-
ident: self.lower_ident(i.kind.ident().unwrap()),
715-
span: self.lower_span(i.span),
716-
}
707+
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemId {
708+
hir::ForeignItemId { owner_id: self.owner_id(i.id) }
717709
}
718710

719711
fn lower_variant(&mut self, item_kind: &ItemKind, v: &Variant) -> hir::Variant<'hir> {
@@ -972,32 +964,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
972964
self.arena.alloc(item)
973965
}
974966

975-
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
976-
let (ident, kind) = match &i.kind {
977-
AssocItemKind::Const(box ConstItem { ident, .. }) => {
978-
(*ident, hir::AssocItemKind::Const)
979-
}
980-
AssocItemKind::Type(box TyAlias { ident, .. }) => (*ident, hir::AssocItemKind::Type),
981-
AssocItemKind::Fn(box Fn { ident, sig, .. }) => {
982-
(*ident, hir::AssocItemKind::Fn { has_self: sig.decl.has_self() })
983-
}
984-
AssocItemKind::Delegation(box delegation) => (
985-
delegation.ident,
986-
hir::AssocItemKind::Fn {
987-
has_self: self.delegatee_is_method(i.id, delegation.id, i.span, false),
988-
},
989-
),
990-
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
991-
panic!("macros should have been expanded by now")
992-
}
993-
};
994-
let id = hir::TraitItemId { owner_id: self.owner_id(i.id) };
995-
hir::TraitItemRef {
996-
id,
997-
ident: self.lower_ident(ident),
998-
span: self.lower_span(i.span),
999-
kind,
1000-
}
967+
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemId {
968+
hir::TraitItemId { owner_id: self.owner_id(i.id) }
1001969
}
1002970

1003971
/// Construct `ExprKind::Err` for the given `span`.
@@ -1128,41 +1096,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
11281096
span: self.lower_span(i.span),
11291097
defaultness,
11301098
has_delayed_lints: !self.delayed_lints.is_empty(),
1131-
};
1132-
self.arena.alloc(item)
1133-
}
1134-
1135-
fn lower_impl_item_ref(&mut self, i: &AssocItem, is_in_trait_impl: bool) -> hir::ImplItemRef {
1136-
hir::ImplItemRef {
1137-
id: hir::ImplItemId { owner_id: self.owner_id(i.id) },
1138-
// `unwrap` is safe because `AssocItemKind::{MacCall,DelegationMac}` are the only
1139-
// assoc item kinds without an identifier and they cannot reach here.
1140-
ident: self.lower_ident(i.kind.ident().unwrap()),
1141-
span: self.lower_span(i.span),
1142-
kind: match &i.kind {
1143-
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
1144-
AssocItemKind::Type(..) => hir::AssocItemKind::Type,
1145-
AssocItemKind::Fn(box Fn { sig, .. }) => {
1146-
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
1147-
}
1148-
AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn {
1149-
has_self: self.delegatee_is_method(
1150-
i.id,
1151-
delegation.id,
1152-
i.span,
1153-
is_in_trait_impl,
1154-
),
1155-
},
1156-
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
1157-
panic!("macros should have been expanded by now")
1158-
}
1159-
},
11601099
trait_item_def_id: self
11611100
.resolver
11621101
.get_partial_res(i.id)
11631102
.map(|r| r.expect_full_res().opt_def_id())
11641103
.unwrap_or(None),
1165-
}
1104+
};
1105+
self.arena.alloc(item)
1106+
}
1107+
1108+
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemId {
1109+
hir::ImplItemId { owner_id: self.owner_id(i.id) }
11661110
}
11671111

11681112
fn lower_defaultness(

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -681,46 +681,30 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
681681
return (false, false, None);
682682
}
683683
let my_def = self.body.source.def_id();
684-
let my_hir = self.infcx.tcx.local_def_id_to_hir_id(my_def.as_local().unwrap());
685684
let Some(td) =
686685
self.infcx.tcx.impl_of_method(my_def).and_then(|x| self.infcx.tcx.trait_id_of_impl(x))
687686
else {
688687
return (false, false, None);
689688
};
689+
690+
let implemented_trait_item = self.infcx.tcx.associated_item(my_def).trait_item_def_id;
691+
690692
(
691693
true,
692694
td.is_local(),
693-
td.as_local().and_then(|tld| match self.infcx.tcx.hir_node_by_def_id(tld) {
694-
Node::Item(hir::Item {
695-
kind: hir::ItemKind::Trait(_, _, _, _, _, items), ..
696-
}) => {
697-
let mut f_in_trait_opt = None;
698-
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
699-
let hi = fi.hir_id();
700-
if !matches!(k, hir::AssocItemKind::Fn { .. }) {
701-
continue;
702-
}
703-
if self.infcx.tcx.hir_name(hi) != self.infcx.tcx.hir_name(my_hir) {
704-
continue;
705-
}
706-
f_in_trait_opt = Some(hi);
707-
break;
708-
}
709-
f_in_trait_opt.and_then(|f_in_trait| {
710-
if let Node::TraitItem(ti) = self.infcx.tcx.hir_node(f_in_trait)
711-
&& let hir::TraitItemKind::Fn(sig, _) = ti.kind
712-
&& let Some(ty) = sig.decl.inputs.get(local.index() - 1)
713-
&& let hir::TyKind::Ref(_, mut_ty) = ty.kind
714-
&& let hir::Mutability::Not = mut_ty.mutbl
715-
&& sig.decl.implicit_self.has_implicit_self()
716-
{
717-
Some(ty.span)
718-
} else {
719-
None
720-
}
721-
})
695+
implemented_trait_item.and_then(|f_in_trait| {
696+
let f_in_trait = f_in_trait.as_local()?;
697+
if let Node::TraitItem(ti) = self.infcx.tcx.hir_node_by_def_id(f_in_trait)
698+
&& let hir::TraitItemKind::Fn(sig, _) = ti.kind
699+
&& let Some(ty) = sig.decl.inputs.get(local.index() - 1)
700+
&& let hir::TyKind::Ref(_, mut_ty) = ty.kind
701+
&& let hir::Mutability::Not = mut_ty.mutbl
702+
&& sig.decl.implicit_self.has_implicit_self()
703+
{
704+
Some(ty.span)
705+
} else {
706+
None
722707
}
723-
_ => None,
724708
}),
725709
)
726710
}

0 commit comments

Comments
 (0)