rustc_hir_analysis/
delegation.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Support inheriting generic parameters and predicates for function delegation.
//!
//! For more information about delegation design, see the tracking issue #118212.

use std::assert_matches::debug_assert_matches;

use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::{ErrorGuaranteed, Span};
use rustc_type_ir::visit::TypeVisitableExt;

type RemapTable = FxHashMap<u32, u32>;

struct ParamIndexRemapper<'tcx> {
    tcx: TyCtxt<'tcx>,
    remap_table: RemapTable,
}

impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ParamIndexRemapper<'tcx> {
    fn cx(&self) -> TyCtxt<'tcx> {
        self.tcx
    }

    fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
        if !ty.has_param() {
            return ty;
        }

        if let ty::Param(param) = ty.kind()
            && let Some(index) = self.remap_table.get(&param.index)
        {
            return Ty::new_param(self.tcx, *index, param.name);
        }
        ty.super_fold_with(self)
    }

    fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
        if let ty::ReEarlyParam(param) = r.kind()
            && let Some(index) = self.remap_table.get(&param.index).copied()
        {
            return ty::Region::new_early_param(self.tcx, ty::EarlyParamRegion {
                index,
                name: param.name,
            });
        }
        r
    }

    fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
        if let ty::ConstKind::Param(param) = ct.kind()
            && let Some(idx) = self.remap_table.get(&param.index)
        {
            let param = ty::ParamConst::new(*idx, param.name);
            return ty::Const::new_param(self.tcx, param);
        }
        ct.super_fold_with(self)
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
enum FnKind {
    Free,
    AssocInherentImpl,
    AssocTrait,
    AssocTraitImpl,
}

fn fn_kind<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> FnKind {
    debug_assert_matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn);

    let parent = tcx.parent(def_id);
    match tcx.def_kind(parent) {
        DefKind::Trait => FnKind::AssocTrait,
        DefKind::Impl { of_trait: true } => FnKind::AssocTraitImpl,
        DefKind::Impl { of_trait: false } => FnKind::AssocInherentImpl,
        _ => FnKind::Free,
    }
}

/// Given the current context(caller and callee `FnKind`), it specifies
/// the policy of predicates and generic parameters inheritance.
#[derive(Clone, Copy, Debug, PartialEq)]
enum InheritanceKind {
    /// Copying all predicates and parameters, including those of the parent
    /// container.
    ///
    /// Boolean value defines whether the `Self` parameter or `Self: Trait`
    /// predicate are copied. It's always equal to `false` except when
    /// delegating from a free function to a trait method.
    ///
    /// FIXME(fn_delegation): This often leads to type inference
    /// errors. Support providing generic arguments or restrict use sites.
    WithParent(bool),
    /// The trait implementation should be compatible with the original trait.
    /// Therefore, for trait implementations only the method's own parameters
    /// and predicates are copied.
    Own,
}

struct GenericsBuilder<'tcx> {
    tcx: TyCtxt<'tcx>,
    sig_id: DefId,
    parent: Option<DefId>,
    inh_kind: InheritanceKind,
}

impl<'tcx> GenericsBuilder<'tcx> {
    fn new(tcx: TyCtxt<'tcx>, sig_id: DefId) -> GenericsBuilder<'tcx> {
        GenericsBuilder { tcx, sig_id, parent: None, inh_kind: InheritanceKind::WithParent(false) }
    }

    fn with_parent(mut self, parent: DefId) -> Self {
        self.parent = Some(parent);
        self
    }

    fn with_inheritance_kind(mut self, inh_kind: InheritanceKind) -> Self {
        self.inh_kind = inh_kind;
        self
    }

    fn build(self) -> ty::Generics {
        let mut own_params = vec![];

        let sig_generics = self.tcx.generics_of(self.sig_id);
        if let InheritanceKind::WithParent(has_self) = self.inh_kind
            && let Some(parent_def_id) = sig_generics.parent
        {
            let sig_parent_generics = self.tcx.generics_of(parent_def_id);
            own_params.append(&mut sig_parent_generics.own_params.clone());
            if !has_self {
                own_params.remove(0);
            }
        }
        own_params.append(&mut sig_generics.own_params.clone());

        // Lifetime parameters must be declared before type and const parameters.
        // Therefore, When delegating from a free function to a associated function,
        // generic parameters need to be reordered:
        //
        // trait Trait<'a, A> {
        //     fn foo<'b, B>(...) {...}
        // }
        //
        // reuse Trait::foo;
        // desugaring:
        // fn foo<'a, 'b, This: Trait<'a, A>, A, B>(...) {
        //     Trait::foo(...)
        // }
        own_params.sort_by_key(|key| key.kind.is_ty_or_const());

        let param_def_id_to_index =
            own_params.iter().map(|param| (param.def_id, param.index)).collect();

        let (parent_count, has_self) = if let Some(def_id) = self.parent {
            let parent_generics = self.tcx.generics_of(def_id);
            let parent_kind = self.tcx.def_kind(def_id);
            (parent_generics.count(), parent_kind == DefKind::Trait)
        } else {
            (0, false)
        };

        for (idx, param) in own_params.iter_mut().enumerate() {
            param.index = (idx + parent_count) as u32;
            // FIXME(fn_delegation): Default parameters are not inherited, because they are
            // not permitted in functions. Therefore, there are 2 options here:
            //
            // - We can create non-default generic parameters.
            // - We can substitute default parameters into the signature.
            //
            // At the moment, first option has been selected as the most general.
            if let ty::GenericParamDefKind::Type { has_default, .. }
            | ty::GenericParamDefKind::Const { has_default, .. } = &mut param.kind
            {
                *has_default = false;
            }
        }

        ty::Generics {
            parent: self.parent,
            parent_count,
            own_params,
            param_def_id_to_index,
            has_self,
            has_late_bound_regions: sig_generics.has_late_bound_regions,
            host_effect_index: sig_generics.host_effect_index,
        }
    }
}

struct PredicatesBuilder<'tcx> {
    tcx: TyCtxt<'tcx>,
    sig_id: DefId,
    parent: Option<DefId>,
    inh_kind: InheritanceKind,
    args: ty::GenericArgsRef<'tcx>,
}

impl<'tcx> PredicatesBuilder<'tcx> {
    fn new(
        tcx: TyCtxt<'tcx>,
        args: ty::GenericArgsRef<'tcx>,
        sig_id: DefId,
    ) -> PredicatesBuilder<'tcx> {
        PredicatesBuilder {
            tcx,
            sig_id,
            parent: None,
            inh_kind: InheritanceKind::WithParent(false),
            args,
        }
    }

    fn with_parent(mut self, parent: DefId) -> Self {
        self.parent = Some(parent);
        self
    }

    fn with_inheritance_kind(mut self, inh_kind: InheritanceKind) -> Self {
        self.inh_kind = inh_kind;
        self
    }

    fn build(self) -> ty::GenericPredicates<'tcx> {
        struct PredicatesCollector<'tcx> {
            tcx: TyCtxt<'tcx>,
            preds: Vec<(ty::Clause<'tcx>, Span)>,
            args: ty::GenericArgsRef<'tcx>,
        }

        impl<'tcx> PredicatesCollector<'tcx> {
            fn new(tcx: TyCtxt<'tcx>, args: ty::GenericArgsRef<'tcx>) -> PredicatesCollector<'tcx> {
                PredicatesCollector { tcx, preds: vec![], args }
            }

            fn with_own_preds(
                mut self,
                f: impl Fn(DefId) -> ty::GenericPredicates<'tcx>,
                def_id: DefId,
            ) -> Self {
                let preds = f(def_id).instantiate_own(self.tcx, self.args);
                self.preds.extend(preds);
                self
            }

            fn with_preds(
                mut self,
                f: impl Fn(DefId) -> ty::GenericPredicates<'tcx> + Copy,
                def_id: DefId,
            ) -> Self {
                let preds = f(def_id);
                if let Some(parent_def_id) = preds.parent {
                    self = self.with_own_preds(f, parent_def_id);
                }
                self.with_own_preds(f, def_id)
            }
        }
        let collector = PredicatesCollector::new(self.tcx, self.args);

        // `explicit_predicates_of` is used here to avoid copying `Self: Trait` predicate.
        // Note: `predicates_of` query can also add inferred outlives predicates, but that
        // is not the case here as `sig_id` is either a trait or a function.
        let preds = match self.inh_kind {
            InheritanceKind::WithParent(false) => {
                collector.with_preds(|def_id| self.tcx.explicit_predicates_of(def_id), self.sig_id)
            }
            InheritanceKind::WithParent(true) => {
                collector.with_preds(|def_id| self.tcx.predicates_of(def_id), self.sig_id)
            }
            InheritanceKind::Own => {
                collector.with_own_preds(|def_id| self.tcx.predicates_of(def_id), self.sig_id)
            }
        }
        .preds;

        ty::GenericPredicates {
            parent: self.parent,
            predicates: self.tcx.arena.alloc_from_iter(preds),
            // FIXME(fn_delegation): Support effects.
            effects_min_tys: ty::List::empty(),
        }
    }
}

struct GenericArgsBuilder<'tcx> {
    tcx: TyCtxt<'tcx>,
    remap_table: RemapTable,
    sig_id: DefId,
    def_id: LocalDefId,
}

impl<'tcx> GenericArgsBuilder<'tcx> {
    fn new(tcx: TyCtxt<'tcx>, sig_id: DefId, def_id: LocalDefId) -> GenericArgsBuilder<'tcx> {
        GenericArgsBuilder { tcx, remap_table: FxHashMap::default(), sig_id, def_id }
    }

    fn build_from_args(mut self, args: ty::GenericArgsRef<'tcx>) -> ty::GenericArgsRef<'tcx> {
        let caller_generics = self.tcx.generics_of(self.def_id);
        let callee_generics = self.tcx.generics_of(self.sig_id);

        for caller_param in &caller_generics.own_params {
            let callee_index =
                callee_generics.param_def_id_to_index(self.tcx, caller_param.def_id).unwrap();
            self.remap_table.insert(callee_index, caller_param.index);
        }

        let mut folder = ParamIndexRemapper { tcx: self.tcx, remap_table: self.remap_table };
        args.fold_with(&mut folder)
    }
}

fn create_generic_args<'tcx>(
    tcx: TyCtxt<'tcx>,
    def_id: LocalDefId,
    sig_id: DefId,
) -> ty::GenericArgsRef<'tcx> {
    let builder = GenericArgsBuilder::new(tcx, sig_id, def_id);

    let caller_kind = fn_kind(tcx, def_id.into());
    let callee_kind = fn_kind(tcx, sig_id);
    match (caller_kind, callee_kind) {
        (FnKind::Free, FnKind::Free)
        | (FnKind::Free, FnKind::AssocTrait)
        | (FnKind::AssocInherentImpl, FnKind::Free)
        | (FnKind::AssocTrait, FnKind::Free)
        | (FnKind::AssocTrait, FnKind::AssocTrait) => {
            let args = ty::GenericArgs::identity_for_item(tcx, sig_id);
            builder.build_from_args(args)
        }

        (FnKind::AssocTraitImpl, FnKind::AssocTrait) => {
            let callee_generics = tcx.generics_of(sig_id);
            let parent = tcx.parent(def_id.into());
            let parent_args =
                tcx.impl_trait_header(parent).unwrap().trait_ref.instantiate_identity().args;

            let trait_args = ty::GenericArgs::identity_for_item(tcx, sig_id);
            let method_args = tcx.mk_args_from_iter(trait_args.iter().skip(callee_generics.parent_count));
            let method_args = builder.build_from_args(method_args);

            tcx.mk_args_from_iter(parent_args.iter().chain(method_args))
        }

        (FnKind::AssocInherentImpl, FnKind::AssocTrait) => {
            let parent = tcx.parent(def_id.into());
            let self_ty = tcx.type_of(parent).instantiate_identity();
            let generic_self_ty = ty::GenericArg::from(self_ty);

            let trait_args = ty::GenericArgs::identity_for_item(tcx, sig_id);
            let trait_args = builder.build_from_args(trait_args);

            let args = std::iter::once(generic_self_ty).chain(trait_args.iter().skip(1));
            tcx.mk_args_from_iter(args)
        }

        // For trait impl's `sig_id` is always equal to the corresponding trait method.
        (FnKind::AssocTraitImpl, _)
        | (_, FnKind::AssocTraitImpl)
        // Delegation to inherent methods is not yet supported.
        | (_, FnKind::AssocInherentImpl) => unreachable!(),
    }
}

// FIXME(fn_delegation): Move generics inheritance to the AST->HIR lowering.
// For now, generic parameters are not propagated to the generated call,
// which leads to inference errors:
//
// fn foo<T>(x: i32) {}
//
// reuse foo as bar;
// desugaring:
// fn bar<T>() {
//   foo::<_>() // ERROR: type annotations needed
// }
pub(crate) fn inherit_generics_for_delegation_item<'tcx>(
    tcx: TyCtxt<'tcx>,
    def_id: LocalDefId,
    sig_id: DefId,
) -> ty::Generics {
    let builder = GenericsBuilder::new(tcx, sig_id);

    let caller_kind = fn_kind(tcx, def_id.into());
    let callee_kind = fn_kind(tcx, sig_id);
    match (caller_kind, callee_kind) {
        (FnKind::Free, FnKind::Free)
        | (FnKind::Free, FnKind::AssocTrait) => builder.with_inheritance_kind(InheritanceKind::WithParent(true)).build(),

        (FnKind::AssocTraitImpl, FnKind::AssocTrait) => {
            builder
            .with_parent(tcx.parent(def_id.into()))
            .with_inheritance_kind(InheritanceKind::Own)
            .build()
        }

        (FnKind::AssocInherentImpl, FnKind::AssocTrait)
        | (FnKind::AssocTrait, FnKind::AssocTrait) => {
            builder
            .with_parent(tcx.parent(def_id.into()))
            .build()
        }

        (FnKind::AssocInherentImpl, FnKind::Free)
        | (FnKind::AssocTrait, FnKind::Free) => {
            builder
            .with_parent(tcx.parent(def_id.into()))
            .build()
        }

        // For trait impl's `sig_id` is always equal to the corresponding trait method.
        (FnKind::AssocTraitImpl, _)
        | (_, FnKind::AssocTraitImpl)
        // Delegation to inherent methods is not yet supported.
        | (_, FnKind::AssocInherentImpl) => unreachable!(),
    }
}

pub(crate) fn inherit_predicates_for_delegation_item<'tcx>(
    tcx: TyCtxt<'tcx>,
    def_id: LocalDefId,
    sig_id: DefId,
) -> ty::GenericPredicates<'tcx> {
    let args = create_generic_args(tcx, def_id, sig_id);
    let builder = PredicatesBuilder::new(tcx, args, sig_id);

    let caller_kind = fn_kind(tcx, def_id.into());
    let callee_kind = fn_kind(tcx, sig_id);
    match (caller_kind, callee_kind) {
        (FnKind::Free, FnKind::Free)
        | (FnKind::Free, FnKind::AssocTrait) => {
            builder.with_inheritance_kind(InheritanceKind::WithParent(true)).build()
        }

        (FnKind::AssocTraitImpl, FnKind::AssocTrait) => {
            builder
            .with_parent(tcx.parent(def_id.into()))
            .with_inheritance_kind(InheritanceKind::Own)
            .build()
        }

        (FnKind::AssocInherentImpl, FnKind::AssocTrait)
        | (FnKind::AssocTrait, FnKind::AssocTrait)
        | (FnKind::AssocInherentImpl, FnKind::Free)
        | (FnKind::AssocTrait, FnKind::Free) => {
            builder
                .with_parent(tcx.parent(def_id.into()))
                .build()
        }

        // For trait impl's `sig_id` is always equal to the corresponding trait method.
        (FnKind::AssocTraitImpl, _)
        | (_, FnKind::AssocTraitImpl)
        // Delegation to inherent methods is not yet supported.
        | (_, FnKind::AssocInherentImpl) => unreachable!(),
    }
}

fn check_constraints<'tcx>(
    tcx: TyCtxt<'tcx>,
    def_id: LocalDefId,
    sig_id: DefId,
) -> Result<(), ErrorGuaranteed> {
    let mut ret = Ok(());

    let mut emit = |descr| {
        ret = Err(tcx.dcx().emit_err(crate::errors::UnsupportedDelegation {
            span: tcx.def_span(def_id),
            descr,
            callee_span: tcx.def_span(sig_id),
        }));
    };

    if tcx.has_host_param(sig_id) {
        emit("delegation to a function with effect parameter is not supported yet");
    }

    if let Some(local_sig_id) = sig_id.as_local()
        && tcx.hir().opt_delegation_sig_id(local_sig_id).is_some()
    {
        emit("recursive delegation is not supported yet");
    }

    ret
}

pub(crate) fn inherit_sig_for_delegation_item<'tcx>(
    tcx: TyCtxt<'tcx>,
    def_id: LocalDefId,
) -> &'tcx [Ty<'tcx>] {
    let sig_id = tcx.hir().opt_delegation_sig_id(def_id).unwrap();
    let caller_sig = tcx.fn_sig(sig_id);
    if let Err(err) = check_constraints(tcx, def_id, sig_id) {
        let sig_len = caller_sig.instantiate_identity().skip_binder().inputs().len() + 1;
        let err_type = Ty::new_error(tcx, err);
        return tcx.arena.alloc_from_iter((0..sig_len).map(|_| err_type));
    }
    let args = create_generic_args(tcx, def_id, sig_id);

    // Bound vars are also inherited from `sig_id`.
    // They will be rebound later in `lower_fn_ty`.
    let sig = caller_sig.instantiate(tcx, args).skip_binder();
    let sig_iter = sig.inputs().iter().cloned().chain(std::iter::once(sig.output()));
    tcx.arena.alloc_from_iter(sig_iter)
}