Skip to content

Commit c2d7c9c

Browse files
committed
Put GenericsGenerationResults into DelegationResolution, cleanups
1 parent 575fe88 commit c2d7c9c

2 files changed

Lines changed: 58 additions & 61 deletions

File tree

compiler/rustc_ast_lowering/src/delegation/mod.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
//!
2727
//! Where `callee_path` is a path in delegation item e.g. `<Type as Trait>::name`.
2828
//! `sig_id` is a id of item from which the signature is inherited. It may be a delegation
29-
//! item id (`item_id`) in case of impl trait or path resolution id (`path_id`) otherwise.
29+
//! item id (`item_id`) in case of impl trait or path res id (`path_id`) otherwise.
3030
//!
31-
//! Since we do not have a proper way to obtain function type information by path resolution
31+
//! Since we do not have a proper way to obtain function type information by path res
3232
//! in AST, we mark each function parameter type as `InferDelegation` and inherit it during
3333
//! HIR ty lowering.
3434
//!
@@ -80,25 +80,26 @@ impl<'hir> LoweringContext<'_, 'hir> {
8080
let span = self.lower_span(delegation.last_segment_span());
8181

8282
let resolver = DelegationResolverForLowering(self);
83-
let Ok((res, mut generics)) = resolver.resolve_delegation(delegation, span) else {
83+
let Ok(mut res) = resolver.resolve_delegation(delegation, span) else {
8484
return self.generate_delegation_error(span, delegation);
8585
};
8686

8787
self.add_attributes_if_needed(&res);
8888

8989
let (body_id, call_expr_id, unused_target_expr) =
90-
self.lower_delegation_body(delegation, &res, &mut generics);
90+
self.lower_delegation_body(delegation, &mut res);
9191

92-
let decl = self.lower_delegation_decl(&res, &generics, call_expr_id, unused_target_expr);
92+
let decl =
93+
self.lower_delegation_decl(&res, &res.generics, call_expr_id, unused_target_expr);
9394

9495
let sig = self.lower_delegation_sig(res.sig_id, decl, span);
9596

9697
let ident = self.lower_ident(delegation.ident);
9798

9899
let generics = self.arena.alloc(hir::Generics {
99100
has_where_clause_predicates: false,
100-
params: self.arena.alloc_from_iter(generics.all_params()),
101-
predicates: self.arena.alloc_from_iter(generics.all_predicates()),
101+
params: self.arena.alloc_from_iter(res.generics.all_params()),
102+
predicates: self.arena.alloc_from_iter(res.generics.all_predicates()),
102103
span,
103104
where_clause_span: span,
104105
});
@@ -112,21 +113,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
112113

113114
fn lower_delegation_decl(
114115
&mut self,
115-
resolution: &DelegationResolution<'hir>,
116+
res: &DelegationResolution<'hir>,
116117
generics: &GenericsGenerationResults<'hir>,
117118
call_expr_id: HirId,
118119
unused_target_expr: bool,
119120
) -> &'hir hir::FnDecl<'hir> {
120-
let &DelegationResolution { source, call_path_node_id, span, .. } = resolution;
121-
let ParamInfo { param_count, c_variadic, splatted } = resolution.param_info;
121+
let &DelegationResolution { source, call_path_node_id, span, .. } = res;
122+
let ParamInfo { param_count, c_variadic, splatted } = res.param_info;
122123

123124
// The last parameter in C variadic functions is skipped in the signature,
124125
// like during regular lowering.
125126
let decl_param_count = param_count - c_variadic as usize;
126127
let inputs = self.arena.alloc_from_iter((0..decl_param_count).map(|arg| hir::Ty {
127128
hir_id: self.next_id(),
128129
kind: hir::TyKind::InferDelegation(hir::InferDelegation::Sig(
129-
resolution.sig_id,
130+
res.sig_id,
130131
hir::InferDelegationSig::Input(arg),
131132
)),
132133
span,
@@ -135,7 +136,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
135136
let output = self.arena.alloc(hir::Ty {
136137
hir_id: self.next_id(),
137138
kind: hir::TyKind::InferDelegation(hir::InferDelegation::Sig(
138-
resolution.sig_id,
139+
res.sig_id,
139140
hir::InferDelegationSig::Output(self.arena.alloc(hir::DelegationInfo {
140141
call_expr_id,
141142
call_path_res: self.get_resolution_id(call_path_node_id),
@@ -250,17 +251,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
250251
fn lower_delegation_body(
251252
&mut self,
252253
delegation: &Delegation,
253-
resolution: &DelegationResolution<'hir>,
254-
generics: &mut GenericsGenerationResults<'hir>,
254+
res: &mut DelegationResolution<'hir>,
255255
) -> (BodyId, HirId, bool) {
256256
let block = delegation.body.as_deref();
257257
let mut call_expr_id = HirId::INVALID;
258258
let mut unused_target_expr = false;
259259

260260
let block_id = self.lower_body(|this| {
261-
let &DelegationResolution {
262-
param_info, span, should_generate_block, is_method, ..
263-
} = resolution;
261+
let &mut DelegationResolution {
262+
param_info,
263+
span,
264+
should_generate_block,
265+
is_method,
266+
..
267+
} = res;
264268

265269
let ParamInfo { param_count, .. } = param_info;
266270

@@ -314,7 +318,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
314318
}
315319

316320
let (final_expr, hir_id) =
317-
this.finalize_body_lowering(delegation, stmts, args, generics, resolution, span);
321+
this.finalize_body_lowering(delegation, stmts, args, res, span);
318322

319323
call_expr_id = hir_id;
320324

@@ -331,8 +335,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
331335
delegation: &Delegation,
332336
stmts: &'hir [hir::Stmt<'hir>],
333337
args: Vec<hir::Expr<'hir>>,
334-
generics: &mut GenericsGenerationResults<'hir>,
335-
resolution: &DelegationResolution<'hir>,
338+
res: &mut DelegationResolution<'hir>,
336339
span: Span,
337340
) -> (hir::Expr<'hir>, HirId) {
338341
let path = self.lower_qpath(
@@ -353,9 +356,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
353356
new_path.segments = self.arena.alloc_from_iter(
354357
new_path.segments.iter().enumerate().map(|(idx, segment)| {
355358
if idx + 2 == len {
356-
self.process_segment(span, segment, &mut generics.parent)
359+
self.process_segment(span, segment, &mut res.generics.parent)
357360
} else if idx + 1 == len {
358-
self.process_segment(span, segment, &mut generics.child)
361+
self.process_segment(span, segment, &mut res.generics.child)
359362
} else {
360363
segment.clone()
361364
}
@@ -364,9 +367,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
364367

365368
// Explicitly create `Self` self-type in case of infers or static
366369
// free-to-trait reuses.
367-
let ty = match generics.self_ty_propagation_kind {
370+
let ty = match res.generics.self_ty_propagation_kind {
368371
Some(hir::DelegationSelfTyPropagationKind::SelfParam) => {
369-
let self_param = generics.parent.generics.find_self_param();
372+
let self_param = res.generics.parent.generics.find_self_param();
370373
let path = self.create_generic_arg_path(self_param);
371374
let kind = hir::TyKind::Path(path);
372375

@@ -386,7 +389,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
386389
};
387390

388391
if let Some(hir::DelegationSelfTyPropagationKind::SelfTy(id)) =
389-
generics.self_ty_propagation_kind.as_mut()
392+
res.generics.self_ty_propagation_kind.as_mut()
390393
{
391394
*id = match new_path {
392395
hir::QPath::Resolved(ty, _) => {
@@ -401,7 +404,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
401404
let args = self.arena.alloc_from_iter(args);
402405
let call = self.mk_expr(hir::ExprKind::Call(callee_path, args), span);
403406

404-
let expr = if let Some(new_type) = resolution.parent_newtype {
407+
let expr = if let Some(new_type) = res.parent_newtype {
405408
let variant = new_type.variant(VariantIdx::ZERO);
406409
let res = Res::Def(DefKind::Fn, variant.ctor.expect("must have constructor").1);
407410

compiler/rustc_ast_lowering/src/delegation/resolution.rs

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub(super) struct DelegationResolution<'tcx> {
8181
pub(super) call_path_node_id: NodeId,
8282
pub(super) source: DelegationSource,
8383
pub(super) parent_newtype: Option<ty::AdtDef<'tcx>>,
84+
pub(super) generics: GenericsGenerationResults<'tcx>,
8485
}
8586

8687
pub(super) struct DelegationResolverForLowering<'a, T>(pub(super) &'a mut T);
@@ -90,22 +91,25 @@ impl<'tcx, T: LoweringContextForResolution<'tcx>> DelegationResolverForLowering<
9091
&self,
9192
delegation: &Delegation,
9293
span: Span,
93-
) -> Result<(DelegationResolution<'tcx>, GenericsGenerationResults<'tcx>), ErrorGuaranteed>
94-
{
94+
) -> Result<DelegationResolution<'tcx>, ErrorGuaranteed> {
9595
let tcx = self.0.tcx();
9696
let def_id = self.0.owner_id();
9797

98-
let Some(info) = tcx.resolutions(()).delegation_infos.get(&def_id) else {
99-
return Err(tcx.dcx().span_delayed_bug(
100-
span,
101-
format!("delegation resolution record was not found for {:?}", def_id),
102-
));
103-
};
104-
10598
// Delegation can be missing from the `delegations_resolutions` table
10699
// in illegal places such as function bodies in extern blocks (see #151356).
107-
let sig_id =
108-
info.resolution_id.and_then(|id| self.check_for_cycles(id, span).map(|_| id))?;
100+
let sig_id = tcx
101+
.resolutions(())
102+
.delegation_infos
103+
.get(&def_id)
104+
.map(|info| {
105+
info.resolution_id.and_then(|id| self.check_for_cycles(id, span).map(|_| id))
106+
})
107+
.unwrap_or_else(|| {
108+
Err(tcx.dcx().span_delayed_bug(
109+
span,
110+
format!("delegation resolution record was not found for {:?}", def_id),
111+
))
112+
})?;
109113

110114
let is_method = match tcx.def_kind(sig_id) {
111115
DefKind::Fn => false,
@@ -120,24 +124,23 @@ impl<'tcx, T: LoweringContextForResolution<'tcx>> DelegationResolverForLowering<
120124
// FIXME(splat): use `sig.splatted()` once FnSig has it
121125
let param_info = ParamInfo { param_count, c_variadic: sig.c_variadic(), splatted: None };
122126

123-
self.check_block_soundness(delegation, sig_id, is_method, param_count)?;
127+
let should_generate_block =
128+
self.check_block_soundness(delegation, sig_id, is_method, param_count)?;
124129

125130
let parent_newtype = self.should_wrap_return_value(delegation);
131+
let generics = self.resolve_generics(delegation, sig_id);
126132

127-
let resolution = DelegationResolution {
128-
should_generate_block: self.should_generate_block(delegation, sig_id, is_method),
133+
Ok(DelegationResolution {
134+
should_generate_block,
129135
is_method,
130136
param_info,
131137
span,
132138
sig_id,
133139
source: delegation.source,
134140
call_path_node_id: delegation.id,
135141
parent_newtype,
136-
};
137-
138-
let generics = self.resolve_generics(delegation, sig_id);
139-
140-
Ok((resolution, generics))
142+
generics,
143+
})
141144
}
142145

143146
fn check_for_cycles(&self, mut def_id: DefId, span: Span) -> Result<(), ErrorGuaranteed> {
@@ -173,11 +176,13 @@ impl<'tcx, T: LoweringContextForResolution<'tcx>> DelegationResolverForLowering<
173176
sig_id: DefId,
174177
is_method: bool,
175178
param_count: usize,
176-
) -> Result<(), ErrorGuaranteed> {
177-
let Some(block) = delegation.body.as_ref() else { return Ok(()) };
178-
179+
) -> Result<bool, ErrorGuaranteed> {
179180
let tcx = self.0.tcx();
180-
let should_generate_block = self.should_generate_block(delegation, sig_id, is_method);
181+
let should_generate_block = is_method
182+
|| matches!(tcx.def_kind(sig_id), DefKind::Fn)
183+
|| matches!(delegation.source, DelegationSource::Single);
184+
185+
let Some(block) = delegation.body.as_ref() else { return Ok(should_generate_block) };
181186

182187
// Report an error if user has explicitly specified delegation's target expression
183188
// in a single delegation when reused function has no params.
@@ -208,23 +213,12 @@ impl<'tcx, T: LoweringContextForResolution<'tcx>> DelegationResolverForLowering<
208213
// If there are definitions inside and we can't delete target expression, then report an error.
209214
// FIXME(fn_delegation): support deletion of target expression with defs inside.
210215
if should_generate_block || !contains_defs {
211-
Ok(())
216+
Ok(should_generate_block)
212217
} else {
213218
Err(tcx.dcx().emit_err(DelegationAttemptedBlockWithDefsDeletion { span: block.span }))
214219
}
215220
}
216221

217-
fn should_generate_block(
218-
&self,
219-
delegation: &Delegation,
220-
sig_id: DefId,
221-
is_method: bool,
222-
) -> bool {
223-
is_method
224-
|| matches!(self.0.tcx().def_kind(sig_id), DefKind::Fn)
225-
|| matches!(delegation.source, DelegationSource::Single)
226-
}
227-
228222
fn should_wrap_return_value(&self, delegation: &Delegation) -> Option<ty::AdtDef<'tcx>> {
229223
let tcx = self.0.tcx();
230224
let parent = tcx.local_parent(self.0.owner_id());

0 commit comments

Comments
 (0)