summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/hir-ty/src/infer/coerce.rs
blob: 3293534a068bd7dba56c0e4ff0e79a304ad68fdb (plain)
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Coercion logic. Coercions are certain type conversions that can implicitly
//! happen in certain places, e.g. weakening `&mut` to `&` or deref coercions
//! like going from `&Vec<T>` to `&[T]`.
//!
//! See <https://doc.rust-lang.org/nomicon/coercions.html> and
//! `rustc_hir_analysis/check/coercion.rs`.

use std::{iter, sync::Arc};

use chalk_ir::{cast::Cast, BoundVar, Goal, Mutability, TyVariableKind};
use hir_def::{
    expr::ExprId,
    lang_item::{LangItem, LangItemTarget},
};
use stdx::always;

use crate::{
    autoderef::{Autoderef, AutoderefKind},
    db::HirDatabase,
    infer::{
        Adjust, Adjustment, AutoBorrow, InferOk, InferenceContext, OverloadedDeref, PointerCast,
        TypeError, TypeMismatch,
    },
    static_lifetime, Canonical, DomainGoal, FnPointer, FnSig, Guidance, InEnvironment, Interner,
    Solution, Substitution, TraitEnvironment, Ty, TyBuilder, TyExt, TyKind,
};

use super::unify::InferenceTable;

pub(crate) type CoerceResult = Result<InferOk<(Vec<Adjustment>, Ty)>, TypeError>;

/// Do not require any adjustments, i.e. coerce `x -> x`.
fn identity(_: Ty) -> Vec<Adjustment> {
    vec![]
}

fn simple(kind: Adjust) -> impl FnOnce(Ty) -> Vec<Adjustment> {
    move |target| vec![Adjustment { kind, target }]
}

/// This always returns `Ok(...)`.
fn success(
    adj: Vec<Adjustment>,
    target: Ty,
    goals: Vec<InEnvironment<Goal<Interner>>>,
) -> CoerceResult {
    Ok(InferOk { goals, value: (adj, target) })
}

#[derive(Clone, Debug)]
pub(super) struct CoerceMany {
    expected_ty: Ty,
}

impl CoerceMany {
    pub(super) fn new(expected: Ty) -> Self {
        CoerceMany { expected_ty: expected }
    }

    /// Merge two types from different branches, with possible coercion.
    ///
    /// Mostly this means trying to coerce one to the other, but
    ///  - if we have two function types for different functions or closures, we need to
    ///    coerce both to function pointers;
    ///  - if we were concerned with lifetime subtyping, we'd need to look for a
    ///    least upper bound.
    pub(super) fn coerce(
        &mut self,
        ctx: &mut InferenceContext<'_>,
        expr: Option<ExprId>,
        expr_ty: &Ty,
    ) {
        let expr_ty = ctx.resolve_ty_shallow(expr_ty);
        self.expected_ty = ctx.resolve_ty_shallow(&self.expected_ty);

        // Special case: two function types. Try to coerce both to
        // pointers to have a chance at getting a match. See
        // https://github.com/rust-lang/rust/blob/7b805396bf46dce972692a6846ce2ad8481c5f85/src/librustc_typeck/check/coercion.rs#L877-L916
        let sig = match (self.expected_ty.kind(Interner), expr_ty.kind(Interner)) {
            (TyKind::FnDef(..) | TyKind::Closure(..), TyKind::FnDef(..) | TyKind::Closure(..)) => {
                // FIXME: we're ignoring safety here. To be more correct, if we have one FnDef and one Closure,
                // we should be coercing the closure to a fn pointer of the safety of the FnDef
                cov_mark::hit!(coerce_fn_reification);
                let sig =
                    self.expected_ty.callable_sig(ctx.db).expect("FnDef without callable sig");
                Some(sig)
            }
            _ => None,
        };
        if let Some(sig) = sig {
            let target_ty = TyKind::Function(sig.to_fn_ptr()).intern(Interner);
            let result1 = ctx.table.coerce_inner(self.expected_ty.clone(), &target_ty);
            let result2 = ctx.table.coerce_inner(expr_ty.clone(), &target_ty);
            if let (Ok(result1), Ok(result2)) = (result1, result2) {
                ctx.table.register_infer_ok(result1);
                ctx.table.register_infer_ok(result2);
                return self.expected_ty = target_ty;
            }
        }

        // It might not seem like it, but order is important here: If the expected
        // type is a type variable and the new one is `!`, trying it the other
        // way around first would mean we make the type variable `!`, instead of
        // just marking it as possibly diverging.
        if ctx.coerce(expr, &expr_ty, &self.expected_ty).is_ok() {
            /* self.expected_ty is already correct */
        } else if ctx.coerce(expr, &self.expected_ty, &expr_ty).is_ok() {
            self.expected_ty = expr_ty;
        } else {
            if let Some(id) = expr {
                ctx.result.type_mismatches.insert(
                    id.into(),
                    TypeMismatch { expected: self.expected_ty.clone(), actual: expr_ty },
                );
            }
            cov_mark::hit!(coerce_merge_fail_fallback);
            /* self.expected_ty is already correct */
        }
    }

    pub(super) fn complete(self) -> Ty {
        self.expected_ty
    }
}

pub fn could_coerce(
    db: &dyn HirDatabase,
    env: Arc<TraitEnvironment>,
    tys: &Canonical<(Ty, Ty)>,
) -> bool {
    coerce(db, env, tys).is_ok()
}

pub(crate) fn coerce(
    db: &dyn HirDatabase,
    env: Arc<TraitEnvironment>,
    tys: &Canonical<(Ty, Ty)>,
) -> Result<(Vec<Adjustment>, Ty), TypeError> {
    let mut table = InferenceTable::new(db, env);
    let vars = table.fresh_subst(tys.binders.as_slice(Interner));
    let ty1_with_vars = vars.apply(tys.value.0.clone(), Interner);
    let ty2_with_vars = vars.apply(tys.value.1.clone(), Interner);
    let (adjustments, ty) = table.coerce(&ty1_with_vars, &ty2_with_vars)?;
    // default any type vars that weren't unified back to their original bound vars
    // (kind of hacky)
    let find_var = |iv| {
        vars.iter(Interner).position(|v| match v.interned() {
            chalk_ir::GenericArgData::Ty(ty) => ty.inference_var(Interner),
            chalk_ir::GenericArgData::Lifetime(lt) => lt.inference_var(Interner),
            chalk_ir::GenericArgData::Const(c) => c.inference_var(Interner),
        } == Some(iv))
    };
    let fallback = |iv, kind, default, binder| match kind {
        chalk_ir::VariableKind::Ty(_ty_kind) => find_var(iv)
            .map_or(default, |i| BoundVar::new(binder, i).to_ty(Interner).cast(Interner)),
        chalk_ir::VariableKind::Lifetime => find_var(iv)
            .map_or(default, |i| BoundVar::new(binder, i).to_lifetime(Interner).cast(Interner)),
        chalk_ir::VariableKind::Const(ty) => find_var(iv)
            .map_or(default, |i| BoundVar::new(binder, i).to_const(Interner, ty).cast(Interner)),
    };
    // FIXME also map the types in the adjustments
    Ok((adjustments, table.resolve_with_fallback(ty, &fallback)))
}

impl<'a> InferenceContext<'a> {
    /// Unify two types, but may coerce the first one to the second one
    /// using "implicit coercion rules" if needed.
    pub(super) fn coerce(
        &mut self,
        expr: Option<ExprId>,
        from_ty: &Ty,
        to_ty: &Ty,
    ) -> Result<Ty, TypeError> {
        let from_ty = self.resolve_ty_shallow(from_ty);
        let to_ty = self.resolve_ty_shallow(to_ty);
        let (adjustments, ty) = self.table.coerce(&from_ty, &to_ty)?;
        if let Some(expr) = expr {
            self.write_expr_adj(expr, adjustments);
        }
        Ok(ty)
    }
}

impl<'a> InferenceTable<'a> {
    /// Unify two types, but may coerce the first one to the second one
    /// using "implicit coercion rules" if needed.
    pub(crate) fn coerce(
        &mut self,
        from_ty: &Ty,
        to_ty: &Ty,
    ) -> Result<(Vec<Adjustment>, Ty), TypeError> {
        let from_ty = self.resolve_ty_shallow(from_ty);
        let to_ty = self.resolve_ty_shallow(to_ty);
        match self.coerce_inner(from_ty, &to_ty) {
            Ok(InferOk { value: (adjustments, ty), goals }) => {
                self.register_infer_ok(InferOk { value: (), goals });
                Ok((adjustments, ty))
            }
            Err(e) => {
                // FIXME deal with error
                Err(e)
            }
        }
    }

    fn coerce_inner(&mut self, from_ty: Ty, to_ty: &Ty) -> CoerceResult {
        if from_ty.is_never() {
            // Subtle: If we are coercing from `!` to `?T`, where `?T` is an unbound
            // type variable, we want `?T` to fallback to `!` if not
            // otherwise constrained. An example where this arises:
            //
            //     let _: Option<?T> = Some({ return; });
            //
            // here, we would coerce from `!` to `?T`.
            if let TyKind::InferenceVar(tv, TyVariableKind::General) = to_ty.kind(Interner) {
                self.set_diverging(*tv, true);
            }
            return success(simple(Adjust::NeverToAny)(to_ty.clone()), to_ty.clone(), vec![]);
        }

        // Consider coercing the subtype to a DST
        if let Ok(ret) = self.try_coerce_unsized(&from_ty, to_ty) {
            return Ok(ret);
        }

        // Examine the supertype and consider auto-borrowing.
        match to_ty.kind(Interner) {
            TyKind::Raw(mt, _) => return self.coerce_ptr(from_ty, to_ty, *mt),
            TyKind::Ref(mt, _, _) => return self.coerce_ref(from_ty, to_ty, *mt),
            _ => {}
        }

        match from_ty.kind(Interner) {
            TyKind::FnDef(..) => {
                // Function items are coercible to any closure
                // type; function pointers are not (that would
                // require double indirection).
                // Additionally, we permit coercion of function
                // items to drop the unsafe qualifier.
                self.coerce_from_fn_item(from_ty, to_ty)
            }
            TyKind::Function(from_fn_ptr) => {
                // We permit coercion of fn pointers to drop the
                // unsafe qualifier.
                self.coerce_from_fn_pointer(from_ty.clone(), from_fn_ptr, to_ty)
            }
            TyKind::Closure(_, from_substs) => {
                // Non-capturing closures are coercible to
                // function pointers or unsafe function pointers.
                // It cannot convert closures that require unsafe.
                self.coerce_closure_to_fn(from_ty.clone(), from_substs, to_ty)
            }
            _ => {
                // Otherwise, just use unification rules.
                self.unify_and(&from_ty, to_ty, identity)
            }
        }
    }

    /// Unify two types (using sub or lub) and produce a specific coercion.
    fn unify_and<F>(&mut self, t1: &Ty, t2: &Ty, f: F) -> CoerceResult
    where
        F: FnOnce(Ty) -> Vec<Adjustment>,
    {
        self.try_unify(t1, t2)
            .and_then(|InferOk { goals, .. }| success(f(t1.clone()), t1.clone(), goals))
    }

    fn coerce_ptr(&mut self, from_ty: Ty, to_ty: &Ty, to_mt: Mutability) -> CoerceResult {
        let (is_ref, from_mt, from_inner) = match from_ty.kind(Interner) {
            TyKind::Ref(mt, _, ty) => (true, mt, ty),
            TyKind::Raw(mt, ty) => (false, mt, ty),
            _ => return self.unify_and(&from_ty, to_ty, identity),
        };

        coerce_mutabilities(*from_mt, to_mt)?;

        // Check that the types which they point at are compatible.
        let from_raw = TyKind::Raw(to_mt, from_inner.clone()).intern(Interner);

        // Although references and unsafe ptrs have the same
        // representation, we still register an Adjust::DerefRef so that
        // regionck knows that the region for `a` must be valid here.
        if is_ref {
            self.unify_and(&from_raw, to_ty, |target| {
                vec![
                    Adjustment { kind: Adjust::Deref(None), target: from_inner.clone() },
                    Adjustment { kind: Adjust::Borrow(AutoBorrow::RawPtr(to_mt)), target },
                ]
            })
        } else if *from_mt != to_mt {
            self.unify_and(
                &from_raw,
                to_ty,
                simple(Adjust::Pointer(PointerCast::MutToConstPointer)),
            )
        } else {
            self.unify_and(&from_raw, to_ty, identity)
        }
    }

    /// Reborrows `&mut A` to `&mut B` and `&(mut) A` to `&B`.
    /// To match `A` with `B`, autoderef will be performed,
    /// calling `deref`/`deref_mut` where necessary.
    fn coerce_ref(&mut self, from_ty: Ty, to_ty: &Ty, to_mt: Mutability) -> CoerceResult {
        let from_mt = match from_ty.kind(Interner) {
            &TyKind::Ref(mt, _, _) => {
                coerce_mutabilities(mt, to_mt)?;
                mt
            }
            _ => return self.unify_and(&from_ty, to_ty, identity),
        };

        // NOTE: this code is mostly copied and adapted from rustc, and
        // currently more complicated than necessary, carrying errors around
        // etc.. This complication will become necessary when we actually track
        // details of coercion errors though, so I think it's useful to leave
        // the structure like it is.

        let snapshot = self.snapshot();

        let mut autoderef = Autoderef::new(self, from_ty.clone());
        let mut first_error = None;
        let mut found = None;

        while let Some((referent_ty, autoderefs)) = autoderef.next() {
            if autoderefs == 0 {
                // Don't let this pass, otherwise it would cause
                // &T to autoref to &&T.
                continue;
            }

            // At this point, we have deref'd `a` to `referent_ty`.  So
            // imagine we are coercing from `&'a mut Vec<T>` to `&'b mut [T]`.
            // In the autoderef loop for `&'a mut Vec<T>`, we would get
            // three callbacks:
            //
            // - `&'a mut Vec<T>` -- 0 derefs, just ignore it
            // - `Vec<T>` -- 1 deref
            // - `[T]` -- 2 deref
            //
            // At each point after the first callback, we want to
            // check to see whether this would match out target type
            // (`&'b mut [T]`) if we autoref'd it. We can't just
            // compare the referent types, though, because we still
            // have to consider the mutability. E.g., in the case
            // we've been considering, we have an `&mut` reference, so
            // the `T` in `[T]` needs to be unified with equality.
            //
            // Therefore, we construct reference types reflecting what
            // the types will be after we do the final auto-ref and
            // compare those. Note that this means we use the target
            // mutability [1], since it may be that we are coercing
            // from `&mut T` to `&U`.
            let lt = static_lifetime(); // FIXME: handle lifetimes correctly, see rustc
            let derefd_from_ty = TyKind::Ref(to_mt, lt, referent_ty).intern(Interner);
            match autoderef.table.try_unify(&derefd_from_ty, to_ty) {
                Ok(result) => {
                    found = Some(result.map(|()| derefd_from_ty));
                    break;
                }
                Err(err) => {
                    if first_error.is_none() {
                        first_error = Some(err);
                    }
                }
            }
        }

        // Extract type or return an error. We return the first error
        // we got, which should be from relating the "base" type
        // (e.g., in example above, the failure from relating `Vec<T>`
        // to the target type), since that should be the least
        // confusing.
        let InferOk { value: ty, goals } = match found {
            Some(d) => d,
            None => {
                self.rollback_to(snapshot);
                let err = first_error.expect("coerce_borrowed_pointer had no error");
                return Err(err);
            }
        };
        if ty == from_ty && from_mt == Mutability::Not && autoderef.step_count() == 1 {
            // As a special case, if we would produce `&'a *x`, that's
            // a total no-op. We end up with the type `&'a T` just as
            // we started with.  In that case, just skip it
            // altogether. This is just an optimization.
            //
            // Note that for `&mut`, we DO want to reborrow --
            // otherwise, this would be a move, which might be an
            // error. For example `foo(self.x)` where `self` and
            // `self.x` both have `&mut `type would be a move of
            // `self.x`, but we auto-coerce it to `foo(&mut *self.x)`,
            // which is a borrow.
            always!(to_mt == Mutability::Not); // can only coerce &T -> &U
            return success(vec![], ty, goals);
        }

        let mut adjustments = auto_deref_adjust_steps(&autoderef);
        adjustments
            .push(Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(to_mt)), target: ty.clone() });

        success(adjustments, ty, goals)
    }

    /// Attempts to coerce from the type of a Rust function item into a function pointer.
    fn coerce_from_fn_item(&mut self, from_ty: Ty, to_ty: &Ty) -> CoerceResult {
        match to_ty.kind(Interner) {
            TyKind::Function(_) => {
                let from_sig = from_ty.callable_sig(self.db).expect("FnDef had no sig");

                // FIXME check ABI: Intrinsics are not coercible to function pointers
                // FIXME Safe `#[target_feature]` functions are not assignable to safe fn pointers (RFC 2396)

                // FIXME rustc normalizes assoc types in the sig here, not sure if necessary

                let from_sig = from_sig.to_fn_ptr();
                let from_fn_pointer = TyKind::Function(from_sig.clone()).intern(Interner);
                let ok = self.coerce_from_safe_fn(
                    from_fn_pointer.clone(),
                    &from_sig,
                    to_ty,
                    |unsafe_ty| {
                        vec![
                            Adjustment {
                                kind: Adjust::Pointer(PointerCast::ReifyFnPointer),
                                target: from_fn_pointer,
                            },
                            Adjustment {
                                kind: Adjust::Pointer(PointerCast::UnsafeFnPointer),
                                target: unsafe_ty,
                            },
                        ]
                    },
                    simple(Adjust::Pointer(PointerCast::ReifyFnPointer)),
                )?;

                Ok(ok)
            }
            _ => self.unify_and(&from_ty, to_ty, identity),
        }
    }

    fn coerce_from_fn_pointer(
        &mut self,
        from_ty: Ty,
        from_f: &FnPointer,
        to_ty: &Ty,
    ) -> CoerceResult {
        self.coerce_from_safe_fn(
            from_ty,
            from_f,
            to_ty,
            simple(Adjust::Pointer(PointerCast::UnsafeFnPointer)),
            identity,
        )
    }

    fn coerce_from_safe_fn<F, G>(
        &mut self,
        from_ty: Ty,
        from_fn_ptr: &FnPointer,
        to_ty: &Ty,
        to_unsafe: F,
        normal: G,
    ) -> CoerceResult
    where
        F: FnOnce(Ty) -> Vec<Adjustment>,
        G: FnOnce(Ty) -> Vec<Adjustment>,
    {
        if let TyKind::Function(to_fn_ptr) = to_ty.kind(Interner) {
            if let (chalk_ir::Safety::Safe, chalk_ir::Safety::Unsafe) =
                (from_fn_ptr.sig.safety, to_fn_ptr.sig.safety)
            {
                let from_unsafe =
                    TyKind::Function(safe_to_unsafe_fn_ty(from_fn_ptr.clone())).intern(Interner);
                return self.unify_and(&from_unsafe, to_ty, to_unsafe);
            }
        }
        self.unify_and(&from_ty, to_ty, normal)
    }

    /// Attempts to coerce from the type of a non-capturing closure into a
    /// function pointer.
    fn coerce_closure_to_fn(
        &mut self,
        from_ty: Ty,
        from_substs: &Substitution,
        to_ty: &Ty,
    ) -> CoerceResult {
        match to_ty.kind(Interner) {
            // if from_substs is non-capturing (FIXME)
            TyKind::Function(fn_ty) => {
                // We coerce the closure, which has fn type
                //     `extern "rust-call" fn((arg0,arg1,...)) -> _`
                // to
                //     `fn(arg0,arg1,...) -> _`
                // or
                //     `unsafe fn(arg0,arg1,...) -> _`
                let safety = fn_ty.sig.safety;
                let pointer_ty = coerce_closure_fn_ty(from_substs, safety);
                self.unify_and(
                    &pointer_ty,
                    to_ty,
                    simple(Adjust::Pointer(PointerCast::ClosureFnPointer(safety))),
                )
            }
            _ => self.unify_and(&from_ty, to_ty, identity),
        }
    }

    /// Coerce a type using `from_ty: CoerceUnsized<ty_ty>`
    ///
    /// See: <https://doc.rust-lang.org/nightly/std/marker/trait.CoerceUnsized.html>
    fn try_coerce_unsized(&mut self, from_ty: &Ty, to_ty: &Ty) -> CoerceResult {
        // These 'if' statements require some explanation.
        // The `CoerceUnsized` trait is special - it is only
        // possible to write `impl CoerceUnsized<B> for A` where
        // A and B have 'matching' fields. This rules out the following
        // two types of blanket impls:
        //
        // `impl<T> CoerceUnsized<T> for SomeType`
        // `impl<T> CoerceUnsized<SomeType> for T`
        //
        // Both of these trigger a special `CoerceUnsized`-related error (E0376)
        //
        // We can take advantage of this fact to avoid performing unnecessary work.
        // If either `source` or `target` is a type variable, then any applicable impl
        // would need to be generic over the self-type (`impl<T> CoerceUnsized<SomeType> for T`)
        // or generic over the `CoerceUnsized` type parameter (`impl<T> CoerceUnsized<T> for
        // SomeType`).
        //
        // However, these are exactly the kinds of impls which are forbidden by
        // the compiler! Therefore, we can be sure that coercion will always fail
        // when either the source or target type is a type variable. This allows us
        // to skip performing any trait selection, and immediately bail out.
        if from_ty.is_ty_var() {
            return Err(TypeError);
        }
        if to_ty.is_ty_var() {
            return Err(TypeError);
        }

        // Handle reborrows before trying to solve `Source: CoerceUnsized<Target>`.
        let reborrow = match (from_ty.kind(Interner), to_ty.kind(Interner)) {
            (TyKind::Ref(from_mt, _, from_inner), &TyKind::Ref(to_mt, _, _)) => {
                coerce_mutabilities(*from_mt, to_mt)?;

                let lt = static_lifetime();
                Some((
                    Adjustment { kind: Adjust::Deref(None), target: from_inner.clone() },
                    Adjustment {
                        kind: Adjust::Borrow(AutoBorrow::Ref(to_mt)),
                        target: TyKind::Ref(to_mt, lt, from_inner.clone()).intern(Interner),
                    },
                ))
            }
            (TyKind::Ref(from_mt, _, from_inner), &TyKind::Raw(to_mt, _)) => {
                coerce_mutabilities(*from_mt, to_mt)?;

                Some((
                    Adjustment { kind: Adjust::Deref(None), target: from_inner.clone() },
                    Adjustment {
                        kind: Adjust::Borrow(AutoBorrow::RawPtr(to_mt)),
                        target: TyKind::Raw(to_mt, from_inner.clone()).intern(Interner),
                    },
                ))
            }
            _ => None,
        };
        let coerce_from =
            reborrow.as_ref().map_or_else(|| from_ty.clone(), |(_, adj)| adj.target.clone());

        let krate = self.trait_env.krate;
        let coerce_unsized_trait = match self.db.lang_item(krate, LangItem::CoerceUnsized) {
            Some(LangItemTarget::Trait(trait_)) => trait_,
            _ => return Err(TypeError),
        };

        let coerce_unsized_tref = {
            let b = TyBuilder::trait_ref(self.db, coerce_unsized_trait);
            if b.remaining() != 2 {
                // The CoerceUnsized trait should have two generic params: Self and T.
                return Err(TypeError);
            }
            b.push(coerce_from).push(to_ty.clone()).build()
        };

        let goal: InEnvironment<DomainGoal> =
            InEnvironment::new(&self.trait_env.env, coerce_unsized_tref.cast(Interner));

        let canonicalized = self.canonicalize(goal);

        // FIXME: rustc's coerce_unsized is more specialized -- it only tries to
        // solve `CoerceUnsized` and `Unsize` goals at this point and leaves the
        // rest for later. Also, there's some logic about sized type variables.
        // Need to find out in what cases this is necessary
        let solution = self
            .db
            .trait_solve(krate, canonicalized.value.clone().cast(Interner))
            .ok_or(TypeError)?;

        match solution {
            Solution::Unique(v) => {
                canonicalized.apply_solution(
                    self,
                    Canonical {
                        binders: v.binders,
                        // FIXME handle constraints
                        value: v.value.subst,
                    },
                );
            }
            Solution::Ambig(Guidance::Definite(subst)) => {
                // FIXME need to record an obligation here
                canonicalized.apply_solution(self, subst)
            }
            // FIXME actually we maybe should also accept unknown guidance here
            _ => return Err(TypeError),
        };
        let unsize =
            Adjustment { kind: Adjust::Pointer(PointerCast::Unsize), target: to_ty.clone() };
        let adjustments = match reborrow {
            None => vec![unsize],
            Some((deref, autoref)) => vec![deref, autoref, unsize],
        };
        success(adjustments, to_ty.clone(), vec![])
    }
}

fn coerce_closure_fn_ty(closure_substs: &Substitution, safety: chalk_ir::Safety) -> Ty {
    let closure_sig = closure_substs.at(Interner, 0).assert_ty_ref(Interner).clone();
    match closure_sig.kind(Interner) {
        TyKind::Function(fn_ty) => TyKind::Function(FnPointer {
            num_binders: fn_ty.num_binders,
            sig: FnSig { safety, ..fn_ty.sig },
            substitution: fn_ty.substitution.clone(),
        })
        .intern(Interner),
        _ => TyKind::Error.intern(Interner),
    }
}

fn safe_to_unsafe_fn_ty(fn_ty: FnPointer) -> FnPointer {
    FnPointer {
        num_binders: fn_ty.num_binders,
        sig: FnSig { safety: chalk_ir::Safety::Unsafe, ..fn_ty.sig },
        substitution: fn_ty.substitution,
    }
}

fn coerce_mutabilities(from: Mutability, to: Mutability) -> Result<(), TypeError> {
    match (from, to) {
        (Mutability::Mut, Mutability::Mut | Mutability::Not)
        | (Mutability::Not, Mutability::Not) => Ok(()),
        (Mutability::Not, Mutability::Mut) => Err(TypeError),
    }
}

pub(super) fn auto_deref_adjust_steps(autoderef: &Autoderef<'_, '_>) -> Vec<Adjustment> {
    let steps = autoderef.steps();
    let targets =
        steps.iter().skip(1).map(|(_, ty)| ty.clone()).chain(iter::once(autoderef.final_ty()));
    steps
        .iter()
        .map(|(kind, _source)| match kind {
            // We do not know what kind of deref we require at this point yet
            AutoderefKind::Overloaded => Some(OverloadedDeref(Mutability::Not)),
            AutoderefKind::Builtin => None,
        })
        .zip(targets)
        .map(|(autoderef, target)| Adjustment { kind: Adjust::Deref(autoderef), target })
        .collect()
}